1 package be
.nikiroo
.utils
.serial
.server
;
3 import java
.io
.IOException
;
4 import java
.net
.Socket
;
6 import be
.nikiroo
.utils
.Version
;
9 * This class implements a simple server that can listen for connections and
10 * send/receive Strings.
12 * Note: this {@link ServerString} has to be discarded after use (cannot be
17 abstract public class ServerString
extends Server
{
19 * Create a new server that will start listening on the network when
20 * {@link ServerString#start()} is called.
23 * the port to listen on, or 0 to assign any unallocated port
24 * found (which can later on be queried via
25 * {@link ServerString#getPort()}
27 * use a SSL connection (or not)
30 * in case of I/O error
32 public ServerString(int port
, boolean ssl
) throws IOException
{
37 * Create a new server that will start listening on the network when
38 * {@link ServerString#start()} is called.
41 * the server name (only used for debug info and traces)
43 * the port to listen on
45 * use a SSL connection (or not)
48 * in case of I/O error
50 public ServerString(String name
, int port
, boolean ssl
) throws IOException
{
51 super(name
, port
, ssl
);
55 protected ConnectActionServer
createConnectActionServer(Socket s
) {
56 return new ConnectActionServerString(s
) {
58 public void action(Version clientVersion
) throws Exception
{
60 for (String data
= rec(); data
!= null; data
= rec()) {
63 rep
= onRequest(this, clientVersion
, data
);
64 } catch (Exception e
) {
74 } catch (NullPointerException e
) {
75 // Client has no data any more, we quit
78 + ": client has data no more, stopping connection");
83 public void connect() {
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
;