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 * use a SSL connection (or not)
31 * in case of I/O error
32 * @throws UnknownHostException
33 * if the IP address of the host could not be determined
34 * @throws IllegalArgumentException
35 * if the port parameter is outside the specified range of valid
36 * port values, which is between 0 and 65535, inclusive
38 public ServerObject(int port
, boolean ssl
) throws IOException
{
43 * Create a new server that will start listening on the network when
44 * {@link ServerObject#start()} is called.
47 * the server name (only used for debug info and traces)
49 * the port to listen on
51 * use a SSL connection (or not)
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 ServerObject(String name
, int port
, boolean ssl
) throws IOException
{
62 super(name
, port
, ssl
);
66 protected ConnectActionServer
createConnectActionServer(Socket s
) {
67 return new ConnectActionServerObject(s
) {
69 public void action(Version clientVersion
) throws Exception
{
71 for (Object data
= rec(); true; data
= rec()) {
74 rep
= onRequest(this, clientVersion
, data
);
75 } catch (Exception e
) {
80 } catch (NullPointerException e
) {
81 // Client has no data any more, we quit
86 protected void onError(Exception e
) {
87 ServerObject
.this.onError(e
);
93 * This is the method that is called on each client request.
95 * You are expected to react to it and return an answer (which can be NULL).
99 * @param clientVersion
102 * the data sent by the client (which can be NULL)
104 * @return the answer to return to the client (which can be NULL)
107 * in case of an exception, the error will only be logged
109 abstract protected Object
onRequest(ConnectActionServerObject action
,
110 Version clientVersion
, Object data
) throws Exception
;