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