public void invokeAsync(Request req, double timeout, RequestWaiter waiter) { // TODO: Consider replacing this by a watcher on the target synchronized(this) { // ensure we have exactly 1 valid connection across threads if (target == null || ! target.isValid()) target = supervisor.connect(new Spec(hostname, port)); } target.invokeAsync(req, timeout, waiter); }
/** * Convenience method for connecting to a peer, invoking a method * and disconnecting. * * @param spec the address to connect to * @param req the invocation request * @param timeout request timeout in seconds **/ public void invokeBatch(Spec spec, Request req, double timeout) { Target target = connectSync(spec); try { target.invokeSync(req, timeout); } finally { target.close(); } }
private void handleRegisterCallbackDone(Request req, String name, String spec, Target target){ String stored = services.get(name); if (stored != null) { // too late if ( ! stored.equals(spec)) { req.setError(ErrorCode.METHOD_FAILED, "service '" + name + "' registered with another spec"); } req.returnRequest(); target.close(); return; } target.setContext(name); target.addWatcher(monitor); services.put(name, spec); targets.put(name, target); req.returnRequest(); updated(); }
public Target getTarget(Timer timer) throws InterruptedException { TimeUnit unit = TimeUnit.MILLISECONDS; targetLock.tryLock(timer.timeLeft(unit) , unit ); try { if (target == null || !target.isValid()) connect(timer); return target; } finally { targetLock.unlock(); } } }
if (req.errorCode() != ErrorCode.METHOD_FAILED) { log.log(Level.INFO, logMessagePrefix() + " failed, will disconnect: " + req.errorMessage() + " (code " + req.errorCode() + ")"); target.close(); target = null; } else { target.close(); target = null; req.parameters().add(new StringValue(mySpec)); log.log(Level.FINE, logMessagePrefix() + " now"); target.invokeAsync(req, 35.0, reqWait);
@Override public void close() { target.close(); }
target.invokeAsync(req, timeout, this);
private void rpc_unregister(Request req) { String name = req.parameters().get(0).asString(); String spec = req.parameters().get(1).asString(); String stored = services.get(name); if (stored != null) { if (stored.equals(spec)) { Target target = targets.remove(name); target.removeWatcher(monitor); services.remove(name); target.close(); updated(); } else { req.setError(ErrorCode.METHOD_FAILED, "service '" + name + "' registered with another spec"); } } }
void receive(FileReference reference, String filename, byte[] content) { log.log(LogLevel.INFO, "Preparing receive call for " + reference.value() + " and file " + filename); XXHash64 hasher = XXHashFactory.fastestInstance().hash64(); Request fileBlob = new Request("filedistribution.receiveFile"); log.log(LogLevel.INFO, "Calling " + fileBlob.methodName() + " with target " + target); fileBlob.parameters().add(new StringValue(reference.value())); fileBlob.parameters().add(new StringValue(filename)); fileBlob.parameters().add(new DataValue(content)); fileBlob.parameters().add(new Int64Value(hasher.hash(ByteBuffer.wrap(content), 0))); fileBlob.parameters().add(new Int32Value(0)); fileBlob.parameters().add(new StringValue("OK")); log.log(LogLevel.INFO, "Doing invokeSync"); target.invokeSync(fileBlob, 5); log.log(LogLevel.INFO, "Done with invokeSync"); } }
@Override public final void send(RoutingNode recipient, Version version, byte[] payload, long timeRemaining) { SendContext ctx = new SendContext(recipient, timeRemaining); RPCServiceAddress address = (RPCServiceAddress)recipient.getServiceAddress(); Message msg = recipient.getMessage(); Route route = new Route(recipient.getRoute()); Hop hop = route.removeHop(0); Request req = encodeRequest(version, route, address,msg, timeRemaining, payload, ctx.trace.getLevel()); if (ctx.trace.shouldTrace(TraceLevel.SEND_RECEIVE)) { ctx.trace.trace(TraceLevel.SEND_RECEIVE, "Sending message (version " + version + ") from " + clientIdent + " to '" + address.getServiceName() + "' with " + ctx.timeout + " seconds timeout."); } if (hop.getIgnoreResult()) { address.getTarget().getJRTTarget().invokeVoid(req); if (ctx.trace.shouldTrace(TraceLevel.SEND_RECEIVE)) { ctx.trace.trace(TraceLevel.SEND_RECEIVE, "Not waiting for a reply from '" + address.getServiceName() + "'."); } Reply reply = new EmptyReply(); reply.getTrace().swap(ctx.trace); net.getOwner().deliverReply(reply, recipient); } else { req.setContext(ctx); address.getTarget().getJRTTarget().invokeAsync(req, ctx.timeout, this); } req.discardParameters(); // allow garbage collection of request parameters }
private void handleTargetDown(Target target) { String name = (String) target.getContext(); targets.remove(name); services.remove(name); updated(); }
private void connect(Timer timer) throws InterruptedException { while (timer.isTimeLeft()) { pause(); target = supervisor.connectSync(spec); if (target.isValid()) { log.log(LogLevel.DEBUG, "Successfully connected to '" + spec + "', this = " + System.identityHashCode(this)); pauseTime = 0; logCount = 0; return; } else { logWarning(); } } }
target.close(); target = null; req.parameters().add(new Int32Value(specsGeneration)); // gencnt req.parameters().add(new Int32Value(5000)); // mstimeout target.invokeAsync(req, 40.0, reqWait);
/** * <p>This method is used for explicit reference counting targets to allow * reusing open connections. When the reference count reaches 0, the * connection is closed.</p> * * @see #addRef() */ public void subRef() { if (ref.decrementAndGet() == 0) { target.close(); } }
public RegisterCallback(Request req, String name, String spec) { req.detach(); registerReq = req; this.name = name; this.spec = spec; target = orb.connect(new Spec(spec)); Request cbReq = new Request("slobrok.callback.listNamesServed"); target.invokeAsync(cbReq, 5.0, this); }
target.invokeSync(request, rpcTimeout);
/** * Closes all unused target connections. Unless the force argument is true, this method will allow a grace period * for all connections after last use before it starts closing them. This allows the most recently used connections * to stay open. * * @param force Whether or not to force flush. */ public synchronized void flushTargets(boolean force) { Iterator<Entry> it = targets.values().iterator(); long currentTime = timer.milliTime(); long expireTime = currentTime - expireMillis; while (it.hasNext()) { Entry entry = it.next(); RPCTarget target = entry.target; if (target.getJRTTarget().isValid()) { if (target.getRefCount() > 1) { entry.lastUse = currentTime; continue; // someone is using this } if (!force) { if (entry.lastUse > expireTime) { continue; // not sufficiently idle } } } target.subRef(); it.remove(); } }
target = supervisor.connect(new Spec(connectspec)); Request request=createRequest(method,arguments); target.invokeSync(request,10.0); if (request.isError()) { System.err.println("error(" + request.errorCode() + "): " + request.errorMessage()); target.close(); if (supervisor!=null) supervisor.transport().shutdown().join();
/** * Invoked from the transport thread, requested by the shutdown * method. */ private void handleShutdown() { if (req != null) { req.abort(); req = null; } if (target != null) { target.close(); target = null; } specs = new Entry[0]; }
public Request invoke(Request req, Target target, double timeout) { SingleRequestWaiter waiter = null; synchronized (this) { if (active) { waiter = new SingleRequestWaiter(); target.invokeAsync(req, timeout, waiter); pending = req; } else { req.setError(ErrorCode.ABORT, "Aborted by user"); } } if (waiter != null) { waiter.waitDone(); synchronized (this) { pending = null; } } return req; }