1999-06-07 21:33:30 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
1999-05-31 05:18:19 +00:00
|
|
|
*
|
|
|
|
* The contents of this file are subject to the Netscape Public License
|
|
|
|
* Version 1.0 (the "NPL"); you may not use this file except in
|
|
|
|
* compliance with the NPL. You may obtain a copy of the NPL at
|
|
|
|
* http://www.mozilla.org/NPL/
|
|
|
|
*
|
|
|
|
* Software distributed under the NPL is distributed on an "AS IS" basis,
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
|
|
|
|
* for the specific language governing rights and limitations under the
|
|
|
|
* NPL.
|
|
|
|
*
|
|
|
|
* The Initial Developer of this code under the NPL is Netscape
|
|
|
|
* Communications Corporation. Portions created by Netscape are
|
|
|
|
* Copyright (C) 1998 Netscape Communications Corporation. All Rights
|
|
|
|
* Reserved.
|
|
|
|
*/
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
#ifdef WIN32
|
1999-06-03 05:45:23 +00:00
|
|
|
#define USE_TIMERS // Only use nsITimer on Windows (for now...)
|
1999-05-31 05:18:19 +00:00
|
|
|
#include <windows.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "nspr.h"
|
|
|
|
|
|
|
|
#include "nsISocketTransportService.h"
|
|
|
|
#include "nsIEventQueueService.h"
|
|
|
|
#include "nsIServiceManager.h"
|
1999-06-07 21:33:30 +00:00
|
|
|
#include "nsIChannel.h"
|
1999-05-31 05:18:19 +00:00
|
|
|
#include "nsIStreamObserver.h"
|
|
|
|
#include "nsIStreamListener.h"
|
1999-06-08 20:57:32 +00:00
|
|
|
#include "nsIBuffer.h"
|
|
|
|
#include "nsIBufferInputStream.h"
|
|
|
|
#include "nsIBufferOutputStream.h"
|
1999-05-31 05:18:19 +00:00
|
|
|
#include "nsIThread.h"
|
1999-06-03 05:45:23 +00:00
|
|
|
#include "nsITimer.h"
|
1999-05-31 05:18:19 +00:00
|
|
|
|
|
|
|
#include "nsCRT.h"
|
|
|
|
|
1999-07-03 04:14:28 +00:00
|
|
|
// forward declarations...
|
1999-06-03 05:45:23 +00:00
|
|
|
class TestConnection;
|
|
|
|
|
1999-05-31 05:18:19 +00:00
|
|
|
static NS_DEFINE_CID(kSocketTransportServiceCID, NS_SOCKETTRANSPORTSERVICE_CID);
|
|
|
|
static NS_DEFINE_CID(kEventQueueServiceCID, NS_EVENTQUEUESERVICE_CID);
|
|
|
|
|
|
|
|
static PRTime gElapsedTime;
|
|
|
|
static int gKeepRunning = 1;
|
|
|
|
static nsIEventQueue* gEventQ = nsnull;
|
|
|
|
|
1999-06-03 05:45:23 +00:00
|
|
|
#define NUM_TEST_THREADS 5
|
|
|
|
|
|
|
|
static TestConnection* gConnections[NUM_TEST_THREADS];
|
|
|
|
static nsIThread* gThreads[NUM_TEST_THREADS];
|
|
|
|
static nsITimer* gPeriodicTimer;
|
1999-05-31 05:18:19 +00:00
|
|
|
|
|
|
|
|
1999-07-03 04:14:28 +00:00
|
|
|
void Pump_PLEvents(void)
|
|
|
|
{
|
|
|
|
while ( gKeepRunning ) {
|
|
|
|
#ifdef WIN32
|
|
|
|
MSG msg;
|
1999-05-31 05:18:19 +00:00
|
|
|
|
1999-07-03 04:14:28 +00:00
|
|
|
if (GetMessage(&msg, NULL, 0, 0)) {
|
|
|
|
TranslateMessage(&msg);
|
|
|
|
DispatchMessage(&msg);
|
|
|
|
} else {
|
|
|
|
gKeepRunning = FALSE;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
#ifdef XP_MAC
|
|
|
|
/* Mac stuff is missing here! */
|
|
|
|
#else
|
1999-07-10 11:26:59 +00:00
|
|
|
nsresult rv;
|
1999-07-03 04:14:28 +00:00
|
|
|
PLEvent *gEvent;
|
|
|
|
rv = gEventQ->GetEvent(&gEvent);
|
|
|
|
rv = gEventQ->HandleEvent(gEvent);
|
|
|
|
#endif /* XP_UNIX */
|
|
|
|
#endif /* !WIN32 */
|
|
|
|
}
|
1999-05-31 05:18:19 +00:00
|
|
|
|
1999-07-03 04:14:28 +00:00
|
|
|
}
|
1999-05-31 05:18:19 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// -----
|
|
|
|
//
|
1999-07-03 04:14:28 +00:00
|
|
|
// TestConnection class...
|
1999-05-31 05:18:19 +00:00
|
|
|
//
|
|
|
|
// -----
|
1999-07-03 04:14:28 +00:00
|
|
|
|
|
|
|
class TestConnection : public nsIRunnable,
|
|
|
|
public nsIStreamListener
|
1999-05-31 05:18:19 +00:00
|
|
|
{
|
|
|
|
public:
|
1999-07-03 04:14:28 +00:00
|
|
|
TestConnection(const char* aHostName, PRInt32 aPort, PRBool aAsyncFlag);
|
|
|
|
~TestConnection();
|
1999-05-31 05:18:19 +00:00
|
|
|
|
1999-07-03 04:14:28 +00:00
|
|
|
// nsISupports interface...
|
1999-05-31 05:18:19 +00:00
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
|
1999-07-03 04:14:28 +00:00
|
|
|
// nsIRunnable interface...
|
|
|
|
NS_IMETHOD Run(void);
|
|
|
|
|
1999-05-31 05:18:19 +00:00
|
|
|
// IStreamListener interface...
|
1999-07-07 08:08:40 +00:00
|
|
|
NS_IMETHOD OnStartRequest(nsIChannel* channel, nsISupports* context);
|
1999-05-31 05:18:19 +00:00
|
|
|
|
1999-07-07 08:08:40 +00:00
|
|
|
NS_IMETHOD OnDataAvailable(nsIChannel* channel, nsISupports* context,
|
1999-06-28 20:37:10 +00:00
|
|
|
nsIInputStream *aIStream,
|
1999-05-31 05:18:19 +00:00
|
|
|
PRUint32 aSourceOffset,
|
|
|
|
PRUint32 aLength);
|
|
|
|
|
1999-07-07 08:08:40 +00:00
|
|
|
NS_IMETHOD OnStopRequest(nsIChannel* channel, nsISupports* context,
|
1999-06-07 21:33:30 +00:00
|
|
|
nsresult aStatus,
|
1999-07-01 19:30:20 +00:00
|
|
|
const PRUnichar* aMsg);
|
1999-06-07 21:33:30 +00:00
|
|
|
|
1999-07-03 04:14:28 +00:00
|
|
|
// TestConnection methods...
|
|
|
|
nsresult WriteBuffer(void);
|
|
|
|
nsresult ReadBuffer(void);
|
1999-05-31 05:18:19 +00:00
|
|
|
|
1999-07-03 04:14:28 +00:00
|
|
|
nsresult Process(void);
|
1999-05-31 05:18:19 +00:00
|
|
|
|
1999-07-03 04:14:28 +00:00
|
|
|
nsresult Suspend(void);
|
|
|
|
nsresult Resume(void);
|
1999-05-31 05:18:19 +00:00
|
|
|
|
1999-07-03 04:14:28 +00:00
|
|
|
protected:
|
1999-07-10 11:26:59 +00:00
|
|
|
nsIBuffer* mBuffer;
|
1999-07-03 04:14:28 +00:00
|
|
|
nsIBufferInputStream* mStream;
|
1999-05-31 05:18:19 +00:00
|
|
|
|
1999-07-03 04:14:28 +00:00
|
|
|
nsIInputStream* mInStream;
|
|
|
|
nsIOutputStream* mOutStream;
|
1999-05-31 05:18:19 +00:00
|
|
|
|
1999-07-03 04:14:28 +00:00
|
|
|
nsIChannel* mTransport;
|
1999-05-31 05:18:19 +00:00
|
|
|
|
1999-07-03 04:14:28 +00:00
|
|
|
PRBool mIsAsync;
|
|
|
|
PRInt32 mBufferLength;
|
|
|
|
char mBufferChar;
|
1999-05-31 05:18:19 +00:00
|
|
|
|
1999-07-03 04:14:28 +00:00
|
|
|
PRInt32 mBytesRead;
|
|
|
|
};
|
1999-05-31 05:18:19 +00:00
|
|
|
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
1999-07-07 08:08:40 +00:00
|
|
|
TestConnection::OnStartRequest(nsIChannel* channel, nsISupports* context)
|
1999-05-31 05:18:19 +00:00
|
|
|
{
|
1999-07-03 04:14:28 +00:00
|
|
|
printf("\n+++ TestConnection::OnStartRequest +++. Context = %p\n", context);
|
1999-05-31 05:18:19 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
1999-07-07 08:08:40 +00:00
|
|
|
TestConnection::OnDataAvailable(nsIChannel* channel, nsISupports* context,
|
1999-07-03 04:14:28 +00:00
|
|
|
nsIInputStream *aIStream,
|
|
|
|
PRUint32 aSourceOffset,
|
|
|
|
PRUint32 aLength)
|
1999-05-31 05:18:19 +00:00
|
|
|
{
|
|
|
|
char buf[1025];
|
|
|
|
PRUint32 amt;
|
|
|
|
|
1999-07-03 04:14:28 +00:00
|
|
|
printf("\n+++ TestConnection::OnDavaAvailable +++."
|
|
|
|
"\tContext = %p length = %d\n",
|
|
|
|
context, aLength);
|
1999-05-31 05:18:19 +00:00
|
|
|
do {
|
|
|
|
nsresult rv = aIStream->Read(buf, 1024, &amt);
|
|
|
|
mBytesRead += amt;
|
|
|
|
buf[amt] = '\0';
|
1999-06-14 23:42:12 +00:00
|
|
|
puts(buf);
|
1999-05-31 05:18:19 +00:00
|
|
|
} while (amt != 0);
|
|
|
|
|
1999-07-03 04:14:28 +00:00
|
|
|
if (mBufferLength == mBytesRead) {
|
1999-05-31 05:18:19 +00:00
|
|
|
mBytesRead = 0;
|
1999-07-03 04:14:28 +00:00
|
|
|
WriteBuffer();
|
1999-05-31 05:18:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
1999-07-07 08:08:40 +00:00
|
|
|
TestConnection::OnStopRequest(nsIChannel* channel,
|
|
|
|
nsISupports* context,
|
1999-07-03 04:14:28 +00:00
|
|
|
nsresult aStatus,
|
|
|
|
const PRUnichar* aMsg)
|
1999-05-31 05:18:19 +00:00
|
|
|
{
|
1999-07-03 04:14:28 +00:00
|
|
|
printf("\n+++ TestConnection::OnStopRequest (status = %x) +++."
|
|
|
|
"\tContext = %p\n",
|
|
|
|
aStatus, context);
|
1999-05-31 05:18:19 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-06-03 05:45:23 +00:00
|
|
|
TestConnection::TestConnection(const char* aHostName, PRInt32 aPort, PRBool aAsyncFlag)
|
1999-05-31 05:18:19 +00:00
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
NS_INIT_REFCNT();
|
|
|
|
|
1999-06-03 05:45:23 +00:00
|
|
|
mIsAsync = aAsyncFlag;
|
1999-07-03 04:14:28 +00:00
|
|
|
|
1999-05-31 05:18:19 +00:00
|
|
|
mBufferLength = 255;
|
1999-06-03 05:45:23 +00:00
|
|
|
mBufferChar = 'a';
|
1999-07-03 04:14:28 +00:00
|
|
|
mBytesRead = 0;
|
1999-05-31 05:18:19 +00:00
|
|
|
|
|
|
|
mTransport = nsnull;
|
1999-07-10 11:26:59 +00:00
|
|
|
mBuffer = nsnull;
|
1999-05-31 05:18:19 +00:00
|
|
|
mStream = nsnull;
|
|
|
|
|
1999-06-03 05:45:23 +00:00
|
|
|
mInStream = nsnull;
|
|
|
|
mOutStream = nsnull;
|
1999-05-31 05:18:19 +00:00
|
|
|
|
|
|
|
// Create a socket transport...
|
|
|
|
NS_WITH_SERVICE(nsISocketTransportService, sts, kSocketTransportServiceCID, &rv);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
rv = sts->CreateTransport(aHostName, aPort, &mTransport);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
1999-06-03 05:45:23 +00:00
|
|
|
if (mIsAsync) {
|
|
|
|
// Create a stream for the data being written to the server...
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
1999-07-10 11:26:59 +00:00
|
|
|
rv = NS_NewBuffer(&mBuffer, 1024, 4096, nsnull);
|
|
|
|
rv = NS_NewBufferInputStream(&mStream, mBuffer);
|
1999-06-03 05:45:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// Synchronous transport...
|
|
|
|
else {
|
1999-06-11 01:37:43 +00:00
|
|
|
rv = mTransport->OpenInputStream(0, -1, &mInStream);
|
|
|
|
rv = mTransport->OpenOutputStream(0, &mOutStream);
|
1999-06-03 05:45:23 +00:00
|
|
|
}
|
|
|
|
}
|
1999-05-31 05:18:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TestConnection::~TestConnection()
|
|
|
|
{
|
1999-06-03 05:45:23 +00:00
|
|
|
NS_IF_RELEASE(mTransport);
|
|
|
|
// Async resources...
|
1999-05-31 05:18:19 +00:00
|
|
|
NS_IF_RELEASE(mStream);
|
1999-07-10 11:26:59 +00:00
|
|
|
NS_IF_RELEASE(mBuffer);
|
1999-05-31 05:18:19 +00:00
|
|
|
|
1999-06-03 05:45:23 +00:00
|
|
|
// Sync resources...
|
|
|
|
NS_IF_RELEASE(mInStream);
|
|
|
|
NS_IF_RELEASE(mOutStream);
|
1999-05-31 05:18:19 +00:00
|
|
|
}
|
|
|
|
|
1999-07-03 04:14:28 +00:00
|
|
|
NS_IMPL_THREADSAFE_ADDREF(TestConnection);
|
|
|
|
NS_IMPL_THREADSAFE_RELEASE(TestConnection);
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
TestConnection::QueryInterface(const nsIID& aIID, void* *aInstancePtr)
|
|
|
|
{
|
|
|
|
if (NULL == aInstancePtr) {
|
|
|
|
return NS_ERROR_NULL_POINTER;
|
|
|
|
}
|
|
|
|
if (aIID.Equals(nsCOMTypeInfo<nsIRunnable>::GetIID()) ||
|
|
|
|
aIID.Equals(nsCOMTypeInfo<nsISupports>::GetIID())) {
|
|
|
|
*aInstancePtr = NS_STATIC_CAST(nsIRunnable*, this);
|
|
|
|
NS_ADDREF_THIS();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
if (aIID.Equals(nsCOMTypeInfo<nsIStreamListener>::GetIID())) {
|
|
|
|
*aInstancePtr = NS_STATIC_CAST(nsIStreamListener*, this);
|
|
|
|
NS_ADDREF_THIS();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
if (aIID.Equals(nsCOMTypeInfo<nsIStreamObserver>::GetIID())) {
|
|
|
|
*aInstancePtr = NS_STATIC_CAST(nsIStreamObserver*, this);
|
|
|
|
NS_ADDREF_THIS();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
return NS_NOINTERFACE;
|
|
|
|
}
|
1999-05-31 05:18:19 +00:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
TestConnection::Run(void)
|
|
|
|
{
|
1999-06-07 07:12:56 +00:00
|
|
|
nsresult rv = NS_OK;
|
1999-06-03 05:45:23 +00:00
|
|
|
|
1999-07-03 04:14:28 +00:00
|
|
|
// Create the Event Queue for this thread...
|
|
|
|
NS_WITH_SERVICE(nsIEventQueueService, eventQService, kEventQueueServiceCID, &rv);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
rv = eventQService->CreateThreadEventQueue();
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
1999-06-03 05:45:23 +00:00
|
|
|
//
|
|
|
|
// Make sure that all resources were allocated in the constructor...
|
|
|
|
//
|
|
|
|
if (!mTransport) {
|
|
|
|
rv = NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
if (mIsAsync) {
|
|
|
|
//
|
|
|
|
// Initiate an async read...
|
|
|
|
//
|
1999-07-03 04:14:28 +00:00
|
|
|
rv = mTransport->AsyncRead(0, -1, mTransport, this);
|
1999-06-03 05:45:23 +00:00
|
|
|
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
printf("Error: AsyncRead failed...");
|
1999-07-03 04:14:28 +00:00
|
|
|
} else {
|
|
|
|
rv = WriteBuffer();
|
1999-06-03 05:45:23 +00:00
|
|
|
}
|
1999-07-03 04:14:28 +00:00
|
|
|
Pump_PLEvents();
|
1999-06-03 05:45:23 +00:00
|
|
|
}
|
1999-07-03 04:14:28 +00:00
|
|
|
/*
|
1999-06-03 05:45:23 +00:00
|
|
|
while (NS_SUCCEEDED(rv)) {
|
|
|
|
rv = WriteBuffer();
|
|
|
|
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
rv = ReadBuffer();
|
|
|
|
}
|
1999-05-31 05:18:19 +00:00
|
|
|
|
|
|
|
if (mBufferChar == 'z') {
|
|
|
|
mBufferChar = 'a';
|
|
|
|
} else {
|
|
|
|
mBufferChar++;
|
|
|
|
}
|
|
|
|
}
|
1999-07-03 04:14:28 +00:00
|
|
|
*/
|
1999-05-31 05:18:19 +00:00
|
|
|
}
|
|
|
|
|
1999-06-03 05:45:23 +00:00
|
|
|
printf("Transport thread exiting...\n");
|
1999-05-31 05:18:19 +00:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
nsresult TestConnection::WriteBuffer(void)
|
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
char *buffer;
|
1999-06-03 05:45:23 +00:00
|
|
|
PRInt32 size;
|
1999-05-31 05:18:19 +00:00
|
|
|
PRUint32 bytesWritten;
|
|
|
|
|
1999-07-03 04:14:28 +00:00
|
|
|
if (mBufferChar == 'z') {
|
|
|
|
mBufferChar = 'a';
|
|
|
|
} else {
|
|
|
|
mBufferChar++;
|
|
|
|
}
|
|
|
|
|
1999-06-11 08:36:40 +00:00
|
|
|
printf("\n+++ Request is: %c. Context = %p\n", mBufferChar, mTransport);
|
|
|
|
|
1999-05-31 05:18:19 +00:00
|
|
|
// Create and fill a test buffer of data...
|
|
|
|
buffer = (char*)PR_Malloc(mBufferLength + 4);
|
|
|
|
|
|
|
|
if (buffer) {
|
1999-06-03 05:45:23 +00:00
|
|
|
for (size=0; size<mBufferLength-2; size++) {
|
|
|
|
buffer[size] = mBufferChar;
|
|
|
|
}
|
|
|
|
buffer[size++] = '\r';
|
|
|
|
buffer[size++] = '\n';
|
|
|
|
buffer[size] = 0;
|
|
|
|
|
|
|
|
//
|
|
|
|
// Async case...
|
|
|
|
//
|
|
|
|
if (mStream) {
|
1999-07-10 11:26:59 +00:00
|
|
|
// rv = mStream->Fill(buffer, size, &bytesWritten);
|
|
|
|
rv = mBuffer->Write(buffer, size, &bytesWritten);
|
1999-06-03 05:45:23 +00:00
|
|
|
|
|
|
|
// Write the buffer to the server...
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
1999-06-29 01:52:30 +00:00
|
|
|
rv = mTransport->AsyncWrite(mStream, 0, bytesWritten, mTransport, /* mOutputObserver */ nsnull);
|
1999-06-03 05:45:23 +00:00
|
|
|
}
|
|
|
|
// Wait for the write to complete...
|
1999-07-03 04:14:28 +00:00
|
|
|
if (NS_FAILED(rv)) {
|
1999-06-03 05:45:23 +00:00
|
|
|
printf("Error: AsyncWrite failed...");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//
|
|
|
|
// Synchronous case...
|
|
|
|
//
|
|
|
|
else if (mOutStream) {
|
|
|
|
rv = mOutStream->Write(buffer, size, &bytesWritten);
|
1999-05-31 05:18:19 +00:00
|
|
|
}
|
|
|
|
PR_Free(buffer);
|
|
|
|
} else {
|
|
|
|
rv = NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
nsresult TestConnection::ReadBuffer(void)
|
|
|
|
{
|
1999-06-03 05:45:23 +00:00
|
|
|
nsresult rv = NS_OK;
|
|
|
|
|
|
|
|
//
|
|
|
|
// Synchronous case...
|
|
|
|
//
|
|
|
|
if (mInStream) {
|
|
|
|
char *buffer;
|
|
|
|
PRUint32 bytesRead;
|
1999-05-31 05:18:19 +00:00
|
|
|
|
1999-06-03 05:45:23 +00:00
|
|
|
buffer = (char*)PR_Malloc(mBufferLength + 4);
|
1999-05-31 05:18:19 +00:00
|
|
|
|
1999-06-03 05:45:23 +00:00
|
|
|
if (buffer) {
|
|
|
|
rv = mInStream->Read(buffer, mBufferLength+2, &bytesRead);
|
|
|
|
|
|
|
|
PR_Free(buffer);
|
|
|
|
}
|
1999-05-31 05:18:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult TestConnection::Process(void)
|
|
|
|
{
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-06-03 05:45:23 +00:00
|
|
|
nsresult TestConnection::Suspend(void)
|
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
if (mTransport) {
|
|
|
|
rv = mTransport->Suspend();
|
|
|
|
} else {
|
|
|
|
rv = NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult TestConnection::Resume(void)
|
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
if (mTransport) {
|
|
|
|
rv = mTransport->Resume();
|
|
|
|
} else {
|
|
|
|
rv = NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#if defined(USE_TIMERS)
|
|
|
|
|
|
|
|
void TimerCallback(nsITimer* aTimer, void* aClosure)
|
|
|
|
{
|
|
|
|
static PRBool flag = PR_FALSE;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (flag) {
|
|
|
|
printf("Resuming connections...\n");
|
|
|
|
} else {
|
|
|
|
printf("Suspending connections...\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
for (i=0; i<NUM_TEST_THREADS; i++) {
|
|
|
|
TestConnection* connection;
|
|
|
|
|
|
|
|
connection = gConnections[i];
|
|
|
|
if (connection) {
|
|
|
|
if (flag) {
|
|
|
|
connection->Resume();
|
|
|
|
} else {
|
|
|
|
connection->Suspend();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
flag = !flag;
|
|
|
|
|
|
|
|
NS_RELEASE(gPeriodicTimer);
|
|
|
|
|
|
|
|
if (NS_OK == NS_NewTimer(&gPeriodicTimer)) {
|
|
|
|
gPeriodicTimer->Init(TimerCallback, nsnull, 1000*5);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* USE_TIMERS */
|
|
|
|
|
1999-06-22 18:24:02 +00:00
|
|
|
nsresult NS_AutoregisterComponents()
|
|
|
|
{
|
|
|
|
nsresult rv = nsComponentManager::AutoRegister(nsIComponentManager::NS_Startup, NULL /* default */);
|
|
|
|
return rv;
|
|
|
|
}
|
1999-05-31 05:18:19 +00:00
|
|
|
|
|
|
|
int
|
|
|
|
main(int argc, char* argv[])
|
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
// -----
|
|
|
|
//
|
|
|
|
// Parse the command line args...
|
|
|
|
//
|
|
|
|
// -----
|
|
|
|
/// if (argc < 3) {
|
|
|
|
/// printf("usage: %s <host> <path>\n", argv[0]);
|
|
|
|
/// return -1;
|
|
|
|
/// }
|
|
|
|
|
1999-07-03 04:14:28 +00:00
|
|
|
char* hostName;
|
1999-05-31 05:18:19 +00:00
|
|
|
int port = 80;
|
1999-07-03 04:14:28 +00:00
|
|
|
|
|
|
|
if (argc < 2) {
|
|
|
|
hostName = "chainsaw";
|
|
|
|
} else {
|
|
|
|
hostName = argv[1];
|
|
|
|
}
|
|
|
|
printf("Using %s as echo server...\n", hostName);
|
|
|
|
|
1999-05-31 05:18:19 +00:00
|
|
|
// -----
|
|
|
|
//
|
|
|
|
// Initialize XPCom...
|
|
|
|
//
|
|
|
|
// -----
|
1999-06-22 18:24:02 +00:00
|
|
|
|
|
|
|
rv = NS_AutoregisterComponents();
|
1999-05-31 05:18:19 +00:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
// Create the Event Queue for this thread...
|
|
|
|
NS_WITH_SERVICE(nsIEventQueueService, eventQService, kEventQueueServiceCID, &rv);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
rv = eventQService->CreateThreadEventQueue();
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
eventQService->GetThreadEventQueue(PR_CurrentThread(), &gEventQ);
|
|
|
|
|
|
|
|
int i;
|
|
|
|
|
1999-06-03 05:45:23 +00:00
|
|
|
//
|
|
|
|
// Create the connections and threads...
|
|
|
|
//
|
|
|
|
for (i=0; i<NUM_TEST_THREADS; i++) {
|
1999-07-03 04:14:28 +00:00
|
|
|
gConnections[i] = new TestConnection(hostName, 7, PR_TRUE);
|
1999-06-03 05:45:23 +00:00
|
|
|
rv = NS_NewThread(&gThreads[i], gConnections[i]);
|
1999-05-31 05:18:19 +00:00
|
|
|
}
|
|
|
|
|
1999-06-03 05:45:23 +00:00
|
|
|
#if defined(USE_TIMERS)
|
|
|
|
//
|
|
|
|
// Start up the timer to test Suspend/Resume APIs on the transport...
|
|
|
|
//
|
|
|
|
if (NS_OK == NS_NewTimer(&gPeriodicTimer)) {
|
|
|
|
gPeriodicTimer->Init(TimerCallback, nsnull, 1000);
|
|
|
|
}
|
|
|
|
#endif /* USE_TIMERS */
|
1999-05-31 05:18:19 +00:00
|
|
|
|
1999-06-03 05:45:23 +00:00
|
|
|
|
1999-05-31 05:18:19 +00:00
|
|
|
// Enter the message pump to allow the URL load to proceed.
|
1999-07-03 04:14:28 +00:00
|
|
|
Pump_PLEvents();
|
1999-05-31 05:18:19 +00:00
|
|
|
|
|
|
|
PRTime endTime;
|
|
|
|
endTime = PR_Now();
|
|
|
|
printf("Elapsed time: %ld\n", (PRInt32)(endTime/1000UL-gElapsedTime/1000UL));
|
|
|
|
|
|
|
|
NS_RELEASE(eventQService);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|