UartBus source documentation
UartbusStreamerEndpoint.java
1 package eu.javaexperience.electronic.uartbus.rpc.client;
2 
3 import java.io.Closeable;
4 import java.io.IOException;
5 
8 import eu.javaexperience.interfaces.simple.SimpleGet;
9 import eu.javaexperience.log.JavaExperienceLoggingFacility;
10 import eu.javaexperience.log.LogLevel;
11 import eu.javaexperience.log.Loggable;
12 import eu.javaexperience.log.Logger;
13 import eu.javaexperience.log.LoggingTools;
14 import eu.javaexperience.multithread.notify.WaitForEvents;
15 import eu.javaexperience.patterns.behavioral.mediator.EventMediator;
16 import eu.javaexperience.reflect.Mirror;
17 
18 public class UartbusStreamerEndpoint implements Closeable
19 {
20  protected static final Logger LOG = JavaExperienceLoggingFacility.getLogger(new Loggable("UartbusStreamerEndpoint"));
21 
22  protected SimpleGet<UartbusConnection> getConnection;
23  protected volatile UartbusConnection workingConnection;
24 
25  public UartbusStreamerEndpoint(SimpleGet<UartbusConnection> getConnection)
26  {
27  this.getConnection = getConnection;
28  }
29 
30  public UartbusConnection getApi()
31  {
32  if(null == workingConnection)
33  {
34  workingConnection = getConnection.get();
35  UartbusTools.initConnection(workingConnection);
36  }
37  return workingConnection;
38  }
39 
40  public void sendPacket(byte[] data)
41  {
42  try
43  {
44  getApi().sendPacket(data);
45  }
46  catch (IOException e)
47  {
48  Mirror.propagateAnyway(e);
49  }
50  }
51 
52  public void cleanupConncection()
53  {
54  workingConnection = null;
55  }
56 
57  protected EventMediator<byte[]> packetStream = new EventMediator<>();
58 
59  public EventMediator<byte[]> getPacketStreamer()
60  {
61  return packetStream;
62  }
63 
64  protected Thread streamer;
65  protected volatile boolean runStreamer;
66 
67  public synchronized void startStreaming()
68  {
69  if(null == streamer)
70  {
71  WaitForEvents w = new WaitForEvents(1);
72  WaitForEvents[] ws = new WaitForEvents[] {w};
73 
74  runStreamer = true;
75  streamer = new Thread()
76  {
77  public void run()
78  {
79  try
80  {
81  while(runStreamer)
82  {
83  byte[] data = null;
84 
85  UartbusConnection conn = null;
86  try
87  {
88  conn = getApi();
89  }
90  catch(Exception e)
91  {
92  LoggingTools.tryLogFormatException(LOG, LogLevel.FATAL, e, "Exception occurred while getting connection for packet streaming (stopping): ");
93  break;
94  }
95 
96  if(null == conn)
97  {
98  break;
99  }
100 
101  if(null != ws[0])
102  {
103  ws[0].call();
104  ws[0] = null;
105  }
106 
107  try
108  {
109  data = conn.getNextPacket();
110  }
111  catch(Exception e)
112  {
113  LoggingTools.tryLogFormatException(LOG, LogLevel.ERROR, e, "Exception while receiving packet (continue): ");
114  cleanupConncection();
115  }
116 
117  if(null != data)
118  {
119  packetStream.dispatchEvent(data);
120  }
121  }
122  }
123  catch(Exception e)
124  {
125  LoggingTools.tryLogFormatException(LOG, LogLevel.FATAL, e, "Fatal exception occurred while packet streaming(stopping): ");
126  }
127  finally
128  {
129  runStreamer = false;
130  }
131  }
132  };
133 
134  streamer.start();
135  w.waitForAllEvent();
136  return;
137  }
138 
139  throw new RuntimeException("Packet streaming already running");
140  }
141 
142  /**
143  * Shuts down the Api endpoint but doesn't close the connection
144  * */
145  public synchronized void close()
146  {
147  if(runStreamer)
148  {
149  runStreamer = false;
150  streamer.interrupt();
151  }
152  }
153 }