1 package be
.nikiroo
.utils
.serial
.server
;
3 import java
.io
.IOException
;
4 import java
.net
.Socket
;
5 import java
.net
.UnknownHostException
;
7 import be
.nikiroo
.utils
.Version
;
10 * This class implements a simple server that can listen for connections and
11 * send/receive objects.
13 * Note: this {@link ServerObject} has to be discarded after use (cannot be
18 abstract public class ServerObject
extends Server
{
20 * Create a new server that will start listening on the network when
21 * {@link ServerObject#start()} is called.
24 * the port to listen on, or 0 to assign any unallocated port
25 * found (which can later on be queried via
26 * {@link ServerObject#getPort()}
28 * an optional key to encrypt all the communications (if NULL,
29 * everything will be sent in clear text)
32 * in case of I/O error
33 * @throws UnknownHostException
34 * if the IP address of the host could not be determined
35 * @throws IllegalArgumentException
36 * if the port parameter is outside the specified range of valid
37 * port values, which is between 0 and 65535, inclusive
39 public ServerObject(int port
, String key
) throws IOException
{
44 * Create a new server that will start listening on the network when
45 * {@link ServerObject#start()} is called.
48 * the server name (only used for debug info and traces)
50 * the port to listen on
52 * an optional key to encrypt all the communications (if NULL,
53 * everything will be sent in clear text)
56 * in case of I/O error
57 * @throws UnknownHostException
58 * if the IP address of the host could not be determined
59 * @throws IllegalArgumentException
60 * if the port parameter is outside the specified range of valid
61 * port values, which is between 0 and 65535, inclusive
63 public ServerObject(String name
, int port
, String key
) throws IOException
{
64 super(name
, port
, key
);
68 protected ConnectActionServer
createConnectActionServer(Socket s
) {
69 return new ConnectActionServerObject(s
, key
) {
71 public void action(Version clientVersion
) throws Exception
{
72 long id
= getNextId();
74 for (Object data
= rec(); true; data
= rec()) {
77 rep
= onRequest(this, clientVersion
, data
, id
);
81 } catch (Exception e
) {
87 } catch (NullPointerException e
) {
88 // Client has no data any more, we quit
89 onRequestDone(id
, getBytesReceived(), getBytesSent());
94 protected void onError(Exception e
) {
95 ServerObject
.this.onError(e
);
101 protected ConnectActionClient
getConnectionToMe()
102 throws UnknownHostException
, IOException
{
103 return new ConnectActionClientObject(new Socket((String
) null,
108 * This is the method that is called on each client request.
110 * You are expected to react to it and return an answer (which can be NULL).
115 * the data sent by the client (which can be NULL)
117 * an ID to identify this request (will also be re-used for
118 * {@link ServerObject#onRequestDone(long, long, long)}.
120 * @return the answer to return to the client (which can be NULL)
123 * in case of an exception, the error will only be logged
125 protected Object
onRequest(ConnectActionServerObject action
,
126 Version clientVersion
, Object data
,
127 @SuppressWarnings("unused") long id
) throws Exception
{
128 // TODO: change to abstract when deprecated method is removed
129 // Default implementation for compat
130 return onRequest(action
, clientVersion
, data
);
136 * @deprecated SSL support has been replaced by key-based encryption.
138 * Please use the version with key encryption (this deprecated
139 * version uses an empty key when <tt>ssl</tt> is TRUE and no
140 * key (NULL) when <tt>ssl</tt> is FALSE).
143 public ServerObject(int port
, boolean ssl
) throws IOException
{
144 this(port
, ssl ?
"" : null);
148 * @deprecated SSL support has been replaced by key-based encryption.
150 * Please use the version with key encryption (this deprecated
151 * version uses an empty key when <tt>ssl</tt> is TRUE and no
152 * key (NULL) when <tt>ssl</tt> is FALSE).
155 public ServerObject(String name
, int port
, boolean ssl
) throws IOException
{
156 this(name
, port
, ssl ?
"" : null);
160 * Will be called if the correct version is not overrided.
162 * @deprecated use the version with the id.
167 * the data sent by the client
169 * @return the answer to return to the client
172 * in case of an exception, the error will only be logged
175 @SuppressWarnings("unused")
176 protected Object
onRequest(ConnectActionServerObject action
,
177 Version version
, Object data
) throws Exception
{