karoo::datagram_server Class Reference

This class allows you to receive datagrams. More...

#include <datagram.h>

Inheritance diagram for karoo::datagram_server:

karoo::referable

List of all members.

Public Member Functions

 datagram_server (int port, size_t message_size, exeque *output_queue, exeque *run_queue, datagram_message_factory *factory)
void setLogger (const log &logger)
 Set the logger object.
datagram_status create ()
 Create the server and bind the socket.
void run ()
 this implements the "receive loop".
void start ()
 Start the server's receive loop.
void stop ()
 Stop the server's receive loop.
datagram_status getStatus () const
 Get the status of the server.

Protected Member Functions

void close ()
 Close the datagram socket.
datagram_status read (datagram_message *message)
 read one datagram from the socket.

Protected Attributes

int port
 the listening port
struct sockaddr_in addr
 the address structure.
int sock
 the socket file handle
datagram_status status
 the current sttaus of the server, should be DATAGRAM_OK if all is working normally.
size_t message_size
 the number of bytes to pre-allocat to buffer an incoming datagram
exequeoutput_queue
 the queue to use for dispatching datagram messages that have been received.
exequerun_queue
 the queue to use for runnning this server.
datagram_message_factoryfactory
 this factory is used for creating datagrams suitable for buffering incoming messages, and then handling them.
karoo_mutex running_sem
 used to keep accesses to the running varaible thread-safe
bool running
 true while the server is still running.
unsigned fail_count
 counts the number of consecutive failures in the receive loop
log logger
 This must be set up in order for errors, debugs, etc to to displayed or logged somewhere.


Detailed Description

This class allows you to receive datagrams.

Note that a datagram_server does not have provision for sending a datagram.

See also:
datagram_client.

Member Function Documentation

void karoo::datagram_server::close (  )  [protected]

Close the datagram socket.

This method is only ever called from in the receive loop and also in the destructer.

datagram_status karoo::datagram_server::create (  ) 

Create the server and bind the socket.

Returns:
the status of the server after attempting to create it. If DATAGRAM_OK is returned, then it is created normally. Note that if this method is not called before the server is started by calling start(), then inside the receive loop, it will output a warning to the logger, and then call create() anyway.

datagram_status karoo::datagram_server::getStatus (  )  const [inline]

Get the status of the server.

This should be DATAGRAM_OK if all is well.

References status.

datagram_status karoo::datagram_server::read ( datagram_message message  )  [protected]

read one datagram from the socket.

This is a blocking call. It should only ever be called from inside the receive loop.

Parameters:
message a pointer to the new datagram_message object that will buffer and then handle the message.
Returns:
the status of the server after the read.

void karoo::datagram_server::run (  ) 

this implements the "receive loop".

if the status of the server is DATAGRAM_OK, then it will get a new datagram_message from the factory, and call read(). If the status remains DATAGRAM_OK, then it will set up the message to destroy itself after being run (datagram_message inherits from pebble), and then will add the datagram_message to the output_queue. When the datagram_message::run() is called in the output_queue, it will presumably do something to handle the message (but thats up to whoever implemented the datagram_message_factory. In the case of a rock_datagram_factory and rock_datagram_message, it calls the parent's rock::handle() method.

void karoo::datagram_server::setLogger ( const log &  logger  ) 

Set the logger object.

Parameters:
logger the log object for sending warnings, errors etc... to


Member Data Documentation

struct sockaddr_in karoo::datagram_server::addr [read, protected]

the address structure.

This is initialised to allow the server to receive from any internet address.

the queue to use for dispatching datagram messages that have been received.

This queue typically will only be lightly utilised by the server unless messages are coming in very quickly. Note that rock_datagram_message::run() calls rock::handle(this), which in turn calls all the handlers that therock is configured with, until one returns true to indicate that it has handled it. This may be time consuming, depending on the design of your handlers. ... but it is reasonably safe to assume that you could run other tasks in the same queue.

the queue to use for runnning this server.

This is used for the (blocking) receive loop, so it will be 100% utilised. Any task you add to this queue will only be executed after a datagram is received, which (if not many messages are being received) could be after a long delay. It would make sense though to perhaps put some statistics gathering, or housekeeping task into this queue.

true while the server is still running.

Once set to false, the server receive loop will exit.


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

Generated on Tue Feb 16 15:04:30 2010 for Karoo by  doxygen 1.5.8