Improve TraceHandler, fix server system + tests:
[nikiroo-utils.git] / src / be / nikiroo / utils / serial / ConnectAction.java
CommitLineData
ce0974c4
NR
1package be.nikiroo.utils.serial;
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;
10
11abstract class ConnectAction {
12 private Socket s;
13 private boolean server;
14 private Version version;
15
16 private Object lock = new Object();
17 private BufferedReader in;
18 private OutputStreamWriter out;
19 private boolean contentToSend;
20
21 // serverVersion = null on server (or bad clients)
22 abstract public void action(Version serverVersion) throws Exception;
23
24 // server = version NULL
25 protected ConnectAction(Socket s, boolean server, Version version) {
26 this.s = s;
27 this.server = server;
28
29 if (version == null) {
30 this.version = new Version();
31 } else {
32 this.version = version;
33 }
34 }
35
36 public void connectAsync() {
37 new Thread(new Runnable() {
cd0c27d2 38 @Override
ce0974c4
NR
39 public void run() {
40 connect();
41 }
42 }).start();
43 }
44
08a58812 45 // connect, do the action (sync)
ce0974c4
NR
46 public void connect() {
47 try {
48 in = new BufferedReader(new InputStreamReader(s.getInputStream(),
49 "UTF-8"));
50 try {
51 out = new OutputStreamWriter(s.getOutputStream(), "UTF-8");
52 try {
53 if (server) {
54 action(version);
55 } else {
56 String v = sendString("VERSION " + version.toString());
57 if (v != null && v.startsWith("VERSION ")) {
58 v = v.substring("VERSION ".length());
59 }
60
61 action(new Version(v));
62 }
63 } finally {
64 out.close();
65 }
66 } finally {
67 in.close();
68 }
69 } catch (Exception e) {
70 onError(e);
71 } finally {
72 try {
73 s.close();
74 } catch (Exception e) {
75 onError(e);
76 }
77 }
78 }
79
80 // (also, server never get anything)
81 public Object send(Object data) throws IOException, NoSuchFieldException,
82 NoSuchMethodException, ClassNotFoundException {
83 synchronized (lock) {
84 String rep = sendString(new Exporter().append(data).toString(true));
08a58812
NR
85 if (rep != null) {
86 return new Importer().read(rep).getValue();
87 }
88
89 return null;
ce0974c4
NR
90 }
91 }
92
93 public Object flush() throws NoSuchFieldException, NoSuchMethodException,
94 ClassNotFoundException, IOException, java.lang.NullPointerException {
95 String str = flushString();
96 if (str == null) {
97 throw new NullPointerException("No more data from client");
98 }
99
100 return new Importer().read(str).getValue();
101 }
102
cd0c27d2
NR
103 /**
104 * Handler called when the client {@link Version} is received.
105 *
106 * @param clientVersion
107 * the client {@link Version}
108 */
109 protected void onClientVersionReceived(
110 @SuppressWarnings("unused") Version clientVersion) {
ce0974c4
NR
111 }
112
cd0c27d2
NR
113 /**
114 * Handler called when an unexpected error occurs in the code.
115 *
116 * @param e
117 * the exception that occurred
118 */
119 protected void onError(@SuppressWarnings("unused") Exception e) {
ce0974c4
NR
120 }
121
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) {
125 out.write(line);
126 out.write("\n");
127
128 if (server) {
129 out.flush();
130 return null;
ce0974c4 131 }
cd0c27d2
NR
132
133 contentToSend = true;
134 return flushString();
ce0974c4
NR
135 }
136 }
137
138 // server can receive something even without pending content
139 private String flushString() throws IOException {
140 synchronized (lock) {
141 if (server || contentToSend) {
142 if (contentToSend) {
143 out.flush();
144 contentToSend = false;
145 }
146
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());
154
155 line = in.readLine();
156 }
157
158 return line;
ce0974c4 159 }
cd0c27d2
NR
160
161 return null;
ce0974c4
NR
162 }
163 }
164}