26 #define UNUSED(v) ((void) v)
28 #define WAIT_FOR_RETVAL (command!=SENDEVENT)
30 static pthread_mutex_t eailock = PTHREAD_MUTEX_INITIALIZER;
31 #define EAILOCK pthread_mutex_lock(&eailock);
32 #define EAIUNLOCK pthread_mutex_unlock(&eailock);
35 int _X3D_FreeWRL_Swig_FD = 0;
36 int _X3D_FreeWRL_listen_FD = 0;
39 int receivedData= FALSE;
45 void X3D_error(
char *msg) {
53 char *sendBuffer = NULL;
54 int sendBufferSize = 0;
65 void verifySendBufferSize (
int len) {
66 if (len < (sendBufferSize-50))
return;
69 while (len>(sendBufferSize-200)) sendBufferSize+=1024;
70 sendBuffer = realloc(sendBuffer,sendBufferSize);
74 int _X3D_countWords(
char *ptr) {
87 void *freewrlSwigThread(
void* nada) {
93 struct sockaddr_in servaddr, cliaddr;
98 iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
100 X3D_error(
"WSAStartup failed to load winsock2 ws2_32.dll\n");
106 if ((_X3D_FreeWRL_listen_FD= socket(AF_INET, SOCK_STREAM, 0)) < 0) {
107 X3D_error(
"ERROR opening swig socket");
113 setsockopt(_X3D_FreeWRL_listen_FD, SOL_SOCKET, SO_REUSEADDR, (
char *)&on,
sizeof(on));
115 bzero(&servaddr,
sizeof(servaddr));
116 servaddr.sin_family = AF_INET;
117 servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
118 servaddr.sin_port = htons(EAIBASESOCKET+ 500);
120 if (bind((_X3D_FreeWRL_listen_FD), (
struct sockaddr *) &servaddr,
sizeof(servaddr)) < 0) {
121 X3D_error(
"ERROR in bind");
124 if (listen(_X3D_FreeWRL_listen_FD, 1024) < 0) {
125 X3D_error(
"ERROR in listen");
130 len =
sizeof(cliaddr);
133 _X3D_FreeWRL_Swig_FD = accept((_X3D_FreeWRL_listen_FD), (
struct sockaddr*) &cliaddr, &len);
135 _X3D_FreeWRL_Swig_FD = accept((_X3D_FreeWRL_listen_FD), (
struct sockaddr*) &cliaddr, (socklen_t *) &len);
145 pthread_mutex_t mut_relist = PTHREAD_MUTEX_INITIALIZER;
146 pthread_mutex_t mut_evlist = PTHREAD_MUTEX_INITIALIZER;
147 pthread_mutex_t mut_re = PTHREAD_MUTEX_INITIALIZER;
149 pthread_cond_t condition_relist_nonempty = PTHREAD_COND_INITIALIZER;
150 pthread_cond_t condition_evlist_nonempty = PTHREAD_COND_INITIALIZER;
151 pthread_cond_t condition_re_done = PTHREAD_COND_INITIALIZER;
156 char *cbline = strdup(readbuffer);
157 item = ml_new(cbline);
158 list = ml_append(list, item);
161 void _enqueue_readbuffer_re(
char *readbuffer)
163 pthread_mutex_lock (&mut_relist);
164 relist = _enqueue_readbuffer(relist,readbuffer);
165 pthread_cond_signal(&condition_relist_nonempty);
166 pthread_mutex_unlock (&mut_relist);
168 void _enqueue_readbuffer_ev(
char *readbuffer)
170 pthread_mutex_lock (&mut_evlist);
171 evlist = _enqueue_readbuffer(evlist,readbuffer);
172 pthread_cond_signal(&condition_evlist_nonempty);
173 pthread_mutex_unlock (&mut_evlist);
175 char* dequeue_readbuffer(
s_list_t **plist)
179 char *readbuffer = ml_elem(list);
180 *plist = ml_delete_self(list, list);
185 int waiting_for_RE = 0;
186 char *dequeue_readbuffer_wait_re()
189 pthread_mutex_lock (&mut_re);
192 pthread_mutex_lock (&mut_relist);
194 pthread_cond_wait(&condition_relist_nonempty, &mut_relist);
195 readbuffer = dequeue_readbuffer(&relist);
196 pthread_mutex_unlock (&mut_relist);
199 pthread_cond_signal(&condition_re_done);
200 pthread_mutex_unlock (&mut_re);
203 char* dequeue_readbuffer_ev(
int wait)
206 pthread_mutex_lock (&mut_evlist);
209 pthread_mutex_unlock (&mut_evlist);
212 pthread_cond_wait(&condition_evlist_nonempty, &mut_evlist);
214 readbuffer = dequeue_readbuffer(&evlist);
215 pthread_mutex_unlock (&mut_evlist);
225 void dequeue_callback_ev(
int wait)
230 if(waiting_for_RE)
return;
231 cbline = dequeue_readbuffer_ev(wait);
233 _handleFreeWRLcallback (cbline) ;
237 cbline = dequeue_readbuffer_ev(wait);
239 pthread_mutex_lock (&mut_re);
241 pthread_cond_wait(&condition_re_done, &mut_re);
242 pthread_mutex_unlock (&mut_re);
243 _handleFreeWRLcallback (cbline) ;
251 void *freewrlEVcallbackThread(
void* nada) {
253 dequeue_callback_ev(1);
260 void *freewrlReadThread(
void* nada) {
262 char readbuffer[2048];
270 FD_SET(_X3D_FreeWRL_FD, &rfds);
275 retval = select(_X3D_FreeWRL_FD+1, &rfds, NULL, NULL, NULL);
278 if(retval)printf(
"+");
284 retval = recv(_X3D_FreeWRL_FD, readbuffer, 2048, 0);
285 if (retval == SOCKET_ERROR) {
287 retval = read(_X3D_FreeWRL_FD,readbuffer,2048);
290 printf(
"ERROR reading fromsocket\n");
293 readbuffer[retval] =
'\0';
296 if (strncmp (
"RE",readbuffer,2) == 0) {
300 _enqueue_readbuffer_re(readbuffer);
301 }
else if (strncmp (
"EV",readbuffer,2) == 0) {
303 _handleFreeWRLcallback(readbuffer);
305 _enqueue_readbuffer_ev(readbuffer);
306 }
else if (strncmp (
"QUIT",readbuffer,4) == 0) {
309 printf (
"readThread - unknown prefix - %s\n",readbuffer);
320 char readbuffer[2048];
322 static char *sendToFreeWRL(
char *callerbuffer,
int size,
int waitForResponse) {
328 printf (
"sendToFreeWRL - sending :%s:\n",callerbuffer);
333 receivedData = FALSE;
334 retval = send(_X3D_FreeWRL_FD, callerbuffer, size, 0);
335 if (retval == SOCKET_ERROR )
338 retval = write(_X3D_FreeWRL_FD, callerbuffer, size);
340 printf (
"sendToFreeWRL, sent callbuffer %s of size %d, retval %d\n",callerbuffer,size,retval);
345 X3D_error(
"ERROR writing to socket");
347 if (waitForResponse) {
351 while (!receivedData) {
357 rb = dequeue_readbuffer_wait_re();
358 strcpy(readbuffer,rb);
366 printf(
"Client got: %s\n",readbuffer);
379 while ((*ptr !=
'\0') && (*ptr <=
' ')) ptr++;
382 printf (
"found a reply\n");
387 if (sscanf(ptr,
"%lf",&mytime) != 1) {
388 printf (
"huh, expected the time, got %s\n",ptr);
392 printf (
"time of command is %lf\n",mytime);
399 printf (
"this should be the query number: %s\n",ptr);
402 if (sscanf(ptr,
"%d",&readquery) != 1) {
403 printf (
"huh, expected the time, got %s\n",ptr);
407 printf (
"query returned is %d\n",readquery);
410 if (_X3D_queryno != readquery) {
411 printf (
"server: warning, _X3D_queryno %d != received %d\n",_X3D_queryno,readquery);
420 strncpy(callerbuffer,readbuffer,retval);
425 printf (
"sendToFreeWRL, returning %p\n",ptr);
432 void _X3D_sendEvent (
char command,
char *
string,
int line) {
436 verifySendBufferSize (strlen(
string));
439 sprintf (sendBuffer,
"%d %c %s\n",_X3D_queryno,command,
string);
440 myptr = sendToFreeWRL(sendBuffer, strlen(sendBuffer),WAIT_FOR_RETVAL);
445 char *_X3D_makeShortCommand (
char command) {
449 verifySendBufferSize (100);
450 sprintf (sendBuffer,
"%d %c\n",_X3D_queryno,command);
451 myptr = sendToFreeWRL(sendBuffer, strlen(sendBuffer),WAIT_FOR_RETVAL);
454 printf (
"makeShortCommand, buffer now %s\n",myptr);
459 char *_X3D_make1VoidCommand (
char command,
int adr) {
463 verifySendBufferSize (100);
464 sprintf (sendBuffer,
"%d %c %d\n",_X3D_queryno,command,adr);
465 myptr = sendToFreeWRL(sendBuffer, strlen(sendBuffer),WAIT_FOR_RETVAL);
468 printf (
"make1VoidCommand, buffer now %s\n",myptr);
473 char *_X3D_make1StringCommand (
char command,
char *name) {
477 printf (
"start _X3D_make1StringCommand, command %c char %s\n",command,name);
481 verifySendBufferSize (strlen(name));
482 sprintf (sendBuffer,
"%d %c %s\n",_X3D_queryno,command,name);
483 myptr = sendToFreeWRL(sendBuffer, strlen(sendBuffer),WAIT_FOR_RETVAL);
487 printf (
"make1StringCommand, buffer now %s\n",myptr);
494 char *_X3D_make2StringCommand (
char command,
char *str1,
char *str2) {
496 char sendBuffer[2048];
499 verifySendBufferSize ( strlen(str1) + strlen(str2));
500 sprintf (sendBuffer,
"%d %c %s%s\n",_X3D_queryno,command,str1,str2);
501 myptr = sendToFreeWRL(sendBuffer, strlen(sendBuffer),WAIT_FOR_RETVAL);
505 printf (
"make2StringCommand, buffer now %s\n",myptr);
511 char *_X3D_Browser_SendEventType(
int adr,
char *name,
char *evtype) {
515 verifySendBufferSize (100);
516 sprintf (sendBuffer,
"%d %c %d %s %s\n",_X3D_queryno, GETFIELDTYPE, adr, name, evtype);
518 myptr = sendToFreeWRL(sendBuffer, strlen(sendBuffer),TRUE);
521 printf (
"_X3D_Browser_SendEventType, buffer now %s\n",myptr);
526 char * _RegisterListener (X3DEventOut *node,
int adin) {
530 verifySendBufferSize (100);
532 printf (
"in RegisterListener, we have query %d advise index %d nodeptr %d offset %d datatype %d datasize %d field %s\n",
534 adin, node->nodeptr, node->offset, node->datatype, node->datasize, node->field);
542 sprintf (sendBuffer,
"%u %c %d %d %c %d\n",
547 mapFieldTypeToEAItype(node->datatype),
550 myptr = sendToFreeWRL(sendBuffer, strlen(sendBuffer),TRUE);
553 printf (
"_X3D_Browser_SendEventType, buffer now %s\n",myptr);