From 131f215319bc740b3730878e4ebaeb08c4c930a5 Mon Sep 17 00:00:00 2001 From: "darin%netscape.com" Date: Mon, 4 Nov 2002 19:46:34 +0000 Subject: [PATCH] implemented ADD_TARGET/DEL_TARGET messages. added ipcIDList git-svn-id: svn://10.0.0.236/trunk@132936 18797224-902f-48f8-a5cc-f745e15eee43 --- mozilla/modules/ipc/common/Makefile.in | 1 + mozilla/modules/ipc/common/ipcIDList.cpp | 62 +++++++++++ mozilla/modules/ipc/common/ipcIDList.h | 102 ++++++++++++++++++ mozilla/modules/ipc/common/ipcLog.cpp | 37 +++++++ mozilla/modules/ipc/common/ipcLog.h | 37 +++++++ mozilla/modules/ipc/common/ipcStringList.cpp | 37 +++++++ mozilla/modules/ipc/common/ipcStringList.h | 37 +++++++ mozilla/modules/ipc/daemon/ipcClient.cpp | 43 ++++++++ mozilla/modules/ipc/daemon/ipcClient.h | 18 +++- .../modules/ipc/daemon/ipcCommandModule.cpp | 6 +- mozilla/modules/ipc/daemon/ipcd.cpp | 17 +-- 11 files changed, 384 insertions(+), 13 deletions(-) create mode 100644 mozilla/modules/ipc/common/ipcIDList.cpp create mode 100644 mozilla/modules/ipc/common/ipcIDList.h diff --git a/mozilla/modules/ipc/common/Makefile.in b/mozilla/modules/ipc/common/Makefile.in index 123074ba17d..244fb37d1ed 100644 --- a/mozilla/modules/ipc/common/Makefile.in +++ b/mozilla/modules/ipc/common/Makefile.in @@ -57,6 +57,7 @@ CPPSRCS = \ ipcMessage.cpp \ ipcMessagePrimitives.cpp \ ipcStringList.cpp \ + ipcIDList.cpp \ ipcm.cpp EXPORTS = \ diff --git a/mozilla/modules/ipc/common/ipcIDList.cpp b/mozilla/modules/ipc/common/ipcIDList.cpp new file mode 100644 index 00000000000..0f64a3a0dfa --- /dev/null +++ b/mozilla/modules/ipc/common/ipcIDList.cpp @@ -0,0 +1,62 @@ +/* ***** BEGIN LICENSE BLOCK ***** + * Version: MPL 1.1/GPL 2.0/LGPL 2.1 + * + * The contents of this file are subject to the Mozilla Public License Version + * 1.1 (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * http://www.mozilla.org/MPL/ + * + * Software distributed under the License is distributed on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License + * for the specific language governing rights and limitations under the + * License. + * + * The Original Code is Mozilla IPC. + * + * The Initial Developer of the Original Code is + * Netscape Communications Corporation. + * Portions created by the Initial Developer are Copyright (C) 2002 + * the Initial Developer. All Rights Reserved. + * + * Contributor(s): + * Darin Fisher + * + * Alternatively, the contents of this file may be used under the terms of + * either the GNU General Public License Version 2 or later (the "GPL"), or + * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), + * in which case the provisions of the GPL or the LGPL are applicable instead + * of those above. If you wish to allow use of your version of this file only + * under the terms of either the GPL or the LGPL, and not to allow others to + * use your version of this file under the terms of the MPL, indicate your + * decision by deleting the provisions above and replace them with the notice + * and other provisions required by the GPL or the LGPL. If you do not delete + * the provisions above, a recipient may use your version of this file under + * the terms of any one of the MPL, the GPL or the LGPL. + * + * ***** END LICENSE BLOCK ***** */ + +#include "ipcIDList.h" + +ipcIDNode * +ipcIDList::FindNode(ipcIDNode *node, const nsID &id) +{ + while (node) { + if (node->Equals(id)) + return node; + node = node->mNext; + } + return NULL; +} + +ipcIDNode * +ipcIDList::FindNodeBefore(ipcIDNode *node, const nsID &id) +{ + ipcIDNode *prev = NULL; + while (node) { + if (node->Equals(id)) + return prev; + prev = node; + node = node->mNext; + } + return NULL; +} diff --git a/mozilla/modules/ipc/common/ipcIDList.h b/mozilla/modules/ipc/common/ipcIDList.h new file mode 100644 index 00000000000..f4eba010932 --- /dev/null +++ b/mozilla/modules/ipc/common/ipcIDList.h @@ -0,0 +1,102 @@ +/* ***** BEGIN LICENSE BLOCK ***** + * Version: MPL 1.1/GPL 2.0/LGPL 2.1 + * + * The contents of this file are subject to the Mozilla Public License Version + * 1.1 (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * http://www.mozilla.org/MPL/ + * + * Software distributed under the License is distributed on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License + * for the specific language governing rights and limitations under the + * License. + * + * The Original Code is Mozilla IPC. + * + * The Initial Developer of the Original Code is + * Netscape Communications Corporation. + * Portions created by the Initial Developer are Copyright (C) 2002 + * the Initial Developer. All Rights Reserved. + * + * Contributor(s): + * Darin Fisher + * + * Alternatively, the contents of this file may be used under the terms of + * either the GNU General Public License Version 2 or later (the "GPL"), or + * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), + * in which case the provisions of the GPL or the LGPL are applicable instead + * of those above. If you wish to allow use of your version of this file only + * under the terms of either the GPL or the LGPL, and not to allow others to + * use your version of this file under the terms of the MPL, indicate your + * decision by deleting the provisions above and replace them with the notice + * and other provisions required by the GPL or the LGPL. If you do not delete + * the provisions above, a recipient may use your version of this file under + * the terms of any one of the MPL, the GPL or the LGPL. + * + * ***** END LICENSE BLOCK ***** */ + +#ifndef ipcIDList_h__ +#define ipcIDList_h__ + +#include "nsID.h" +#include "ipcList.h" + +//----------------------------------------------------------------------------- +// nsID node +//----------------------------------------------------------------------------- + +class ipcIDNode +{ +public: + ipcIDNode(const nsID &id) + : mID(id) + { } + + const nsID &Value() const { return mID; } + + PRBool Equals(const nsID &id) const { return mID.Equals(id); } + + class ipcIDNode *mNext; +private: + nsID mID; +}; + +//----------------------------------------------------------------------------- +// singly-linked list of nsIDs +//----------------------------------------------------------------------------- + +class ipcIDList : public ipcList +{ +public: + typedef ipcList Super; + + void Prepend(const nsID &id) + { + Super::Prepend(new ipcIDNode(id)); + } + + void Append(const nsID &id) + { + Super::Append(new ipcIDNode(id)); + } + + const ipcIDNode *Find(const nsID &id) const + { + return FindNode(mHead, id); + } + + void FindAndDelete(const nsID &id) + { + ipcIDNode *node = FindNodeBefore(mHead, id); + if (node) + DeleteAfter(node); + else + DeleteFirst(); + } + +private: + static ipcIDNode *FindNode (ipcIDNode *head, const nsID &id); + static ipcIDNode *FindNodeBefore(ipcIDNode *head, const nsID &id); +}; + +#endif // !ipcIDList_h__ diff --git a/mozilla/modules/ipc/common/ipcLog.cpp b/mozilla/modules/ipc/common/ipcLog.cpp index b2d1de37e03..80014b0acaa 100644 --- a/mozilla/modules/ipc/common/ipcLog.cpp +++ b/mozilla/modules/ipc/common/ipcLog.cpp @@ -1,3 +1,40 @@ +/* ***** BEGIN LICENSE BLOCK ***** + * Version: MPL 1.1/GPL 2.0/LGPL 2.1 + * + * The contents of this file are subject to the Mozilla Public License Version + * 1.1 (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * http://www.mozilla.org/MPL/ + * + * Software distributed under the License is distributed on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License + * for the specific language governing rights and limitations under the + * License. + * + * The Original Code is Mozilla IPC. + * + * The Initial Developer of the Original Code is + * Netscape Communications Corporation. + * Portions created by the Initial Developer are Copyright (C) 2002 + * the Initial Developer. All Rights Reserved. + * + * Contributor(s): + * Darin Fisher + * + * Alternatively, the contents of this file may be used under the terms of + * either the GNU General Public License Version 2 or later (the "GPL"), or + * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), + * in which case the provisions of the GPL or the LGPL are applicable instead + * of those above. If you wish to allow use of your version of this file only + * under the terms of either the GPL or the LGPL, and not to allow others to + * use your version of this file under the terms of the MPL, indicate your + * decision by deleting the provisions above and replace them with the notice + * and other provisions required by the GPL or the LGPL. If you do not delete + * the provisions above, a recipient may use your version of this file under + * the terms of any one of the MPL, the GPL or the LGPL. + * + * ***** END LICENSE BLOCK ***** */ + #ifdef XP_UNIX #include #include diff --git a/mozilla/modules/ipc/common/ipcLog.h b/mozilla/modules/ipc/common/ipcLog.h index 46f2cc9c772..f668e08f25b 100644 --- a/mozilla/modules/ipc/common/ipcLog.h +++ b/mozilla/modules/ipc/common/ipcLog.h @@ -1,3 +1,40 @@ +/* ***** BEGIN LICENSE BLOCK ***** + * Version: MPL 1.1/GPL 2.0/LGPL 2.1 + * + * The contents of this file are subject to the Mozilla Public License Version + * 1.1 (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * http://www.mozilla.org/MPL/ + * + * Software distributed under the License is distributed on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License + * for the specific language governing rights and limitations under the + * License. + * + * The Original Code is Mozilla IPC. + * + * The Initial Developer of the Original Code is + * Netscape Communications Corporation. + * Portions created by the Initial Developer are Copyright (C) 2002 + * the Initial Developer. All Rights Reserved. + * + * Contributor(s): + * Darin Fisher + * + * Alternatively, the contents of this file may be used under the terms of + * either the GNU General Public License Version 2 or later (the "GPL"), or + * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), + * in which case the provisions of the GPL or the LGPL are applicable instead + * of those above. If you wish to allow use of your version of this file only + * under the terms of either the GPL or the LGPL, and not to allow others to + * use your version of this file under the terms of the MPL, indicate your + * decision by deleting the provisions above and replace them with the notice + * and other provisions required by the GPL or the LGPL. If you do not delete + * the provisions above, a recipient may use your version of this file under + * the terms of any one of the MPL, the GPL or the LGPL. + * + * ***** END LICENSE BLOCK ***** */ + #ifndef ipcLog_h__ #define ipcLog_h__ diff --git a/mozilla/modules/ipc/common/ipcStringList.cpp b/mozilla/modules/ipc/common/ipcStringList.cpp index 91c7f88cb27..5464f9a7096 100644 --- a/mozilla/modules/ipc/common/ipcStringList.cpp +++ b/mozilla/modules/ipc/common/ipcStringList.cpp @@ -1,3 +1,40 @@ +/* ***** BEGIN LICENSE BLOCK ***** + * Version: MPL 1.1/GPL 2.0/LGPL 2.1 + * + * The contents of this file are subject to the Mozilla Public License Version + * 1.1 (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * http://www.mozilla.org/MPL/ + * + * Software distributed under the License is distributed on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License + * for the specific language governing rights and limitations under the + * License. + * + * The Original Code is Mozilla IPC. + * + * The Initial Developer of the Original Code is + * Netscape Communications Corporation. + * Portions created by the Initial Developer are Copyright (C) 2002 + * the Initial Developer. All Rights Reserved. + * + * Contributor(s): + * Darin Fisher + * + * Alternatively, the contents of this file may be used under the terms of + * either the GNU General Public License Version 2 or later (the "GPL"), or + * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), + * in which case the provisions of the GPL or the LGPL are applicable instead + * of those above. If you wish to allow use of your version of this file only + * under the terms of either the GPL or the LGPL, and not to allow others to + * use your version of this file under the terms of the MPL, indicate your + * decision by deleting the provisions above and replace them with the notice + * and other provisions required by the GPL or the LGPL. If you do not delete + * the provisions above, a recipient may use your version of this file under + * the terms of any one of the MPL, the GPL or the LGPL. + * + * ***** END LICENSE BLOCK ***** */ + #include "ipcStringList.h" void * diff --git a/mozilla/modules/ipc/common/ipcStringList.h b/mozilla/modules/ipc/common/ipcStringList.h index cde6b5a14b2..79e5f321ea7 100644 --- a/mozilla/modules/ipc/common/ipcStringList.h +++ b/mozilla/modules/ipc/common/ipcStringList.h @@ -1,3 +1,40 @@ +/* ***** BEGIN LICENSE BLOCK ***** + * Version: MPL 1.1/GPL 2.0/LGPL 2.1 + * + * The contents of this file are subject to the Mozilla Public License Version + * 1.1 (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * http://www.mozilla.org/MPL/ + * + * Software distributed under the License is distributed on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License + * for the specific language governing rights and limitations under the + * License. + * + * The Original Code is Mozilla IPC. + * + * The Initial Developer of the Original Code is + * Netscape Communications Corporation. + * Portions created by the Initial Developer are Copyright (C) 2002 + * the Initial Developer. All Rights Reserved. + * + * Contributor(s): + * Darin Fisher + * + * Alternatively, the contents of this file may be used under the terms of + * either the GNU General Public License Version 2 or later (the "GPL"), or + * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), + * in which case the provisions of the GPL or the LGPL are applicable instead + * of those above. If you wish to allow use of your version of this file only + * under the terms of either the GPL or the LGPL, and not to allow others to + * use your version of this file under the terms of the MPL, indicate your + * decision by deleting the provisions above and replace them with the notice + * and other provisions required by the GPL or the LGPL. If you do not delete + * the provisions above, a recipient may use your version of this file under + * the terms of any one of the MPL, the GPL or the LGPL. + * + * ***** END LICENSE BLOCK ***** */ + #ifndef ipcStringList_h__ #define ipcStringList_h__ diff --git a/mozilla/modules/ipc/daemon/ipcClient.cpp b/mozilla/modules/ipc/daemon/ipcClient.cpp index f46a1778ee9..26dd3dece34 100644 --- a/mozilla/modules/ipc/daemon/ipcClient.cpp +++ b/mozilla/modules/ipc/daemon/ipcClient.cpp @@ -42,6 +42,7 @@ #include "ipcClient.h" #include "ipcMessage.h" #include "ipcd.h" +#include "ipcm.h" int ipcClient::gLastID = 0; @@ -60,6 +61,9 @@ ipcClient::Init() mSendOffset = 0; + // every client must be able to handle IPCM messages. + mTargets.Append(IPCM_TARGET); + // wait for the client to send us a command return PR_POLL_READ; } @@ -74,6 +78,7 @@ ipcClient::Finalize() delete mInMsg; mOutMsgQ.DeleteAll(); mNames.DeleteAll(); + mTargets.DeleteAll(); return 0; } @@ -168,6 +173,44 @@ ipcClient::DelName(const char *name) mNames.FindAndDelete(name); } +void +ipcClient::AddTarget(const nsID &target) +{ + LOG(("adding client target\n")); + + if (HasTarget(target)) + return; + + mTargets.Append(target); +} + +void +ipcClient::DelTarget(const nsID &target) +{ + LOG(("deleting client target\n")); + + // + // cannot remove the IPCM target + // + if (!target.Equals(IPCM_TARGET)) + mTargets.FindAndDelete(target); +} + +PRBool +ipcClient::EnqueueOutboundMsg(ipcMessage *msg) +{ + LOG(("enqueue outbound message\n")); + + if (!HasTarget(msg->Target())) { + LOG((" no registered message handler\n")); + delete msg; + return PR_FALSE; + } + + mOutMsgQ.Append(msg); + return PR_TRUE; +} + // // called to write out any messages from the outgoing queue. // diff --git a/mozilla/modules/ipc/daemon/ipcClient.h b/mozilla/modules/ipc/daemon/ipcClient.h index a35e675aa08..d144d019072 100644 --- a/mozilla/modules/ipc/daemon/ipcClient.h +++ b/mozilla/modules/ipc/daemon/ipcClient.h @@ -41,8 +41,7 @@ #include "prio.h" #include "ipcMessageQ.h" #include "ipcStringList.h" - -class ipcMessage; +#include "ipcIDList.h" //----------------------------------------------------------------------------- // ipcClient @@ -65,7 +64,18 @@ public: void DelName(const char *name); PRBool HasName(const char *name) const { return mNames.Find(name) != NULL; } - void EnqueueOutboundMsg(ipcMessage *msg) { mOutMsgQ.Append(msg); } + void AddTarget(const nsID &target); + void DelTarget(const nsID &target); + PRBool HasTarget(const nsID &target) const { return mTargets.Find(target) != NULL; } + + // + // returns TRUE if successfully enqueued. will return FALSE if client + // does not have a registered message handler for this message's target. + // + // on success or failure, this function takes ownership of |msg| and will + // delete it when appropriate. + // + PRBool EnqueueOutboundMsg(ipcMessage *msg); private: int WriteMsgs(PRFileDesc *fd); @@ -74,7 +84,7 @@ private: int mID; ipcStringList mNames; - //ipcList mTargets; + ipcIDList mTargets; ipcMessage *mInMsg; // buffer for incoming message ipcMessageQ mOutMsgQ; // outgoing message queue diff --git a/mozilla/modules/ipc/daemon/ipcCommandModule.cpp b/mozilla/modules/ipc/daemon/ipcCommandModule.cpp index 8a9db3e0c85..fbe3c743e9e 100644 --- a/mozilla/modules/ipc/daemon/ipcCommandModule.cpp +++ b/mozilla/modules/ipc/daemon/ipcCommandModule.cpp @@ -99,14 +99,16 @@ public: { LOG(("got CLIENT_ADD_TARGET\n")); - // XXX implement me + ipcMessageCast msg(rawMsg); + client->AddTarget(msg->Target()); } void OnClientDelTarget(ipcClient *client, const ipcMessage *rawMsg) { LOG(("got CLIENT_DEL_TARGET\n")); - // XXX implement me + ipcMessageCast msg(rawMsg); + client->DelTarget(msg->Target()); } void OnQueryClientByName(ipcClient *client, const ipcMessage *rawMsg) diff --git a/mozilla/modules/ipc/daemon/ipcd.cpp b/mozilla/modules/ipc/daemon/ipcd.cpp index 6e89b99c1ee..965a8a8e71f 100644 --- a/mozilla/modules/ipc/daemon/ipcd.cpp +++ b/mozilla/modules/ipc/daemon/ipcd.cpp @@ -379,13 +379,16 @@ int IPC_SendMsg(ipcClient *client, ipcMessage *msg) IPC_SendMsg(&clients[i], msg); } else { - client->EnqueueOutboundMsg(msg); - // - // since this client's Process method may have already been - // called, we need to explicitly set the PR_POLL_WRITE flag. - // - int client_index = client - clients; - poll_fds[client_index].in_flags |= PR_POLL_WRITE; + if (client->EnqueueOutboundMsg(msg)) { + // + // the message was successfully enqueued... + // + // since this client's Process method may have already been + // called, we need to explicitly set the PR_POLL_WRITE flag. + // + int client_index = client - clients; + poll_fds[client_index].in_flags |= PR_POLL_WRITE; + } } return 0; }