1 package eu.javaexperience.electronic.uartbus.rpc.client.device;
3 import java.lang.reflect.Method;
4 import java.lang.reflect.Type;
5 import java.util.Arrays;
6 import java.util.concurrent.TimeUnit;
19 protected int address;
21 public long timeout = 100;
22 public TimeUnit timeoutUnit = TimeUnit.MILLISECONDS;
23 public int retryCount = 5;
25 protected final ProxyHelpedLazyImplementation<UbDeviceNs, UbDeviceNsLazyImpl, UbDevStdNsRoot> handler;
29 return handler.getRoot();
35 this.address = address;
39 public int getAddress()
44 public static <R extends UbDeviceNs> ProxyHelpedLazyImplementation<UbDeviceNs, UbDeviceNsLazyImpl, R> createNsHandler(
UartBusDevice device, Class<R> rootCls)
51 public Object handleInterfaceCall
59 if(
UbDeviceNs.class.isAssignableFrom(method.getReturnType()))
62 if(
"cast".equals(method.getName()))
64 return wrapWithClass((Class) params[0], root);
66 else if(
"customNs".equals(method.getName()))
68 return stepPath(root, (Class) params[0],
true, (
short) params[1]);
76 return stepPath(root, (Class<R>) method.getReturnType(), null != ui, null == ui?-1:ui.ns(), params);
79 return device.handleDeviceRpcCall(root.path, method, params);
82 public Object stepPath(
UbDeviceNsLazyImpl root, Class<R> cls,
boolean hasNs,
short ns, Object... objs)
throws Exception
84 if(null == objs || 0 == objs.length)
86 byte[] p = Arrays.copyOf(root.path, root.path.length+1);
87 p[root.path.length] = (byte) ns;
106 protected void handleException(Throwable e)
108 Mirror.throwCheckedExceptionAsUnchecked(e);
114 Mirror.propagateAnyway(e);
119 protected Object handleDeviceRpcCall(byte[] path, Method method, Object[] params)
throws Exception
126 throw new IllegalStateException(
"Uartbus RPC function doesn't have @UbIndex annotation used to identify RPC function index: "+method);
129 path = Arrays.copyOf(path, path.length+1);
130 path[path.length-1] = (byte) ns.ns();
132 if(null != params && params.length > 0)
137 Type ret = method.getReturnType();
138 TransactionException trex = null;
139 byte[] data = pa.done();
141 for(
int i=0;i<retryCount;++i)
152 byte[] res = tr.ensureResponse(timeout, timeoutUnit, method.toString());
157 Arrays.copyOfRange(res, path.length+1, res.length)
160 catch(TransactionException ex)
170 TransactionException te =
new TransactionException(
"Device not responded after "+retryCount+
" attempt.");
171 te.addSuppressed(trex);
175 public void setTimeout(
long timeout, TimeUnit unit)
177 this.timeout = timeout;
178 this.timeoutUnit = unit;