com.google.bitcoin.core
Class PeerSocketHandler

java.lang.Object
  extended by com.google.bitcoin.net.AbstractTimeoutHandler
      extended by com.google.bitcoin.core.PeerSocketHandler
All Implemented Interfaces:
StreamParser
Direct Known Subclasses:
Peer

public abstract class PeerSocketHandler
extends AbstractTimeoutHandler
implements StreamParser

Handles high-level message (de)serialization for peers, acting as the bridge between the com.google.bitcoin.net classes and Peer.


Field Summary
protected  PeerAddress peerAddress
           
 
Constructor Summary
PeerSocketHandler(NetworkParameters params, InetSocketAddress remoteIp)
           
PeerSocketHandler(NetworkParameters params, PeerAddress peerAddress)
           
 
Method Summary
 void close()
          Closes the connection to the peer if one exists, or immediately closes the connection as soon as it opens
 PeerAddress getAddress()
           
 int getMaxMessageSize()
          Returns the maximum message size of a message on the socket.
protected abstract  void processMessage(Message m)
          Called every time a message is received from the network
 int receiveBytes(ByteBuffer buff)
          Called when new bytes are available from the remote end.
 void sendMessage(Message message)
          Sends the given message to the peer.
 void setWriteTarget(MessageWriteTarget writeTarget)
          Sets the MessageWriteTarget used to write messages to the peer.
protected  void timeoutOccurred()
           
 
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
 
Methods inherited from interface com.google.bitcoin.net.StreamParser
connectionClosed, connectionOpened
 

Field Detail

peerAddress

protected PeerAddress peerAddress
Constructor Detail

PeerSocketHandler

public PeerSocketHandler(NetworkParameters params,
                         InetSocketAddress remoteIp)

PeerSocketHandler

public PeerSocketHandler(NetworkParameters params,
                         PeerAddress peerAddress)
Method Detail

sendMessage

public void sendMessage(Message message)
                 throws NotYetConnectedException
Sends the given message to the peer. Due to the asynchronousness of network programming, there is no guarantee the peer will have received it. Throws NotYetConnectedException if we are not yet connected to the remote peer. TODO: Maybe use something other than the unchecked NotYetConnectedException here

Throws:
NotYetConnectedException

close

public void close()
Closes the connection to the peer if one exists, or immediately closes the connection as soon as it opens


timeoutOccurred

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

processMessage

protected abstract void processMessage(Message m)
                                throws Exception
Called every time a message is received from the network

Throws:
Exception

receiveBytes

public int receiveBytes(ByteBuffer buff)
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

setWriteTarget

public void setWriteTarget(MessageWriteTarget writeTarget)
Sets the MessageWriteTarget used to write messages to the peer. This should almost never be called, it is called automatically by NioClient or NioClientManager once the socket finishes initialization.

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

getAddress

public PeerAddress getAddress()
Returns:
the IP address and port of peer.


Copyright © 2014. All rights reserved.