Auto cache cleaning + better err in ImageUtilsAnd
[fanfix.git] / src / be / nikiroo / utils / serial / server / ConnectAction.java
... / ...
CommitLineData
1package be.nikiroo.utils.serial.server;
2
3import java.io.BufferedReader;
4import java.io.IOException;
5import java.io.InputStreamReader;
6import java.io.OutputStreamWriter;
7import java.net.Socket;
8
9import be.nikiroo.utils.Version;
10import be.nikiroo.utils.serial.Exporter;
11import be.nikiroo.utils.serial.Importer;
12
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 */
22abstract class ConnectAction {
23 private Socket s;
24 private boolean server;
25 private Version version;
26 private Version clientVersion;
27
28 private Object lock = new Object();
29 private BufferedReader in;
30 private OutputStreamWriter out;
31 private boolean contentToSend;
32
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);
66
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 */
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 }
87
88 clientVersion = new Version();
89 }
90
91 /**
92 * The version of this client-or-server.
93 *
94 * @return the version
95 */
96 public Version getVersion() {
97 return version;
98 }
99
100 /**
101 * Actually start the process (this is synchronous).
102 */
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) {
111 String line = in.readLine();
112 if (line != null && line.startsWith("VERSION ")) {
113 // "VERSION client-version" (VERSION 1.0.0)
114 Version clientVersion = new Version(
115 line.substring("VERSION ".length()));
116 this.clientVersion = clientVersion;
117 Version v = negotiateVersion(clientVersion);
118 if (v == null) {
119 v = new Version();
120 }
121
122 sendString("VERSION " + v.toString());
123 }
124
125 action(clientVersion);
126 } else {
127 String v = sendString("VERSION " + version.toString());
128 if (v != null && v.startsWith("VERSION ")) {
129 v = v.substring("VERSION ".length());
130 }
131
132 action(new Version(v));
133 }
134 } finally {
135 out.close();
136 }
137 } finally {
138 in.close();
139 }
140 } catch (Exception e) {
141 onError(e);
142 } finally {
143 try {
144 s.close();
145 } catch (Exception e) {
146 onError(e);
147 }
148 }
149 }
150
151 /**
152 * Serialise and send the given object to the counter part (and, only for
153 * client, return the deserialised answer -- the server will always receive
154 * NULL).
155 *
156 * @param data
157 * the data to send
158 *
159 * @return the answer (which can be NULL) if this action is a client, always
160 * NULL if it is a server
161 *
162 * @throws IOException
163 * in case of I/O error
164 * @throws NoSuchFieldException
165 * if the serialised data contains information about a field
166 * which does actually not exist in the class we know of
167 * @throws NoSuchMethodException
168 * if a class described in the serialised data cannot be created
169 * because it is not compatible with this code
170 * @throws ClassNotFoundException
171 * if a class described in the serialised data cannot be found
172 */
173 protected Object sendObject(Object data) throws IOException,
174 NoSuchFieldException, NoSuchMethodException, ClassNotFoundException {
175 synchronized (lock) {
176 String rep = sendString(new Exporter().append(data).toString(true));
177 if (rep != null) {
178 return new Importer().read(rep).getValue();
179 }
180
181 return null;
182 }
183 }
184
185 /**
186 * Reserved for the server: flush the data to the client and retrieve its
187 * answer.
188 * <p>
189 * Also used internally for the client (only do something if there is
190 * contentToSend).
191 * <p>
192 * Will only flush the data if there is contentToSend.
193 *
194 * @return the deserialised answer (which can actually be NULL)
195 *
196 * @throws IOException
197 * in case of I/O error
198 * @throws NoSuchFieldException
199 * if the serialised data contains information about a field
200 * which does actually not exist in the class we know of
201 * @throws NoSuchMethodException
202 * if a class described in the serialised data cannot be created
203 * because it is not compatible with this code
204 * @throws ClassNotFoundException
205 * if a class described in the serialised data cannot be found
206 * @throws java.lang.NullPointerException
207 * if the counter part has no data to send
208 */
209 protected Object recObject() throws IOException, NoSuchFieldException,
210 NoSuchMethodException, ClassNotFoundException,
211 java.lang.NullPointerException {
212 String str = recString();
213 if (str == null) {
214 throw new NullPointerException("No more data available");
215 }
216
217 return new Importer().read(str).getValue();
218 }
219
220 /**
221 * Send the given string to the counter part (and, only for client, return
222 * the answer -- the server will always receive NULL).
223 *
224 * @param line
225 * the data to send (we will add a line feed)
226 *
227 * @return the answer if this action is a client (without the added line
228 * feed), NULL if it is a server
229 *
230 * @throws IOException
231 * in case of I/O error
232 */
233 protected String sendString(String line) throws IOException {
234 synchronized (lock) {
235 out.write(line);
236 out.write("\n");
237
238 if (server) {
239 out.flush();
240 return null;
241 }
242
243 contentToSend = true;
244 return recString();
245 }
246 }
247
248 /**
249 * Reserved for the server (externally): flush the data to the client and
250 * retrieve its answer.
251 * <p>
252 * Also used internally for the client (only do something if there is
253 * contentToSend).
254 * <p>
255 * Will only flush the data if there is contentToSend.
256 *
257 * @return the answer (which can be NULL)
258 *
259 * @throws IOException
260 * in case of I/O error
261 */
262 protected String recString() throws IOException {
263 synchronized (lock) {
264 if (server || contentToSend) {
265 if (contentToSend) {
266 out.flush();
267 contentToSend = false;
268 }
269
270 return in.readLine();
271 }
272
273 return null;
274 }
275 }
276}