UartBus source documentation
UartbusRpcClientTools.java
1 package eu.javaexperience.electronic.uartbus.rpc.client;
2 
3 import java.io.IOException;
4 
6 import eu.javaexperience.interfaces.simple.SimpleGet;
7 import eu.javaexperience.interfaces.simple.publish.SimplePublish1;
8 import eu.javaexperience.log.JavaExperienceLoggingFacility;
9 import eu.javaexperience.log.LogLevel;
10 import eu.javaexperience.log.Loggable;
11 import eu.javaexperience.log.Logger;
12 import eu.javaexperience.log.LoggingTools;
13 import eu.javaexperience.reflect.Mirror;
14 import eu.javaexperience.retry.RetryTools;
15 import eu.javaexperience.rpc.bidirectional.BidirectionalRpcDefaultProtocol;
16 import eu.javaexperience.rpc.javaclient.JavaRpcClientTools;
17 import eu.javaexperience.rpc.javaclient.JavaRpcParallelClient;
18 
20 {
21  private UartbusRpcClientTools(){}
22 
23  protected static final Logger LOG = JavaExperienceLoggingFacility.getLogger(new Loggable("UartbusRpcClientTools"));
24 
25  public static JavaRpcParallelClient openIpParallelClient(String ip, int port) throws IOException
26  {
27  LoggingTools.tryLogFormat(LOG, LogLevel.DEBUG, "openIpParallelClient(%s, %s)", ip,port);
28  JavaRpcParallelClient ret = JavaRpcClientTools.createClientWithIpPort(ip, port, BidirectionalRpcDefaultProtocol.DEFAULT_PROTOCOL_HANDLER_WITH_CLASS);
29  ret.startPacketRead();
30  return ret;
31  }
32 
33  public static UartbusConnection createApi(JavaRpcParallelClient cli)
34  {
35  return cli.createApiObject(UartbusConnection.class, "uartbus");
36  }
37 
38  public static UartbusStreamerEndpoint openIpEndpoint(String ip, int port, SimplePublish1<UartbusConnection> connectionInitializer, boolean reconnect)
39  {
40  if(reconnect)
41  {
42  return openIpEndpoint(ip, port, connectionInitializer, RetryTools.getDefaultReconnectTimeMillisecs());
43  }
44  else
45  {
46  return new UartbusStreamerEndpoint(()->
47  {
48  try
49  {
50  return createApi(openIpParallelClient(ip, port));
51  }
52  catch (IOException e)
53  {
54  Mirror.propagateAnyway(e);
55  return null;
56  }
57  });
58  }
59  }
60 
61  public static <T> SimpleGet<T> waitReconnect(SimpleGet<T> get, String entity, int... reconn)
62  {
63  return RetryTools.waitReconnect(get, entity, LOG, LogLevel.WARNING, reconn);
64  }
65 
66  public static UartbusStreamerEndpoint openIpEndpoint(String ip, int port, SimplePublish1<UartbusConnection> connectionInitializer, int... reconnectRetryDelays)
67  {
68  SimpleGet<JavaRpcParallelClient> apiReconnect = waitReconnect
69  (
70  ()->
71  {
72  try
73  {
74  JavaRpcParallelClient ret = openIpParallelClient(ip, port);
75  ret.getServerEventMediator().addEventListener((e)->System.err.println("UNHANDLED: "+e));
76  if(null != connectionInitializer)
77  {
78  connectionInitializer.publish(createApi(ret));
79  }
80  return ret;
81  }
82  catch(IOException e)
83  {
84  Mirror.propagateAnyway(e);
85  return null;
86  }
87  },
88  "Uartbus API connection to "+ip+":"+port,
89  reconnectRetryDelays
90  );
91 
92  return new UartbusStreamerEndpoint(()->createApi(apiReconnect.get()));
93  }
94 }