1 | /* ***** BEGIN LICENSE BLOCK *****
|
---|
2 | * Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
---|
3 | *
|
---|
4 | * The contents of this file are subject to the Mozilla Public License Version
|
---|
5 | * 1.1 (the "License"); you may not use this file except in compliance with
|
---|
6 | * the License. You may obtain a copy of the License at
|
---|
7 | * http://www.mozilla.org/MPL/
|
---|
8 | *
|
---|
9 | * Software distributed under the License is distributed on an "AS IS" basis,
|
---|
10 | * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
---|
11 | * for the specific language governing rights and limitations under the
|
---|
12 | * License.
|
---|
13 | *
|
---|
14 | * The Original Code is Mozilla IPC.
|
---|
15 | *
|
---|
16 | * The Initial Developer of the Original Code is
|
---|
17 | * Netscape Communications Corporation.
|
---|
18 | * Portions created by the Initial Developer are Copyright (C) 2002
|
---|
19 | * the Initial Developer. All Rights Reserved.
|
---|
20 | *
|
---|
21 | * Contributor(s):
|
---|
22 | * Darin Fisher <[email protected]>
|
---|
23 | *
|
---|
24 | * Alternatively, the contents of this file may be used under the terms of
|
---|
25 | * either the GNU General Public License Version 2 or later (the "GPL"), or
|
---|
26 | * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
---|
27 | * in which case the provisions of the GPL or the LGPL are applicable instead
|
---|
28 | * of those above. If you wish to allow use of your version of this file only
|
---|
29 | * under the terms of either the GPL or the LGPL, and not to allow others to
|
---|
30 | * use your version of this file under the terms of the MPL, indicate your
|
---|
31 | * decision by deleting the provisions above and replace them with the notice
|
---|
32 | * and other provisions required by the GPL or the LGPL. If you do not delete
|
---|
33 | * the provisions above, a recipient may use your version of this file under
|
---|
34 | * the terms of any one of the MPL, the GPL or the LGPL.
|
---|
35 | *
|
---|
36 | * ***** END LICENSE BLOCK ***** */
|
---|
37 |
|
---|
38 | #include "ipcIService.h"
|
---|
39 | #include "ipcIMessageObserver.h"
|
---|
40 | #include "ipcILockService.h"
|
---|
41 | #include "ipcCID.h"
|
---|
42 | #include "ipcLockCID.h"
|
---|
43 |
|
---|
44 | #include "nsIEventQueueService.h"
|
---|
45 | #include "nsIServiceManager.h"
|
---|
46 | #include "nsIComponentRegistrar.h"
|
---|
47 |
|
---|
48 | #include "nsString.h"
|
---|
49 | #include "prmem.h"
|
---|
50 |
|
---|
51 | static const nsID kIPCMTargetID =
|
---|
52 | { /* 753ca8ff-c8c2-4601-b115-8c2944da1150 */
|
---|
53 | 0x753ca8ff,
|
---|
54 | 0xc8c2,
|
---|
55 | 0x4601,
|
---|
56 | {0xb1, 0x15, 0x8c, 0x29, 0x44, 0xda, 0x11, 0x50}
|
---|
57 | };
|
---|
58 |
|
---|
59 | static const nsID kTestTargetID =
|
---|
60 | { /* e628fc6e-a6a7-48c7-adba-f241d1128fb8 */
|
---|
61 | 0xe628fc6e,
|
---|
62 | 0xa6a7,
|
---|
63 | 0x48c7,
|
---|
64 | {0xad, 0xba, 0xf2, 0x41, 0xd1, 0x12, 0x8f, 0xb8}
|
---|
65 | };
|
---|
66 |
|
---|
67 | #define RETURN_IF_FAILED(rv, step) \
|
---|
68 | PR_BEGIN_MACRO \
|
---|
69 | if (NS_FAILED(rv)) { \
|
---|
70 | printf("*** %s failed: rv=%x\n", step, rv); \
|
---|
71 | return rv;\
|
---|
72 | } \
|
---|
73 | PR_END_MACRO
|
---|
74 |
|
---|
75 | static NS_DEFINE_CID(kEventQueueServiceCID, NS_EVENTQUEUESERVICE_CID);
|
---|
76 | static nsIEventQueue* gEventQ = nsnull;
|
---|
77 | static PRBool gKeepRunning = PR_TRUE;
|
---|
78 | static ipcIService *gIpcServ = nsnull;
|
---|
79 | static ipcILockService *gIpcLockServ = nsnull;
|
---|
80 |
|
---|
81 | static void
|
---|
82 | SendMsg(ipcIService *ipc, PRUint32 cID, const nsID &target, const char *data, PRUint32 dataLen, PRBool sync = PR_FALSE)
|
---|
83 | {
|
---|
84 | printf("*** sending message: [to-client=%u dataLen=%u]\n", cID, dataLen);
|
---|
85 |
|
---|
86 | nsresult rv;
|
---|
87 |
|
---|
88 | rv = ipc->SendMessage(cID, target, (const PRUint8 *) data, dataLen);
|
---|
89 | if (NS_FAILED(rv)) {
|
---|
90 | printf("*** sending message failed: rv=%x\n", rv);
|
---|
91 | return;
|
---|
92 | }
|
---|
93 |
|
---|
94 | if (sync) {
|
---|
95 | rv = ipc->WaitMessage(cID, target, nsnull, PR_UINT32_MAX);
|
---|
96 | if (NS_FAILED(rv))
|
---|
97 | printf("*** waiting for message failed: rv=%x\n", rv);
|
---|
98 | }
|
---|
99 | }
|
---|
100 |
|
---|
101 | //-----------------------------------------------------------------------------
|
---|
102 |
|
---|
103 | class myIpcMessageObserver : public ipcIMessageObserver
|
---|
104 | {
|
---|
105 | public:
|
---|
106 | NS_DECL_ISUPPORTS
|
---|
107 | NS_DECL_IPCIMESSAGEOBSERVER
|
---|
108 | };
|
---|
109 | NS_IMPL_ISUPPORTS1(myIpcMessageObserver, ipcIMessageObserver)
|
---|
110 |
|
---|
111 | NS_IMETHODIMP
|
---|
112 | myIpcMessageObserver::OnMessageAvailable(PRUint32 sender, const nsID &target, const PRUint8 *data, PRUint32 dataLen)
|
---|
113 | {
|
---|
114 | printf("*** got message: [sender=%u data=%s]\n", sender, (const char *) data);
|
---|
115 | return NS_OK;
|
---|
116 | }
|
---|
117 |
|
---|
118 | //-----------------------------------------------------------------------------
|
---|
119 |
|
---|
120 | #if 0
|
---|
121 | class myIpcClientQueryHandler : public ipcIClientQueryHandler
|
---|
122 | {
|
---|
123 | public:
|
---|
124 | NS_DECL_ISUPPORTS
|
---|
125 | NS_DECL_IPCICLIENTQUERYHANDLER
|
---|
126 | };
|
---|
127 |
|
---|
128 | NS_IMPL_ISUPPORTS1(myIpcClientQueryHandler, ipcIClientQueryHandler)
|
---|
129 |
|
---|
130 | NS_IMETHODIMP
|
---|
131 | myIpcClientQueryHandler::OnQueryComplete(PRUint32 aQueryID,
|
---|
132 | nsresult aStatus,
|
---|
133 | PRUint32 aClientID,
|
---|
134 | const char **aNames,
|
---|
135 | PRUint32 aNameCount,
|
---|
136 | const nsID **aTargets,
|
---|
137 | PRUint32 aTargetCount)
|
---|
138 | {
|
---|
139 | printf("*** query complete [queryID=%u status=0x%x clientID=%u]\n",
|
---|
140 | aQueryID, aStatus, aClientID);
|
---|
141 |
|
---|
142 | PRUint32 i;
|
---|
143 | printf("*** names:\n");
|
---|
144 | for (i = 0; i < aNameCount; ++i)
|
---|
145 | printf("*** %d={%s}\n", i, aNames[i]);
|
---|
146 | printf("*** targets:\n");
|
---|
147 | for (i = 0; i < aTargetCount; ++i) {
|
---|
148 | const char *trailer;
|
---|
149 | if (aTargets[i]->Equals(kTestTargetID))
|
---|
150 | trailer = " (TEST_TARGET_ID)";
|
---|
151 | else if (aTargets[i]->Equals(kIPCMTargetID))
|
---|
152 | trailer = " (IPCM_TARGET_ID)";
|
---|
153 | else
|
---|
154 | trailer = " (unknown)";
|
---|
155 | char *str = aTargets[i]->ToString();
|
---|
156 | printf("*** %d=%s%s\n", i, str, trailer);
|
---|
157 | PR_Free(str);
|
---|
158 | }
|
---|
159 |
|
---|
160 | if (aClientID != 0) {
|
---|
161 | const char hello[] = "hello friend!";
|
---|
162 | SendMsg(gIpcServ, aClientID, kTestTargetID, hello, sizeof(hello));
|
---|
163 | }
|
---|
164 |
|
---|
165 | return NS_OK;
|
---|
166 | }
|
---|
167 | #endif
|
---|
168 |
|
---|
169 | //-----------------------------------------------------------------------------
|
---|
170 |
|
---|
171 | #if 0
|
---|
172 | class myIpcLockNotify : public ipcILockNotify
|
---|
173 | {
|
---|
174 | public:
|
---|
175 | NS_DECL_ISUPPORTS
|
---|
176 | NS_DECL_IPCILOCKNOTIFY
|
---|
177 | };
|
---|
178 |
|
---|
179 | NS_IMPL_ISUPPORTS1(myIpcLockNotify, ipcILockNotify)
|
---|
180 |
|
---|
181 | NS_IMETHODIMP
|
---|
182 | myIpcLockNotify::OnAcquireLockComplete(const char *lockName, nsresult status)
|
---|
183 | {
|
---|
184 | printf("*** OnAcquireLockComplete [lock=%s status=%x]\n", lockName, status);
|
---|
185 | gIpcLockServ->ReleaseLock(lockName);
|
---|
186 | return NS_OK;
|
---|
187 | }
|
---|
188 | #endif
|
---|
189 |
|
---|
190 | //-----------------------------------------------------------------------------
|
---|
191 |
|
---|
192 | int main(int argc, char **argv)
|
---|
193 | {
|
---|
194 | nsresult rv;
|
---|
195 |
|
---|
196 | {
|
---|
197 | nsCOMPtr<nsIServiceManager> servMan;
|
---|
198 | NS_InitXPCOM2(getter_AddRefs(servMan), nsnull, nsnull);
|
---|
199 | nsCOMPtr<nsIComponentRegistrar> registrar = do_QueryInterface(servMan);
|
---|
200 | NS_ASSERTION(registrar, "Null nsIComponentRegistrar");
|
---|
201 | if (registrar)
|
---|
202 | registrar->AutoRegister(nsnull);
|
---|
203 |
|
---|
204 | // Create the Event Queue for this thread...
|
---|
205 | nsCOMPtr<nsIEventQueueService> eqs =
|
---|
206 | do_GetService(kEventQueueServiceCID, &rv);
|
---|
207 | RETURN_IF_FAILED(rv, "do_GetService(EventQueueService)");
|
---|
208 |
|
---|
209 | rv = eqs->CreateMonitoredThreadEventQueue();
|
---|
210 | RETURN_IF_FAILED(rv, "CreateMonitoredThreadEventQueue");
|
---|
211 |
|
---|
212 | rv = eqs->GetThreadEventQueue(NS_CURRENT_THREAD, &gEventQ);
|
---|
213 | RETURN_IF_FAILED(rv, "GetThreadEventQueue");
|
---|
214 |
|
---|
215 | printf("*** getting ipc service\n");
|
---|
216 | nsCOMPtr<ipcIService> ipcServ(do_GetService(IPC_SERVICE_CONTRACTID, &rv));
|
---|
217 | RETURN_IF_FAILED(rv, "do_GetService(ipcServ)");
|
---|
218 | NS_ADDREF(gIpcServ = ipcServ);
|
---|
219 |
|
---|
220 | if (argc > 1) {
|
---|
221 | printf("*** using client name [%s]\n", argv[1]);
|
---|
222 | gIpcServ->AddName(argv[1]);
|
---|
223 | }
|
---|
224 |
|
---|
225 | ipcServ->DefineTarget(kTestTargetID, new myIpcMessageObserver(), PR_TRUE);
|
---|
226 |
|
---|
227 | const char data[] =
|
---|
228 | "01 this is a really long message.\n"
|
---|
229 | "02 this is a really long message.\n"
|
---|
230 | "03 this is a really long message.\n"
|
---|
231 | "04 this is a really long message.\n"
|
---|
232 | "05 this is a really long message.\n"
|
---|
233 | "06 this is a really long message.\n"
|
---|
234 | "07 this is a really long message.\n"
|
---|
235 | "08 this is a really long message.\n"
|
---|
236 | "09 this is a really long message.\n"
|
---|
237 | "10 this is a really long message.\n"
|
---|
238 | "11 this is a really long message.\n"
|
---|
239 | "12 this is a really long message.\n"
|
---|
240 | "13 this is a really long message.\n"
|
---|
241 | "14 this is a really long message.\n"
|
---|
242 | "15 this is a really long message.\n"
|
---|
243 | "16 this is a really long message.\n"
|
---|
244 | "17 this is a really long message.\n"
|
---|
245 | "18 this is a really long message.\n"
|
---|
246 | "19 this is a really long message.\n"
|
---|
247 | "20 this is a really long message.\n"
|
---|
248 | "21 this is a really long message.\n"
|
---|
249 | "22 this is a really long message.\n"
|
---|
250 | "23 this is a really long message.\n"
|
---|
251 | "24 this is a really long message.\n"
|
---|
252 | "25 this is a really long message.\n"
|
---|
253 | "26 this is a really long message.\n"
|
---|
254 | "27 this is a really long message.\n"
|
---|
255 | "28 this is a really long message.\n"
|
---|
256 | "29 this is a really long message.\n"
|
---|
257 | "30 this is a really long message.\n"
|
---|
258 | "31 this is a really long message.\n"
|
---|
259 | "32 this is a really long message.\n"
|
---|
260 | "33 this is a really long message.\n"
|
---|
261 | "34 this is a really long message.\n"
|
---|
262 | "35 this is a really long message.\n"
|
---|
263 | "36 this is a really long message.\n"
|
---|
264 | "37 this is a really long message.\n"
|
---|
265 | "38 this is a really long message.\n"
|
---|
266 | "39 this is a really long message.\n"
|
---|
267 | "40 this is a really long message.\n"
|
---|
268 | "41 this is a really long message.\n"
|
---|
269 | "42 this is a really long message.\n"
|
---|
270 | "43 this is a really long message.\n"
|
---|
271 | "44 this is a really long message.\n"
|
---|
272 | "45 this is a really long message.\n"
|
---|
273 | "46 this is a really long message.\n"
|
---|
274 | "47 this is a really long message.\n"
|
---|
275 | "48 this is a really long message.\n"
|
---|
276 | "49 this is a really long message.\n"
|
---|
277 | "50 this is a really long message.\n"
|
---|
278 | "51 this is a really long message.\n"
|
---|
279 | "52 this is a really long message.\n"
|
---|
280 | "53 this is a really long message.\n"
|
---|
281 | "54 this is a really long message.\n"
|
---|
282 | "55 this is a really long message.\n"
|
---|
283 | "56 this is a really long message.\n"
|
---|
284 | "57 this is a really long message.\n"
|
---|
285 | "58 this is a really long message.\n"
|
---|
286 | "59 this is a really long message.\n"
|
---|
287 | "60 this is a really long message.\n";
|
---|
288 | SendMsg(ipcServ, 0, kTestTargetID, data, sizeof(data), PR_TRUE);
|
---|
289 |
|
---|
290 | // PRUint32 queryID;
|
---|
291 | // nsCOMPtr<ipcIClientQueryHandler> handler(new myIpcClientQueryHandler());
|
---|
292 | // ipcServ->QueryClientByName("foopy", handler, PR_FALSE, &queryID);
|
---|
293 |
|
---|
294 | PRUint32 foopyID;
|
---|
295 | nsresult foopyRv = ipcServ->ResolveClientName("foopy", &foopyID);
|
---|
296 | printf("*** query for 'foopy' returned [rv=%x id=%u]\n", foopyRv, foopyID);
|
---|
297 |
|
---|
298 | if (NS_SUCCEEDED(foopyRv)) {
|
---|
299 | const char hello[] = "hello friend!";
|
---|
300 | SendMsg(ipcServ, foopyID, kTestTargetID, hello, sizeof(hello));
|
---|
301 | }
|
---|
302 |
|
---|
303 | //
|
---|
304 | // test lock service
|
---|
305 | //
|
---|
306 | nsCOMPtr<ipcILockService> lockService = do_GetService(IPC_LOCKSERVICE_CONTRACTID, &rv);
|
---|
307 | RETURN_IF_FAILED(rv, "do_GetService(ipcLockServ)");
|
---|
308 | NS_ADDREF(gIpcLockServ = lockService);
|
---|
309 |
|
---|
310 | //nsCOMPtr<ipcILockNotify> notify(new myIpcLockNotify());
|
---|
311 | gIpcLockServ->AcquireLock("blah", PR_TRUE);
|
---|
312 |
|
---|
313 | rv = gIpcLockServ->AcquireLock("foo", PR_TRUE);
|
---|
314 | printf("*** sync AcquireLock returned [rv=%x]\n", rv);
|
---|
315 |
|
---|
316 | PLEvent *ev;
|
---|
317 | while (gKeepRunning) {
|
---|
318 | gEventQ->WaitForEvent(&ev);
|
---|
319 | gEventQ->HandleEvent(ev);
|
---|
320 | }
|
---|
321 |
|
---|
322 | NS_RELEASE(gIpcServ);
|
---|
323 |
|
---|
324 | printf("*** processing remaining events\n");
|
---|
325 |
|
---|
326 | // process any remaining events
|
---|
327 | while (NS_SUCCEEDED(gEventQ->GetEvent(&ev)) && ev)
|
---|
328 | gEventQ->HandleEvent(ev);
|
---|
329 |
|
---|
330 | printf("*** done\n");
|
---|
331 | } // this scopes the nsCOMPtrs
|
---|
332 |
|
---|
333 | // no nsCOMPtrs are allowed to be alive when you call NS_ShutdownXPCOM
|
---|
334 | rv = NS_ShutdownXPCOM(nsnull);
|
---|
335 | NS_ASSERTION(NS_SUCCEEDED(rv), "NS_ShutdownXPCOM failed");
|
---|
336 |
|
---|
337 | return 0;
|
---|
338 | }
|
---|