Package org.apache.avro.Protocol

Examples of org.apache.avro.Protocol.Message


                          + message
                          + "}}").getMessages().values().iterator().next();
  }

  @Test public void oneWay() throws Exception {
    Message m;
    // permit one-way messages w/ null resposne
    m = parseMessage("\"ack\": {"
                     +"\"request\": [],"
                     +"\"response\": \"null\","
                     +"\"one-way\": true}");
    assertTrue(m.isOneWay());
    // permit one-way messages w/o response
    m = parseMessage("\"ack\": {"
                     +"\"request\": [],"
                     +"\"one-way\": true}");
    assertTrue(m.isOneWay());
  }
View Full Code Here


      handshake = bbo.getBufferList();
     
      // read request using remote protocol specification
      context.setRequestCallMeta(META_READER.read(null, in));
      String messageName = in.readString(null).toString();
      Message rm = remote.getMessages().get(messageName);
      if (rm == null)
        throw new AvroRuntimeException("No such remote message: "+messageName);
      Message m = getLocal().getMessages().get(messageName);
      if (m == null)
        throw new AvroRuntimeException("No message named "+messageName
                                       +" in "+getLocal());

      Object request = readRequest(rm.getRequest(), m.getRequest(), in);
     
      context.setMessage(rm);
      for (RPCPlugin plugin : rpcMetaPlugins) {
        plugin.serverReceiveRequest(context);
      }

      // create response using local protocol specification
      if ((m.isOneWay() != rm.isOneWay()) && wasConnected)
        throw new AvroRuntimeException("Not both one-way: "+messageName);

      Object response = null;
     
      try {
        REMOTE.set(remote);
        response = respond(m, request);
        context.setResponse(response);
      } catch (Exception e) {
        error = e;
        context.setError(error);
        LOG.warn("user error", e);
      } finally {
        REMOTE.set(null);
      }
     
      if (m.isOneWay() && wasConnected)           // no response data
        return null;

      out.writeBoolean(error != null);
      if (error == null)
        writeResponse(m.getResponse(), response, out);
      else
        try {
          writeError(m.getErrors(), error, out);
        } catch (UnresolvedUnionException e) {    // unexpected error
          throw error;
        }
    } catch (Exception e) {                       // system error
      LOG.warn("system error", e);
View Full Code Here

      if (requestBytes == null) {
        ByteBufferOutputStream bbo = new ByteBufferOutputStream();
        BinaryEncoder out = ENCODER_FACTORY.binaryEncoder(bbo, encoder);

        // use local protocol to write request
        Message m = getMessage();
        context.setMessage(m);

        writeRequest(m.getRequest(), request, out); // write request payload

        out.flush();
        List<ByteBuffer> payload = bbo.getBufferList();

        writeHandshake(out);                     // prepend handshake if needed

        context.setRequestPayload(payload);
        for (RPCPlugin plugin : rpcMetaPlugins) {
          plugin.clientSendRequest(context);      // get meta-data from plugins
        }
        META_WRITER.write(context.requestCallMeta(), out);

        out.writeString(m.getName());             // write message name

        out.flush();
        bbo.append(payload);

        requestBytes = bbo.getBufferList();
View Full Code Here

     * @return the RPC response.
     * @throws Exception if an error occurs reading/deserializing the response.
     */
    public Object getResponse()
      throws Exception {
      Message lm = request.getMessage();
      Message rm = remote.getMessages().get(request.getMessageName());
      if (rm == null)
        throw new AvroRuntimeException
          ("Not a remote message: "+request.getMessageName());

      Transceiver t = getTransceiver();
      if ((lm.isOneWay() != rm.isOneWay()) && t.isConnected())
        throw new AvroRuntimeException
          ("Not both one-way messages: "+request.getMessageName());

      if (lm.isOneWay() && t.isConnected()) return null; // one-way w/ handshake
     
      RPCContext context = request.getContext();
      context.setResponseCallMeta(META_READER.read(null, in));

      if (!in.readBoolean()) {                      // no error
        Object response = readResponse(rm.getResponse(), lm.getResponse(), in);
        context.setResponse(response);
        for (RPCPlugin plugin : rpcMetaPlugins) {
          plugin.clientReceiveResponse(context);
        }
        return response;

      } else {
        Exception error = readError(rm.getErrors(), lm.getErrors(), in);
        context.setError(error);
        for (RPCPlugin plugin : rpcMetaPlugins) {
          plugin.clientReceiveResponse(context);
        }
        throw error;
View Full Code Here

      line(0, "public interface "+protocol.getName()+" {");

      out.append("\n");
      for (Map.Entry<String,Message> e : protocol.getMessages().entrySet()) {
        String name = e.getKey();
        Message message = e.getValue();
        Schema request = message.getRequest();
        Schema response = message.getResponse();
        line(1, unbox(response)+" "+name+"("+params(request)+")");
        line(2,"throws AvroRemoteException"+errors(message.getErrors())+";");
      }
      line(0, "}");
    } finally {
      out.close();
    }
View Full Code Here

      handshake = bbo.getBufferList();
     
      // read request using remote protocol specification
      context.setRequestCallMeta(META_READER.read(null, in));
      String messageName = in.readString(null).toString();
      Message rm = remote.getMessages().get(messageName);
      if (rm == null)
        throw new AvroRuntimeException("No such remote message: "+messageName);
      Message m = getLocal().getMessages().get(messageName);
      if (m == null)
        throw new AvroRuntimeException("No message named "+messageName
                                       +" in "+getLocal());

      Object request = readRequest(rm.getRequest(), m.getRequest(), in);
     
      context.setMessage(rm);
      for (RPCPlugin plugin : rpcMetaPlugins) {
        plugin.serverReceiveRequest(context);
      }

      // create response using local protocol specification
      if ((m.isOneWay() != rm.isOneWay()) && wasConnected)
        throw new AvroRuntimeException("Not both one-way: "+messageName);

      Object response = null;
     
      try {
        REMOTE.set(remote);
        response = respond(m, request);
        context.setResponse(response);
      } catch (Exception e) {
        error = e;
        context.setError(error);
        LOG.warn("user error", e);
      } finally {
        REMOTE.set(null);
      }
     
      if (m.isOneWay() && wasConnected)           // no response data
        return null;

      out.writeBoolean(error != null);
      if (error == null)
        writeResponse(m.getResponse(), response, out);
      else
        try {
          writeError(m.getErrors(), error, out);
        } catch (UnresolvedUnionException e) {    // unexpected error
          throw error;
        }
    } catch (Exception e) {                       // system error
      LOG.warn("system error", e);
View Full Code Here

  /** Writes a request message and reads a response or error message. */
  public synchronized Object request(String messageName, Object request)
    throws Exception {
    Transceiver t = getTransceiver();
    BinaryDecoder in = null;
    Message m;
    RPCContext context = new RPCContext();
    do {
      ByteBufferOutputStream bbo = new ByteBufferOutputStream();
      //safe to use encoder because this is synchronized
      BinaryEncoder out = ENCODER_FACTORY.binaryEncoder(bbo, encoder);
     
      // use local protocol to write request
      m = getLocal().getMessages().get(messageName);
      if (m == null)
        throw new AvroRuntimeException("Not a local message: "+messageName);
      context.setMessage(m);
   
      writeRequest(m.getRequest(), request, out); // write request payload
     
      out.flush();
      List<ByteBuffer> payload = bbo.getBufferList();
     
      writeHandshake(out);                       // prepend handshake if needed
     
      context.setRequestPayload(payload);
      for (RPCPlugin plugin : rpcMetaPlugins) {
        plugin.clientSendRequest(context);        // get meta-data from plugins
      }
      META_WRITER.write(context.requestCallMeta(), out);

      out.writeString(m.getName());               // write message name

      out.flush();
      bbo.append(payload);
     
      List<ByteBuffer> requestBytes = bbo.getBufferList();

      if (m.isOneWay() && t.isConnected()) {      // send one-way message
        t.writeBuffers(requestBytes);
       
        return null;
      } else {                                    // two-way message
        List<ByteBuffer> response = t.transceive(requestBytes);
        ByteBufferInputStream bbi = new ByteBufferInputStream(response);
        in = DecoderFactory.get().binaryDecoder(bbi, in);
      }
    } while (!readHandshake(in));

    // use remote protocol to read response
    Message rm = remote.getMessages().get(messageName);
    if (rm == null)
      throw new AvroRuntimeException("Not a remote message: "+messageName);

    if ((m.isOneWay() != rm.isOneWay()) && t.isConnected())
      throw new AvroRuntimeException("Not both one-way messages: "+messageName);

    if (m.isOneWay() && t.isConnected()) return null; // one-way w/ handshake
       
    context.setResponseCallMeta(META_READER.read(null, in));
   
    if (!in.readBoolean()) {                      // no error
      Object response = readResponse(rm.getResponse(), in);
      context.setResponse(response);
      for (RPCPlugin plugin : rpcMetaPlugins) {
        plugin.clientReceiveResponse(context);
      }
      return response;
     
    } else {
      Exception error = readError(rm.getErrors(), in);
      context.setError(error);
      for (RPCPlugin plugin : rpcMetaPlugins) {
        plugin.clientReceiveResponse(context);
      }
      throw error;
View Full Code Here

    // define methods
    buffer.append("\n");
    for (Map.Entry<String,Message> entry : protocol.getMessages().entrySet()) {
      String name = entry.getKey();
      Message message = entry.getValue();
      Schema request = message.getRequest();
      Schema response = message.getResponse();
      line(1, type(response, name+"Return")+" "+name+"("+params(request)+")");
      line(2,"throws AvroRemoteException"+errors(message.getErrors())+";");
    }
    line(0, "}");
  }
View Full Code Here

        return bbo.getBufferList();

      // read request using remote protocol specification
      context.setRequestCallMeta(META_READER.read(null, in));
      String messageName = in.readString(null).toString();
      Message m = remote.getMessages().get(messageName);
      if (m == null)
        throw new AvroRuntimeException("No such remote message: "+messageName);
     
      Object request = readRequest(m.getRequest(), in);
     
      for (RPCPlugin plugin : rpcMetaPlugins) {
        plugin.serverReceiveRequest(context);
      }

      // create response using local protocol specification
      m = getLocal().getMessages().get(messageName);
      if (m == null)
        throw new AvroRuntimeException("No such local message: "+messageName);
      Object response = null;
      try {
        response = respond(m, request);
        context.setResponse(response);
      } catch (AvroRemoteException e) {
        error = e;
        context.setError(error);
      } catch (Exception e) {
        LOG.warn("application error", e);
        error = new AvroRemoteException(new Utf8(e.toString()));
        context.setError(error);
      }
     
      for (RPCPlugin plugin : rpcMetaPlugins) {
        plugin.serverSendResponse(context);
      }
     
      META_WRITER.write(context.responseCallMeta(), out);
      out.writeBoolean(error != null);
      if (error == null)
        writeResponse(m.getResponse(), response, out);
      else
        writeError(m.getErrors(), error, out);

    } catch (AvroRuntimeException e) {            // system error
      LOG.warn("system error", e);
      error = new AvroRemoteException(e);
      context.setError(error);
View Full Code Here

  /** Writes a request message and reads a response or error message. */
  public Object request(String messageName, Object request)
    throws IOException {
    Decoder in;
    Message m;
    RPCContext context = new RPCContext();
    do {
      ByteBufferOutputStream bbo = new ByteBufferOutputStream();
      Encoder out = new BinaryEncoder(bbo);

      if (!established)                           // if not established
        writeHandshake(out);                      // prepend handshake

      // use local protocol to write request
      m = getLocal().getMessages().get(messageName);
      if (m == null)
        throw new AvroRuntimeException("Not a local message: "+messageName);
     
      for (RPCPlugin plugin : rpcMetaPlugins) {
        plugin.clientSendRequest(context);
      }
     
      META_WRITER.write(context.requestCallMeta(), out);
      out.writeString(m.getName());       // write message name
      writeRequest(m.getRequest(), request, out); // write request payload
     
      List<ByteBuffer> response =                 // transceive
        getTransceiver().transceive(bbo.getBufferList());
     
      ByteBufferInputStream bbi = new ByteBufferInputStream(response);
      in = new BinaryDecoder(bbi);
      if (!established)                           // if not established
        readHandshake(in);                        // process handshake
    } while (!established);

    // use remote protocol to read response
    m = getRemote().getMessages().get(messageName);
    if (m == null)
      throw new AvroRuntimeException("Not a remote message: "+messageName);
    context.setRequestCallMeta(META_READER.read(null, in));
   
    if (!in.readBoolean()) {                      // no error
      Object response = readResponse(m.getResponse(), in);
      context.setResponse(response);
      for (RPCPlugin plugin : rpcMetaPlugins) {
        plugin.clientReceiveResponse(context);
      }
      return response;
     
    } else {
      AvroRemoteException error = readError(m.getErrors(), in);
      context.setError(error);
      for (RPCPlugin plugin : rpcMetaPlugins) {
        plugin.clientReceiveResponse(context);
      }
      throw error;
View Full Code Here

TOP

Related Classes of org.apache.avro.Protocol.Message

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.