count the bytes we receive/send
[nikiroo-utils.git] / src / be / nikiroo / utils / serial / server / ConnectAction.java
CommitLineData
79ce1a49 1package be.nikiroo.utils.serial.server;
ce0974c4
NR
2
3import java.io.BufferedReader;
4import java.io.IOException;
5import java.io.InputStreamReader;
6import java.io.OutputStreamWriter;
7import java.net.Socket;
8
d9e2136b
N
9import javax.net.ssl.SSLException;
10
ce0974c4 11import be.nikiroo.utils.Version;
79ce1a49
NR
12import be.nikiroo.utils.serial.Exporter;
13import be.nikiroo.utils.serial.Importer;
ce0974c4 14
f157aed8
NR
15/**
16 * Base class used for the client/server basic handling.
17 * <p>
18 * It represents a single action: a client is expected to only execute one
19 * action, while a server is expected to execute one action for each client
20 * action.
21 *
22 * @author niki
23 */
ce0974c4
NR
24abstract class ConnectAction {
25 private Socket s;
26 private boolean server;
27 private Version version;
f157aed8 28 private Version clientVersion;
ce0974c4
NR
29
30 private Object lock = new Object();
31 private BufferedReader in;
32 private OutputStreamWriter out;
33 private boolean contentToSend;
34
4bb7e88e
NR
35 private long bytesReceived;
36 private long bytesSent;
37
f157aed8
NR
38 /**
39 * Method that will be called when an action is performed on either the
40 * client or server this {@link ConnectAction} represent.
41 *
42 * @param version
43 * the counter part version
44 *
45 * @throws Exception
46 * in case of I/O error
47 */
48 abstract protected void action(Version version) throws Exception;
49
50 /**
51 * Method called when we negotiate the version with the client.
52 * <p>
53 * Thus, it is only called on the server.
54 * <p>
55 * Will return the actual server version by default.
56 *
57 * @param clientVersion
58 * the client version
59 *
60 * @return the version to send to the client
61 */
62 abstract protected Version negotiateVersion(Version clientVersion);
63
64 /**
65 * Handler called when an unexpected error occurs in the code.
66 *
67 * @param e
68 * the exception that occurred
69 */
70 abstract protected void onError(Exception e);
ce0974c4 71
f157aed8
NR
72 /**
73 * Create a new {@link ConnectAction}.
74 *
75 * @param s
76 * the socket to bind to
77 * @param server
78 * TRUE for a server action, FALSE for a client action (will
79 * impact the process)
80 * @param version
81 * the version of this client-or-server
82 */
ce0974c4
NR
83 protected ConnectAction(Socket s, boolean server, Version version) {
84 this.s = s;
85 this.server = server;
86
87 if (version == null) {
88 this.version = new Version();
89 } else {
90 this.version = version;
91 }
f157aed8
NR
92
93 clientVersion = new Version();
ce0974c4
NR
94 }
95
f157aed8
NR
96 /**
97 * The version of this client-or-server.
98 *
99 * @return the version
100 */
101 public Version getVersion() {
102 return version;
ce0974c4
NR
103 }
104
4bb7e88e
NR
105 /**
106 * The total amount of bytes received.
107 *
108 * @return the amount of bytes received
109 */
110 public long getBytesReceived() {
111 return bytesReceived;
112 }
113
114 /**
115 * The total amount of bytes sent.
116 *
117 * @return the amount of bytes sent
118 */
119 public long getBytesSent() {
120 return bytesSent;
121 }
122
f157aed8
NR
123 /**
124 * Actually start the process (this is synchronous).
125 */
ce0974c4
NR
126 public void connect() {
127 try {
4bb7e88e
NR
128 in = new BufferedReader(new InputStreamReader(s.getInputStream(),
129 "UTF-8"));
ce0974c4
NR
130 try {
131 out = new OutputStreamWriter(s.getOutputStream(), "UTF-8");
132 try {
133 if (server) {
217a3310
NR
134 String line = in.readLine();
135 if (line != null && line.startsWith("VERSION ")) {
136 // "VERSION client-version" (VERSION 1.0.0)
137 Version clientVersion = new Version(
138 line.substring("VERSION ".length()));
139 this.clientVersion = clientVersion;
140 Version v = negotiateVersion(clientVersion);
141 if (v == null) {
142 v = new Version();
143 }
144
145 sendString("VERSION " + v.toString());
146 }
147
f157aed8 148 action(clientVersion);
ce0974c4
NR
149 } else {
150 String v = sendString("VERSION " + version.toString());
151 if (v != null && v.startsWith("VERSION ")) {
152 v = v.substring("VERSION ".length());
153 }
154
155 action(new Version(v));
156 }
157 } finally {
158 out.close();
0988831f 159 out = null;
ce0974c4
NR
160 }
161 } finally {
162 in.close();
0988831f 163 in = null;
ce0974c4
NR
164 }
165 } catch (Exception e) {
d9e2136b
N
166 if (e instanceof SSLException) {
167 String ciphers = "";
168 for (String cipher : Server.getAnonCiphers()) {
169 if (!ciphers.isEmpty()) {
170 ciphers += ", ";
171 }
172 ciphers += cipher;
173 }
174
4bb7e88e
NR
175 e = new SSLException("SSL error (available SSL ciphers: "
176 + ciphers + ")", e);
d9e2136b 177 }
4bb7e88e 178
ce0974c4
NR
179 onError(e);
180 } finally {
181 try {
182 s.close();
183 } catch (Exception e) {
184 onError(e);
185 }
186 }
187 }
188
f157aed8
NR
189 /**
190 * Serialise and send the given object to the counter part (and, only for
191 * client, return the deserialised answer -- the server will always receive
192 * NULL).
193 *
194 * @param data
195 * the data to send
196 *
197 * @return the answer (which can be NULL) if this action is a client, always
198 * NULL if it is a server
199 *
200 * @throws IOException
201 * in case of I/O error
202 * @throws NoSuchFieldException
203 * if the serialised data contains information about a field
204 * which does actually not exist in the class we know of
205 * @throws NoSuchMethodException
206 * if a class described in the serialised data cannot be created
207 * because it is not compatible with this code
208 * @throws ClassNotFoundException
209 * if a class described in the serialised data cannot be found
210 */
4bb7e88e
NR
211 protected Object sendObject(Object data) throws IOException,
212 NoSuchFieldException, NoSuchMethodException, ClassNotFoundException {
ce0974c4 213 synchronized (lock) {
4bb7e88e
NR
214 String rep = sendString(new Exporter().append(data).toString(true,
215 true));
08a58812
NR
216 if (rep != null) {
217 return new Importer().read(rep).getValue();
218 }
219
220 return null;
ce0974c4
NR
221 }
222 }
223
f157aed8
NR
224 /**
225 * Reserved for the server: flush the data to the client and retrieve its
226 * answer.
227 * <p>
228 * Also used internally for the client (only do something if there is
229 * contentToSend).
230 * <p>
231 * Will only flush the data if there is contentToSend.
232 *
233 * @return the deserialised answer (which can actually be NULL)
234 *
235 * @throws IOException
236 * in case of I/O error
237 * @throws NoSuchFieldException
238 * if the serialised data contains information about a field
239 * which does actually not exist in the class we know of
240 * @throws NoSuchMethodException
241 * if a class described in the serialised data cannot be created
242 * because it is not compatible with this code
243 * @throws ClassNotFoundException
244 * if a class described in the serialised data cannot be found
245 * @throws java.lang.NullPointerException
246 * if the counter part has no data to send
247 */
4bb7e88e
NR
248 protected Object recObject() throws IOException, NoSuchFieldException,
249 NoSuchMethodException, ClassNotFoundException,
250 java.lang.NullPointerException {
79ce1a49 251 String str = recString();
ce0974c4 252 if (str == null) {
f157aed8 253 throw new NullPointerException("No more data available");
ce0974c4
NR
254 }
255
256 return new Importer().read(str).getValue();
257 }
258
cd0c27d2 259 /**
f157aed8
NR
260 * Send the given string to the counter part (and, only for client, return
261 * the answer -- the server will always receive NULL).
cd0c27d2 262 *
f157aed8
NR
263 * @param line
264 * the data to send (we will add a line feed)
cd0c27d2 265 *
f157aed8
NR
266 * @return the answer if this action is a client (without the added line
267 * feed), NULL if it is a server
268 *
269 * @throws IOException
270 * in case of I/O error
cd0c27d2 271 */
f157aed8 272 protected String sendString(String line) throws IOException {
ce0974c4
NR
273 synchronized (lock) {
274 out.write(line);
275 out.write("\n");
4bb7e88e 276 bytesSent += line.length() + 1;
ce0974c4
NR
277
278 if (server) {
279 out.flush();
280 return null;
ce0974c4 281 }
cd0c27d2
NR
282
283 contentToSend = true;
79ce1a49 284 return recString();
ce0974c4
NR
285 }
286 }
287
f157aed8
NR
288 /**
289 * Reserved for the server (externally): flush the data to the client and
290 * retrieve its answer.
291 * <p>
292 * Also used internally for the client (only do something if there is
293 * contentToSend).
294 * <p>
295 * Will only flush the data if there is contentToSend.
296 *
297 * @return the answer (which can be NULL)
298 *
299 * @throws IOException
300 * in case of I/O error
301 */
79ce1a49 302 protected String recString() throws IOException {
ce0974c4
NR
303 synchronized (lock) {
304 if (server || contentToSend) {
305 if (contentToSend) {
306 out.flush();
307 contentToSend = false;
308 }
309
4bb7e88e
NR
310 String line = in.readLine();
311 if (line != null) {
312 bytesReceived += line.length();
313 }
314
315 return line;
ce0974c4 316 }
cd0c27d2
NR
317
318 return null;
ce0974c4
NR
319 }
320 }
321}