Can somebody explain me what I am doing wrong here.

I am including a sample threaded program using pthreads on UNIX. The program
basically uses a separate thread to download a request.. When I've a single
request (thread) then the program works great but if I've a simultaneous
requests (more than one thread fired at the same time) then the threads don't
connect. I don't have too many thread calls in this program for simplicity!
This program compiles on solaris 2.5.1 if you have pthreads. Can somebody
explain me what I am doing worong here! Do I need to muck with the HTEvent loop
to make my multiple thread model to work ? I am also including instrcutions to
compile this file to run. All you w3 GURUs please shed some light on this code!

========================= ptest.c =================================

#include "WWWLib.h"
#include "HTTP.h"
#include "HTChunk.h"
#include "HTReq.h"
#include "HTFile.h"
#include <pthread.h>


static void AlertInit();                     /* sample Alert functions */
static HTAlertCallback   AlertProgressCb;
static HTAlertCallback   AlertMessageCb;
static HTAlertCallback   AlertConfirmCb;
static HTAlertCallback   AlertPromptCb;
static HTAlertCallback   AlertSecretCb;
static HTAlertCallback   AlertUserPwCb;

static BOOL   libInitialized = FALSE;

static int FireThread();
static void *StartRequest();

int 
main( argc, argv)
     int 	argc;
     char       *argv[];
{

  FireThread();      /* fires a thread to down load a request */
	/* If I uncomment the following line thr program does not work */
/*   FireThread(); */ 
  while(1) {
    fprintf(stderr, "Main: sleeping ...\n");
    sleep(20);
    fprintf(stderr, "Main: Waking up...\n");
  }
  fprintf(stdout,"Testing libvuw.a\n");
}

static int
FireThread()
{
  pthread_t      thr;
  pthread_attr_t  attr;
  
  /* default main thread */
	
  if(!libInitialized) {
    HTLibInit("vuwplayback", "1.0");
    HTTransportInit();
    HTNetInit();
    HTProtocolPreemptiveInit();
    HTAAInit();
    AlertInit();       WWWTRACE = ((SHOW_ALL_TRACE)&(~SHOW_MEM_TRACE));   
  }
  pthread_attr_init(&attr);
  pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
  if(pthread_create(&thr, (pthread_attr_t *)&attr, StartRequest, (void *)NULL))
    fprintf(stderr, "failed to create a thread\n");
  pthread_attr_destroy(&attr);
}

void *
StartRequest (clientData)
     void  *clientData;
{
  HTRequest   *reqPtr = HTRequest_new();
  HTList      *converters = HTList_new();
  HTList      *transfer_encodings = HTList_new();
  HTList      *content_encodings = HTList_new();
   
   
  fprintf(stderr, "Starting thread with id  (%d)\n",pthread_self());

  if (!converters) converters = HTList_new();
  if (!transfer_encodings) transfer_encodings = HTList_new();
  if (!content_encodings) content_encodings = HTList_new();
  HTRequest_setOutputFormat(reqPtr, WWW_SOURCE); 
  HTRequest_setOutputStream(reqPtr,  HTFWriter_new(reqPtr, stderr, YES));
  HTRequest_setTransfer(reqPtr, transfer_encodings, YES);
  HTRequest_setConversion(reqPtr,converters,YES);
  HTRequest_setEncoding(reqPtr, content_encodings, YES);
  HTRequest_setFlush(reqPtr, YES);

  HTLoadAbsolute("http://www.rational.com/",reqPtr); 
		/* hard coded the address for simplicity */

  HTList_delete(converters);
  HTList_delete(transfer_encodings);
  HTList_delete(content_encodings);
  HTRequest_delete(reqPtr); 
  fprintf(stderr, "Thread (%d) done..\n",pthread_self());

  return (void *)NULL;
}


/* sample Alert routines for debugging purposes */
void
AlertInit() 
{
  HTAlert_add(AlertProgressCb, HT_A_PROGRESS);
  HTAlert_add(AlertMessageCb, HT_A_MESSAGE);
  HTAlert_add(AlertConfirmCb, HT_A_CONFIRM);
  HTAlert_add(AlertPromptCb, HT_A_PROMPT);
  HTAlert_add(AlertSecretCb, HT_A_SECRET);
  HTAlert_add(AlertUserPwCb, HT_A_USER_PW);
}

static BOOL
AlertProgressCb(reqPtr, opcode, msgNum, dfault, input, reply)
     HTRequest    	*reqPtr;
     HTAlertOpcode	opcode;
     int                msgNum;
     const char         *dfault;
     void               *input;
     HTAlertPar         *reply;
{
  switch (opcode) {
  case HT_PROG_DNS:
    fprintf(stderr, "XX>Looking up %s\n", input ? (char *) input : "");
    break;
       
  case HT_PROG_CONNECT:
    fprintf(stderr, "XX>Contacting %s\n", input ? (char *) input : "");
    break;

  case HT_PROG_ACCEPT:
    fprintf(stderr, "XX>Waiting for connection...\n");
    break;

  case HT_PROG_READ:
    fprintf(stderr, "XX>Reading...\n");
    break;

  case HT_PROG_WRITE:
    fprintf(stderr, "XX>Writing...\n");
    break;

  case HT_PROG_DONE:
    fprintf(stderr, "XX>Connection closed\n");
    break;

  case HT_PROG_WAIT:
    fprintf(stderr, "XX>Waiting for free socket...\n");
    break;

  case HT_PROG_GC:
    fprintf(stderr, "XX>Garbage colleting persistent cache - please wait...\n");
    break;

  default:
    fprintf(stderr, "XX>UNKNOWN PROGRESS STATE\n");
    break;
  }
  return TRUE;
}

static BOOL
AlertMessageCb(reqPtr, opcode, msgNum, dfault, input, reply)
     HTRequest    	*reqPtr;
     HTAlertOpcode	opcode;
     int                msgNum;
     const char         *dfault;
     void               *input;
     HTAlertPar         *reply;
{
  fprintf(stderr, "XX>AlertMessageCb called with opcode %d\n",opcode);
  return TRUE;
}

static BOOL
AlertConfirmCb(reqPtr, opcode, msgNum, dfault, input, reply)
     HTRequest    	*reqPtr;
     HTAlertOpcode	opcode;
     int                msgNum;
     const char         *dfault;
     void               *input;
     HTAlertPar         *reply;
{
  fprintf(stderr, "XX>AlertConfirmCb called with opcode %d\n",opcode);
  return YES;
}

static BOOL
AlertPromptCb(reqPtr, opcode, msgNum, dfault, input, reply)
     HTRequest    	*reqPtr;
     HTAlertOpcode	opcode;
     int                msgNum;
     const char         *dfault;
     void               *input;
     HTAlertPar         *reply;
{
  fprintf(stderr, "XX>AlertPromptCb called with opcode %d\n",opcode);
  return TRUE;
}

static BOOL
AlertSecretCb(reqPtr, opcode, msgNum, dfault, input, reply)
     HTRequest    	*reqPtr;
     HTAlertOpcode	opcode;
     int                msgNum;
     const char         *dfault;
     void               *input;
     HTAlertPar         *reply;
{
  fprintf(stderr, "XX>AlertSecretCb called with opcode %d\n",opcode);
  return TRUE;
}


static BOOL
AlertUserPwCb(reqPtr, opcode, msgNum, dfault, input, reply)
     HTRequest    	*reqPtr;
     HTAlertOpcode	opcode;
     int                msgNum;
     const char         *dfault;
     void               *input;
     HTAlertPar         *reply;
{
  fprintf(stderr, "XX>AlertUserPwCb called with opcode %d\n",opcode);
  return TRUE;
}
     

================================ end ptest.c =============================

Here is how I compiled ptest.c

	gcc  -DHAVE_CONFIG_H    -I. -Iw3lib/   -g -c ptest.c

	gcc -Wall -g    -Wpointer-arith -Wcast-qual -Wcast-align  -Wconversion -Wwrite-strings  -Waggregate-return -o ptest ptest.o  -Lw3lib/ -L. -lpthread  -lwww -ldl -lnsl -lsocket  

w3lib is where I keep all  w3-lib sources and libwww.


After compilation just do  
			> ./ptest 
at your unix prompt.

Received on Thursday, 20 November 1997 10:20:22 UTC