server: re-introduce the client/server versions
[fanfix.git] / src / be / nikiroo / utils / serial / server / ServerObject.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 6
3087aeb5
NR
7import be.nikiroo.utils.Version;
8
79ce1a49
NR
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 */
18abstract 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()}
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 ServerObject(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 ServerObject#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 ServerObject(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 ConnectActionServerObject(s, key) {
79ce1a49 70 @Override
3087aeb5 71 public void action(Version clientVersion) throws Exception {
ae0ca537 72 long id = getNextId();
79ce1a49
NR
73 try {
74 for (Object data = rec(); true; data = rec()) {
75 Object rep = null;
76 try {
ae0ca537 77 rep = onRequest(this, data, id);
a72ea8a7
NR
78 if (isClosing()) {
79 return;
80 }
79ce1a49
NR
81 } catch (Exception e) {
82 onError(e);
83 }
08f80ac5 84
79ce1a49
NR
85 send(rep);
86 }
87 } catch (NullPointerException e) {
88 // Client has no data any more, we quit
ae0ca537 89 onRequestDone(id, getBytesReceived(), getBytesSent());
79ce1a49
NR
90 }
91 }
0ff71477
NR
92
93 @Override
94 protected void onError(Exception e) {
d827da2a 95 ServerObject.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 ConnectActionClientObject(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 (which can be NULL).
111 *
112 * @param action
113 * the client action
79ce1a49
NR
114 * @param data
115 * the data sent by the client (which can be NULL)
ae0ca537
NR
116 * @param id
117 * an ID to identify this request (will also be re-used for
118 * {@link ServerObject#onRequestDone(long, long, long)}.
79ce1a49
NR
119 *
120 * @return the answer to return to the client (which can be NULL)
121 *
122 * @throws Exception
123 * in case of an exception, the error will only be logged
124 */
125 abstract protected Object onRequest(ConnectActionServerObject action,
ae0ca537 126 Object data, long id) throws Exception;
79ce1a49 127}