Back to index

lightning-sunbird  0.9+nobinonly
ipcd.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 "prlog.h"
00039 #include "prio.h"
00040 
00041 #include "ipcConfig.h"
00042 #include "ipcLog.h"
00043 #include "ipcMessage.h"
00044 #include "ipcClient.h"
00045 #include "ipcModuleReg.h"
00046 #include "ipcModule.h"
00047 #include "ipcCommandModule.h"
00048 #include "ipcdPrivate.h"
00049 #include "ipcd.h"
00050 
00051 //-----------------------------------------------------------------------------
00052 
00053 void
00054 IPC_NotifyParent()
00055 {
00056     PRFileDesc *fd = PR_GetInheritedFD(IPC_STARTUP_PIPE_NAME);
00057     if (fd) {
00058         char c = IPC_STARTUP_PIPE_MAGIC;
00059         PR_Write(fd, &c, 1);
00060         PR_Close(fd);
00061     }
00062 }
00063 
00064 //-----------------------------------------------------------------------------
00065 
00066 PRStatus
00067 IPC_DispatchMsg(ipcClient *client, const ipcMessage *msg)
00068 {
00069     PR_ASSERT(client);
00070     PR_ASSERT(msg);
00071 
00072     // remember if client is expecting SYNC_REPLY.  we'll add that flag to the
00073     // next message sent to the client.
00074     if (msg->TestFlag(IPC_MSG_FLAG_SYNC_QUERY)) {
00075         PR_ASSERT(client->GetExpectsSyncReply() == PR_FALSE);
00076         // XXX shouldn't we remember the TargetID as well, and only set the
00077         //     SYNC_REPLY flag on the next message sent to the same TargetID?
00078         client->SetExpectsSyncReply(PR_TRUE);
00079     }
00080 
00081     if (msg->Target().Equals(IPCM_TARGET)) {
00082         IPCM_HandleMsg(client, msg);
00083         return PR_SUCCESS;
00084     }
00085 
00086     return IPC_DispatchMsg(client, msg->Target(), msg->Data(), msg->DataLen());
00087 }
00088 
00089 PRStatus
00090 IPC_SendMsg(ipcClient *client, ipcMessage *msg)
00091 {
00092     PR_ASSERT(msg);
00093 
00094     if (client == NULL) {
00095         //
00096         // broadcast
00097         //
00098         for (int i=0; i<ipcClientCount; ++i)
00099             IPC_SendMsg(&ipcClients[i], msg->Clone());
00100         delete msg;
00101         return PR_SUCCESS;
00102     }
00103 
00104     // add SYNC_REPLY flag to message if client is expecting...
00105     if (client->GetExpectsSyncReply()) {
00106         msg->SetFlag(IPC_MSG_FLAG_SYNC_REPLY);
00107         client->SetExpectsSyncReply(PR_FALSE);
00108     }
00109 
00110     if (client->HasTarget(msg->Target()))
00111         return IPC_PlatformSendMsg(client, msg);
00112 
00113     LOG(("  no registered message handler\n"));
00114     return PR_FAILURE;
00115 }
00116 
00117 void
00118 IPC_NotifyClientUp(ipcClient *client)
00119 {
00120     // notify modules before other clients
00121     IPC_NotifyModulesClientUp(client);
00122 
00123     for (int i=0; i<ipcClientCount; ++i) {
00124         if (&ipcClients[i] != client)
00125             IPC_SendMsg(&ipcClients[i],
00126                 new ipcmMessageClientState(client->ID(), IPCM_CLIENT_STATE_UP));
00127     }
00128 }
00129 
00130 void
00131 IPC_NotifyClientDown(ipcClient *client)
00132 {
00133     // notify modules before other clients
00134     IPC_NotifyModulesClientDown(client);
00135 
00136     for (int i=0; i<ipcClientCount; ++i) {
00137         if (&ipcClients[i] != client)
00138             IPC_SendMsg(&ipcClients[i],
00139                 new ipcmMessageClientState(client->ID(), IPCM_CLIENT_STATE_DOWN));
00140     }
00141 }
00142 
00143 //-----------------------------------------------------------------------------
00144 // IPC daemon methods
00145 //-----------------------------------------------------------------------------
00146 
00147 PRStatus
00148 IPC_SendMsg(ipcClient *client, const nsID &target, const void *data, PRUint32 dataLen)
00149 {
00150     return IPC_SendMsg(client, new ipcMessage(target, (const char *) data, dataLen));
00151 }
00152 
00153 ipcClient *
00154 IPC_GetClientByID(PRUint32 clientID)
00155 {
00156     // linear search OK since number of clients should be small
00157     for (int i = 0; i < ipcClientCount; ++i) {
00158         if (ipcClients[i].ID() == clientID)
00159             return &ipcClients[i];
00160     }
00161     return NULL;
00162 }
00163 
00164 ipcClient *
00165 IPC_GetClientByName(const char *name)
00166 {
00167     // linear search OK since number of clients should be small
00168     for (int i = 0; i < ipcClientCount; ++i) {
00169         if (ipcClients[i].HasName(name))
00170             return &ipcClients[i];
00171     }
00172     return NULL;
00173 }
00174 
00175 void
00176 IPC_EnumClients(ipcClientEnumFunc func, void *closure)
00177 {
00178     PR_ASSERT(func);
00179     for (int i = 0; i < ipcClientCount; ++i) {
00180         if (func(closure, &ipcClients[i], ipcClients[i].ID()) == PR_FALSE)
00181             break;
00182     }
00183 }
00184 
00185 PRUint32
00186 IPC_GetClientID(ipcClient *client)
00187 {
00188     PR_ASSERT(client);
00189     return client->ID();
00190 }
00191 
00192 PRBool
00193 IPC_ClientHasName(ipcClient *client, const char *name)
00194 {
00195     PR_ASSERT(client);
00196     PR_ASSERT(name);
00197     return client->HasName(name);
00198 }
00199 
00200 PRBool
00201 IPC_ClientHasTarget(ipcClient *client, const nsID &target)
00202 {
00203     PR_ASSERT(client);
00204     return client->HasTarget(target);
00205 }
00206 
00207 void
00208 IPC_EnumClientNames(ipcClient *client, ipcClientNameEnumFunc func, void *closure)
00209 {
00210     PR_ASSERT(client);
00211     PR_ASSERT(func);
00212     const ipcStringNode *node = client->Names();
00213     while (node) {
00214         if (func(closure, client, node->Value()) == PR_FALSE)
00215             break;
00216         node = node->mNext;
00217     }
00218 }
00219 
00220 void
00221 IPC_EnumClientTargets(ipcClient *client, ipcClientTargetEnumFunc func, void *closure)
00222 {
00223     PR_ASSERT(client);
00224     PR_ASSERT(func);
00225     const ipcIDNode *node = client->Targets();
00226     while (node) {
00227         if (func(closure, client, node->Value()) == PR_FALSE)
00228             break;
00229         node = node->mNext;
00230     }
00231 }