1 package be
.nikiroo
.utils
.serial
;
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
;
11 abstract class ConnectAction
{
13 private boolean server
;
14 private Version version
;
16 private Object lock
= new Object();
17 private BufferedReader in
;
18 private OutputStreamWriter out
;
19 private boolean contentToSend
;
21 // serverVersion = null on server (or bad clients)
22 abstract public void action(Version serverVersion
) throws Exception
;
24 // server = version NULL
25 protected ConnectAction(Socket s
, boolean server
, Version version
) {
29 if (version
== null) {
30 this.version
= new Version();
32 this.version
= version
;
36 public void connectAsync() {
37 new Thread(new Runnable() {
45 // connect, do the action (sync)
46 public void connect() {
48 in
= new BufferedReader(new InputStreamReader(s
.getInputStream(),
51 out
= new OutputStreamWriter(s
.getOutputStream(), "UTF-8");
56 String v
= sendString("VERSION " + version
.toString());
57 if (v
!= null && v
.startsWith("VERSION ")) {
58 v
= v
.substring("VERSION ".length());
61 action(new Version(v
));
69 } catch (Exception e
) {
74 } catch (Exception e
) {
80 // (also, server never get anything)
81 public Object
send(Object data
) throws IOException
, NoSuchFieldException
,
82 NoSuchMethodException
, ClassNotFoundException
{
84 String rep
= sendString(new Exporter().append(data
).toString(true));
86 return new Importer().read(rep
).getValue();
93 public Object
flush() throws NoSuchFieldException
, NoSuchMethodException
,
94 ClassNotFoundException
, IOException
, java
.lang
.NullPointerException
{
95 String str
= flushString();
97 throw new NullPointerException("No more data from client");
100 return new Importer().read(str
).getValue();
104 * Handler called when the client {@link Version} is received.
106 * @param clientVersion
107 * the client {@link Version}
109 protected void onClientVersionReceived(
110 @SuppressWarnings("unused") Version clientVersion
) {
114 * Handler called when an unexpected error occurs in the code.
117 * the exception that occurred
119 protected void onError(@SuppressWarnings("unused") Exception e
) {
122 // \n included in line, but not in rep (also, server never get anything)
123 private String
sendString(String line
) throws IOException
{
124 synchronized (lock
) {
133 contentToSend
= true;
134 return flushString();
138 // server can receive something even without pending content
139 private String
flushString() throws IOException
{
140 synchronized (lock
) {
141 if (server
|| contentToSend
) {
144 contentToSend
= false;
147 String line
= in
.readLine();
148 if (server
&& line
!= null && line
.startsWith("VERSION ")) {
149 // "VERSION client-version" (VERSION 1.0.0)
150 Version clientVersion
= new Version(
151 line
.substring("VERSION ".length()));
152 onClientVersionReceived(clientVersion
);
153 sendString("VERSION " + version
.toString());
155 line
= in
.readLine();