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 Strings.
13 * Note: this {@link ServerString} has to be discarded after use (cannot be
18 abstract public class ServerString
extends Server
{
20 * Create a new server that will start listening on the network when
21 * {@link ServerString#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 ServerString#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 ServerString(int port
, boolean ssl
) throws IOException
{
43 * Create a new server that will start listening on the network when
44 * {@link ServerString#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 ServerString(String name
, int port
, boolean ssl
) throws IOException
{
62 super(name
, port
, ssl
);
66 protected ConnectActionServer
createConnectActionServer(Socket s
) {
67 return new ConnectActionServerString(s
) {
69 public void action(Version clientVersion
) throws Exception
{
70 for (String data
= rec(); data
!= null; data
= rec()) {
73 rep
= onRequest(this, clientVersion
, data
);
74 } catch (Exception e
) {
87 protected void onError(Exception e
) {
88 ServerString
.this.onError(e
);
94 * This is the method that is called on each client request.
96 * You are expected to react to it and return an answer (NULL will be
97 * converted to an empty {@link String}).
101 * @param clientVersion
104 * the data sent by the client
106 * @return the answer to return to the client
109 * in case of an exception, the error will only be logged
111 abstract protected String
onRequest(ConnectActionServerString action
,
112 Version clientVersion
, String data
) throws Exception
;