Package com.almende.eve.rpc.jsonrpc

Examples of com.almende.eve.rpc.jsonrpc.JSONResponse


              signalData[0] = request;
              signalData[1] = params;
              signalAgent(new AgentSignal<Object[]>(
                  AgentSignal.INVOKE, signalData));
             
              final JSONResponse response = JSONRPC.invoke(me,
                  request, params, me);
             
              signalAgent(new AgentSignal<JSONResponse>(
                  AgentSignal.RESPOND, response));
              try {
                send(response, senderUrl, null, tag);
              } catch (final IOException e) {
                LOG.log(Level.WARNING, getId()
                    + ": Failed to send response.", e);
              }
            }
          });
         
        } else if (jsonMsg instanceof JSONResponse && callbacks != null
            && id != null && !id.isNull()) {
          final JSONResponse response = (JSONResponse) jsonMsg;
          final AsyncCallback<JSONResponse> callback = callbacks.pull(id);
          if (callback != null) {
            host.getPool().execute(new Runnable() {
              @Override
              public void run() {
                signalAgent(new AgentSignal<JSONResponse>(
                    AgentSignal.RESPONSE, response));
                if (response.getError() != null) {
                  callback.onFailure(response.getError());
                } else {
                  callback.onSuccess(response);
                }
              }
            });
          }
        }
      } else {
        LOG.log(Level.WARNING, getId()
            + ": Received non-JSON message:'" + msg + "'");
      }
    } catch (final Exception e) {
      LOG.log(Level.WARNING, "Exception in receiving message", e);
      // generate JSON error response, skipped if it was an incoming
      // notification i.s.o. request.
      final JSONRPCException jsonError = new JSONRPCException(
          JSONRPCException.CODE.INTERNAL_ERROR, e.getMessage(), e);
      final JSONResponse response = new JSONResponse(jsonError);
      response.setId(id);
      signalAgent(new AgentSignal<JSONResponse>(AgentSignal.EXCEPTION,
          response));
      try {
        send(response, senderUrl, null, tag);
      } catch (final Exception e1) {
View Full Code Here


    // This should already been done!
    if (msg instanceof JSONRPCException) {
      LOG.log(Level.WARNING,
          "Send has been called to send an JSONRPCException i.s.o. a JSONMessage...");
      host.sendAsync(receiverUrl,
          new JSONResponse((JSONRPCException) msg), this, tag);
      return;
    }
    host.sendAsync(receiverUrl, msg, this, tag);
  }
View Full Code Here

            } catch (final IOException e1) {
              throw new JSONRPCException(CODE.REMOTE_EXCEPTION,
                  "", e1);
            }
           
            JSONResponse response;
            try {
              response = callback.get();
            } catch (final Exception e) {
              throw new JSONRPCException(CODE.REMOTE_EXCEPTION,
                  "", e);
            }
            final JSONRPCException err = response.getError();
            if (err != null) {
              throw err;
            } else if (response.getResult() != null
                && !method.getReturnType().equals(Void.TYPE)) {
              return TypeUtil.inject(response.getResult(),
                  method.getGenericReturnType());
            } else {
              return null;
            }
          }
View Full Code Here

  // TOOD: cleanup this method?
  public JSONResponse invoke(String receiverId,
      JSONRequest request, RequestParams requestParams) throws Exception {
    Agent receiver = getAgent(receiverId);
    if (receiver != null) {
      JSONResponse response = JSONRPC.invoke(receiver, request, requestParams);
      receiver.destroy();
      return response;
    }
    else {
      throw new Exception("Agent with id '" + receiverId + "' not found");
View Full Code Here

    if (agentId != null) {
      // local agent, invoke locally
      // TODO: provide Sender in requestParams
      RequestParams requestParams = new RequestParams();
      requestParams.put(Sender.class, null);
      JSONResponse response = invoke(agentId, request, requestParams);
      return response;
    }
    else {
      TransportService service = null;
      String protocol = null;
      int separator = receiverUrl.indexOf(":");
      if (separator != -1) {
        protocol = receiverUrl.substring(0, separator);
        service = getTransportService(protocol);
      }
      if (service != null) {
        JSONResponse response = service.send(senderId, receiverUrl, request);
        return response;
      }
      else {
        throw new ProtocolException(
          "No transport service configured for protocol '" + protocol + "'.");
View Full Code Here

    final String receiverId = getAgentId(receiverUrl);
    if (receiverId != null) {
      new Thread(new Runnable () {
        @Override
        public void run() {
          JSONResponse response;
          try {
            // TODO: provide Sender in requestParams
            RequestParams requestParams = new RequestParams();
            requestParams.put(Sender.class, null);
            response = invoke(receiverId, request, requestParams);
View Full Code Here

   
    // invoke the other agent via the state, allowing the state
    // to route the request internally or externally
    String id = UUID.randomUUID().toString();
    JSONRequest request = new JSONRequest(id, method, jsonParams);
    JSONResponse response = getAgentFactory().send(getId(), url, request);
    JSONRPCException err = response.getError();
    if (err != null) {
      throw err;
    }
    if (type != null && type != void.class) {
      return response.getResult(type);
    }
   
    return null;
  }
View Full Code Here

    // invoke the other agent via the AgentHost, allowing the factory
    // to route the request internally or externally
    final JSONRequest request = new JSONRequest(method, jsonParams);
    final SyncCallback<JSONResponse> callback = new SyncCallback<JSONResponse>();
    send(request, url, callback, null);
    JSONResponse response;
    try {
      response = callback.get();
    } catch (final Exception e) {
      throw new JSONRPCException(CODE.REMOTE_EXCEPTION, "", e);
    }
    final JSONRPCException err = response.getError();
    if (err != null) {
      throw err;
    }
    return response;
  }
View Full Code Here

       
      } catch (Exception err) {
        // generate JSON error response
        JSONRPCException jsonError = new JSONRPCException(
            JSONRPCException.CODE.INTERNAL_ERROR, err.getMessage());
        JSONResponse response = new JSONResponse(jsonError);
       
        // send exception as response
        Message msg = new MessageBuilder().withRecipientJids(from)
            .withFromJid(to).withBody(response.toString()).build();
        xmpp.sendMessage(msg);
      }
    }
  }
View Full Code Here

TOP

Related Classes of com.almende.eve.rpc.jsonrpc.JSONResponse

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.