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 Transaction Manager.
|
---|
15 | *
|
---|
16 | * The Initial Developer of the Original Code is
|
---|
17 | * Netscape Communications Corp.
|
---|
18 | * Portions created by the Initial Developer are Copyright (C) 2003
|
---|
19 | * the Initial Developer. All Rights Reserved.
|
---|
20 | *
|
---|
21 | * Contributor(s):
|
---|
22 | * John Gaunt <[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 | // transaction manager includes
|
---|
39 | #include "ipcITransactionService.h"
|
---|
40 | #include "ipcITransactionObserver.h"
|
---|
41 |
|
---|
42 | // ipc daemon includes
|
---|
43 | #include "ipcIService.h"
|
---|
44 |
|
---|
45 | // core & xpcom ns includes
|
---|
46 | #include "nsDebug.h"
|
---|
47 | #include "nsIEventQueueService.h"
|
---|
48 | #include "nsIServiceManager.h"
|
---|
49 | #include "nsIComponentRegistrar.h"
|
---|
50 | #include "nsString.h"
|
---|
51 |
|
---|
52 | // nspr includes
|
---|
53 | #include "prmem.h"
|
---|
54 | #include "plgetopt.h"
|
---|
55 | #include "nspr.h"
|
---|
56 | #include "prlog.h"
|
---|
57 |
|
---|
58 | //////////////////////////////////////////////////////////////////////////////
|
---|
59 | // Testing/Debug/Logging BEGIN
|
---|
60 |
|
---|
61 | const int NameSize = 1024;
|
---|
62 |
|
---|
63 | /* command line options */
|
---|
64 | PRIntn optDebug = 0;
|
---|
65 | char optMode = 's';
|
---|
66 | char *profileName = new char[NameSize];
|
---|
67 | char *queueName = new char[NameSize];
|
---|
68 |
|
---|
69 | char *data = new char[NameSize];
|
---|
70 | PRUint32 dataLen = 10; // includes the null terminator for "test data"
|
---|
71 |
|
---|
72 | // Testing/Debug/Logging END
|
---|
73 | //////////////////////////////////////////////////////////////////////////////
|
---|
74 |
|
---|
75 | #define RETURN_IF_FAILED(rv, step) \
|
---|
76 | PR_BEGIN_MACRO \
|
---|
77 | if (NS_FAILED(rv)) { \
|
---|
78 | printf("*** %s failed: rv=%x\n", step, rv); \
|
---|
79 | return rv;\
|
---|
80 | } \
|
---|
81 | PR_END_MACRO
|
---|
82 |
|
---|
83 | static NS_DEFINE_CID(kEventQueueServiceCID, NS_EVENTQUEUESERVICE_CID);
|
---|
84 | static nsIEventQueue* gEventQ = nsnull;
|
---|
85 | static PRBool gKeepRunning = PR_TRUE;
|
---|
86 | //static PRInt32 gMsgCount = 0;
|
---|
87 | static ipcIService *gIpcServ = nsnull;
|
---|
88 | static ipcITransactionService *gTransServ = nsnull;
|
---|
89 |
|
---|
90 | //-----------------------------------------------------------------------------
|
---|
91 |
|
---|
92 | class myTransactionObserver : public ipcITransactionObserver
|
---|
93 | {
|
---|
94 | public:
|
---|
95 | NS_DECL_ISUPPORTS
|
---|
96 | NS_DECL_IPCITRANSACTIONOBSERVER
|
---|
97 |
|
---|
98 | myTransactionObserver() { }
|
---|
99 | };
|
---|
100 |
|
---|
101 | NS_IMPL_ISUPPORTS1(myTransactionObserver, ipcITransactionObserver)
|
---|
102 |
|
---|
103 | NS_IMETHODIMP myTransactionObserver::OnTransactionAvailable(PRUint32 aQueueID, const PRUint8 *aData, PRUint32 aDataLen)
|
---|
104 | {
|
---|
105 | printf("tmModuleTest: myTransactionObserver::OnTransactionAvailable [%s]\n", aData);
|
---|
106 | return NS_OK;
|
---|
107 | }
|
---|
108 |
|
---|
109 | NS_IMETHODIMP myTransactionObserver::OnAttachReply(PRUint32 aQueueID, PRUint32 aStatus)
|
---|
110 | {
|
---|
111 | printf("tmModuleTest: myTransactionObserver::OnAttachReply [%d]\n", aStatus);
|
---|
112 | return NS_OK;
|
---|
113 | }
|
---|
114 |
|
---|
115 | NS_IMETHODIMP myTransactionObserver::OnDetachReply(PRUint32 aQueueID, PRUint32 aStatus)
|
---|
116 | {
|
---|
117 | printf("tmModuleTest: myTransactionObserver::OnDetachReply [%d]\n", aStatus);
|
---|
118 | return NS_OK;
|
---|
119 | }
|
---|
120 |
|
---|
121 | NS_IMETHODIMP myTransactionObserver::OnFlushReply(PRUint32 aQueueID, PRUint32 aStatus)
|
---|
122 | {
|
---|
123 | printf("tmModuleTest: myTransactionObserver::OnFlushReply [%d]\n", aStatus);
|
---|
124 | return NS_OK;
|
---|
125 | }
|
---|
126 |
|
---|
127 |
|
---|
128 | //-----------------------------------------------------------------------------
|
---|
129 |
|
---|
130 | int main(PRInt32 argc, char *argv[])
|
---|
131 | {
|
---|
132 | nsresult rv;
|
---|
133 |
|
---|
134 | // default string values
|
---|
135 | strcpy(profileName, "defaultProfile");
|
---|
136 | strcpy(queueName, "defaultQueue");
|
---|
137 | strcpy(data, "test data");
|
---|
138 |
|
---|
139 | { // scope the command line option gathering (needed for some reason)
|
---|
140 |
|
---|
141 | // Get command line options
|
---|
142 | PLOptStatus os;
|
---|
143 | PLOptState *opt = PL_CreateOptState(argc, argv, "bdfhlp:q:");
|
---|
144 |
|
---|
145 | while (PL_OPT_EOL != (os = PL_GetNextOpt(opt)))
|
---|
146 | {
|
---|
147 | if (PL_OPT_BAD == os) continue;
|
---|
148 | switch (opt->option)
|
---|
149 | {
|
---|
150 | case 'b': /* broadcast a bunch of messages */
|
---|
151 | printf("tmModuleTest: broadcaster\n");
|
---|
152 | optMode = 'b';
|
---|
153 | break;
|
---|
154 | case 'd': /* debug mode */
|
---|
155 | printf("tmModuleTest: debugging baby\n");
|
---|
156 | optDebug = 1;
|
---|
157 | break;
|
---|
158 | case 'f': /* broadcast and flush */
|
---|
159 | printf("tmModuleTest: flusher\n");
|
---|
160 | optMode = 'f';
|
---|
161 | break;
|
---|
162 | case 'h': /* broadcast and detach */
|
---|
163 | printf("tmModuleTest: hit-n-run\n");
|
---|
164 | optMode = 'h';
|
---|
165 | break;
|
---|
166 | case 'l': /* don't broadcast, just listen */
|
---|
167 | printf("tmModuleTest: listener\n");
|
---|
168 | optMode = 'l';
|
---|
169 | break;
|
---|
170 | case 'p': /* set the profile name */
|
---|
171 | strcpy(profileName, opt->value);
|
---|
172 | printf("tmModuleTest: profilename:%s\n",profileName);
|
---|
173 | break;
|
---|
174 | case 'q': /* set the queue name */
|
---|
175 | strcpy(queueName, opt->value);
|
---|
176 | printf("tmModuleTest: queuename:%s\n",queueName);
|
---|
177 | break;
|
---|
178 | default:
|
---|
179 | printf("tmModuleTest: default\n");
|
---|
180 | break;
|
---|
181 | }
|
---|
182 | }
|
---|
183 | PL_DestroyOptState(opt);
|
---|
184 | } // scope the command line option gathering (needed for some reason)
|
---|
185 |
|
---|
186 | { // scope the nsCOMPtrs
|
---|
187 |
|
---|
188 | printf("tmModuleTest: Starting xpcom\n");
|
---|
189 |
|
---|
190 | // xpcom startup stuff
|
---|
191 | nsCOMPtr<nsIServiceManager> servMan;
|
---|
192 | NS_InitXPCOM2(getter_AddRefs(servMan), nsnull, nsnull);
|
---|
193 | nsCOMPtr<nsIComponentRegistrar> registrar = do_QueryInterface(servMan);
|
---|
194 | NS_ASSERTION(registrar, "Null nsIComponentRegistrar");
|
---|
195 | if (registrar)
|
---|
196 | registrar->AutoRegister(nsnull);
|
---|
197 |
|
---|
198 | // Create the Event Queue for this thread...
|
---|
199 | nsCOMPtr<nsIEventQueueService> eqs = do_GetService(kEventQueueServiceCID, &rv);
|
---|
200 | RETURN_IF_FAILED(rv, "do_GetService(EventQueueService)");
|
---|
201 |
|
---|
202 | rv = eqs->CreateMonitoredThreadEventQueue();
|
---|
203 | RETURN_IF_FAILED(rv, "CreateMonitoredThreadEventQueue");
|
---|
204 |
|
---|
205 | rv = eqs->GetThreadEventQueue(NS_CURRENT_THREAD, &gEventQ);
|
---|
206 | RETURN_IF_FAILED(rv, "GetThreadEventQueue");
|
---|
207 |
|
---|
208 | // Need to make sure the ipc system has been started
|
---|
209 | printf("tmModuleTest: getting ipc service\n");
|
---|
210 | nsCOMPtr<ipcIService> ipcServ(do_GetService("@mozilla.org/ipc/service;1", &rv));
|
---|
211 | RETURN_IF_FAILED(rv, "do_GetService(ipcServ)");
|
---|
212 | NS_ADDREF(gIpcServ = ipcServ);
|
---|
213 |
|
---|
214 | // Get the transaction service
|
---|
215 | printf("tmModuleTest: getting transaction service\n");
|
---|
216 | nsCOMPtr<ipcITransactionService> transServ(do_GetService("@mozilla.org/ipc/transaction-service;1", &rv));
|
---|
217 | RETURN_IF_FAILED(rv, "do_GetService(transServ)");
|
---|
218 | NS_ADDREF(gTransServ = transServ);
|
---|
219 |
|
---|
220 | // transaction specifc startup stuff, done for all cases
|
---|
221 |
|
---|
222 | nsCOMPtr<ipcITransactionObserver> observ = new myTransactionObserver();
|
---|
223 |
|
---|
224 | // initialize the transaction service with a specific profile
|
---|
225 | gTransServ->Init(nsDependentCString(profileName));
|
---|
226 | printf("tmModuleTest: using profileName [%s]\n", profileName);
|
---|
227 |
|
---|
228 | // attach to the queue in the transaction manager
|
---|
229 | gTransServ->Attach(nsDependentCString(queueName), observ, PR_TRUE);
|
---|
230 | printf("tmModuleTest: observing queue [%s]\n", queueName);
|
---|
231 |
|
---|
232 |
|
---|
233 | // run specific patterns based on the mode
|
---|
234 | int i = 0; // wasn't working inside the cases
|
---|
235 | switch (optMode)
|
---|
236 | {
|
---|
237 | case 's':
|
---|
238 | printf("tmModuleTest: start standard\n");
|
---|
239 | // post a couple events
|
---|
240 | for (; i < 5 ; i++) {
|
---|
241 | gTransServ->PostTransaction(nsDependentCString(queueName), (PRUint8 *)data, dataLen);
|
---|
242 | }
|
---|
243 | // listen for events
|
---|
244 | while (gKeepRunning)
|
---|
245 | gEventQ->ProcessPendingEvents();
|
---|
246 | printf("tmModuleTest: end standard\n");
|
---|
247 | break;
|
---|
248 | case 'b':
|
---|
249 | printf("tmModuleTest: start broadcast\n");
|
---|
250 | // post a BUNCH of messages
|
---|
251 | for (; i < 50; i++) {
|
---|
252 | gTransServ->PostTransaction(nsDependentCString(queueName), (PRUint8 *)data, dataLen);
|
---|
253 | }
|
---|
254 | // listen for events
|
---|
255 | while (gKeepRunning)
|
---|
256 | gEventQ->ProcessPendingEvents();
|
---|
257 | printf("tmModuleTest: end broadcast\n");
|
---|
258 | break;
|
---|
259 | case 'f':
|
---|
260 | printf("tmModuleTest: start flush\n");
|
---|
261 | // post a couple events
|
---|
262 | for (; i < 5; i++) {
|
---|
263 | gTransServ->PostTransaction(nsDependentCString(queueName), (PRUint8 *)data, dataLen);
|
---|
264 | }
|
---|
265 | // flush the queue
|
---|
266 | gTransServ->Flush(nsDependentCString(queueName), PR_TRUE);
|
---|
267 | // post a couple events
|
---|
268 | for (i=0; i < 8; i++) {
|
---|
269 | gTransServ->PostTransaction(nsDependentCString(queueName), (PRUint8 *)data, dataLen);
|
---|
270 | }
|
---|
271 | // listen for events
|
---|
272 | while (gKeepRunning)
|
---|
273 | gEventQ->ProcessPendingEvents();
|
---|
274 | // detach
|
---|
275 | gTransServ->Detach(nsDependentCString(queueName));
|
---|
276 | printf("tmModuleTest: end flush\n");
|
---|
277 | break;
|
---|
278 | case 'h':
|
---|
279 | printf("tmModuleTest: start hit-n-run\n");
|
---|
280 | // post a couple events
|
---|
281 | for (; i < 5; i++) {
|
---|
282 | gTransServ->PostTransaction(nsDependentCString(queueName), (PRUint8 *)data, dataLen);
|
---|
283 | }
|
---|
284 | // detach
|
---|
285 | gTransServ->Detach(nsDependentCString(queueName));
|
---|
286 | printf("tmModuleTest: end hit-n-run\n");
|
---|
287 | break;
|
---|
288 | case 'l':
|
---|
289 | printf("tmModuleTest: start listener\n");
|
---|
290 | // listen for events
|
---|
291 | while (gKeepRunning)
|
---|
292 | gEventQ->ProcessPendingEvents();
|
---|
293 | printf("tmModuleTest: end listener\n");
|
---|
294 | break;
|
---|
295 | default :
|
---|
296 | printf("tmModuleTest: start & end default\n");
|
---|
297 | break;
|
---|
298 | }
|
---|
299 |
|
---|
300 | // shutdown process
|
---|
301 |
|
---|
302 | NS_RELEASE(gTransServ);
|
---|
303 | NS_RELEASE(gIpcServ);
|
---|
304 |
|
---|
305 | printf("tmModuleTest: processing remaining events\n");
|
---|
306 |
|
---|
307 | // process any remaining events
|
---|
308 | PLEvent *ev;
|
---|
309 | while (NS_SUCCEEDED(gEventQ->GetEvent(&ev)) && ev)
|
---|
310 | gEventQ->HandleEvent(ev);
|
---|
311 |
|
---|
312 | printf("tmModuleTest: done\n");
|
---|
313 | } // this scopes the nsCOMPtrs
|
---|
314 |
|
---|
315 | // helps with shutdown on some cases
|
---|
316 | PR_Sleep(PR_SecondsToInterval(4));
|
---|
317 |
|
---|
318 | // no nsCOMPtrs are allowed to be alive when you call NS_ShutdownXPCOM
|
---|
319 | rv = NS_ShutdownXPCOM(nsnull);
|
---|
320 | NS_ASSERTION(NS_SUCCEEDED(rv), "NS_ShutdownXPCOM failed");
|
---|
321 |
|
---|
322 | return 0;
|
---|
323 | }
|
---|