1 package be
.nikiroo
.utils
.serial
.server
;
3 import java
.io
.IOException
;
4 import java
.net
.Socket
;
5 import java
.net
.UnknownHostException
;
8 * This class implements a simple server that can listen for connections and
9 * send/receive Strings.
11 * Note: this {@link ServerString} has to be discarded after use (cannot be
16 abstract public class ServerString
extends Server
{
18 * Create a new server that will start listening on the network when
19 * {@link ServerString#start()} is called.
22 * the port to listen on, or 0 to assign any unallocated port
23 * found (which can later on be queried via
24 * {@link ServerString#getPort()}
26 * an optional key to encrypt all the communications (if NULL,
27 * everything will be sent in clear text)
30 * in case of I/O error
31 * @throws UnknownHostException
32 * if the IP address of the host could not be determined
33 * @throws IllegalArgumentException
34 * if the port parameter is outside the specified range of valid
35 * port values, which is between 0 and 65535, inclusive
37 public ServerString(int port
, String key
) throws IOException
{
42 * Create a new server that will start listening on the network when
43 * {@link ServerString#start()} is called.
46 * the server name (only used for debug info and traces)
48 * the port to listen on
50 * an optional key to encrypt all the communications (if NULL,
51 * everything will be sent in clear text)
54 * in case of I/O error
55 * @throws UnknownHostException
56 * if the IP address of the host could not be determined
57 * @throws IllegalArgumentException
58 * if the port parameter is outside the specified range of valid
59 * port values, which is between 0 and 65535, inclusive
61 public ServerString(String name
, int port
, String key
) throws IOException
{
62 super(name
, port
, key
);
66 protected ConnectActionServer
createConnectActionServer(Socket s
) {
67 return new ConnectActionServerString(s
, key
) {
69 public void action() throws Exception
{
70 long id
= getNextId();
71 for (String data
= rec(); data
!= null; data
= rec()) {
74 rep
= onRequest(this, data
, id
);
78 } catch (Exception e
) {
88 onRequestDone(id
, getBytesReceived(), getBytesSent());
92 protected void onError(Exception e
) {
93 ServerString
.this.onError(e
);
99 protected ConnectActionClient
getConnectionToMe()
100 throws UnknownHostException
, IOException
{
101 return new ConnectActionClientString(new Socket((String
) null,
106 * This is the method that is called on each client request.
108 * You are expected to react to it and return an answer (NULL will be
109 * converted to an empty {@link String}).
114 * the data sent by the client
116 * an ID to identify this request (will also be re-used for
117 * {@link ServerObject#onRequestDone(long, long, long)}.
119 * @return the answer to return to the client
122 * in case of an exception, the error will only be logged
124 abstract protected String
onRequest(ConnectActionServerString action
,
125 String data
, long id
) throws Exception
;