1 package be
.nikiroo
.utils
.serial
.server
;
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
;
9 import be
.nikiroo
.utils
.Version
;
10 import be
.nikiroo
.utils
.serial
.Exporter
;
11 import be
.nikiroo
.utils
.serial
.Importer
;
14 * Base class used for the client/server basic handling.
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
22 abstract class ConnectAction
{
24 private boolean server
;
25 private Version version
;
26 private Version clientVersion
;
28 private Object lock
= new Object();
29 private BufferedReader in
;
30 private OutputStreamWriter out
;
31 private boolean contentToSend
;
34 * Method that will be called when an action is performed on either the
35 * client or server this {@link ConnectAction} represent.
38 * the counter part version
41 * in case of I/O error
43 abstract protected void action(Version version
) throws Exception
;
46 * Method called when we negotiate the version with the client.
48 * Thus, it is only called on the server.
50 * Will return the actual server version by default.
52 * @param clientVersion
55 * @return the version to send to the client
57 abstract protected Version
negotiateVersion(Version clientVersion
);
60 * Handler called when an unexpected error occurs in the code.
63 * the exception that occurred
65 abstract protected void onError(Exception e
);
68 * Create a new {@link ConnectAction}.
71 * the socket to bind to
73 * TRUE for a server action, FALSE for a client action (will
76 * the version of this client-or-server
78 protected ConnectAction(Socket s
, boolean server
, Version version
) {
82 if (version
== null) {
83 this.version
= new Version();
85 this.version
= version
;
88 clientVersion
= new Version();
92 * The version of this client-or-server.
96 public Version
getVersion() {
101 * Actually start the process (this is synchronous).
103 public void connect() {
105 in
= new BufferedReader(new InputStreamReader(s
.getInputStream(),
108 out
= new OutputStreamWriter(s
.getOutputStream(), "UTF-8");
111 action(clientVersion
);
113 String v
= sendString("VERSION " + version
.toString());
114 if (v
!= null && v
.startsWith("VERSION ")) {
115 v
= v
.substring("VERSION ".length());
118 action(new Version(v
));
126 } catch (Exception e
) {
131 } catch (Exception e
) {
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
145 * @return the answer (which can be NULL) if this action is a client, always
146 * NULL if it is a server
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
159 protected Object
sendObject(Object data
) throws IOException
,
160 NoSuchFieldException
, NoSuchMethodException
, ClassNotFoundException
{
161 synchronized (lock
) {
162 String rep
= sendString(new Exporter().append(data
).toString(true));
164 return new Importer().read(rep
).getValue();
172 * Reserved for the server: flush the data to the client and retrieve its
175 * Also used internally for the client (only do something if there is
178 * Will only flush the data if there is contentToSend.
180 * @return the deserialised answer (which can actually be NULL)
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
195 protected Object
recObject() throws IOException
, NoSuchFieldException
,
196 NoSuchMethodException
, ClassNotFoundException
,
197 java
.lang
.NullPointerException
{
198 String str
= recString();
200 throw new NullPointerException("No more data available");
203 return new Importer().read(str
).getValue();
207 * Send the given string to the counter part (and, only for client, return
208 * the answer -- the server will always receive NULL).
211 * the data to send (we will add a line feed)
213 * @return the answer if this action is a client (without the added line
214 * feed), NULL if it is a server
216 * @throws IOException
217 * in case of I/O error
219 protected String
sendString(String line
) throws IOException
{
220 synchronized (lock
) {
229 contentToSend
= true;
235 * Reserved for the server (externally): flush the data to the client and
236 * retrieve its answer.
238 * Also used internally for the client (only do something if there is
241 * Will only flush the data if there is contentToSend.
243 * @return the answer (which can be NULL)
245 * @throws IOException
246 * in case of I/O error
248 protected String
recString() throws IOException
{
249 synchronized (lock
) {
250 if (server
|| contentToSend
) {
253 contentToSend
= false;
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()));
261 this.clientVersion
= clientVersion
;
262 Version v
= negotiateVersion(clientVersion
);
266 sendString("VERSION " + v
.toString());
268 line
= in
.readLine();