Commit | Line | Data |
---|---|---|
79ce1a49 NR |
1 | package be.nikiroo.utils.serial.server; |
2 | ||
3 | import java.io.IOException; | |
4 | import java.net.Socket; | |
5 | ||
6 | import be.nikiroo.utils.Version; | |
7 | ||
8 | /** | |
9 | * This class implements a simple server that can listen for connections and | |
10 | * send/receive Strings. | |
11 | * <p> | |
12 | * Note: this {@link ServerString} has to be discarded after use (cannot be | |
13 | * started twice). | |
14 | * | |
15 | * @author niki | |
16 | */ | |
17 | abstract public class ServerString extends Server { | |
18 | /** | |
19 | * Create a new server that will start listening on the network when | |
20 | * {@link ServerString#start()} is called. | |
21 | * | |
22 | * @param port | |
23 | * the port to listen on, or 0 to assign any unallocated port | |
24 | * found (which can later on be queried via | |
25 | * {@link ServerString#getPort()} | |
26 | * @param ssl | |
27 | * use a SSL connection (or not) | |
28 | * | |
29 | * @throws IOException | |
30 | * in case of I/O error | |
31 | */ | |
32 | public ServerString(int port, boolean ssl) throws IOException { | |
33 | super(port, ssl); | |
34 | } | |
35 | ||
36 | /** | |
37 | * Create a new server that will start listening on the network when | |
38 | * {@link ServerString#start()} is called. | |
39 | * | |
40 | * @param name | |
41 | * the server name (only used for debug info and traces) | |
42 | * @param port | |
43 | * the port to listen on | |
44 | * @param ssl | |
45 | * use a SSL connection (or not) | |
46 | * | |
47 | * @throws IOException | |
48 | * in case of I/O error | |
49 | */ | |
50 | public ServerString(String name, int port, boolean ssl) throws IOException { | |
51 | super(name, port, ssl); | |
52 | } | |
53 | ||
54 | @Override | |
55 | protected ConnectActionServer createConnectActionServer(Socket s) { | |
56 | return new ConnectActionServerString(s) { | |
57 | @Override | |
58 | public void action(Version clientVersion) throws Exception { | |
8537d55a NR |
59 | for (String data = rec(); data != null; data = rec()) { |
60 | String rep = null; | |
61 | try { | |
62 | rep = onRequest(this, clientVersion, data); | |
63 | } catch (Exception e) { | |
64 | onError(e); | |
65 | } | |
79ce1a49 | 66 | |
8537d55a NR |
67 | if (rep == null) { |
68 | rep = ""; | |
79ce1a49 | 69 | } |
79ce1a49 | 70 | |
8537d55a | 71 | send(rep); |
79ce1a49 NR |
72 | } |
73 | } | |
0ff71477 NR |
74 | |
75 | @Override | |
76 | protected void onError(Exception e) { | |
d827da2a | 77 | ServerString.this.onError(e); |
0ff71477 | 78 | } |
79ce1a49 NR |
79 | }; |
80 | } | |
81 | ||
82 | /** | |
83 | * This is the method that is called on each client request. | |
84 | * <p> | |
85 | * You are expected to react to it and return an answer (NULL will be | |
86 | * converted to an empty {@link String}). | |
87 | * | |
88 | * @param action | |
89 | * the client action | |
90 | * @param clientVersion | |
91 | * the client version | |
92 | * @param data | |
93 | * the data sent by the client | |
94 | * | |
95 | * @return the answer to return to the client | |
96 | * | |
97 | * @throws Exception | |
98 | * in case of an exception, the error will only be logged | |
99 | */ | |
100 | abstract protected String onRequest(ConnectActionServerString action, | |
101 | Version clientVersion, String data) throws Exception; | |
102 | } |