~gnomefreak/firefox-extensions/firegpg.ubuntu

« back to all changes in this revision

Viewing changes to FireGPGCall/ipc/tests/pipetest.cpp

  • Committer: John Vivirito
  • Date: 2008-08-12 11:47:33 UTC
  • Revision ID: gnomefreak@ubuntu.com-20080812114733-hn73tjxi26ylibrf
* import of upstream source version 0.5.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * The contents of this file are subject to the Mozilla Public
 
3
 * License Version 1.1 (the "MPL"); you may not use this file
 
4
 * except in compliance with the MPL. You may obtain a copy of
 
5
 * the MPL at http://www.mozilla.org/MPL/
 
6
 * 
 
7
 * Software distributed under the MPL is distributed on an "AS
 
8
 * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
 
9
 * implied. See the MPL for the specific language governing
 
10
 * rights and limitations under the MPL.
 
11
 * 
 
12
 * The Original Code is protoZilla.
 
13
 * 
 
14
 * The Initial Developer of the Original Code is Ramalingam Saravanan.
 
15
 * Portions created by Ramalingam Saravanan <svn@xmlterm.org> are
 
16
 * Copyright (C) 2000 Ramalingam Saravanan. All Rights Reserved.
 
17
 * 
 
18
 * Contributor(s):
 
19
 * 
 
20
 * Alternatively, the contents of this file may be used under the
 
21
 * terms of the GNU General Public License (the "GPL"), in which case
 
22
 * the provisions of the GPL are applicable instead of
 
23
 * those above. If you wish to allow use of your version of this
 
24
 * file only under the terms of the GPL and not to allow
 
25
 * others to use your version of this file under the MPL, indicate
 
26
 * your decision by deleting the provisions above and replace them
 
27
 * with the notice and other provisions required by the GPL.
 
28
 * If you do not delete the provisions above, a recipient
 
29
 * may use your version of this file under either the MPL or the
 
30
 * GPL.
 
31
 */
 
32
 
 
33
 
 
34
//#define DEBUG_pipet 1
 
35
 
 
36
/**
 
37
 * A Test application that exercises the PipeTransport module.
 
38
 */
 
39
 
 
40
#include "nspr.h"
 
41
#include "nsIRequestObserver.h"
 
42
#include "nsIStreamListener.h"
 
43
#include "nsIPipeTransport.h"
 
44
#include "nsIServiceManager.h"
 
45
#include "nsXPIDLString.h"
 
46
#include "nsIEventQueueService.h"
 
47
#include "nsIThread.h"
 
48
#include "nsIRunnable.h"
 
49
#include "nsIInputStream.h"
 
50
#include "nsIOutputStream.h"
 
51
#include "netCore.h"
 
52
 
 
53
PRUint32 gExited = 0;
 
54
 
 
55
// Helper class to handle polling of STDOUT pipe
 
56
class nsListenerImpl : public nsIStreamListener
 
57
{
 
58
public:
 
59
    NS_DECL_ISUPPORTS
 
60
    NS_DECL_NSIREQUESTOBSERVER
 
61
    NS_DECL_NSISTREAMLISTENER
 
62
 
 
63
    nsListenerImpl();
 
64
    virtual ~nsListenerImpl() {};
 
65
 
 
66
protected:
 
67
 
 
68
};
 
69
 
 
70
// nsListenerImpl implementation
 
71
nsListenerImpl::nsListenerImpl(void)
 
72
{
 
73
    NS_INIT_ISUPPORTS();
 
74
}
 
75
 
 
76
 
 
77
// nsISupports implementation
 
78
NS_IMPL_THREADSAFE_ISUPPORTS1 (nsListenerImpl, nsIStreamListener);
 
79
 
 
80
// nsIStreamListener implementation
 
81
NS_IMETHODIMP
 
82
nsListenerImpl::OnStartRequest(nsIRequest *aRequest, nsISupports *aContext) {
 
83
    printf("nsListenerImpl::OnStartRequest\n");
 
84
    return NS_OK;
 
85
}
 
86
 
 
87
 
 
88
NS_IMETHODIMP
 
89
nsListenerImpl::OnStopRequest(nsIRequest* aRequest, nsISupports* aContext,
 
90
                               nsresult aStatus)
 
91
{
 
92
    printf("nsListenerImpl::OnStopRequest\n");
 
93
    return NS_OK;
 
94
}
 
95
 
 
96
 
 
97
// nsIStreamListener method
 
98
NS_IMETHODIMP
 
99
nsListenerImpl::OnDataAvailable(nsIRequest* aRequest, nsISupports* aContext,
 
100
                               nsIInputStream *aInputStream, PRUint32 aSourceOffset,
 
101
                               PRUint32 aLength) {
 
102
  nsresult rv;
 
103
 
 
104
#ifdef DEBUG_pipet
 
105
  printf("nsListenerImpl::OnDataAvailable, offset=%d, length=%d\n",
 
106
         aSourceOffset, aLength);
 
107
#endif
 
108
 
 
109
  char buf[81];
 
110
  PRUint32 readCount = 0;
 
111
 
 
112
  while (aLength > 0) {
 
113
        
 
114
    rv = aInputStream->Read((char*) buf, 80, &readCount);
 
115
 
 
116
    if (NS_FAILED(rv) || (readCount <= 0))
 
117
      return NS_ERROR_FAILURE;
 
118
 
 
119
    buf[readCount] = '\0';
 
120
#ifdef DEBUG_pipet
 
121
    printf("READ(%d): ", readCount);
 
122
#endif
 
123
    printf("%s", buf);
 
124
 
 
125
    aLength -= readCount;
 
126
  }
 
127
 
 
128
  return NS_OK;
 
129
}
 
130
 
 
131
 
 
132
// Helper class to handle polling of STDOUT pipe
 
133
class nsConsolePoll : public nsIRunnable
 
134
{
 
135
public:
 
136
  NS_DECL_ISUPPORTS
 
137
  NS_DECL_NSIRUNNABLE
 
138
 
 
139
  nsConsolePoll(nsIPipeTransport* aPipeT);
 
140
  virtual ~nsConsolePoll();
 
141
 
 
142
protected:
 
143
  nsCOMPtr<nsIPipeTransport> mPipeT;
 
144
};
 
145
 
 
146
// nsConsolePoll implementation
 
147
 
 
148
// nsISupports implementation
 
149
NS_IMPL_THREADSAFE_ISUPPORTS1 (nsConsolePoll, nsIRunnable);
 
150
 
 
151
// nsConsolePoll implementation
 
152
nsConsolePoll::nsConsolePoll(nsIPipeTransport* aPipeT)
 
153
{
 
154
    NS_INIT_ISUPPORTS();
 
155
    mPipeT = aPipeT;
 
156
}
 
157
 
 
158
nsConsolePoll::~nsConsolePoll()
 
159
{
 
160
  mPipeT = nsnull;
 
161
}
 
162
 
 
163
// nsIRunnable implementation
 
164
NS_IMETHODIMP
 
165
nsConsolePoll::Run()
 
166
{
 
167
  nsresult rv = NS_OK;
 
168
 
 
169
#ifdef DEBUG_pipet
 
170
  nsCOMPtr<nsIThread> myThread;
 
171
  rv = nsIThread::GetCurrent(getter_AddRefs(myThread));
 
172
  printf("nsConsolePoll::Run: myThread=%x\n", (int) myThread.get());
 
173
#endif
 
174
 
 
175
  nsCOMPtr<nsIOutputStream> stdinWrite;
 
176
  rv = mPipeT->OpenOutputStream(0, PRUint32(-1), 0,
 
177
                                getter_AddRefs(stdinWrite));
 
178
  if (NS_FAILED(rv)) return rv;
 
179
 
 
180
  printf("***Accepting console line input\n");
 
181
 
 
182
  char line[81];
 
183
  PRUint32 writeCount;
 
184
 
 
185
  for (;;) {
 
186
    fgets(line, 81, stdin);
 
187
    if (strstr(line, "quit") == line)
 
188
      break;
 
189
#ifdef DEBUG_pipet
 
190
    printf("CONSOLE: %s", line);
 
191
#endif
 
192
    rv = stdinWrite->Write(line, strlen(line), &writeCount);
 
193
    if (NS_FAILED(rv)) return rv;
 
194
  }
 
195
 
 
196
  gExited = 11;
 
197
 
 
198
  return rv;
 
199
}
 
200
 
 
201
main()
 
202
{
 
203
    nsresult rv;
 
204
 
 
205
    // Initialize XPCOM
 
206
    rv = NS_InitXPCOM2(nsnull, nsnull, nsnull);
 
207
    if (NS_FAILED(rv))
 
208
    {
 
209
        printf("ERROR: XPCOM intialization error [%x].\n", rv);
 
210
        return -1;
 
211
    }
 
212
 
 
213
    printf("pipetest: Creating event Q\n");
 
214
    
 
215
    nsCOMPtr<nsIEventQueueService> service = do_GetService(NS_EVENTQUEUESERVICE_CONTRACTID, &rv);
 
216
    if (NS_FAILED(rv)) return rv;
 
217
 
 
218
    rv = service->CreateThreadEventQueue();
 
219
    if (NS_FAILED(rv)) return rv;
 
220
 
 
221
    nsCOMPtr<nsIEventQueue> currentThreadQ;
 
222
    rv = service->GetThreadEventQueue(NS_CURRENT_THREAD, 
 
223
                                  getter_AddRefs(currentThreadQ));
 
224
    if (NS_FAILED(rv)) return rv;
 
225
        
 
226
    (void) nsComponentManager::AutoRegister(nsIComponentManagerObsolete::NS_Startup, nsnull);
 
227
 
 
228
    // Create an instance of our component
 
229
    nsCOMPtr<nsIPipeTransport> mypipet = do_CreateInstance(NS_PIPETRANSPORT_CONTRACTID, &rv);
 
230
    if (NS_FAILED(rv)) {
 
231
        printf("pipetest: ERROR: creating PipeTransport [%x]\n", rv);
 
232
        return -2;
 
233
    }
 
234
 
 
235
    // Call methods on our component to test it out.
 
236
    printf("pipetest: Testing PipeTransport interface\n");
 
237
 
 
238
    PRUint32 nArgs = 0;
 
239
#ifdef XP_WIN
 
240
    const char *executable = "bash";
 
241
    char **args = NULL;
 
242
 
 
243
#else
 
244
    const char *executable = "/bin/sh";
 
245
    char **args = NULL;
 
246
#endif
 
247
 
 
248
    rv = mypipet->Init(executable,
 
249
                       (const char **)args, nArgs,
 
250
                       NULL, 0,
 
251
                       0, "",
 
252
                       false, false, nsnull);
 
253
    if (NS_FAILED(rv)) {
 
254
        printf("pipetest: ERROR: Calling Init [%x]\n", rv);
 
255
        return -3;
 
256
    }
 
257
 
 
258
    // Call ReadStdout
 
259
    nsCOMPtr<nsIRequest> pipeRequest;
 
260
    nsIStreamListener* listener = new nsListenerImpl();
 
261
    rv = mypipet->AsyncRead( listener, (nsISupports*) nsnull,
 
262
                             0, PRUint32(-1), 0,
 
263
                             getter_AddRefs(pipeRequest) );
 
264
    if (NS_FAILED(rv)) {
 
265
        printf("pipetest: ERROR: Calling ReadStdout [%x]\n", rv);
 
266
        return -3;
 
267
    }
 
268
 
 
269
    // Console input helper class
 
270
    nsConsolePoll* consolePoll = new nsConsolePoll(mypipet);
 
271
    nsCOMPtr<nsIThread> consoleThread;
 
272
 
 
273
    // Spin up a new thread to handle STDOUT polling
 
274
    rv = NS_NewThread(getter_AddRefs(consoleThread),
 
275
                      NS_STATIC_CAST(nsIRunnable*, consolePoll));
 
276
 
 
277
    // Process events until we're finished.
 
278
    PLEvent *event;
 
279
    PRBool eventAvailable;
 
280
    PRIntervalTime sleepInterval = PR_MillisecondsToInterval(20);
 
281
 
 
282
    for (;;) {
 
283
      if (gExited) {
 
284
        // Kill process
 
285
        rv = pipeRequest->Cancel(NS_BINDING_ABORTED);
 
286
        if (gExited == 1) break;
 
287
        gExited--;
 
288
      }
 
289
 
 
290
      rv = currentThreadQ->EventAvailable(eventAvailable);
 
291
      if (NS_FAILED(rv)) return rv;
 
292
 
 
293
      if (eventAvailable) {
 
294
        rv = currentThreadQ->WaitForEvent(&event);
 
295
        if (NS_FAILED(rv)) return rv;
 
296
 
 
297
        rv = currentThreadQ->HandleEvent(event);
 
298
        if (NS_FAILED(rv)) return rv;
 
299
 
 
300
      } else {
 
301
        PR_Sleep(sleepInterval);
 
302
      }
 
303
    }
 
304
 
 
305
  if (NS_FAILED(rv)) {
 
306
      printf("pipetest: ERROR: Calling Kill [%x]\n", rv);
 
307
      return NS_ERROR_FAILURE;
 
308
  }
 
309
 
 
310
  printf("pipetest: Finished testing ....\n");
 
311
  mypipet = nsnull;
 
312
 
 
313
  printf("pipetest: Destroying event Q\n");
 
314
  rv = service->DestroyThreadEventQueue();
 
315
  if (NS_FAILED(rv)) return rv;
 
316
 
 
317
  // Shutdown XPCOM
 
318
  NS_ShutdownXPCOM(nsnull);
 
319
  return NS_OK;
 
320
}