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 | |
79ce1a49 NR |
7 | /** |
8 | * This class implements a simple server that can listen for connections and | |
9 | * send/receive Strings. | |
10 | * <p> | |
11 | * Note: this {@link ServerString} has to be discarded after use (cannot be | |
12 | * started twice). | |
13 | * | |
14 | * @author niki | |
15 | */ | |
16 | abstract public class ServerString extends Server { | |
17 | /** | |
18 | * Create a new server that will start listening on the network when | |
19 | * {@link ServerString#start()} is called. | |
20 | * | |
21 | * @param port | |
22 | * the port to listen on, or 0 to assign any unallocated port | |
23 | * found (which can later on be queried via | |
24 | * {@link ServerString#getPort()} | |
8468bb79 NR |
25 | * @param key |
26 | * an optional key to encrypt all the communications (if NULL, | |
27 | * everything will be sent in clear text) | |
79ce1a49 NR |
28 | * |
29 | * @throws IOException | |
30 | * in case of I/O error | |
f4053377 NR |
31 | * @throws UnknownHostException |
32 | * if the IP address of the host could not be determined | |
33 | * @throws IllegalArgumentException | |
34 | * if the port parameter is outside the specified range of valid | |
35 | * port values, which is between 0 and 65535, inclusive | |
79ce1a49 | 36 | */ |
8468bb79 NR |
37 | public ServerString(int port, String key) throws IOException { |
38 | super(port, key); | |
79ce1a49 NR |
39 | } |
40 | ||
41 | /** | |
42 | * Create a new server that will start listening on the network when | |
43 | * {@link ServerString#start()} is called. | |
44 | * | |
45 | * @param name | |
46 | * the server name (only used for debug info and traces) | |
47 | * @param port | |
48 | * the port to listen on | |
8468bb79 NR |
49 | * @param key |
50 | * an optional key to encrypt all the communications (if NULL, | |
51 | * everything will be sent in clear text) | |
79ce1a49 NR |
52 | * |
53 | * @throws IOException | |
54 | * in case of I/O error | |
f4053377 NR |
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 | |
79ce1a49 | 60 | */ |
8468bb79 NR |
61 | public ServerString(String name, int port, String key) throws IOException { |
62 | super(name, port, key); | |
79ce1a49 NR |
63 | } |
64 | ||
65 | @Override | |
66 | protected ConnectActionServer createConnectActionServer(Socket s) { | |
8468bb79 | 67 | return new ConnectActionServerString(s, key) { |
79ce1a49 | 68 | @Override |
08f80ac5 | 69 | public void action() throws Exception { |
ae0ca537 | 70 | long id = getNextId(); |
8537d55a NR |
71 | for (String data = rec(); data != null; data = rec()) { |
72 | String rep = null; | |
73 | try { | |
ae0ca537 | 74 | rep = onRequest(this, data, id); |
a72ea8a7 NR |
75 | if (isClosing()) { |
76 | return; | |
77 | } | |
8537d55a NR |
78 | } catch (Exception e) { |
79 | onError(e); | |
80 | } | |
79ce1a49 | 81 | |
8537d55a NR |
82 | if (rep == null) { |
83 | rep = ""; | |
79ce1a49 | 84 | } |
8537d55a | 85 | send(rep); |
79ce1a49 | 86 | } |
ae0ca537 NR |
87 | |
88 | onRequestDone(id, getBytesReceived(), getBytesSent()); | |
79ce1a49 | 89 | } |
0ff71477 NR |
90 | |
91 | @Override | |
92 | protected void onError(Exception e) { | |
d827da2a | 93 | ServerString.this.onError(e); |
0ff71477 | 94 | } |
79ce1a49 NR |
95 | }; |
96 | } | |
97 | ||
340e6065 NR |
98 | @Override |
99 | protected ConnectActionClient getConnectionToMe() | |
100 | throws UnknownHostException, IOException { | |
101 | return new ConnectActionClientString(new Socket((String) null, | |
102 | getPort()), key); | |
103 | } | |
104 | ||
79ce1a49 NR |
105 | /** |
106 | * This is the method that is called on each client request. | |
107 | * <p> | |
108 | * You are expected to react to it and return an answer (NULL will be | |
109 | * converted to an empty {@link String}). | |
110 | * | |
111 | * @param action | |
112 | * the client action | |
79ce1a49 NR |
113 | * @param data |
114 | * the data sent by the client | |
ae0ca537 NR |
115 | * @param id |
116 | * an ID to identify this request (will also be re-used for | |
117 | * {@link ServerObject#onRequestDone(long, long, long)}. | |
79ce1a49 NR |
118 | * |
119 | * @return the answer to return to the client | |
120 | * | |
121 | * @throws Exception | |
122 | * in case of an exception, the error will only be logged | |
123 | */ | |
124 | abstract protected String onRequest(ConnectActionServerString action, | |
ae0ca537 | 125 | String data, long id) throws Exception; |
79ce1a49 | 126 | } |