com.google.bitcoin.net
Class ProtobufParser<MessageType extends com.google.protobuf.MessageLite>

java.lang.Object
  extended by com.google.bitcoin.net.AbstractTimeoutHandler
      extended by com.google.bitcoin.net.ProtobufParser<MessageType>
All Implemented Interfaces:
StreamParser

public class ProtobufParser<MessageType extends com.google.protobuf.MessageLite>
extends AbstractTimeoutHandler
implements StreamParser

A handler which is used in NioServer and NioClient to split up incoming data streams into protobufs and provide an interface for writing protobufs to the connections.

Messages are encoded with a 4-byte signed integer (big endian) prefix to indicate their length followed by the serialized protobuf


Nested Class Summary
static interface ProtobufParser.Listener<MessageType extends com.google.protobuf.MessageLite>
          An interface which can be implemented to handle callbacks as new messages are generated and socket events occur.
 
Constructor Summary
ProtobufParser(ProtobufParser.Listener<MessageType> handler, MessageType prototype, int maxMessageSize, int timeoutMillis)
          Creates a new protobuf handler.
 
Method Summary
 void closeConnection()
          Closes this connection, eventually triggering a connectionClosed() event.
 void connectionClosed()
          Called when the connection socket is closed
 void connectionOpened()
          Called when the connection socket is first opened
 int getMaxMessageSize()
          Returns the maximum message size of a message on the socket.
 int receiveBytes(ByteBuffer buff)
          Called when new bytes are available from the remote end.
 void setWriteTarget(MessageWriteTarget writeTarget)
          Called when this parser is attached to an upstream write target (ie a low-level connection handler).
protected  void timeoutOccurred()
           
 void write(MessageType msg)
          Writes the given message to the other side of the connection, prefixing it with the proper 4-byte prefix.
 
Methods inherited from class com.google.bitcoin.net.AbstractTimeoutHandler
resetTimeout, setSocketTimeout, setTimeoutEnabled
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ProtobufParser

public ProtobufParser(ProtobufParser.Listener<MessageType> handler,
                      MessageType prototype,
                      int maxMessageSize,
                      int timeoutMillis)
Creates a new protobuf handler.

Parameters:
handler - The callback listener
prototype - The default instance of the message type used in both directions of this channel. This should be the return value from MessageType#getDefaultInstanceForType()
maxMessageSize - The maximum message size (not including the 4-byte length prefix). Note that this has an upper bound of Integer.MAX_VALUE - 4
timeoutMillis - The timeout between messages before the connection is automatically closed. Only enabled after the connection is established.
Method Detail

setWriteTarget

public void setWriteTarget(MessageWriteTarget writeTarget)
Description copied from interface: StreamParser
Called when this parser is attached to an upstream write target (ie a low-level connection handler). This writeTarget should be stored and used to close the connection or write data to the socket.

Specified by:
setWriteTarget in interface StreamParser

getMaxMessageSize

public int getMaxMessageSize()
Description copied from interface: StreamParser
Returns the maximum message size of a message on the socket. This is used in calculating size of buffers to allocate.

Specified by:
getMaxMessageSize in interface StreamParser

closeConnection

public void closeConnection()
Closes this connection, eventually triggering a connectionClosed() event.


timeoutOccurred

protected void timeoutOccurred()
Specified by:
timeoutOccurred in class AbstractTimeoutHandler

receiveBytes

public int receiveBytes(ByteBuffer buff)
                 throws Exception
Description copied from interface: StreamParser

Called when new bytes are available from the remote end. This should only ever be called by the single writeTarget associated with any given StreamParser, multiple callers will likely confuse implementations.

Implementers/callers must follow the following conventions exactly:

Specified by:
receiveBytes in interface StreamParser
Returns:
The amount of bytes consumed which should not be provided again
Throws:
Exception

connectionClosed

public void connectionClosed()
Description copied from interface: StreamParser
Called when the connection socket is closed

Specified by:
connectionClosed in interface StreamParser

connectionOpened

public void connectionOpened()
Description copied from interface: StreamParser
Called when the connection socket is first opened

Specified by:
connectionOpened in interface StreamParser

write

public void write(MessageType msg)
           throws IllegalStateException

Writes the given message to the other side of the connection, prefixing it with the proper 4-byte prefix.

Provides a write-order guarantee.

Throws:
IllegalStateException - If the encoded message is larger than the maximum message size.


Copyright © 2014. All rights reserved.