> Hi.
>
> A while back you recomended using a hash table w/function pointers
> to decide which function a server runs in response to a particular command.
> I am adding that to my server, and before I write one, was wondering if there
> is already something like that in your CDEV server? Or do you just use the
> same hash setup Jie gave me to use to hash the strings themselves. Thanks!
>
> --David
There's really no special trick to building this thing...
Bruce and I used the following approach (generally speaking), to
do the model server command parsing...
// *******************************************************************
// * Include StringHash.h you'll need it
// *******************************************************************
#include <StringHash.h>
// *******************************************************************
// * Create a typedef that all of the functions that you'll be
// * calling must conform to.
// *******************************************************************
typedef void (*FunctionPtrType)(cdevData * in, cdevData *&out);
// *******************************************************************
// * Create the functions you'll be calling
// *******************************************************************
void getThisFunction ( cdevData *in, cdevData *&out)
{
if(out==NULL) out=new cdevData;
else out->remove();
out->insert("value", "this");
}
void getThatFunction ( cdevData *in, cdevData *&out)
{
if(out==NULL) out=new cdevData;
else out->remove();
out->insert("value", "that");
}
void badRequest ( cdevData *in, cdevData &*out)
{
if(out==NULL) out=new cdevData;
else out->remove();
out->insert("completionCode", CDEV_ERROR);
}
// *******************************************************************
// * Add a commandHash hash table to your server structure.
// *******************************************************************
class YourServerClass : public cdevServer
{
protected:
StringHash commandHash;
public:
// **********************************************************
// * Populate the hash table in the contructor...
// **********************************************************
YourServerClass ( void )
{
commandHash.insert("get this", getThisFunction);
commandHash.insert("get that", getThatFunction);
}
// **********************************************************
// * In the processMessages method extract the function
// * pointer from the hash table and execute it.
// **********************************************************
void processMessages ( void )
{
cdevMessage * msg;
while(dequeue(msg))
{
cdevData * data = msg->getData();
FunctionPtrType ptr;
// ******************************************
// * Copy the method associated with the
// * command string to the function ptr...
// * If it's not there, use the badRequest
// * function ptr.
// ******************************************
ptr = commandHash.find(msg->getMessage());
if(!ptr) ptr = badRequest;
// ******************************************
// * Call the function using the cdevData
// * object provided with the message.
// ******************************************
(*ptr)(data, data);
// ******************************************
// * Put the data back into the outbound
// * message if necessary.
// ******************************************
if(data!=msg->getData()) msg->setData(data);
enqueue(msg);
delete msg;
}
}
};
Let me know if you have any problem with this approach...
Walt
--
=============================================================
Walt Akers Voice: (757)269-7669 E-Mail: akers@cebaf.gov
Thomas Jefferson National Accelerator Facility
12000 Jefferson Avenue, MS 16A
Newport News, Va 23606
=============================================================