cleaning up ipcCommandModule

This commit is contained in:
darin%netscape.com 2002-11-12 01:13:51 +00:00
parent 484631ec73
commit 92160369ad

View File

@ -46,193 +46,198 @@
#include "ipcd.h" #include "ipcd.h"
#include "ipcm.h" #include "ipcm.h"
typedef void (* ipcmMsgHandler)(ipcClient *, const ipcMessage *); struct ipcCommandModule
//
// helpers
//
static char **
ipcm_BuildStringArray(const ipcStringNode *nodes)
{ {
size_t count = 0; typedef void (* MsgHandler)(ipcClient *, const ipcMessage *);
const ipcStringNode *node; //
// helpers
for (node = nodes; node; node = node->mNext) //
count++;
char **strs = (char **) calloc(count + 1, sizeof(char *)); static char **
if (!strs) BuildStringArray(const ipcStringNode *nodes)
return NULL; {
size_t count = 0;
count = 0; const ipcStringNode *node;
for (node = nodes; node; node = node->mNext, ++count)
strs[count] = (char *) node->Value(); for (node = nodes; node; node = node->mNext)
count++;
return strs; char **strs = (char **) calloc(count + 1, sizeof(char *));
} if (!strs)
return NULL;
static nsID ** count = 0;
ipcm_BuildIDArray(const ipcIDNode *nodes) for (node = nodes; node; node = node->mNext, ++count)
{ strs[count] = (char *) node->Value();
size_t count = 0;
const ipcIDNode *node; return strs;
for (node = nodes; node; node = node->mNext)
count++;
nsID **ids = (nsID **) calloc(count + 1, sizeof(nsID *));
if (!ids)
return NULL;
count = 0;
for (node = nodes; node; node = node->mNext, ++count)
ids[count] = (nsID *) &node->Value();
return ids;
}
//
// message handlers
//
static void
ipcm_OnPing(ipcClient *client, const ipcMessage *rawMsg)
{
LOG(("got PING\n"));
IPC_SendMsg(client, new ipcmMessagePing());
}
static void
ipcm_OnClientHello(ipcClient *client, const ipcMessage *rawMsg)
{
LOG(("got CLIENT_HELLO\n"));
ipcMessageCast<ipcmMessageClientHello> msg(rawMsg);
const char *name = msg->PrimaryName();
if (name)
client->AddName(name);
IPC_SendMsg(client, new ipcmMessageClientID(client->ID()));
}
static void
ipcm_OnClientAddName(ipcClient *client, const ipcMessage *rawMsg)
{
LOG(("got CLIENT_ADD_NAME\n"));
ipcMessageCast<ipcmMessageClientAddName> msg(rawMsg);
const char *name = msg->Name();
if (name)
client->AddName(name);
}
static void
ipcm_OnClientDelName(ipcClient *client, const ipcMessage *rawMsg)
{
LOG(("got CLIENT_DEL_NAME\n"));
ipcMessageCast<ipcmMessageClientDelName> msg(rawMsg);
const char *name = msg->Name();
if (name)
client->DelName(name);
}
static void
ipcm_OnClientAddTarget(ipcClient *client, const ipcMessage *rawMsg)
{
LOG(("got CLIENT_ADD_TARGET\n"));
ipcMessageCast<ipcmMessageClientAddTarget> msg(rawMsg);
client->AddTarget(msg->Target());
}
static void
ipcm_OnClientDelTarget(ipcClient *client, const ipcMessage *rawMsg)
{
LOG(("got CLIENT_DEL_TARGET\n"));
ipcMessageCast<ipcmMessageClientDelTarget> msg(rawMsg);
client->DelTarget(msg->Target());
}
static void
ipcm_OnQueryClientByName(ipcClient *client, const ipcMessage *rawMsg)
{
LOG(("got QUERY_CLIENT_BY_NAME\n"));
ipcMessageCast<ipcmMessageQueryClientByName> msg(rawMsg);
ipcClient *result = IPC_GetClientByName(msg->Name());
if (result) {
LOG((" client exists w/ ID = %u\n", result->ID()));
IPC_SendMsg(client, new ipcmMessageClientID(result->ID()));
} }
else {
LOG((" client does not exist\n")); static nsID **
IPC_SendMsg(client, new ipcmMessageError(IPCM_ERROR_CLIENT_NOT_FOUND)); BuildIDArray(const ipcIDNode *nodes)
{
size_t count = 0;
const ipcIDNode *node;
for (node = nodes; node; node = node->mNext)
count++;
nsID **ids = (nsID **) calloc(count + 1, sizeof(nsID *));
if (!ids)
return NULL;
count = 0;
for (node = nodes; node; node = node->mNext, ++count)
ids[count] = (nsID *) &node->Value();
return ids;
} }
}
static void //
ipcm_OnQueryClientInfo(ipcClient *client, const ipcMessage *rawMsg) // message handlers
{ //
LOG(("got QUERY_CLIENT_INFO\n"));
ipcMessageCast<ipcmMessageQueryClientInfo> msg(rawMsg); static void
ipcClient *result = IPC_GetClientByID(msg->ClientID()); OnPing(ipcClient *client, const ipcMessage *rawMsg)
if (result) { {
char **names = ipcm_BuildStringArray(result->Names()); LOG(("got PING\n"));
nsID **targets = ipcm_BuildIDArray(result->Targets());
IPC_SendMsg(client, new ipcmMessageClientInfo(result->ID(), IPC_SendMsg(client, new ipcmMessagePing());
(const char **) names,
(const nsID **)targets));
free(names);
free(targets);
} }
else {
LOG((" client does not exist\n")); static void
IPC_SendMsg(client, new ipcmMessageError(IPCM_ERROR_CLIENT_NOT_FOUND)); OnClientHello(ipcClient *client, const ipcMessage *rawMsg)
{
LOG(("got CLIENT_HELLO\n"));
ipcMessageCast<ipcmMessageClientHello> msg(rawMsg);
const char *name = msg->PrimaryName();
if (name)
client->AddName(name);
IPC_SendMsg(client, new ipcmMessageClientID(client->ID()));
} }
}
static void static void
ipcm_OnForward(ipcClient *client, const ipcMessage *rawMsg) OnClientAddName(ipcClient *client, const ipcMessage *rawMsg)
{ {
LOG(("got FORWARD\n")); LOG(("got CLIENT_ADD_NAME\n"));
ipcMessageCast<ipcmMessageForward> msg(rawMsg); ipcMessageCast<ipcmMessageClientAddName> msg(rawMsg);
const char *name = msg->Name();
ipcClient *dest = IPC_GetClientByID(msg->DestClientID()); if (name)
if (!dest) { client->AddName(name);
LOG((" destination client not found!\n"));
return;
} }
ipcMessage *newMsg = new ipcMessage(msg->InnerTarget(),
msg->InnerData(), static void
msg->InnerDataLen()); OnClientDelName(ipcClient *client, const ipcMessage *rawMsg)
IPC_SendMsg(dest, newMsg); {
} LOG(("got CLIENT_DEL_NAME\n"));
ipcMessageCast<ipcmMessageClientDelName> msg(rawMsg);
const char *name = msg->Name();
if (name)
client->DelName(name);
}
static void
OnClientAddTarget(ipcClient *client, const ipcMessage *rawMsg)
{
LOG(("got CLIENT_ADD_TARGET\n"));
ipcMessageCast<ipcmMessageClientAddTarget> msg(rawMsg);
client->AddTarget(msg->Target());
}
static void
OnClientDelTarget(ipcClient *client, const ipcMessage *rawMsg)
{
LOG(("got CLIENT_DEL_TARGET\n"));
ipcMessageCast<ipcmMessageClientDelTarget> msg(rawMsg);
client->DelTarget(msg->Target());
}
static void
OnQueryClientByName(ipcClient *client, const ipcMessage *rawMsg)
{
LOG(("got QUERY_CLIENT_BY_NAME\n"));
ipcMessageCast<ipcmMessageQueryClientByName> msg(rawMsg);
ipcClient *result = IPC_GetClientByName(msg->Name());
if (result) {
LOG((" client exists w/ ID = %u\n", result->ID()));
IPC_SendMsg(client, new ipcmMessageClientID(result->ID()));
}
else {
LOG((" client does not exist\n"));
IPC_SendMsg(client, new ipcmMessageError(IPCM_ERROR_CLIENT_NOT_FOUND));
}
}
static void
OnQueryClientInfo(ipcClient *client, const ipcMessage *rawMsg)
{
LOG(("got QUERY_CLIENT_INFO\n"));
ipcMessageCast<ipcmMessageQueryClientInfo> msg(rawMsg);
ipcClient *result = IPC_GetClientByID(msg->ClientID());
if (result) {
char **names = BuildStringArray(result->Names());
nsID **targets = BuildIDArray(result->Targets());
IPC_SendMsg(client, new ipcmMessageClientInfo(result->ID(),
(const char **) names,
(const nsID **) targets));
free(names);
free(targets);
}
else {
LOG((" client does not exist\n"));
IPC_SendMsg(client, new ipcmMessageError(IPCM_ERROR_CLIENT_NOT_FOUND));
}
}
static void
OnForward(ipcClient *client, const ipcMessage *rawMsg)
{
LOG(("got FORWARD\n"));
ipcMessageCast<ipcmMessageForward> msg(rawMsg);
ipcClient *dest = IPC_GetClientByID(msg->DestClientID());
if (!dest) {
LOG((" destination client not found!\n"));
return;
}
ipcMessage *newMsg = new ipcMessage(msg->InnerTarget(),
msg->InnerData(),
msg->InnerDataLen());
IPC_SendMsg(dest, newMsg);
}
};
void void
IPCM_HandleMsg(ipcClient *client, const ipcMessage *rawMsg) IPCM_HandleMsg(ipcClient *client, const ipcMessage *rawMsg)
{ {
static ipcmMsgHandler handlers[] = static ipcCommandModule::MsgHandler handlers[] =
{ {
ipcm_OnPing, ipcCommandModule::OnPing,
NULL, // ERROR NULL, // ERROR
ipcm_OnClientHello, ipcCommandModule::OnClientHello,
NULL, // CLIENT_ID NULL, // CLIENT_ID
NULL, // CLIENT_INFO NULL, // CLIENT_INFO
ipcm_OnClientAddName, ipcCommandModule::OnClientAddName,
ipcm_OnClientDelName, ipcCommandModule::OnClientDelName,
ipcm_OnClientAddTarget, ipcCommandModule::OnClientAddTarget,
ipcm_OnClientDelTarget, ipcCommandModule::OnClientDelTarget,
ipcm_OnQueryClientByName, ipcCommandModule::OnQueryClientByName,
ipcm_OnQueryClientInfo, ipcCommandModule::OnQueryClientInfo,
ipcm_OnForward, ipcCommandModule::OnForward,
}; };
int type = IPCM_GetMsgType(rawMsg); int type = IPCM_GetMsgType(rawMsg);
@ -240,7 +245,7 @@ IPCM_HandleMsg(ipcClient *client, const ipcMessage *rawMsg)
if (type < IPCM_MSG_TYPE_UNKNOWN) { if (type < IPCM_MSG_TYPE_UNKNOWN) {
if (handlers[type]) { if (handlers[type]) {
ipcmMsgHandler handler = handlers[type]; ipcCommandModule::MsgHandler handler = handlers[type];
handler(client, rawMsg); handler(client, rawMsg);
} }
} }