Commit | Line | Data |
---|---|---|
79ce1a49 NR |
1 | package be.nikiroo.utils.serial.server; |
2 | ||
3 | import java.io.IOException; | |
4 | import java.net.Socket; | |
f4053377 | 5 | import java.net.UnknownHostException; |
79ce1a49 | 6 | |
3087aeb5 NR |
7 | import be.nikiroo.utils.Version; |
8 | ||
79ce1a49 NR |
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()} | |
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 | 70 | @Override |
3087aeb5 | 71 | public void action(Version clientVersion) throws Exception { |
ae0ca537 | 72 | long id = getNextId(); |
8537d55a NR |
73 | for (String data = rec(); data != null; data = rec()) { |
74 | String rep = null; | |
75 | try { | |
28c42081 | 76 | rep = onRequest(this, clientVersion, data, id); |
a72ea8a7 NR |
77 | if (isClosing()) { |
78 | return; | |
79 | } | |
8537d55a NR |
80 | } catch (Exception e) { |
81 | onError(e); | |
82 | } | |
79ce1a49 | 83 | |
8537d55a NR |
84 | if (rep == null) { |
85 | rep = ""; | |
79ce1a49 | 86 | } |
8537d55a | 87 | send(rep); |
79ce1a49 | 88 | } |
ae0ca537 NR |
89 | |
90 | onRequestDone(id, getBytesReceived(), getBytesSent()); | |
79ce1a49 | 91 | } |
0ff71477 NR |
92 | |
93 | @Override | |
94 | protected void onError(Exception e) { | |
d827da2a | 95 | ServerString.this.onError(e); |
0ff71477 | 96 | } |
79ce1a49 NR |
97 | }; |
98 | } | |
99 | ||
340e6065 NR |
100 | @Override |
101 | protected ConnectActionClient getConnectionToMe() | |
102 | throws UnknownHostException, IOException { | |
103 | return new ConnectActionClientString(new Socket((String) null, | |
104 | getPort()), key); | |
105 | } | |
106 | ||
79ce1a49 NR |
107 | /** |
108 | * This is the method that is called on each client request. | |
109 | * <p> | |
110 | * You are expected to react to it and return an answer (NULL will be | |
111 | * converted to an empty {@link String}). | |
112 | * | |
113 | * @param action | |
114 | * the client action | |
28c42081 NR |
115 | * @param clientVersion |
116 | * the client version | |
79ce1a49 NR |
117 | * @param data |
118 | * the data sent by the client | |
ae0ca537 NR |
119 | * @param id |
120 | * an ID to identify this request (will also be re-used for | |
121 | * {@link ServerObject#onRequestDone(long, long, long)}. | |
79ce1a49 NR |
122 | * |
123 | * @return the answer to return to the client | |
124 | * | |
125 | * @throws Exception | |
126 | * in case of an exception, the error will only be logged | |
127 | */ | |
28c42081 NR |
128 | protected String onRequest(ConnectActionServerString action, |
129 | Version clientVersion, String data, | |
130 | @SuppressWarnings("unused") long id) throws Exception { | |
131 | // TODO: change to abstract when deprecated method is removed | |
132 | // Default implementation for compat | |
133 | return onRequest(action, clientVersion, data); | |
134 | } | |
135 | ||
136 | // Deprecated // | |
137 | ||
138 | /** | |
139 | * @deprecated SSL support has been replaced by key-based encryption. | |
140 | * <p> | |
141 | * Please use the version with key encryption (this deprecated | |
142 | * version uses an empty key when <tt>ssl</tt> is TRUE and no | |
143 | * key (NULL) when <tt>ssl</tt> is FALSE). | |
144 | */ | |
145 | @Deprecated | |
146 | public ServerString(int port, boolean ssl) throws IOException { | |
147 | this(port, ssl ? "" : null); | |
148 | } | |
149 | ||
150 | /** | |
151 | * @deprecated SSL support has been replaced by key-based encryption. | |
152 | * <p> | |
153 | * Please use the version with key encryption (this deprecated | |
154 | * version uses an empty key when <tt>ssl</tt> is TRUE and no | |
155 | * key (NULL) when <tt>ssl</tt> is FALSE). | |
156 | */ | |
157 | @Deprecated | |
158 | public ServerString(String name, int port, boolean ssl) throws IOException { | |
159 | this(name, port, ssl ? "" : null); | |
160 | } | |
161 | ||
162 | /** | |
163 | * Will be called if the correct version is not overrided. | |
164 | * | |
165 | * @deprecated use the version with the id. | |
166 | * | |
167 | * @param action | |
168 | * the client action | |
169 | * @param data | |
170 | * the data sent by the client | |
171 | * | |
172 | * @return the answer to return to the client | |
173 | * | |
174 | * @throws Exception | |
175 | * in case of an exception, the error will only be logged | |
176 | */ | |
177 | @Deprecated | |
178 | @SuppressWarnings("unused") | |
179 | protected String onRequest(ConnectActionServerString action, | |
180 | Version version, String data) throws Exception { | |
181 | return null; | |
182 | } | |
79ce1a49 | 183 | } |