Package com.almende.eve.rpc.jsonrpc

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


   * @throws Exception
   */
  @Override
  public JSONResponse send(final String senderId, final String receiverUrl,
      final JSONRequest request) throws Exception {
    JSONResponse response;

    // invoke via http request
    String req = request.toString();
    String resp = HttpUtil.post(receiverUrl, req);

    try {
      response = new JSONResponse(resp);
    } catch (JSONRPCException err) {
      response = new JSONResponse(err);
    }
    return response;
  }
View Full Code Here


      final JSONRequest request,
      final AsyncCallback<JSONResponse> callback) {
    new Thread(new Runnable () {
      @Override
      public void run() {
        JSONResponse response;
        try {
          response = send(senderId, receiverUrl, request);
          callback.onSuccess(response);
        } catch (Exception e) {
          callback.onFailure(e);
View Full Code Here

            // Find and execute the corresponding callback
            String id = json.has("id") ? json.get("id").asText() : null;
            AsyncCallback<JSONResponse> callback =
                (id != null) ? callbacks.pull(id) : null;
            if (callback != null) {
              callback.onSuccess(new JSONResponse(body));
            }
            else {
              /*
              // TODO: is it needed to send this error back?
              // can possibly result in weird loops?
              throw new Exception("Callback with id '" + id + "' not found");
              */
            }
          }
          else if (isRequest(json)) {
            // this is a request
            String senderUrl = message.getFrom();
            JSONRequest request = new JSONRequest(json);
            invoke(senderUrl, request);
          }
          else {
            throw new Exception("Request does not contain a valid JSON-RPC request or response");
          }
        }
        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 reply = new Message();
          reply.setTo(message.getFrom());
          reply.setBody(response.toString());
          conn.sendPacket(reply);
        }
      }
    }
View Full Code Here

     */
    private void invoke (final String senderUrl, final JSONRequest request) {
      new Thread(new Runnable () {
        @Override
        public void run() {
          JSONResponse response;
          try {
            // append the sender to the request parameters
            RequestParams params = new RequestParams();
            params.put(Sender.class, senderUrl);

            // invoke the agent
            response = agentFactory.invoke(agentId, request, params);
          } catch (Exception err) {
            // generate JSON error response
            JSONRPCException jsonError = new JSONRPCException(
                JSONRPCException.CODE.INTERNAL_ERROR, err.getMessage());
            response = new JSONResponse(jsonError);
          }
         
          if (response != null) {
            //String from = StringUtils.parseBareAddress(senderUrl);
            Message reply = new Message();
            reply.setTo(senderUrl);
            reply.setBody(response.toString());
            conn.sendPacket(reply);
          }
        }
      }).start();   
    }
View Full Code Here

            RequestParams params = new RequestParams();
            String senderUrl = "local://" + agentId;
            params.put(Sender.class, senderUrl);
            // TODO: provide itself
           
            JSONResponse resp = host.receive(agentId, request,
                params);
           
            if (interval > 0 && sequential && !cancelled()) {
              start(interval);
            }
            if (resp.getError() != null) {
              throw resp.getError();
            }
          } catch (Exception e) {
            LOG.log(Level.WARNING, "", e);
          } finally {
            if (interval <= 0) {
View Full Code Here

          RequestParams params = new RequestParams();
          String senderUrl = "local://" + myAgent.getId();
          params.put(Sender.class, senderUrl);
         
          //Next call is potentially long duration:
          JSONResponse resp = myAgent.getAgentHost().receive(
              myAgent.getId(), task.getRequest(), params);
         
          if (task.getInterval() > 0 && task.isSequential()) {
            task.setDue(DateTime.now().plus(task.getInterval()));
            task.setActive(false);
            _this.putTask(task, true);
            _this.run();
          }
          if (resp.getError() != null) {
            throw resp.getError();
          }
        } catch (Exception e) {
          LOG.log(Level.SEVERE, myAgent.getId()+": Failed to run scheduled task:"+task.toString(), e);
        }
      }
View Full Code Here

            // to add namespace to method name for JSON-RPC.
            if (method.getName().equals("getId")) {
              return proxyId;
            } else if (method.getName().equals("receive")
                && args.length > 1) {
              JSONResponse response = null;
              if (args[0] != null) {
                response = receive(args[0]);
              }
              if (response != null) {
                JsonNode id = null;
                if (response.getId() != null) {
                  id = response.getId();
                }
                final AsyncCallbackQueue<JSONResponse> cbs = host
                    .getCallbackQueue(proxyId,
                        JSONResponse.class);
                if (cbs != null) {
                  final AsyncCallback<JSONResponse> callback = cbs
                      .pull(id);
                  if (callback != null) {
                    if (response.getError() != null) {
                      callback.onFailure(response
                          .getError());
                    } else {
                      callback.onSuccess(response);
                    }
                  }
                }
              }
              return null;
            } else {
             
              final JSONRequest request = JSONRPC.createRequest(
                  method, args);
             
              final SyncCallback<JSONResponse> callback = new SyncCallback<JSONResponse>();
              final AsyncCallbackQueue<JSONResponse> cbs = host
                  .getCallbackQueue(proxyId,
                      JSONResponse.class);
              if (cbs != null) {
                cbs.push(request.getId(), "", callback);
              }
              try {
                host.sendAsync(receiverUrl, request, agent,
                    null);
              } 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

            + pushParams);
      }
      final String method = pushParams.get("method").textValue();
      final ObjectNode params = (ObjectNode) JOM.getInstance().readTree(
          pushParams.get("params").textValue());
      final JSONResponse res = JSONRPC.invoke(myAgent, new JSONRequest(method,
          params), myAgent);
     
      final JsonNode result = res.getResult();
      if (pushParams.has("onChange")
          && pushParams.get("onChange").asBoolean()) {
        if (lastRes != null && lastRes.equals(result)) {
          return;
        }
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

        } else {
          LOG.warning("Message unknown type:" + msg.getClass());
        }
        if (json != null) {
          if (JSONRPC.isResponse(json)) {
            final JSONResponse response = new JSONResponse(json);
            jsonMsg = response;
          } else if (JSONRPC.isRequest(json)) {
            final JSONRequest request = new JSONRequest(json);
            jsonMsg = request;
          } else {
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.