libglass::Protocol Class Reference

#include <protocol.h>

Inheritance diagram for libglass::Protocol:

libglass::TCP libglass::UDP

List of all members.

Public Types

enum  connectionType { None, Server, Client }

Public Member Functions

 Protocol ()
virtual ~Protocol ()
connectionType getConnectionType (void) const
int listen (unsigned int port=0)
nodeFD accept (void)
nodeFD connect (const char *host, unsigned int port=0)
void disconnect (nodeFD node=-1)
bool send (Packet &p)
int recv (Packet &p)
virtual nodeId getUniqueId (void)=0

Protected Member Functions

void setOwner (const Glass *g)
nodeFD IdToFD (nodeId id)
nodeId FDToId (nodeFD fd)
nodeId registerNode (nodeFD fd)
bool unregisterNode (nodeId id)
bool unregisterNode (nodeFD fd)
virtual int _listen (unsigned int port)=0
virtual nodeFD _accept (void)=0
virtual nodeFD _connect (const char *host, unsigned int port)=0
virtual void _disconnect (nodeFD node)=0
virtual bool _send (Packet &p)=0
virtual int _recv (Packet &p)=0

Protected Attributes

connectionType type

Friends

class Glass


Detailed Description

The Protocol abstract base class. All protocols should be derived from this class.

Definition at line 35 of file protocol.h.


Member Enumeration Documentation

Possible types.

Enumerator:
None 
Server 
Client 

Definition at line 40 of file protocol.h.


Constructor & Destructor Documentation

libglass::Protocol::Protocol (  ) 

Constructor.

virtual libglass::Protocol::~Protocol (  )  [virtual]

Virtual destructor. Responsible for closing all connections.


Member Function Documentation

virtual nodeFD libglass::Protocol::_accept ( void   )  [protected, pure virtual]

This is the derived class implementation of accept().

See also:
nodeFD accept(void)

Implemented in libglass::TCP, and libglass::UDP.

virtual nodeFD libglass::Protocol::_connect ( const char *  host,
unsigned int  port 
) [protected, pure virtual]

This is the derived class implementation of connect().

See also:
nodeFD connect(const char *host, unsigned int port);

Implemented in libglass::TCP, and libglass::UDP.

virtual void libglass::Protocol::_disconnect ( nodeFD  node  )  [protected, pure virtual]

This is the derived class implementation of disconnect().

See also:
void disconnect(nodeFD node)

Implemented in libglass::TCP, and libglass::UDP.

virtual int libglass::Protocol::_listen ( unsigned int  port  )  [protected, pure virtual]

This is the derived class implementation of listen().

See also:
int listen(unsigned int port)

Implemented in libglass::TCP, and libglass::UDP.

virtual int libglass::Protocol::_recv ( Packet p  )  [protected, pure virtual]

This is the derived class implementation of recv().

See also:
int recv(Packet &p)

Implemented in libglass::TCP, and libglass::UDP.

virtual bool libglass::Protocol::_send ( Packet p  )  [protected, pure virtual]

This is the derived class implementation of send().

See also:
bool _send(Packet &p)

Implemented in libglass::TCP, and libglass::UDP.

nodeFD libglass::Protocol::accept ( void   ) 

Waits for a connection.

When the connection is successful, you must register the new FD by calling registerNode().

See also:
listen(), connect().
Return values:
-1 if protocol type is not server.
-2 if could not open connection.
>=0 otherwise, a glass node file descriptor to the node connected.

nodeFD libglass::Protocol::connect ( const char *  host,
unsigned int  port = 0 
)

Connects to a given host and port.

Parameters:
host The host to contact.
port The port to connect.
See also:
listen(), accept().
Return values:
-1 Already initialized or NULL argument.
-2 Network error.
>=0 A glass node file descriptor to the node.

void libglass::Protocol::disconnect ( nodeFD  node = -1  ) 

Closes the connection to a given node. Derived classes may choose exactly what to do.

Parameters:
node The glass node descriptor

nodeId libglass::Protocol::FDToId ( nodeFD  fd  )  [protected]

Converts a nodeFD to a nodeId.

Wrapper to the real call, which is a member of Glass().

Parameters:
fd The node fd.
Returns:
The associated nodeId.
See also:
Glass::FDToId().

connectionType libglass::Protocol::getConnectionType ( void   )  const

Gets connection type.

Returns:
The connection type.

virtual nodeId libglass::Protocol::getUniqueId ( void   )  [pure virtual]

Returns:

Implemented in libglass::TCP.

nodeFD libglass::Protocol::IdToFD ( nodeId  id  )  [protected]

Converts a nodeId to a nodeFD.

Wrapper to the real call, which is a member of Glass().

Parameters:
id The node id.
Returns:
The associated nodeFD.
See also:
Glass::IdToFD().

int libglass::Protocol::listen ( unsigned int  port = 0  ) 

Opens a port for listening to connections.

Must not block. If you need to block, do it in accept().

Parameters:
port The port to listen to.
See also:
accept(), connect().
Return values:
0 Ok.
-1 Already initialized.
-2 Network error.

int libglass::Protocol::recv ( Packet p  ) 

Receives a message.

This function gets the first packet in queue, and fills the Packet structure with the data.

Parameters:
p The packet to store the message.
See also:
send().
Return values:
3 Invalid packet format.
2 Timeout (may not be implemented by the derived class). This is not an error, and ought to be handled properly by the caller.
1 Connection closed.
Returns:
0 OK.
Return values:
-1 Protocol not initialized, or error reading.
-2 Invalid "from" descriptor/unregistered node

nodeId libglass::Protocol::registerNode ( nodeFD  fd  )  [protected]

Registers a node, by getting its descriptor.

Wrapper to the real call, which is a member of Glass().

Parameters:
fd A valid node descriptor.
Returns:
A new node Id, or nobodyId if an error occurred.
See also:
Glass::unregisterNode().

bool libglass::Protocol::send ( Packet p  ) 

Sends a message.

This function must be reentrant, that is, it may be called simultaneously by two or more threads. It's up to the implementation to make it truly concurrent (the preferred alternative) or use some kind of locking system, such as a mutex.

Parameters:
p The packet to send.
See also:
recv().
Return values:
true Ok.
false Protocol not initialized or transmission error.

void libglass::Protocol::setOwner ( const Glass g  )  [protected]

Sets our owner.

Called by Glass(). The reason this is not passed as argument to the constructor is that the Protocol is not constructed by the Glass class.

Parameters:
g The owner.

bool libglass::Protocol::unregisterNode ( nodeFD  fd  )  [protected]

bool libglass::Protocol::unregisterNode ( nodeId  id  )  [protected]

Unregisters a id/fd association.

Wrapper to the real call, which is a member of Glass().

Parameters:
id The node id.
Return values:
true This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
Wrapper to the real call, which is a member of Glass().

Parameters:
id The node id.
Return values:
true 


Friends And Related Function Documentation

friend class Glass [friend]

Definition at line 48 of file protocol.h.


Member Data Documentation

This instance type.

Definition at line 46 of file protocol.h.


The documentation for this class was generated from the following file:

Generated on Fri May 28 13:19:01 2010 for libGlass by  doxygen 1.5.8