| 1 |
/* |
| 2 |
* junixsocket |
| 3 |
* |
| 4 |
* Copyright 2009-2022 Christian Kohlschütter |
| 5 |
* |
| 6 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 |
* you may not use this file except in compliance with the License. |
| 8 |
* You may obtain a copy of the License at |
| 9 |
* |
| 10 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 11 |
* |
| 12 |
* Unless required by applicable law or agreed to in writing, software |
| 13 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 14 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 |
* See the License for the specific language governing permissions and |
| 16 |
* limitations under the License. |
| 17 |
*/ |
| 18 |
package org.newsclub.net.unix.demo.server; |
| 19 |
|
| 20 |
import java.io.IOException; |
| 21 |
import java.net.ServerSocket; |
| 22 |
import java.net.Socket; |
| 23 |
import java.net.SocketAddress; |
| 24 |
import java.net.SocketException; |
| 25 |
import java.util.concurrent.Future; |
| 26 |
|
| 27 |
import org.newsclub.net.unix.AFSocketAddress; |
| 28 |
import org.newsclub.net.unix.AFUNIXSocket; |
| 29 |
import org.newsclub.net.unix.server.SocketServer; |
| 30 |
|
| 31 |
/** |
| 32 |
* An {@link SocketServer} that's just good for demo purposes. |
| 33 |
* |
| 34 |
* @author Christian Kohlschütter |
| 35 |
*/ |
| 36 |
abstract class DemoServerBase extends SocketServer<SocketAddress, Socket, ServerSocket> { |
| 37 |
public DemoServerBase(SocketAddress listenAddress) { |
| 38 |
super(listenAddress); |
| 39 |
} |
| 40 |
|
| 41 |
private static String millisToHumanReadable(int millis, String zeroValue) { |
| 42 |
if (millis == 0 && zeroValue != null) { |
| 43 |
return "0 [ms] (" + zeroValue + ")"; |
| 44 |
} else { |
| 45 |
float secs = millis / 1000f; |
| 46 |
if ((secs - (int) secs) == 0) { |
| 47 |
return millis + " [ms] == " + (int) (secs) + "s"; |
| 48 |
} else { |
| 49 |
return millis + " [ms] == " + secs + "s"; |
| 50 |
} |
| 51 |
} |
| 52 |
} |
| 53 |
|
| 54 |
@Override |
| 55 |
protected void onServerStarting() { |
| 56 |
System.out.println(); |
| 57 |
System.out.println("Creating server: " + getClass().getName()); |
| 58 |
System.out.println("with the following configuration:"); |
| 59 |
System.out.println("- maxConcurrentConnections: " + getMaxConcurrentConnections()); |
| 60 |
System.out.println("- serverTimeout: " + millisToHumanReadable(getServerTimeout(), "none")); |
| 61 |
System.out.println("- socketTimeout: " + millisToHumanReadable(getSocketTimeout(), "none")); |
| 62 |
System.out.println("- serverBusyTimeout: " + millisToHumanReadable(getServerBusyTimeout(), |
| 63 |
"none")); |
| 64 |
} |
| 65 |
|
| 66 |
@Override |
| 67 |
protected void onServerBound(SocketAddress address) { |
| 68 |
System.out.println("Created server -- bound to " + address); |
| 69 |
} |
| 70 |
|
| 71 |
@Override |
| 72 |
protected void onServerBusy(long busySince) { |
| 73 |
System.out.println("Server is busy"); |
| 74 |
} |
| 75 |
|
| 76 |
@Override |
| 77 |
protected void onServerReady(int activeCount) { |
| 78 |
System.out.println("Active connections: " + activeCount |
| 79 |
+ "; waiting for the next connection..."); |
| 80 |
} |
| 81 |
|
| 82 |
@Override |
| 83 |
protected void onServerStopped(ServerSocket theServerSocket) { |
| 84 |
System.out.println("Close server " + theServerSocket); |
| 85 |
} |
| 86 |
|
| 87 |
@Override |
| 88 |
protected void onSubmitted(Socket socket, Future<?> submit) { |
| 89 |
System.out.println("Accepted: " + socket); |
| 90 |
} |
| 91 |
|
| 92 |
@Override |
| 93 |
protected void onBeforeServingSocket(Socket socket) { |
| 94 |
System.out.println("Serving socket: " + socket); |
| 95 |
if (socket instanceof AFUNIXSocket) { |
| 96 |
try { |
| 97 |
System.out.println("Client's credentials: " + ((AFUNIXSocket) socket).getPeerCredentials()); |
| 98 |
} catch (IOException e) { |
| 99 |
e.printStackTrace(); |
| 100 |
} |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
@Override |
| 105 |
protected void onServerShuttingDown() { |
| 106 |
System.out.println("Nothing going on for a long time, I better stop listening"); |
| 107 |
} |
| 108 |
|
| 109 |
@Override |
| 110 |
protected void onSocketExceptionDuringAccept(SocketException e) { |
| 111 |
e.printStackTrace(); |
| 112 |
} |
| 113 |
|
| 114 |
@Override |
| 115 |
protected void onSocketExceptionAfterAccept(Socket socket, SocketException e) { |
| 116 |
System.out.println("Closed (not executed): " + socket); |
| 117 |
} |
| 118 |
|
| 119 |
@Override |
| 120 |
protected void onServingException(Socket socket, Exception e) { |
| 121 |
if (socket.isClosed()) { |
| 122 |
// "Broken pipe", etc. |
| 123 |
System.out.println("The other end disconnected (" + e.getMessage() + "): " + socket); |
| 124 |
return; |
| 125 |
} |
| 126 |
System.err.println("Exception thrown in " + socket + ", connected: " + socket.isConnected() |
| 127 |
+ ", " + socket.isBound() + "," + socket.isClosed() + "," + socket.isInputShutdown() + "," |
| 128 |
+ socket.isOutputShutdown()); |
| 129 |
e.printStackTrace(); |
| 130 |
} |
| 131 |
|
| 132 |
@Override |
| 133 |
protected void onAfterServingSocket(Socket socket) { |
| 134 |
System.out.println("Closed: " + socket); |
| 135 |
} |
| 136 |
|
| 137 |
@Override |
| 138 |
protected void onListenException(Exception e) { |
| 139 |
e.printStackTrace(); |
| 140 |
} |
| 141 |
|
| 142 |
@Override |
| 143 |
protected ServerSocket newServerSocket() throws IOException { |
| 144 |
SocketAddress listenAddress = getListenAddress(); |
| 145 |
if (listenAddress instanceof AFSocketAddress) { |
| 146 |
return ((AFSocketAddress) listenAddress).getAddressFamily().newServerSocket(); |
| 147 |
} else { |
| 148 |
return new ServerSocket(); |
| 149 |
} |
| 150 |
} |
| 151 |
} |