MMSThreadServer Class Reference

This class is the base class for all threads which works as a server. More...

#include <mmsthreadserver.h>

Inheritance diagram for MMSThreadServer:

List of all members.


Public Member Functions

 MMSThreadServer (int queue_size=1000, string identity="MMSThreadServer", bool blocking=true)
 constructor
 ~MMSThreadServer ()
 destructor
bool start ()
 Start the server thread.
virtual void processData (void *in_data, int in_data_len, void **out_data, int *out_data_len)
 Process a new event from the client.
bool trigger (void *in_data, int in_data_len, void **out_data=NULL, int *out_data_len=NULL)
 Trigger a new event to the server.

Public Attributes

sigc::signal< void, void
*, int, void **, int * > 
onProcessData
 Set one or more callbacks for the onProcessData event.

Private Member Functions

void threadMain ()
 server thread

Private Attributes

pthread_t server_tid
 id of the server thread
MMSTS_QUEUE_ITEM ** queue
 request queue (ring buffer)
int queue_size
 number of items in the queue
int queue_rp
 current item in the queue (read pointer)
int queue_wp
 first free item in the queue (write pointer)
bool buffer_full
 mark the ring buffer as full
pthread_cond_t cond
 variable for conditional handling (server side)
pthread_mutex_t mutex
 mutex for conditional handling (server side)
bool blocking
 in non-blocking mode the caller of trigger() will get control directly after triggering and do not wait until server has finished processData()

Classes

struct  MMSTS_QUEUE_ITEM
 describes one item/request in the queue More...

Detailed Description

This class is the base class for all threads which works as a server.

This class includes the base functionality e.g. the handshake between server and client threads. You can use the onProcessData() callback if you do not want to derive your own class from MMSThreadServer.

Author:
Jens Schneider

Definition at line 46 of file mmsthreadserver.h.


Constructor & Destructor Documentation

MMSThreadServer::MMSThreadServer ( int  queue_size = 1000,
string  identity = "MMSThreadServer",
bool  blocking = true 
)

constructor

Parameters:
queue_size maximum items in the queue
identity identity of the server thread used for logging etc.
blocking blocking or non-blocking mode, see trigger()
Note:
In non-blocking mode the caller of trigger() will get control directly after triggering and do not wait until server has finished processData() of the previous trigger() call.

Definition at line 38 of file mmsthreadserver.cpp.

MMSThreadServer::~MMSThreadServer (  ) 

destructor

Definition at line 62 of file mmsthreadserver.cpp.


Member Function Documentation

void MMSThreadServer::threadMain (  )  [private, virtual]

server thread

Implements MMSThread.

Definition at line 77 of file mmsthreadserver.cpp.

bool MMSThreadServer::start ( void   )  [virtual]

Start the server thread.

This method starts the server thread. This has to be done before the first trigger() call.

Returns:
true if successful
Note:
The method returns false, if the server is already started.

Reimplemented from MMSThread.

Definition at line 69 of file mmsthreadserver.cpp.

void MMSThreadServer::processData ( void *  in_data,
int  in_data_len,
void **  out_data,
int *  out_data_len 
) [virtual]

Process a new event from the client.

Reimplemented in MMSFBBackEndInterface.

Definition at line 127 of file mmsthreadserver.cpp.

bool MMSThreadServer::trigger ( void *  in_data,
int  in_data_len,
void **  out_data = NULL,
int *  out_data_len = NULL 
)

Trigger a new event to the server.

This method sends data (in_data) from the caller (client) thread to the server thread. If MMSThreadServer runs in blocking mode (see constructor), the caller of trigger() will be blocked and is waiting for the answer from the server. If MMSThreadServer runs in non-blocking mode, the caller of trigger() will get control immediately and do not wait until server has finished processData() of the previous trigger() call.

Parameters:
in_data pointer to data to put to the server
in_data_len length of in_data
out_data address of a pointer to receive data from the server
out_data_len address of a integer to get the length of out_data
Returns:
true if successful
Note:
If server's queue is full, the caller is blocked until enough space in the queue.

The handling of out_data and out_data_len is dependend on the implementation of the processData() method which is done in classes derived from MMSThreadServer.

If MMSThreadServer runs in non-blocking mode, out_data and out_data_len are not supported.

Definition at line 131 of file mmsthreadserver.cpp.


Member Data Documentation

pthread_t MMSThreadServer::server_tid [private]

id of the server thread

Definition at line 49 of file mmsthreadserver.h.

request queue (ring buffer)

Definition at line 68 of file mmsthreadserver.h.

number of items in the queue

Definition at line 71 of file mmsthreadserver.h.

current item in the queue (read pointer)

Definition at line 74 of file mmsthreadserver.h.

first free item in the queue (write pointer)

Definition at line 77 of file mmsthreadserver.h.

mark the ring buffer as full

Definition at line 80 of file mmsthreadserver.h.

pthread_cond_t MMSThreadServer::cond [private]

variable for conditional handling (server side)

Definition at line 83 of file mmsthreadserver.h.

pthread_mutex_t MMSThreadServer::mutex [private]

mutex for conditional handling (server side)

Definition at line 86 of file mmsthreadserver.h.

bool MMSThreadServer::blocking [private]

in non-blocking mode the caller of trigger() will get control directly after triggering and do not wait until server has finished processData()

Definition at line 90 of file mmsthreadserver.h.

sigc::signal<void, void *, int, void **, int *> MMSThreadServer::onProcessData

Set one or more callbacks for the onProcessData event.

The connected callbacks will be called from MMSThreadServer::processData() and will be run within the context of server thread.

A callback method must be defined like this:

void myclass::mycallbackmethod(void *in_data, int in_data_len, void **out_data, int *out_data_len);

Parameters:
in_data pointer to data to put to the server
in_data_len length of in_data
out_data address of a pointer to receive data from the server
out_data_len address of a integer to get the length of out_data
To connect your callback to onProcessData do this:

sigc::connection connection; connection = mythreadserver->onProcessData.connect(sigc::mem_fun(myobject,&myclass::mycallbackmethod));

To disconnect your callback do this:

connection.disconnect();

Please note:

You HAVE TO disconnect myobject from onProcessData BEFORE myobject will be deleted!!! Else an abnormal program termination can occur. You HAVE TO call the disconnect() method of sigc::connection explicitly. The destructor will NOT do this!!!

Definition at line 178 of file mmsthreadserver.h.


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