Package com.almende.eve.rpc.jsonrpc

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


          // TODO: cleanup logger info
          logger.info("request agentUrl =" + agentUrl + ", agentId=" + agentId + " request=" + json + ", sender=" + from.getId());

          // invoke the agent
          JSONRequest request = new JSONRequest(json);
          JSONResponse response = agentFactory.invoke(agentId, request, params);

          // reply to message
              Message msg = new MessageBuilder()
                .withRecipientJids(from)
                .withFromJid(to)
                .withBody(response.toString())
                .build();
              xmpp.sendMessage(msg)
        }
        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 msg = new MessageBuilder()
              .withRecipientJids(from)
              .withFromJid(to)
              .withBody(response.toString())
              .build();
            xmpp.sendMessage(msg);
      }
    }
    }
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.receive(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

        new Class[] { agentInterface }, new InvocationHandler() {
          public Object invoke(Object proxy, Method method,
              Object[] args) throws Throwable {
            JSONRequest request = JSONRPC.createRequest(method,
                args);
            JSONResponse response = send(sender, receiverUrl,
                request);

            JSONRPCException err = response.getError();
            if (err != null) {
              throw err;
            } else if (response.getResult() != null
                && !method.getReturnType().equals(Void.TYPE)) {
              return response.getResult(method.getReturnType());
            } else {
              return null;
            }
          }
        });
View Full Code Here

   */
  public JSONResponse receive(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

            // TODO: if method calls for Namespace getter, return
            // new proxy for subtype. All calls to that proxy need
            // to add namespace to method name for JSON-RPC.
            JSONRequest request = JSONRPC.createRequest(method,
                args);
            JSONResponse response = send(sender, receiverUrl,
                request);
           
            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

  public JSONResponse receive(String receiverId, JSONRequest request,
      RequestParams requestParams) throws JSONRPCException {
    try {
      Agent receiver = getAgent(receiverId);
      if (receiver != null) {
        JSONResponse response = JSONRPC.invoke(receiver, request,
            requestParams, receiver);
        return response;
      }
    } catch (Exception e) {
      throw new JSONRPCException("Couldn't instantiate agent for id '"
View Full Code Here

    } else {
      TransportService service = null;
      service = getTransportService(protocol);
     
      if (service != null) {
        JSONResponse response = service.send(senderUrl,
            receiverUrl.toASCIIString(), request);
        return response;
      } else {
        throw new ProtocolException(
            "No transport service configured for protocol '"
View Full Code Here

    if (doesShortcut && receiverId != null) {
      // local shortcut
      new Thread(new Runnable() {
        @Override
        public void run() {
          JSONResponse response;
          try {
            String senderUrl = null;
            if (sender != null) {
              senderUrl = getSenderUrl(sender.getId(),
                  receiverUrl.toASCIIString());
View Full Code Here

   
    // invoke the other agent via the AgentHost, allowing the factory
    // to route the request internally or externally
    String id = UUID.randomUUID().toString();
    JSONRequest request = new JSONRequest(id, method, jsonParams);
    JSONResponse response = getAgentHost().send(this, url, request);
    JSONRPCException err = response.getError();
    if (err != null) {
      throw err;
    }
    return response;
  }
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.