Version 3.1.6: fix Bridge, Serialiser, Progress:
[nikiroo-utils.git] / src / be / nikiroo / utils / serial / server / ServerObject.java
1 package be.nikiroo.utils.serial.server;
2
3 import java.io.IOException;
4 import java.net.Socket;
5 import java.net.UnknownHostException;
6
7 import be.nikiroo.utils.Version;
8
9 /**
10 * This class implements a simple server that can listen for connections and
11 * send/receive objects.
12 * <p>
13 * Note: this {@link ServerObject} has to be discarded after use (cannot be
14 * started twice).
15 *
16 * @author niki
17 */
18 abstract public class ServerObject extends Server {
19 /**
20 * Create a new server that will start listening on the network when
21 * {@link ServerObject#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 ServerObject#getPort()}
27 * @param ssl
28 * use a SSL connection (or not)
29 *
30 * @throws IOException
31 * in case of I/O error
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
37 */
38 public ServerObject(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 ServerObject#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
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
60 */
61 public ServerObject(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 ConnectActionServerObject(s) {
68 @Override
69 public void action(Version clientVersion) throws Exception {
70 try {
71 for (Object data = rec(); true; data = rec()) {
72 Object rep = null;
73 try {
74 rep = onRequest(this, clientVersion, data);
75 } catch (Exception e) {
76 onError(e);
77 }
78 send(rep);
79 }
80 } catch (NullPointerException e) {
81 // Client has no data any more, we quit
82 }
83 }
84
85 @Override
86 protected void onError(Exception e) {
87 ServerObject.this.onError(e);
88 }
89 };
90 }
91
92 /**
93 * This is the method that is called on each client request.
94 * <p>
95 * You are expected to react to it and return an answer (which can be NULL).
96 *
97 * @param action
98 * the client action
99 * @param clientVersion
100 * the client version
101 * @param data
102 * the data sent by the client (which can be NULL)
103 *
104 * @return the answer to return to the client (which can be NULL)
105 *
106 * @throws Exception
107 * in case of an exception, the error will only be logged
108 */
109 abstract protected Object onRequest(ConnectActionServerObject action,
110 Version clientVersion, Object data) throws Exception;
111 }