...
[nikiroo-utils.git] / src / be / nikiroo / utils / serial / server / 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 /**
8 * This class implements a simple server that can listen for connections and
9 * send/receive Strings.
10 * <p>
11 * Note: this {@link ServerString} has to be discarded after use (cannot be
12 * started twice).
13 *
14 * @author niki
15 */
16 abstract public class ServerString extends Server {
17 /**
18 * Create a new server that will start listening on the network when
19 * {@link ServerString#start()} is called.
20 *
21 * @param port
22 * the port to listen on, or 0 to assign any unallocated port
23 * found (which can later on be queried via
24 * {@link ServerString#getPort()}
25 * @param key
26 * an optional key to encrypt all the communications (if NULL,
27 * everything will be sent in clear text)
28 *
29 * @throws IOException
30 * in case of I/O error
31 * @throws UnknownHostException
32 * if the IP address of the host could not be determined
33 * @throws IllegalArgumentException
34 * if the port parameter is outside the specified range of valid
35 * port values, which is between 0 and 65535, inclusive
36 */
37 public ServerString(int port, String key) throws IOException {
38 super(port, key);
39 }
40
41 /**
42 * Create a new server that will start listening on the network when
43 * {@link ServerString#start()} is called.
44 *
45 * @param name
46 * the server name (only used for debug info and traces)
47 * @param port
48 * the port to listen on
49 * @param key
50 * an optional key to encrypt all the communications (if NULL,
51 * everything will be sent in clear text)
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, String key) throws IOException {
62 super(name, port, key);
63 }
64
65 @Override
66 protected ConnectActionServer createConnectActionServer(Socket s) {
67 return new ConnectActionServerString(s, key) {
68 @Override
69 public void action() throws Exception {
70 for (String data = rec(); data != null; data = rec()) {
71 String rep = null;
72 try {
73 rep = onRequest(this, data);
74 if (isClosing()) {
75 return;
76 }
77 } catch (Exception e) {
78 onError(e);
79 }
80
81 if (rep == null) {
82 rep = "";
83 }
84
85 send(rep);
86 }
87 }
88
89 @Override
90 protected void onError(Exception e) {
91 ServerString.this.onError(e);
92 }
93 };
94 }
95
96 /**
97 * This is the method that is called on each client request.
98 * <p>
99 * You are expected to react to it and return an answer (NULL will be
100 * converted to an empty {@link String}).
101 *
102 * @param action
103 * the client action
104 * @param data
105 * the data sent by the client
106 *
107 * @return the answer to return to the client
108 *
109 * @throws Exception
110 * in case of an exception, the error will only be logged
111 */
112 abstract protected String onRequest(ConnectActionServerString action,
113 String data) throws Exception;
114 }