RobotCore 1.0
WPILib port to raspberry Pi
Loading...
Searching...
No Matches
Network.java
1/*
2 * Copyright (C) 2022 John H. Gaby
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, version 3 of the License.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 *
16 * Contact: robotics@gabysoft.com
17 */
18
19package robotCore;
20
21import java.io.IOException;
22import java.io.InputStream;
23import java.io.OutputStream;
24import java.io.PrintStream;
25import java.net.ServerSocket;
26import java.net.Socket;
27
28
44public class Network
45{
46 private String m_host;
47 private int m_port;
48 private Receiver m_receiver;
49 private NetworkReceiver m_networkReceiver;
50 private int m_retryTime;
51 private PrintStream m_printStream = null;
52 private Object m_lock = new Object();
53
54 private ServerSocket m_serverSocket = null;
55 private Socket m_clientSocket = null;
56 private InputStream m_inputStream = null;
57 private OutputStream m_outputStream = null;
58
59
67 public interface NetworkReceiver
68 {
76 public void processData(String command);
77 public void connected();
78 public void disconnected();
79 }
80
81
94 public void connect(NetworkReceiver receiver, String host, int port, int retryTime)
95 {
96 m_host = host;
97 m_port = port;
98 m_networkReceiver = receiver;
99 m_retryTime = retryTime;
100 m_receiver = new Receiver();
101
102 new Thread(m_receiver).start();
103 }
104
118 public void connect(NetworkReceiver receiver, String host, int port)
119 {
120 connect(receiver, host, port, 5000);
121 }
122
129 public void sendMessage(String message)
130 {
131 if (m_networkReceiver != null)
132 {
133 synchronized(m_lock)
134 {
135 if (m_printStream != null)
136 {
137 m_printStream.println(message);
138 }
139 }
140 }
141 }
142
153 public void listen(NetworkReceiver receiver, int port)
154 {
155 m_host = null;
156 m_port = port;
157 m_networkReceiver = receiver;
158 m_receiver = new Receiver();
159
160 new Thread(m_receiver).start();
161 }
162
167 public void closeConnection()
168 {
169 System.out.println("CloseConnection");
170
171 m_networkReceiver.disconnected();
172
173 synchronized(m_lock)
174 {
175 if (m_printStream != null)
176 {
177 m_printStream.close();
178 m_printStream = null;
179 }
180
181 if (m_outputStream != null)
182 {
183 try {
184 m_outputStream.close();
185 } catch (IOException e) {
186 }
187 m_outputStream = null;
188 }
189
190 if (m_inputStream != null)
191 {
192 try {
193 m_inputStream.close();
194 } catch (IOException e) {
195 }
196 m_inputStream = null;
197 }
198
199 if (m_clientSocket != null)
200 {
201 try {
202 m_clientSocket.close();
203 } catch (IOException e) {
204 }
205 m_clientSocket = null;
206 }
207
208 if (m_serverSocket != null)
209 {
210 try {
211 m_serverSocket.close();
212 } catch (IOException e) {
213 }
214 m_serverSocket = null;
215 }
216 }
217 }
218
219 private class Receiver implements Runnable
220 {
221 private void runClient()
222 {
223 Logger.log("Network", 1, String.format("%s:%d: Client Thread started", m_host, m_port));
224
225 while (true)
226 {
227 Logger.log("Network", 1, String.format("Connecting to %s:%d", m_host, m_port));
228
229 try
230 {
231 Socket clientSocket = new Socket(m_host, m_port);
232 InputStream inputStream = clientSocket.getInputStream();
233 OutputStream outputStream = clientSocket.getOutputStream();
234 PrintStream printStream = new PrintStream(outputStream);
235
236 synchronized(m_lock)
237 {
238 m_clientSocket = clientSocket;
239 m_inputStream = inputStream;
240 m_outputStream = outputStream;
241 m_printStream = printStream;
242 }
243
244 String command = "";
245
246 Logger.log("Network", 1, String.format("Connected to %s:%d", m_host, m_port));
247
248 m_networkReceiver.connected();
249
250 while (true)
251 {
252 int ch = m_inputStream.read();
253
254 if (ch < 0)
255 {
256 Logger.log("Network", 2, "Connection lost");
257
258 break;
259 }
260
261 if (ch == '\n')
262 {
263 if (command.length() >= 1)
264 {
265 m_networkReceiver.processData(command);
266 }
267
268 command = "";
269
270 }
271 else if (ch >= 0)
272 {
273 command += (char) ch;
274 }
275 else
276 {
277 Thread.sleep(5);
278 }
279 }
280
281 }
282 catch (Exception ex)
283 {
284 Logger.log("Network", 3, "Receiver exception: " + ex);
285 // ex.printStackTrace(System.out);
286 }
287
289
290 if (m_retryTime == 0)
291 {
292 break;
293 }
294
295 try {
296 Thread.sleep(m_retryTime);
297 } catch (InterruptedException e) {
298
299 }
300 }
301
302 Logger.log("Network", 1, String.format("%s:%d: Thread exit", m_host, m_port));
303 }
304
305 private void runHost()
306 {
307 Logger.log("Network", 1, String.format("Host Thread started on port %d", m_port));
308
309 while (true)
310 {
311 Logger.log("Network", 1, String.format("Waiting for connection on port %d", m_port));
312
313 try
314 {
315 m_serverSocket = new ServerSocket(m_port);
316 m_clientSocket = m_serverSocket.accept();
317 synchronized(m_lock)
318 {
319 m_inputStream = m_clientSocket.getInputStream();
320 m_outputStream = m_clientSocket.getOutputStream();
321 m_printStream = new PrintStream(m_outputStream);
322 }
323
324 int ch;
325 String command = "";
326
327 Logger.log("Network", 2, "Host connected");
328
329 m_networkReceiver.connected();
330
331 do
332 {
333 ch = m_inputStream.read();
334
335 if (ch == '\n')
336 {
337 m_networkReceiver.processData(command);
338 command = "";
339
340 }
341 else if (ch != -1)
342 {
343 command += (char) ch;
344 }
345 } while (true); //while (ch > 0);
346
347 // Logger.log("Network", 1, "Read loop exit");
348 }
349 catch (Exception ex)
350 {
351 Logger.log("Network", 3, "Host network error: " + ex);
352
354
355 try {
356 Thread.sleep(10);
357 } catch (InterruptedException e) {
358
359 }
360 }
361 }
362 }
363
364 @Override
365 public void run()
366 {
367 if (m_host == null)
368 {
369 runHost();
370 }
371 else
372 {
373 runClient();
374 }
375 }
376 }
377}
The Logger class provides a mechanism to log messages.
Definition Logger.java:54
static void log(String tag, int level, String message, boolean noLogToConsole)
Definition Logger.java:161
The Network class handles network connections.
Definition Network.java:45
void connect(NetworkReceiver receiver, String host, int port)
Definition Network.java:118
void closeConnection()
Definition Network.java:167
void sendMessage(String message)
Definition Network.java:129
void connect(NetworkReceiver receiver, String host, int port, int retryTime)
Definition Network.java:94
void listen(NetworkReceiver receiver, int port)
Definition Network.java:153
The NetworkReceiver interface provides a callback for received data on a network connection.
Definition Network.java:68
void processData(String command)