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 public void connect() {
47 in
= new BufferedReader(new InputStreamReader(s
.getInputStream(),
50 out
= new OutputStreamWriter(s
.getOutputStream(), "UTF-8");
55 String v
= sendString("VERSION " + version
.toString());
56 if (v
!= null && v
.startsWith("VERSION ")) {
57 v
= v
.substring("VERSION ".length());
60 action(new Version(v
));
68 } catch (Exception e
) {
73 } catch (Exception e
) {
79 // (also, server never get anything)
80 public Object
send(Object data
) throws IOException
, NoSuchFieldException
,
81 NoSuchMethodException
, ClassNotFoundException
{
83 String rep
= sendString(new Exporter().append(data
).toString(true));
84 return new Importer().read(rep
).getValue();
88 public Object
flush() throws NoSuchFieldException
, NoSuchMethodException
,
89 ClassNotFoundException
, IOException
, java
.lang
.NullPointerException
{
90 String str
= flushString();
92 throw new NullPointerException("No more data from client");
95 return new Importer().read(str
).getValue();
99 * Handler called when the client {@link Version} is received.
101 * @param clientVersion
102 * the client {@link Version}
104 protected void onClientVersionReceived(
105 @SuppressWarnings("unused") Version clientVersion
) {
109 * Handler called when an unexpected error occurs in the code.
112 * the exception that occurred
114 protected void onError(@SuppressWarnings("unused") Exception e
) {
117 // \n included in line, but not in rep (also, server never get anything)
118 private String
sendString(String line
) throws IOException
{
119 synchronized (lock
) {
128 contentToSend
= true;
129 return flushString();
133 // server can receive something even without pending content
134 private String
flushString() throws IOException
{
135 synchronized (lock
) {
136 if (server
|| contentToSend
) {
139 contentToSend
= false;
142 String line
= in
.readLine();
143 if (server
&& line
!= null && line
.startsWith("VERSION ")) {
144 // "VERSION client-version" (VERSION 1.0.0)
145 Version clientVersion
= new Version(
146 line
.substring("VERSION ".length()));
147 onClientVersionReceived(clientVersion
);
148 sendString("VERSION " + version
.toString());
150 line
= in
.readLine();