Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members   Related Pages  

LinkLayerInterface Class Reference

The LinkLayerInterface handles the set of all connections to neighbor nodes. More...

#include <LinkLayerInterface.h>

Inheritance diagram for LinkLayerInterface::

BasicObject ListenerInterface ObservableInterface List of all members.

Public Methods

 LinkLayerInterface ()
 Create the Link Layer. More...

virtual ~LinkLayerInterface ()
 Destructor. More...

int getNumConnections ()
 Returns the current number of open connections. More...

bool isConnected ()
 Return true if there is a connection to any other node, false otherwise. More...

bool isConnectedTo (IpAddress ipAddr)
 Check connection table to see if we are connected to specified node. More...

NodegetRandomNeighbor ()
 Return a random neighbor from our connection list. More...

NodegetRandomNeighbor (Node *node)
 Return a random neighbor from our connection list, except the one that you pass in. More...

bool getConnection (int index, Node *retVal)
 Get a copy of the node at the specified index in the connection table. More...

NodegetConnection (int index)
void registerPacketHandler (PacketHandlerInterface *packetHandler)
 Registers an object to be called whenever a packet is received. More...

void handleEvent (ObservableInterface *observed, int event, void *object)
 This is called by the Catcher object whenever it discovers a new Node. More...

int connect (Node *node)
 Connects to the specified node. More...

void changeListeningPortTo (int port)
 Change the listening port. More...

int sendPacket (NpPacket *packet, Node *node)
 Send a packet to the specified node. More...

int sendPacket (ConnectionPacket *packet)
 Send packet to a random neighbor. More...

int broadcast (NpPacket *packet)
 Send packet to all neighbors. More...

int broadcast (NpPacket *packet, Node *node)
 Send packet to all neighbors but the one specified. More...

int close (Node *)
 Close link to the specified Node. More...

virtual void toStream (std::ostream &out)

Protected Methods

void listenImpl ()
 This function is meant to be started as a pthread. More...

void peerConnectImpl ()
 This function is meant to be started as a pthread. More...

void pollAllSocketsImpl (void)
 It will read packets coming in from the Net and then hand them off to the registered handler. More...

void add (ConnectionTableEntry *)
 Add a node to the connection table. More...

void remove (int connTableIndex)
 Remove an entry from the connection table. More...

ConnectionTableEntryaccept ()
 Accept handles incoming connect requests. More...

void closeLink (int connTableIndex)
 Close the link to neighbor. More...


Static Protected Methods

void * listen (void *)
 This function is meant to be started as a pthread. More...

void * peerConnect (void *)
 This function is meant to be started as a pthread. More...

void * pollAllSockets (void *)
 This function is meant to be started as a pthread. More...


Protected Attributes

Condition m_connectionClosed
 Used to signal when a connection is closed. More...

Mutex m_connectionTableLock
 a lock for the connection table. More...

vector< ConnectionTableEntry *> m_connectionTable
 an array of pointers to nodes we have connections to. More...

pthread_t m_listenThreadId
 The thread ID for the listen function. More...

pthread_t m_peerConnectThreadId
 The thread ID for the peerConnect function. More...

pthread_t m_receivePacketThreadId
Condition m_newConnection
 Used to signal when we have accepted a new connection. More...

Condition m_nodeAdded
 Used to signal when a new node is added. More...

fd_set m_socketSet
 the set of all current connections. More...

int m_highestFd
 on *UNIX systems, you must provide "select" with the highest descriptor of the three sets you hand in, plus one. This var keeps track of the highest-number descriptor. More...

PacketHandlerInterfacem_packetHandler
 Pointer to packet handler - assigned by the registerHandler function. More...

ListenerInterfacem_connectionListener
 Pointer to object that wants to be informed of node events. More...

string m_state
 state string. More...

TRanrotBGenerator m_randomNumberGenerator

Detailed Description

The LinkLayerInterface handles the set of all connections to neighbor nodes.

Each node must take care of itself, but the management of all of them falls to this class.

This class is based on the connection table. The connection table is maintained without any holes. This allows for faster searching and adding, but slower removal because it has to squeeze the array down when one member is removed. This way makes the programming with this array very easy.

I was thinking about using a Reactor design pattern in order to combine accepting connections and reading data. But this allows an attacker to hold up your data flow by spoofing connections. This is why I have separated the listening for new connections from the receiving of data on existing connections.

Definition at line 17 of file LinkLayerInterface.h.


Constructor & Destructor Documentation

LinkLayerInterface::LinkLayerInterface
 

Create the Link Layer.

This will start up the following threads:

1) listen: a thread that will listen for connections from other nodes on the default port defined in the Config singleton.

2) peerConnect: actively tries to connect to a random set of peers. The peer list comes from the catcher.

3) receivePacket: reads a packet from any of the connected sockets and passes it to the registered listener. This thread will automatically drop connections out of the connection table that have been severed.

Definition at line 40 of file LinkLayerInterface.cpp.

LinkLayerInterface::~LinkLayerInterface [virtual]
 

Destructor.

Destroys the mutexes and condition variables and the threads.

Todo:
see inside

Definition at line 81 of file LinkLayerInterface.cpp.


Member Function Documentation

ConnectionTableEntry * LinkLayerInterface::accept [protected]
 

Accept handles incoming connect requests.

After a connection is accepted, is it treated exactly like all other connections.

LLI->listen must be called before this function is called.

Returns:
the Node accepted from if successful, NULL otherwise

Definition at line 391 of file LinkLayerInterface.cpp.

Referenced by listenImpl().

void LinkLayerInterface::add ConnectionTableEntry * entry [protected]
 

Add a node to the connection table.

Definition at line 606 of file LinkLayerInterface.cpp.

Referenced by accept(), and connect().

int LinkLayerInterface::broadcast NpPacket * packet,
Node * node
 

Send packet to all neighbors but the one specified.

Definition at line 929 of file LinkLayerInterface.cpp.

int LinkLayerInterface::broadcast NpPacket * packet
 

Send packet to all neighbors.

Definition at line 913 of file LinkLayerInterface.cpp.

Referenced by NetworkLayer::discover(), and NetworkLayer::ping().

void LinkLayerInterface::changeListeningPortTo int port
 

Change the listening port.

Definition at line 528 of file LinkLayerInterface.cpp.

int LinkLayerInterface::close Node * node
 

Close link to the specified Node.

Returns:
1 on success, 0 on failure.

Definition at line 870 of file LinkLayerInterface.cpp.

void LinkLayerInterface::closeLink int connectionTableIndex [protected]
 

Close the link to neighbor.

Definition at line 889 of file LinkLayerInterface.cpp.

Referenced by close(), and pollAllSocketsImpl().

int LinkLayerInterface::connect Node * node
 

Connects to the specified node.

searches through its connection table looking for holes. If there arent any, add this connection to the end of the list.

Returns:
true if connect was sucessful, false if there was an error.

Definition at line 448 of file LinkLayerInterface.cpp.

Referenced by peerConnectImpl().

Node * LinkLayerInterface::getConnection int index
 

Definition at line 793 of file LinkLayerInterface.cpp.

bool LinkLayerInterface::getConnection int index,
Node * retval
 

Get a copy of the node at the specified index in the connection table.

You must supply a pointer to an existing node which will be overwritten with the value of the node found at the index. This function will return false if the index is invalid, otherwise it will return true.

Definition at line 782 of file LinkLayerInterface.cpp.

Referenced by add(), ConnectionManagerCommand::getHtmlInterface(), VisualizerFrame::getInitialNeighborNodes(), pollAllSocketsImpl(), and sendPacket().

int LinkLayerInterface::getNumConnections
 

Returns the current number of open connections.

Definition at line 729 of file LinkLayerInterface.cpp.

Referenced by SummaryScreenCommand::getHtmlInterface(), ConnectionManagerCommand::getHtmlInterface(), VisualizerFrame::getInitialNeighborNodes(), NetworkLayer::handleConnectPacket(), listenImpl(), and peerConnectImpl().

Node * LinkLayerInterface::getRandomNeighbor Node * node
 

Return a random neighbor from our connection list, except the one that you pass in.

If we are not connected to any nodes, or only connected to one, this function will return NULL.

Definition at line 758 of file LinkLayerInterface.cpp.

Node * LinkLayerInterface::getRandomNeighbor
 

Return a random neighbor from our connection list.

A neighbor is a node we are currently connected to.

Definition at line 740 of file LinkLayerInterface.cpp.

Referenced by NetworkLayer::connectForward(), NetworkLayer::handleDiscoveryPacket(), and NetworkLayer::makeVc().

void LinkLayerInterface::handleEvent ObservableInterface * observable,
int event,
void * object
[virtual]
 

This is called by the Catcher object whenever it discovers a new Node.

This alerts the peerConnect thread that we have a new node. It is that thread that decides what to do with it.

Reimplemented from ListenerInterface.

Definition at line 903 of file LinkLayerInterface.cpp.

bool LinkLayerInterface::isConnected
 

Return true if there is a connection to any other node, false otherwise.

Definition at line 720 of file LinkLayerInterface.cpp.

Referenced by ServiceTable::connectService(), and NetworkLayer::makeVc().

bool LinkLayerInterface::isConnectedTo IpAddress ipAddr
 

Check connection table to see if we are connected to specified node.

Parameters:
node   The IP addr of the node we are looking for
Returns:
true if there is a connection to the IP address specified, false otherwise.

Definition at line 702 of file LinkLayerInterface.cpp.

Referenced by accept(), and connect().

void * LinkLayerInterface::listen void * arg [static, protected]
 

This function is meant to be started as a pthread.

This is a wrapper around listenImpl().

Parameters:
arg   A pointer to a LinkLayerInterface object

Definition at line 326 of file LinkLayerInterface.cpp.

void LinkLayerInterface::listenImpl [protected]
 

This function is meant to be started as a pthread.

This makes the localhost listen for incoming connections on its listeing port defined in selfNode.

Parameters:
arg   A pointer to a LinkLayerInterface object

Definition at line 340 of file LinkLayerInterface.cpp.

Referenced by listen().

void * LinkLayerInterface::peerConnect void * arg [static, protected]
 

This function is meant to be started as a pthread.

It will actively start up connections until it has run out of nodes to connect to or has reached MIN_CONNECTIONS as defined in the config file.

Parameters:
arg   A pointer to a LinkLayerInterface object

Definition at line 252 of file LinkLayerInterface.cpp.

void LinkLayerInterface::peerConnectImpl [protected]
 

This function is meant to be started as a pthread.

It will actively start up connections until it has run out of nodes to connect to or has reached MIN_CONNECTIONS as defined in the config file.

Parameters:
arg   A pointer to a LinkLayerInterface object

Definition at line 268 of file LinkLayerInterface.cpp.

Referenced by peerConnect().

void * LinkLayerInterface::pollAllSockets void * arg [static, protected]
 

This function is meant to be started as a pthread.

It is a wrapper for pollAllThreadsImpl.

Definition at line 127 of file LinkLayerInterface.cpp.

void LinkLayerInterface::pollAllSocketsImpl void [protected]
 

It will read packets coming in from the Net and then hand them off to the registered handler.

Poll all open sockets, read a packet if any have arrived. If a packet is read, this function will return immediately. If no packets are available, this function will block until there is data available. It will also close any sockets that have been disconnected by their peer. The parameters in this function are the return values.

Parameters:
fromNode   The Node we got this packet from
packet   The packet we read from that node
Returns:
An error code (0 if something failed, 1 if everything was OK)

Definition at line 150 of file LinkLayerInterface.cpp.

Referenced by pollAllSockets().

void LinkLayerInterface::registerPacketHandler PacketHandlerInterface * packetHandler
 

Registers an object to be called whenever a packet is received.

Only one packet handler may be registered.

Definition at line 562 of file LinkLayerInterface.cpp.

Referenced by NetworkLayer::NetworkLayer().

void LinkLayerInterface::remove int connectionTableIndex [protected]
 

Remove an entry from the connection table.

This automatically closes the connection if it open.

Parameters:
connTableIndex   The array index of the node you want to remove.

Definition at line 631 of file LinkLayerInterface.cpp.

Referenced by closeLink().

int LinkLayerInterface::sendPacket ConnectionPacket * packet
 

Send packet to a random neighbor.

Used for creation of virtual circuits.

Definition at line 808 of file LinkLayerInterface.cpp.

int LinkLayerInterface::sendPacket NpPacket * packet,
Node * node
 

Send a packet to the specified node.

Parameters:
packet   The packet to send.
node   The node to send to.
Returns:
The number of bytes sent, <0 on error.

Definition at line 839 of file LinkLayerInterface.cpp.

Referenced by NetworkLayer::connectForward(), NetworkLayer::connectTerminate(), NetworkLayer::forwardPacket(), NetworkLayer::handleDiscoveryPacket(), NetworkLayer::handleEstablishedPacket(), NetworkLayer::handleImHerePacket(), NetworkLayer::handlePingPacket(), NetworkLayer::makeVc(), and NetworkLayer::send().

void LinkLayerInterface::toStream std::ostream & out [virtual]
 

Reimplemented from BasicObject.

Definition at line 945 of file LinkLayerInterface.cpp.


Member Data Documentation

Condition LinkLayerInterface::m_connectionClosed [protected]
 

Used to signal when a connection is closed.

Definition at line 107 of file LinkLayerInterface.h.

ListenerInterface* LinkLayerInterface::m_connectionListener [protected]
 

Pointer to object that wants to be informed of node events.

Definition at line 142 of file LinkLayerInterface.h.

vector<ConnectionTableEntry*> LinkLayerInterface::m_connectionTable [protected]
 

an array of pointers to nodes we have connections to.

Definition at line 113 of file LinkLayerInterface.h.

Mutex LinkLayerInterface::m_connectionTableLock [protected]
 

a lock for the connection table.

Definition at line 110 of file LinkLayerInterface.h.

int LinkLayerInterface::m_highestFd [protected]
 

on *UNIX systems, you must provide "select" with the highest descriptor of the three sets you hand in, plus one. This var keeps track of the highest-number descriptor.

Definition at line 136 of file LinkLayerInterface.h.

pthread_t LinkLayerInterface::m_listenThreadId [protected]
 

The thread ID for the listen function.

Definition at line 116 of file LinkLayerInterface.h.

Condition LinkLayerInterface::m_newConnection [protected]
 

Used to signal when we have accepted a new connection.

Definition at line 125 of file LinkLayerInterface.h.

Condition LinkLayerInterface::m_nodeAdded [protected]
 

Used to signal when a new node is added.

Definition at line 128 of file LinkLayerInterface.h.

PacketHandlerInterface* LinkLayerInterface::m_packetHandler [protected]
 

Pointer to packet handler - assigned by the registerHandler function.

Definition at line 139 of file LinkLayerInterface.h.

pthread_t LinkLayerInterface::m_peerConnectThreadId [protected]
 

The thread ID for the peerConnect function.

Definition at line 119 of file LinkLayerInterface.h.

TRanrotBGenerator LinkLayerInterface::m_randomNumberGenerator [protected]
 

Definition at line 147 of file LinkLayerInterface.h.

pthread_t LinkLayerInterface::m_receivePacketThreadId [protected]
 

Definition at line 122 of file LinkLayerInterface.h.

fd_set LinkLayerInterface::m_socketSet [protected]
 

the set of all current connections.

Definition at line 131 of file LinkLayerInterface.h.

string LinkLayerInterface::m_state [protected]
 

state string.

Definition at line 145 of file LinkLayerInterface.h.


The documentation for this class was generated from the following files:
Generated at Thu Jul 11 13:31:57 2002 for Peekabooty by doxygen1.2.9 written by Dimitri van Heesch, © 1997-2001