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 * 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 ServerString(int port
, String key
) throws IOException
{
44 * Create a new server that will start listening on the network when
45 * {@link ServerString#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 ServerString(String name
, int port
, String key
) throws IOException
{
64 super(name
, port
, key
);
68 protected ConnectActionServer
createConnectActionServer(Socket s
) {
69 return new ConnectActionServerString(s
, key
) {
71 public void action(Version clientVersion
) throws Exception
{
72 for (String data
= rec(); data
!= null; data
= rec()) {
75 rep
= onRequest(this, clientVersion
, data
);
79 } catch (Exception e
) {
92 protected void onError(Exception e
) {
93 ServerString
.this.onError(e
);
99 * This is the method that is called on each client request.
101 * You are expected to react to it and return an answer (NULL will be
102 * converted to an empty {@link String}).
106 * @param clientVersion
109 * the data sent by the client
111 * @return the answer to return to the client
114 * in case of an exception, the error will only be logged
116 abstract protected String
onRequest(ConnectActionServerString action
,
117 Version clientVersion
, String data
) throws Exception
;