3185c6f450f0cda09c4b66d7b1924d917c027417
[fanfix.git] / ServerString.java
1 package be.nikiroo.utils.serial.server;
2
3 import java.io.IOException;
4 import java.net.Socket;
5 import java.net.UnknownHostException;
6
7 import be.nikiroo.utils.Version;
8
9 /**
10 * This class implements a simple server that can listen for connections and
11 * send/receive Strings.
12 * <p>
13 * Note: this {@link ServerString} has to be discarded after use (cannot be
14 * started twice).
15 *
16 * @author niki
17 */
18 abstract public class ServerString extends Server {
19 /**
20 * Create a new server that will start listening on the network when
21 * {@link ServerString#start()} is called.
22 *
23 * @param port
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()}
27 * @param ssl
28 * use a SSL connection (or not)
29 *
30 * @throws IOException
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
37 */
38 public ServerString(int port, boolean ssl) throws IOException {
39 super(port, ssl);
40 }
41
42 /**
43 * Create a new server that will start listening on the network when
44 * {@link ServerString#start()} is called.
45 *
46 * @param name
47 * the server name (only used for debug info and traces)
48 * @param port
49 * the port to listen on
50 * @param ssl
51 * use a SSL connection (or not)
52 *
53 * @throws IOException
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
60 */
61 public ServerString(String name, int port, boolean ssl) throws IOException {
62 super(name, port, ssl);
63 }
64
65 @Override
66 protected ConnectActionServer createConnectActionServer(Socket s) {
67 return new ConnectActionServerString(s) {
68 @Override
69 public void action(Version clientVersion) throws Exception {
70 for (String data = rec(); data != null; data = rec()) {
71 String rep = null;
72 try {
73 rep = onRequest(this, clientVersion, data);
74 } catch (Exception e) {
75 onError(e);
76 }
77
78 if (rep == null) {
79 rep = "";
80 }
81
82 send(rep);
83 }
84 }
85
86 @Override
87 protected void onError(Exception e) {
88 ServerString.this.onError(e);
89 }
90 };
91 }
92
93 /**
94 * This is the method that is called on each client request.
95 * <p>
96 * You are expected to react to it and return an answer (NULL will be
97 * converted to an empty {@link String}).
98 *
99 * @param action
100 * the client action
101 * @param clientVersion
102 * the client version
103 * @param data
104 * the data sent by the client
105 *
106 * @return the answer to return to the client
107 *
108 * @throws Exception
109 * in case of an exception, the error will only be logged
110 */
111 abstract protected String onRequest(ConnectActionServerString action,
112 Version clientVersion, String data) throws Exception;
113 }