/** * Invoke this method. The given request holds the parameters and * will be given as parameter to the handler method. * * @param req the request causing this invocation **/ void invoke(Request req) { try { if (handler != null) { handler.invoke(req); } else { Object[] args = { req }; method.invoke(object, args); } } catch (Exception e) { req.setError(ErrorCode.METHOD_FAILED, e.toString()); } }
/** * Check if this Request contains return types compatible with the * given type string. If this Request contains an error it is * considered incompatible with all possible type strings. If the * return values are not compatible with the given type string and * an error condition is not set, the {@link * ErrorCode#WRONG_RETURN} error will be set. This method is * intended to be used by the RPC client after a method has been * invoked to verify the return value types. Please refer to the * {@link Method} class description for an explanation of type * strings. * * @return true if all is ok and the return types are compatible * with 'returnTypes' * @param returnTypes type string **/ public boolean checkReturnTypes(String returnTypes) { if (errorCode != ErrorCode.NONE) { return false; } if (returnValues.satisfies(returnTypes)) { return true; } setError(ErrorCode.WRONG_RETURN, "checkReturnValues: Wrong return values"); return false; }
/** * Handle connection down. **/ public void handleConnectionDown() { timeoutTask.kill(); req.setError(ErrorCode.CONNECTION, "Connection error"); reqWaiter.handleRequestDone(req); }
/** * Handle timeout. **/ public void run() { if (!conn.cancelReply(this)) { return; } req.setError(ErrorCode.TIMEOUT, "Request timed out after " + timeout + " seconds."); reqWaiter.handleRequestDone(req); } }
public void invoke() { if (method != null) { if (method.checkParameters(request)) { method.invoke(request); } else { request.setError(ErrorCode.WRONG_PARAMS, "Parameters in " + request + " does not match " + method); } } else { request.setError(ErrorCode.NO_SUCH_METHOD, "No such method"); } if (!request.isDetached()) { returnRequest(); } }
/** * Handle user abort. **/ public void handleAbort() { if (!conn.cancelReply(this)) { return; } timeoutTask.kill(); req.setError(ErrorCode.ABORT, "Aborted by user"); reqWaiter.handleRequestDone(req); }
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(); }
private void downloadFile(Request req) { FileReference fileReference = new FileReference(req.parameters().get(0).asString()); log.log(LogLevel.DEBUG, () -> "getFile() called for file reference '" + fileReference.value() + "'"); Optional<File> pathToFile = downloader.getFile(fileReference); try { if (pathToFile.isPresent()) { req.returnValues().add(new StringValue(pathToFile.get().getAbsolutePath())); log.log(LogLevel.DEBUG, () -> "File reference '" + fileReference.value() + "' available at " + pathToFile.get()); } else { log.log(LogLevel.INFO, "File reference '" + fileReference.value() + "' not found, returning error"); req.setError(fileReferenceDoesNotExists, "File reference '" + fileReference.value() + "' not found"); } } catch (Throwable e) { log.log(LogLevel.WARNING, "File reference '" + fileReference.value() + "' got exception: " + e.getMessage()); req.setError(fileReferenceInternalError, "File reference '" + fileReference.value() + "' removed"); } req.returnRequest(); }
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; }
private void rpc_register(Request req) { String name = req.parameters().get(0).asString(); String spec = req.parameters().get(1).asString(); String stored = services.get(name); if (stored == null) { new RegisterCallback(req, name, spec); } else { if ( ! stored.equals(spec)) req.setError(ErrorCode.METHOD_FAILED, "service '" + name + "' registered with another spec"); } }
public void invoke() { if (!conn.postPacket(new RequestPacket(0, replyKey.intValue(), req.methodName(), req.parameters()), this)) { req.setError(ErrorCode.CONNECTION, "Connection error"); reqWaiter.handleRequestDone(req); return; } timeoutTask.schedule(timeout); }
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"); } } }
/** * Handle normal packet reply. The reply may contain either return * values or an error. **/ public void handleReply(Packet packet) { timeoutTask.kill(); if (packet == null) { req.setError(ErrorCode.BAD_REPLY, "Bad reply packet"); } else { int pcode = packet.packetCode(); if (pcode == Packet.PCODE_REPLY) { ReplyPacket rp = (ReplyPacket) packet; req.returnValues(rp.returnValues()); } else if (pcode == Packet.PCODE_ERROR) { ErrorPacket ep = (ErrorPacket) packet; req.setError(ep.errorCode(), ep.errorMessage()); } } reqWaiter.handleRequestDone(req); }
@Override public void handleRequestDone(Request req) { if ( ! req.checkReturnTypes("S")) { registerReq.setError(ErrorCode.METHOD_FAILED, "error during register callback: " + req.errorMessage()); registerReq.returnRequest(); target.close(); return; } String[] names = req.returnValues().get(0).asStringArray(); boolean found = false; for (String n : names) { if (n.equals(name)) { found = true; } } if (!found) { registerReq.setError(ErrorCode.METHOD_FAILED, "register failed: served names does not contain name"); registerReq.returnRequest(); target.close(); return; } handleRegisterCallbackDone(registerReq, name, spec, target); } }
public void returnRequest() { if (!conn.completeRequest(done)) { throw new IllegalStateException("Request already returned"); } if (noReply) { return; } if (!request.isError() && !method.checkReturnValues(request)) { request.setError(ErrorCode.WRONG_RETURN, "Return values in " + request + " does not match " + method); } if (request.isError()) { conn.postPacket(new ErrorPacket(0, replyKey, request.errorCode(), request.errorMessage())); } else { conn.postPacket(new ReplyPacket(0, replyKey, request.returnValues())); } } }
public void getMethodInfo(Request req) { Method method = parent.methodMap().get(req.parameters().get(0).asString()); if (method == null) { req.setError(ErrorCode.METHOD_FAILED, "No Such Method"); return; } req.returnValues().add(new StringValue(method.methodDesc())); req.returnValues().add(new StringValue(method.paramTypes())); req.returnValues().add(new StringValue(method.returnTypes())); int paramCnt = method.paramTypes().length(); int returnCnt = method.returnTypes().length(); String[] ret3_paramName = new String[paramCnt]; String[] ret4_paramDesc = new String[paramCnt]; String[] ret5_returnName = new String[returnCnt]; String[] ret6_returnDesc = new String[returnCnt]; for (int i = 0; i < paramCnt; i++) { ret3_paramName[i] = method.paramName(i); ret4_paramDesc[i] = method.paramDesc(i); } for (int i = 0; i < returnCnt; i++) { ret5_returnName[i] = method.returnName(i); ret6_returnDesc[i] = method.returnDesc(i); } req.returnValues().add(new StringArray(ret3_paramName)); req.returnValues().add(new StringArray(ret4_paramDesc)); req.returnValues().add(new StringArray(ret5_returnName)); req.returnValues().add(new StringArray(ret6_returnDesc)); } }