seda.sandStorm.lib.Gnutella
Class GnutellaServer

java.lang.Object
  extended by seda.sandStorm.lib.Gnutella.GnutellaServer
All Implemented Interfaces:
EventHandlerIF, GnutellaConst

public class GnutellaServer
extends java.lang.Object
implements EventHandlerIF, GnutellaConst

A GnutellaServer is a SandStorm stage which allows outgoing connections to be established to the Gnutella network, and accepts incoming connections. The server has a client sink associated with it, onto which GnutellaConnection and GnutellaPacket events are pushed. When a connection is closed, a SinkClosedEvent is pushed, with the sink pointer set to the GnutellaConnection that closed. If a an outgoing connection fails, a GnutellaConnectFailedevent is pushed.

See Also:
GnutellaConnection, GnutellaPacket

Nested Class Summary
(package private)  class GnutellaServer.connectionState
          Internal class used to monitor state of connections during handshake phase
 
Field Summary
private  boolean acceptIncoming
           
private  java.util.Vector activeConnections
           
private  SinkIF clientSink
           
private  ATcpClientSocket clisock
           
private static byte[] connectMsg
           
private static byte[] connectReplyMsg
           
private  boolean connectUpstream
           
private  java.util.Hashtable connTable
           
private static boolean DEBUG
           
private  java.lang.String hostname
           
private  int listenPort
           
private  ManagerIF mgr
           
private  SinkIF mySink
           
private  java.util.Hashtable newConnTable
           
private static int num_svrs
           
private  java.util.Hashtable pendingConnTable
           
private  int port
           
private  java.util.Hashtable readerTable
           
private  ATcpServerSocket servsock
           
 
Fields inherited from interface seda.sandStorm.lib.Gnutella.GnutellaConst
DEFAULT_DOWNLOAD_PORT, DEFAULT_GNUTELLA_PORT, DEFAULT_HOPS, DEFAULT_PING_INTERVAL, DEFAULT_SPEED, DEFAULT_TTL, GNUTELLA_CONNECT, GNUTELLA_FN_PING, GNUTELLA_FN_PONG, GNUTELLA_FN_PUSH, GNUTELLA_FN_QUERY, GNUTELLA_FN_QUERYHITS, GNUTELLA_OK, MAX_PAYLOAD_SIZE, PACKET_HEADER_SIZE, WRITE_CLOG_THRESHOLD
 
Constructor Summary
GnutellaServer(ManagerIF mgr, SinkIF clientSink)
          Create a Gnutella server listening for incoming connections on the default port of 6346.
GnutellaServer(ManagerIF mgr, SinkIF clientSink, int listenPort)
          Create a Gnutella server listening for incoming connections on the given listenPort.
 
Method Summary
(package private)  void cleanupConnection(ATcpConnection tcpconn, GnutellaConnection gc)
           
(package private)  void closeConnection(ATcpConnection tcpconn, SinkIF compQ)
           
private  void continuePacket(ATcpInPacket pkt)
           
 void destroy()
          Called when an event handler is destroyed.
(package private)  SinkIF getSink()
           
 void handleEvent(QueueElementIF qel)
          Handle the event corresponding to the given QueueElementIF.
 void handleEvents(QueueElementIF[] qelarr)
          Handle the events corresponding to the given QueueElementIF array.
private  void handleHandshake(ATcpInPacket pkt)
           
private  void handleIncomingConnection(ATcpConnection conn)
           
 void init(ConfigDataIF config)
          Called when an event handler is initialized.
 void openConnection(java.net.InetAddress address, int port)
          Open a connection to the given address and port.
 void openConnection(java.lang.String hostname, int port)
          Open a connection to the given hostname and port.
private  void pushNewConnection(ATcpConnection conn)
           
 void registerSink(SinkIF sink)
          Register a sink to receive incoming packets on this connection.
private  void sendBytes(SinkIF sink, byte[] msg)
           
 void sendToAllButOne(GnutellaPacket pkt, GnutellaConnection exclude)
          Send a packet to all nodes but the given node.
 java.lang.String toString()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

DEBUG

private static final boolean DEBUG
See Also:
Constant Field Values

acceptIncoming

private boolean acceptIncoming

connectUpstream

private boolean connectUpstream

hostname

private java.lang.String hostname

port

private int port

listenPort

private int listenPort

servsock

private ATcpServerSocket servsock

clisock

private ATcpClientSocket clisock

mgr

private ManagerIF mgr

mySink

private SinkIF mySink

clientSink

private SinkIF clientSink

readerTable

private java.util.Hashtable readerTable

connTable

private java.util.Hashtable connTable

newConnTable

private java.util.Hashtable newConnTable

pendingConnTable

private java.util.Hashtable pendingConnTable

activeConnections

private java.util.Vector activeConnections

num_svrs

private static int num_svrs

connectMsg

private static byte[] connectMsg

connectReplyMsg

private static byte[] connectReplyMsg
Constructor Detail

GnutellaServer

public GnutellaServer(ManagerIF mgr,
                      SinkIF clientSink)
               throws java.lang.Exception
Create a Gnutella server listening for incoming connections on the default port of 6346.

Throws:
java.lang.Exception

GnutellaServer

public GnutellaServer(ManagerIF mgr,
                      SinkIF clientSink,
                      int listenPort)
               throws java.lang.Exception
Create a Gnutella server listening for incoming connections on the given listenPort. If listenPort == 0, no incoming connections will be accepted. (Outgoing connections can still be established using openConnection.)

Throws:
java.lang.Exception
Method Detail

init

public void init(ConfigDataIF config)
          throws java.io.IOException
Description copied from interface: EventHandlerIF
Called when an event handler is initialized. This method should perform any initialization operations as required by the application.

Specified by:
init in interface EventHandlerIF
Parameters:
config - The set of configuration parameters for the stage.
Throws:
java.io.IOException

destroy

public void destroy()
Description copied from interface: EventHandlerIF
Called when an event handler is destroyed. This method should perform any cleanup or shutdown operations as required by the application before the event handler is removed from the system.

Specified by:
destroy in interface EventHandlerIF

openConnection

public void openConnection(java.lang.String hostname,
                           int port)
                    throws java.net.UnknownHostException
Open a connection to the given hostname and port. When the connection is established, a GnutellaConnection will be pushed to this server's client sink.

Throws:
java.net.UnknownHostException

openConnection

public void openConnection(java.net.InetAddress address,
                           int port)
Open a connection to the given address and port. When the connection is established, a GnutellaConnection will be pushed to this server's client sink.


handleEvent

public void handleEvent(QueueElementIF qel)
Description copied from interface: EventHandlerIF
Handle the event corresponding to the given QueueElementIF. This method is invoked by the system when a single event is pending for the event handler.

Specified by:
handleEvent in interface EventHandlerIF

handleEvents

public void handleEvents(QueueElementIF[] qelarr)
Description copied from interface: EventHandlerIF
Handle the events corresponding to the given QueueElementIF array. This method is invoked when multiple events are pending for the event handler. The application may reorder, filter, or drop these events if it wishes to do so.

Specified by:
handleEvents in interface EventHandlerIF

continuePacket

private void continuePacket(ATcpInPacket pkt)

handleIncomingConnection

private void handleIncomingConnection(ATcpConnection conn)

pushNewConnection

private void pushNewConnection(ATcpConnection conn)

closeConnection

void closeConnection(ATcpConnection tcpconn,
                     SinkIF compQ)

cleanupConnection

void cleanupConnection(ATcpConnection tcpconn,
                       GnutellaConnection gc)

handleHandshake

private void handleHandshake(ATcpInPacket pkt)

sendBytes

private void sendBytes(SinkIF sink,
                       byte[] msg)

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object

registerSink

public void registerSink(SinkIF sink)
Register a sink to receive incoming packets on this connection.


getSink

SinkIF getSink()

sendToAllButOne

public void sendToAllButOne(GnutellaPacket pkt,
                            GnutellaConnection exclude)
Send a packet to all nodes but the given node. Useful for packet routing.