Version 3.1.6: fix Bridge, Serialiser, Progress:
[nikiroo-utils.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()}
27 * @param ssl
28 * use a SSL connection (or not)
29 *
30 * @throws IOException
31 * in case of I/O error
f4053377
NR
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
79ce1a49
NR
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
f4053377
NR
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
79ce1a49
NR
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 {
8537d55a
NR
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 }
79ce1a49 77
8537d55a
NR
78 if (rep == null) {
79 rep = "";
79ce1a49 80 }
79ce1a49 81
8537d55a 82 send(rep);
79ce1a49
NR
83 }
84 }
0ff71477
NR
85
86 @Override
87 protected void onError(Exception e) {
d827da2a 88 ServerString.this.onError(e);
0ff71477 89 }
79ce1a49
NR
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}