Back to index

lightning-sunbird  0.9+nobinonly
ipcCommandModule.cpp
Go to the documentation of this file.
00001 /* ***** BEGIN LICENSE BLOCK *****
00002  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00003  *
00004  * The contents of this file are subject to the Mozilla Public License Version
00005  * 1.1 (the "License"); you may not use this file except in compliance with
00006  * the License. You may obtain a copy of the License at
00007  * http://www.mozilla.org/MPL/
00008  *
00009  * Software distributed under the License is distributed on an "AS IS" basis,
00010  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00011  * for the specific language governing rights and limitations under the
00012  * License.
00013  *
00014  * The Original Code is Mozilla IPC.
00015  *
00016  * The Initial Developer of the Original Code is
00017  * Netscape Communications Corporation.
00018  * Portions created by the Initial Developer are Copyright (C) 2002
00019  * the Initial Developer. All Rights Reserved.
00020  *
00021  * Contributor(s):
00022  *   Darin Fisher <darin@netscape.com>
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either the GNU General Public License Version 2 or later (the "GPL"), or
00026  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 #include <stdlib.h>
00039 #include <string.h>
00040 #include "ipcLog.h"
00041 #include "ipcCommandModule.h"
00042 #include "ipcModule.h"
00043 #include "ipcClient.h"
00044 #include "ipcMessage.h"
00045 #include "ipcMessageUtils.h"
00046 #include "ipcModuleReg.h"
00047 #include "ipcd.h"
00048 #include "ipcm.h"
00049 
00050 struct ipcCommandModule
00051 {
00052     typedef void (* MsgHandler)(ipcClient *, const ipcMessage *);
00053 
00054     //
00055     // helpers
00056     //
00057 
00058     static char **
00059     BuildStringArray(const ipcStringNode *nodes)
00060     {
00061         size_t count = 0;
00062 
00063         const ipcStringNode *node;
00064         
00065         for (node = nodes; node; node = node->mNext)
00066             count++;
00067 
00068         char **strs = (char **) malloc((count + 1) * sizeof(char *));
00069         if (!strs)
00070             return NULL;
00071 
00072         count = 0;
00073         for (node = nodes; node; node = node->mNext, ++count)
00074             strs[count] = (char *) node->Value();
00075         strs[count] = 0;
00076 
00077         return strs;
00078     }
00079 
00080     static nsID **
00081     BuildIDArray(const ipcIDNode *nodes)
00082     {
00083         size_t count = 0;
00084 
00085         const ipcIDNode *node;
00086         
00087         for (node = nodes; node; node = node->mNext)
00088             count++;
00089 
00090         nsID **ids = (nsID **) calloc(count + 1, sizeof(nsID *));
00091         if (!ids)
00092             return NULL;
00093 
00094         count = 0;
00095         for (node = nodes; node; node = node->mNext, ++count)
00096             ids[count] = (nsID *) &node->Value();
00097 
00098         return ids;
00099     }
00100 
00101     //
00102     // message handlers
00103     //
00104 
00105     static void
00106     OnPing(ipcClient *client, const ipcMessage *rawMsg)
00107     {
00108         LOG(("got PING\n"));
00109 
00110         IPC_SendMsg(client, new ipcmMessageResult(IPCM_GetRequestIndex(rawMsg), IPCM_OK));
00111     }
00112 
00113     static void
00114     OnClientHello(ipcClient *client, const ipcMessage *rawMsg)
00115     {
00116         LOG(("got CLIENT_HELLO\n"));
00117 
00118         IPC_SendMsg(client, new ipcmMessageClientID(IPCM_GetRequestIndex(rawMsg), client->ID()));
00119 
00120         //
00121         // NOTE: it would almost make sense for this notification to live
00122         // in the transport layer code.  however, clients expect to receive
00123         // a CLIENT_ID as the first message following a CLIENT_HELLO, so we
00124         // must not allow modules to see a client until after we have sent
00125         // the CLIENT_ID message.
00126         //
00127         IPC_NotifyClientUp(client);
00128     }
00129 
00130     static void
00131     OnClientAddName(ipcClient *client, const ipcMessage *rawMsg)
00132     {
00133         LOG(("got CLIENT_ADD_NAME\n"));
00134 
00135         ipcMessageCast<ipcmMessageClientAddName> msg(rawMsg);
00136         const char *name = msg->Name();
00137         if (name)
00138             client->AddName(name);
00139 
00140         // TODO: send better status code (e.g., suppose name already defined for client)
00141         IPC_SendMsg(client, new ipcmMessageResult(IPCM_GetRequestIndex(rawMsg), IPCM_OK));
00142     }
00143 
00144     static void
00145     OnClientDelName(ipcClient *client, const ipcMessage *rawMsg)
00146     {
00147         LOG(("got CLIENT_DEL_NAME\n"));
00148 
00149         ipcMessageCast<ipcmMessageClientDelName> msg(rawMsg);
00150         const char *name = msg->Name();
00151         if (name)
00152             client->DelName(name);
00153 
00154         // TODO: send better status code (e.g., suppose name not defined for client)
00155         IPC_SendMsg(client, new ipcmMessageResult(IPCM_GetRequestIndex(rawMsg), IPCM_OK));
00156     }
00157 
00158     static void
00159     OnClientAddTarget(ipcClient *client, const ipcMessage *rawMsg)
00160     {
00161         LOG(("got CLIENT_ADD_TARGET\n"));
00162 
00163         ipcMessageCast<ipcmMessageClientAddTarget> msg(rawMsg);
00164         client->AddTarget(msg->Target());
00165 
00166         // TODO: send better status code (e.g., suppose target already defined for client)
00167         IPC_SendMsg(client, new ipcmMessageResult(IPCM_GetRequestIndex(rawMsg), IPCM_OK));
00168     }
00169 
00170     static void
00171     OnClientDelTarget(ipcClient *client, const ipcMessage *rawMsg)
00172     {
00173         LOG(("got CLIENT_DEL_TARGET\n"));
00174 
00175         ipcMessageCast<ipcmMessageClientDelTarget> msg(rawMsg);
00176         client->DelTarget(msg->Target());
00177 
00178         // TODO: send better status code (e.g., suppose target not defined for client)
00179         IPC_SendMsg(client, new ipcmMessageResult(IPCM_GetRequestIndex(rawMsg), IPCM_OK));
00180     }
00181 
00182     static void
00183     OnQueryClientByName(ipcClient *client, const ipcMessage *rawMsg)
00184     {
00185         LOG(("got QUERY_CLIENT_BY_NAME\n"));
00186 
00187         PRUint32 requestIndex = IPCM_GetRequestIndex(rawMsg);
00188 
00189         ipcMessageCast<ipcmMessageQueryClientByName> msg(rawMsg);
00190 
00191         ipcClient *result = IPC_GetClientByName(msg->Name());
00192         if (result) {
00193             LOG(("  client exists w/ ID = %u\n", result->ID()));
00194             IPC_SendMsg(client, new ipcmMessageClientID(requestIndex, result->ID()));
00195         }
00196         else {
00197             LOG(("  client does not exist\n"));
00198             IPC_SendMsg(client, new ipcmMessageResult(requestIndex, IPCM_ERROR_NO_CLIENT));
00199         }
00200     }
00201 
00202 #if 0
00203     static void
00204     OnQueryClientInfo(ipcClient *client, const ipcMessage *rawMsg)
00205     {
00206         LOG(("got QUERY_CLIENT_INFO\n"));
00207 
00208         ipcMessageCast<ipcmMessageQueryClientInfo> msg(rawMsg);
00209         ipcClient *result = IPC_GetClientByID(msg->ClientID());
00210         if (result) {
00211             char **names = BuildStringArray(result->Names());
00212             nsID **targets = BuildIDArray(result->Targets());
00213 
00214             IPC_SendMsg(client, new ipcmMessageClientInfo(result->ID(),
00215                                                           msg->RequestIndex(),
00216                                                           (const char **) names,
00217                                                           (const nsID **) targets));
00218 
00219             free(names);
00220             free(targets);
00221         }
00222         else {
00223             LOG(("  client does not exist\n"));
00224             IPC_SendMsg(client, new ipcmMessageError(IPCM_ERROR_CLIENT_NOT_FOUND, msg->RequestIndex()));
00225         }
00226     }
00227 #endif
00228 
00229     static void
00230     OnForward(ipcClient *client, const ipcMessage *rawMsg)
00231     {
00232         LOG(("got FORWARD\n"));
00233 
00234         ipcMessageCast<ipcmMessageForward> msg(rawMsg);
00235 
00236         ipcClient *dest = IPC_GetClientByID(msg->ClientID());
00237         if (!dest) {
00238             LOG(("  destination client not found!\n"));
00239             IPC_SendMsg(client, new ipcmMessageResult(IPCM_GetRequestIndex(rawMsg), IPCM_ERROR_NO_CLIENT));
00240             return;
00241         }
00242         // inform client that its message will be forwarded
00243         IPC_SendMsg(client, new ipcmMessageResult(IPCM_GetRequestIndex(rawMsg), IPCM_OK));
00244 
00245         ipcMessage *newMsg = new ipcmMessageForward(IPCM_MSG_PSH_FORWARD,
00246                                                     client->ID(),
00247                                                     msg->InnerTarget(),
00248                                                     msg->InnerData(),
00249                                                     msg->InnerDataLen());
00250         IPC_SendMsg(dest, newMsg);
00251     }
00252 };
00253 
00254 void
00255 IPCM_HandleMsg(ipcClient *client, const ipcMessage *rawMsg)
00256 {
00257     static ipcCommandModule::MsgHandler handlers[] =
00258     {
00259         ipcCommandModule::OnPing,
00260         ipcCommandModule::OnForward,
00261         ipcCommandModule::OnClientHello,
00262         ipcCommandModule::OnClientAddName,
00263         ipcCommandModule::OnClientDelName,
00264         ipcCommandModule::OnClientAddTarget,
00265         ipcCommandModule::OnClientDelTarget,
00266         ipcCommandModule::OnQueryClientByName
00267     };
00268 
00269     int type = IPCM_GetType(rawMsg);
00270     LOG(("IPCM_HandleMsg [type=%x]\n", type));
00271 
00272     if (!(type & IPCM_MSG_CLASS_REQ)) {
00273         LOG(("not a request -- ignoring message\n"));
00274         return;
00275     }
00276 
00277     type &= ~IPCM_MSG_CLASS_REQ;
00278     type--;
00279     if (type < 0 || type >= (int) (sizeof(handlers)/sizeof(handlers[0]))) {
00280         LOG(("unknown request -- ignoring message\n")); 
00281         return;
00282     }
00283 
00284     (handlers[type])(client, rawMsg);
00285 }