/** * 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"); } }); }
@Override public Object onCommand(Command command) throws Exception { throw new RemoteException("unknown_command", "command is not known"); } });
/** * Send command without parameters * * @param name of the command * @return deferred result */ public CommandResult send(String name) { return send(name, null, null); }
/** * 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()); }
@Test(timeout = 200) public void testBasicRPC() throws Exception { Interconnection ic = new Interconnection(5); Farcall a = new Farcall(ic.getConnectorA()); Farcall b = new Farcall(ic.getConnectorB()); basicTest(ic, a, b); }
private void doCall(Map<String, Object> input, int serial) { try { Object result = target.onCommand(new Command(input)); sendToRemote("ref", serial, "result", result); } catch (RemoteException e) { sendErrorNoExceptions(serial, e.getRemoteErrorClass(), e.getRemoteErrorText()); } catch (Exception e) { sendErrorNoExceptions(serial, e.getClass().getSimpleName(), e.getMessage()); } }
@Override public Object onCommand(Command command) throws Exception { if (command.is("ping")) { return "pong"; } else if (command.is("echoargs")) { HashMap<String, Object> res = new HashMap<>(); res.put("simple", command.getParams()); res.put("key", command.getKeyParams()); return res; } throw new IllegalArgumentException("unknown command: " + command.getName()); } });
@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); }
@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); }
private void processReply(Map<String, Object> input, Integer ref) { CommandResult dr; // synchronized (resultQueue) { dr = resultQueue.remove(ref); // } if (dr != null) { Object error = input.get("error"); if (error != null) { dr.sendFailure(RemoteException.makeException(error)); } else { Object result = input.get("result"); dr.sendSuccess(result); } } }
@Override public void close() { connected = false; connection.close(); }
/** * Send remote command with all kind of parameters * * @param name of the command * @param params array parameters or null * @param keyParams map parameters or null * @return deferred result */ public CommandResult send(String name, ArrayList<Object> params, HashMap<String, Object> keyParams) { if (worker == null) throw new IllegalStateException("farcall instance must be started"); return sendToRemote("cmd", name, "args", params, "kwargs", keyParams); }
public Interconnection(int capacity) { qa = new ArrayBlockingQueue<Object>(capacity); qb = new ArrayBlockingQueue<Object>(capacity); connectorA = new QueueConnector(qa, qb); connectorB = new QueueConnector(qb, qa); }
private void sendErrorNoExceptions(int serial, String remoteErrorClass, String remoteErrorText) { try { sendError(serial, remoteErrorClass, remoteErrorText); } catch (IOException e) { log.wtf("failed to send asynchronous answer: " + remoteErrorText, e); } }
/** * 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); }
@SuppressWarnings("unchecked") static Exception makeException(Object error) { if (error instanceof Map) { return new RemoteException((Map<String, Object>) error); } return new ProtocolException("bad remote exception record: " + error); }
private void sendError(int serial, String remoteErrorClass, String remoteErrorText) throws IOException { sendToRemote("ref", serial, "error", Ut.mapFromArray( "class", remoteErrorClass, "text", remoteErrorText)); }
/** * Send remote command with only keyed parameters. The arguments shoul be sequence of key, value pairs, where keys * must be String. For example: * <code><pre> * farcall.sendKeyParams("foo", "param1", 10, "param2", 20, "bar", "baz") * </pre></code> * The return value being passed via {@link DeferredResult} is either data returned by the remote party, e.g. {@link * java.util.List}, {@link Map}, simple types, strings, or whatever else {@link Connector} allows, or, in the case * the remote throws and error, instance of the {@link RemoteException} class. * * @param name * @param keysAndValues key1, value1, key2, value2... sequence * @return deferred result */ public CommandResult sendKeyParams(String name, Object... keysAndValues) { return send(name, null, Ut.mapFromArray(keysAndValues)); }