NextableInputStream: separate base behaviour from nextable
[fanfix.git] / src / be / nikiroo / utils / serial / server / ServerString.java
CommitLineData
79ce1a49
NR
1package be.nikiroo.utils.serial.server;
2
3import java.io.IOException;
4import java.net.Socket;
f4053377 5import java.net.UnknownHostException;
79ce1a49
NR
6
7import 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 */
18abstract 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()}
8468bb79
NR
27 * @param key
28 * an optional key to encrypt all the communications (if NULL,
29 * everything will be sent in clear text)
79ce1a49
NR
30 *
31 * @throws IOException
32 * in case of I/O error
f4053377
NR
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
79ce1a49 38 */
8468bb79
NR
39 public ServerString(int port, String key) throws IOException {
40 super(port, key);
79ce1a49
NR
41 }
42
43 /**
44 * Create a new server that will start listening on the network when
45 * {@link ServerString#start()} is called.
46 *
47 * @param name
48 * the server name (only used for debug info and traces)
49 * @param port
50 * the port to listen on
8468bb79
NR
51 * @param key
52 * an optional key to encrypt all the communications (if NULL,
53 * everything will be sent in clear text)
79ce1a49
NR
54 *
55 * @throws IOException
56 * in case of I/O error
f4053377
NR
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
79ce1a49 62 */
8468bb79
NR
63 public ServerString(String name, int port, String key) throws IOException {
64 super(name, port, key);
79ce1a49
NR
65 }
66
67 @Override
68 protected ConnectActionServer createConnectActionServer(Socket s) {
8468bb79 69 return new ConnectActionServerString(s, key) {
79ce1a49
NR
70 @Override
71 public void action(Version clientVersion) throws Exception {
8537d55a
NR
72 for (String data = rec(); data != null; data = rec()) {
73 String rep = null;
74 try {
75 rep = onRequest(this, clientVersion, data);
a72ea8a7
NR
76 if (isClosing()) {
77 return;
78 }
8537d55a
NR
79 } catch (Exception e) {
80 onError(e);
81 }
79ce1a49 82
8537d55a
NR
83 if (rep == null) {
84 rep = "";
79ce1a49 85 }
79ce1a49 86
8537d55a 87 send(rep);
79ce1a49
NR
88 }
89 }
0ff71477
NR
90
91 @Override
92 protected void onError(Exception e) {
d827da2a 93 ServerString.this.onError(e);
0ff71477 94 }
79ce1a49
NR
95 };
96 }
97
98 /**
99 * This is the method that is called on each client request.
100 * <p>
101 * You are expected to react to it and return an answer (NULL will be
102 * converted to an empty {@link String}).
103 *
104 * @param action
105 * the client action
106 * @param clientVersion
107 * the client version
108 * @param data
109 * the data sent by the client
110 *
111 * @return the answer to return to the client
112 *
113 * @throws Exception
114 * in case of an exception, the error will only be logged
115 */
116 abstract protected String onRequest(ConnectActionServerString action,
117 Version clientVersion, String data) throws Exception;
118}