/** * Start the protocol in one-way mode: this instance only issues commands. Any commands from remote will return * error 'unknown_command' */ public void start() { start(new Target() { @Override public Object onCommand(Command command) throws Exception { throw new RemoteException("unknown_command", "command is not known"); } }); }
/** * Create instance but does not start handshake. * * @param myKey * @param input * @param output * * @throws IOException */ public BitrustedConnector(PrivateKey myKey, InputStream input, OutputStream output) throws IOException { this.myKey = myKey; connection = new Farcall(new BossConnector(input, output)); connection.asyncCommands(Executors.newSingleThreadExecutor()); }
/** * Send command without parameters * * @param name of the command * @return deferred result */ public CommandResult send(String name) { return send(name, null, null); }
@Override public void send(Map<String, Object> data) throws IOException { checkConnected(); byte[] packed = mySessionKey.etaEncrypt(Boss.pack(data)); if (!connection.isClosed()) connection.sendParams("block", packed); else throw new EOFException("connection closed"); }
private void receiveCommands() { while (!requestStop) { try { Map<String, Object> input = connector.receive(); if (input == null) break; int serial = ((Number) input.get("serial")).intValue(); if (inSerial++ != serial) throw new ProtocolException("farcall sync lost"); Number ref = (Number) input.get("ref"); if (ref != null) { processReply(input, ref.intValue()); } else { processCommand(input, serial); } } catch (EOFException e) { log.i("closing farcall instance on eof encountered"); close(); } catch (IOException e) { log.wtf("internal error", e); break; } } close(); }
); bc.connect(null); Farcall f = new Farcall(bc); f.start(cmd -> { switch (cmd.getName()) { case "ping": Farcall f = new Farcall(new BitrustedConnector( TestKeys.privateKey(1), s.getInputStream(), s.getOutputStream() ).connect(null)); f.start(); assertEquals(f.send("ping").waitSuccess(), "pong"); f.send("stop").await(20); try { f.send("ping").waitSuccess(); fail("must throw EOF"); } catch (DeferredResult.Error e) {
@Test public void asyncMethods() throws Exception { Interconnection ic = new Interconnection(10); Farcall a = new Farcall(ic.getConnectorA()); Farcall b = new Farcall(ic.getConnectorB()); a.asyncCommands(); a.start(command -> { switch (command.getName()) { case "wait": b.start(); b.send("wait").done((data) -> { waitDone[0] = true; done.fire(null); }); b.send("interrupt").waitSuccess(); done.waitFired(); assertTrue(waitDone[0]);
private void basicTest(Interconnection ic, Farcall a, Farcall b) throws InterruptedException { b.start(new Farcall.Target() { @Override public Object onCommand(Command command) throws Exception { ic.getConnectorA().pause(10); a.start(); a.send("ping").success((Object data) -> lastObject = data).join(); assertEquals("pong", lastObject); a.send("bad", null, null).failure((Object data) -> lastObject = data).join(); assertTrue(lastObject instanceof Farcall.RemoteException); a.send("echoargs", array, map).success((Object data) -> { lastObject = data; }).failure((err) -> {
public BitrustedConnector connect(Predicate<byte[]> isTrustedKey) throws Error, TimeoutException, InterruptedException { Future<?> initDone = pool.submit(() -> { this.isTrustedKey = isTrustedKey; connection.start(this); Binder result = Binder.from( connection.sendKeyParams( "hello", "protocol", "bitrusted",
private CommandResult sendToRemote(Object... keysValues) { HashMap<String, Object> packet = Ut.mapFromArray(keysValues); synchronized (access) { if (isClosed()) { CommandResult closedResult = new CommandResult(0); closedResult.sendFailure(new EOFException("farcall is closed")); return closedResult; } packet.put("serial", outSerial); CommandResult result = new CommandResult(outSerial); // synchronized (resultQueue) { resultQueue.put(outSerial, result); // } try { connector.send(packet); } catch (IOException e) { // synchronized (resultQueue) { resultQueue.remove(outSerial); // } result.sendFailure(e); close(); return result; } outSerial++; return result; } }
@Override public void close() { connected = false; connection.close(); }
StreamConnector sb = new StreamConnector(); Farcall fa = new Farcall(new BossConnector(sa.getInputStream(), sb.getOutputStream())); Farcall fb = new Farcall(new BossConnector(sb.getInputStream(), sa.getOutputStream())); fa.start(command -> { if (command.getName().equals("fast")) { counts[0]++; fb.start(); futures.add(es.submit(() -> { for (int i = 0; i < 10; i++) assertEquals("fast done", fb.send("fast").waitSuccess()); return null; })); futures.add(es.submit(() -> { for (int i = 0; i < 2; i++) assertEquals("slow done", fb.send("slow").waitSuccess()); return null; }));
Interconnection a = new Interconnection(10); Farcall fa = new Farcall(a.getConnectorA()); Farcall fb = new Farcall(a.getConnectorB()); fa.asyncCommands(); fa.start(command -> { if (command.getName().equals("fast")) { counts[0]++; fb.start(); futures.add(es.submit(() -> { for (int i = 0; i < 10; i++) assertEquals("fast done", fb.send("fast").waitSuccess()); return null; })); futures.add(es.submit(() -> { for (int i = 0; i < 2; i++) assertEquals("slow done", fb.send("slow").waitSuccess()); return null; }));
@Test(timeout = 200) public void jsonConnector() throws Exception { StreamConnector sa = new StreamConnector(); StreamConnector sb = new StreamConnector(); JsonConnector connA = new JsonConnector(sa.getInputStream(), sb.getOutputStream()); JsonConnector connB = new JsonConnector(sb.getInputStream(), sa.getOutputStream()); Farcall a = new Farcall(connA); Farcall b = new Farcall(connB); basicTest(null, a, b); }
assertTrue(ca.isConnected()); Farcall fa = new Farcall(ca); Farcall fb = new Farcall(cb); fa.start(command -> { if (command.getName().equals("fast")) { counts[0]++; fb.start(); Future<Object> f1 = pool.submit(() -> fb.send("fast").waitSuccess()); Future<Object> f2 = pool.submit(() -> fb.send("fast").waitSuccess()); Future<Object> f3 = pool.submit(() -> fb.send("fast").waitSuccess()); Future<Object> f4 = pool.submit(() -> fb.send("slow").waitSuccess()); Future<Object> f5 = pool.submit(() -> fb.send("slow").waitSuccess()); assertEquals("fast done", f1.get()); assertEquals("fast done", f2.get());
@Test(timeout = 200) public void bossConnector() throws Exception { StreamConnector sa = new StreamConnector(); StreamConnector sb = new StreamConnector(); BossConnector connA = new BossConnector(sa.getInputStream(), sb.getOutputStream()); BossConnector connB = new BossConnector(sb.getInputStream(), sa.getOutputStream()); Farcall a = new Farcall(connA); Farcall b = new Farcall(connB); basicTest(null, a, b); }
/** * Send Farcall command to the remote with only array parameters. To read remote answer, use returned {@link * DeferredResult} instance. * * @param name command name * @param params array parameters * @return deferred result */ public CommandResult sendParams(String name, Object... params) { return send(name, Ut.arrayToList(params), null); }
assertTrue(ca.isConnected()); Farcall fa = new Farcall(ca); Farcall fb = new Farcall(cb); fa.start(command -> { if (command.getName().equals("fast")) { counts[0]++; fb.start(); futures.add(es.submit(() -> { for (int i = 0; i < 10; i++) assertEquals("fast done", fb.send("fast").waitSuccess()); return null; })); futures.add(es.submit(() -> { for (int i = 0; i < 2; i++) assertEquals("slow done", fb.send("slow").waitSuccess()); return null; }));