1f9833624c552516efa821c214cbb74765f82a55
[nikiroo-utils.git] / src / be / nikiroo / utils / serial / ConnectAction.java
1 package be.nikiroo.utils.serial;
2
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;
8
9 import be.nikiroo.utils.Version;
10
11 abstract 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() {
38 @Override
39 public void run() {
40 connect();
41 }
42 }).start();
43 }
44
45 public void connect() {
46 try {
47 in = new BufferedReader(new InputStreamReader(s.getInputStream(),
48 "UTF-8"));
49 try {
50 out = new OutputStreamWriter(s.getOutputStream(), "UTF-8");
51 try {
52 if (server) {
53 action(version);
54 } else {
55 String v = sendString("VERSION " + version.toString());
56 if (v != null && v.startsWith("VERSION ")) {
57 v = v.substring("VERSION ".length());
58 }
59
60 action(new Version(v));
61 }
62 } finally {
63 out.close();
64 }
65 } finally {
66 in.close();
67 }
68 } catch (Exception e) {
69 onError(e);
70 } finally {
71 try {
72 s.close();
73 } catch (Exception e) {
74 onError(e);
75 }
76 }
77 }
78
79 // (also, server never get anything)
80 public Object send(Object data) throws IOException, NoSuchFieldException,
81 NoSuchMethodException, ClassNotFoundException {
82 synchronized (lock) {
83 String rep = sendString(new Exporter().append(data).toString(true));
84 return new Importer().read(rep).getValue();
85 }
86 }
87
88 public Object flush() throws NoSuchFieldException, NoSuchMethodException,
89 ClassNotFoundException, IOException, java.lang.NullPointerException {
90 String str = flushString();
91 if (str == null) {
92 throw new NullPointerException("No more data from client");
93 }
94
95 return new Importer().read(str).getValue();
96 }
97
98 /**
99 * Handler called when the client {@link Version} is received.
100 *
101 * @param clientVersion
102 * the client {@link Version}
103 */
104 protected void onClientVersionReceived(
105 @SuppressWarnings("unused") Version clientVersion) {
106 }
107
108 /**
109 * Handler called when an unexpected error occurs in the code.
110 *
111 * @param e
112 * the exception that occurred
113 */
114 protected void onError(@SuppressWarnings("unused") Exception e) {
115 }
116
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) {
120 out.write(line);
121 out.write("\n");
122
123 if (server) {
124 out.flush();
125 return null;
126 }
127
128 contentToSend = true;
129 return flushString();
130 }
131 }
132
133 // server can receive something even without pending content
134 private String flushString() throws IOException {
135 synchronized (lock) {
136 if (server || contentToSend) {
137 if (contentToSend) {
138 out.flush();
139 contentToSend = false;
140 }
141
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());
149
150 line = in.readLine();
151 }
152
153 return line;
154 }
155
156 return null;
157 }
158 }
159 }