int tc_error(TCDevice * device, int on_off) { size_t size; if (!device) { TrickErrorHndlr *temp_error_hndlr = NULL; trick_error_report(temp_error_hndlr, TRICK_ERROR_ALERT, __FILE__, __LINE__, "Trying to set up error handler on NULL device"); return (-1); } /* If there is no error handler, initialize one */ if (device->error_handler == NULL) { size = sizeof(TrickErrorHndlr); device->error_handler = (TrickErrorHndlr *) malloc(size); trick_error_init(device->error_handler, NULL, NULL, TRICK_ERROR_ALL); } /* Now turn on/off */ if (on_off == 1) { device->error_handler->report_level = TRICK_ERROR_ALL; } else { device->error_handler->report_level = TRICK_ERROR_SILENT; } return 0; }
int main( int argc, char *argv[] ) { int ctr; BUFFER buffer; int i, j; int any_valid ; pthread_t thread ; int nbytes ; if (argc >= 2) { if (!strcmp(argv[1] , "-h")) { fprintf(stderr,"usage: %s [tag_name]\n",argv[0]); exit(0) ; } else strcpy(other_tag , argv[1]) ; } init_attrBUFFER() ; thread_id = pthread_self() ; num_devices = 0 ; #ifndef __WIN32__ /* Register the interupt signal handler. */ { struct sigaction sigact; /* sigaction() sys call parm */ memset ( &sigact , 0 , sizeof(struct sigaction)) ; sigact.sa_flags = 0; sigact.sa_handler = (void(*)())sigint_hndlr; if ( sigaction( SIGINT, &sigact, NULL ) < 0 ) perror("sigaction() failed for SIGINT"); #if (__sun) sigact.sa_handler = (void(*)())stupid; if ( sigaction( SIGALRM, &sigact, NULL ) < 0 ) perror("sigaction() failed for SIGALRM"); #endif } #endif trick_error_init(&err_hndlr, (TrickErrorFuncPtr)NULL, (TrickErrorDataPtr)NULL, TRICK_ERROR_TRIVIAL); pthread_create(&thread , NULL, continuous_connect , NULL) ; buffer.i1 = 0 ; buffer.s1 = 1 ; buffer.c1 = 2 ; buffer.f1 = 3 ; buffer.d1 = 4 ; buffer.b1 = 8 ; buffer.b2 = 7 ; buffer.b3 = 43 ; buffer.b4 = 357 ; buffer.b5 = 1 ; /* buffer.b1 = 15 ; buffer.b2 = 7 ; buffer.b3 = 95 ; buffer.b4 = 7853 ; buffer.b5 = 6 ; */ ctr = 0; while( 1 ) { any_valid = 0 ; /* write a message to all valid devices */ for ( i = 0 ; i < num_devices ; i++ ) { if (tc_isValid(device[i])) { nbytes = tc_write_byteswap(device[i],(char *)&buffer,sizeof(buffer),attrBUFFER); if (nbytes != 0 ) { fprintf(stderr,"server: wrote [%08x] to client %d\n",buffer.i1++, device[i]->socket); any_valid = 1 ; } } else { /* free a device that is no longer valid and move all valid devices down in the array to fill the hole */ free(device[i]); for ( j = i ; j < (num_devices-1) ; j++ ) { device[j] = device[j+1] ; } num_devices-- ; i-- ; } } if (any_valid) ctr++ ; #ifdef __WIN32__ Sleep(500) ; #else usleep(500000) ; #endif } } /* end main */
int main( int argc, char *argv[] ) { int ctr; int tc_rc; BUFFER buffer ; char other_tag[256] = "test_tag" ; TrickErrorHndlr err_hndlr; int *me ; int status ; if (argc >= 2) { if (!strcmp(argv[1] , "-h")) { fprintf(stderr,"usage: %s [tag_name]\n",argv[0]); exit(0) ; } else { strcpy(other_tag , argv[1]) ; } } #ifndef __WIN32__ /* Register the interupt signal handler. */ { struct sigaction sigact; /* sigaction() sys call parm */ memset ( &sigact , 0 , sizeof(struct sigaction)) ; sigact.sa_flags = 0; sigact.sa_handler = (void(*)())sigint_hndlr; if ( sigaction( SIGINT, &sigact, NULL ) < 0 ) perror("sigaction() failed for SIGINT"); #if (__sun) sigact.sa_handler = (void(*)())stupid; if ( sigaction( SIGALRM, &sigact, NULL ) < 0 ) perror("sigaction() failed for SIGALRM"); #endif } #endif memset((void *)&err_hndlr,'\0',sizeof(TrickErrorHndlr)); trick_error_init(&err_hndlr, (TrickErrorFuncPtr)NULL, (TrickErrorDataPtr)NULL, TRICK_ERROR_TRIVIAL); /* It is possible for multiple multiconnects to try and initiate simultaneously causing some of the connects to fail. This happens if all of the clients have the same tag and are connecting to the same server. This is the V.R. scenario. Continue trying to connect until successful. */ do { device = malloc(sizeof(TCDevice)) ; memset((void *)device,'\0',sizeof(TCDevice)); status = tc_multiconnect(device , "client" , other_tag , &err_hndlr); if (status != TC_SUCCESS) { fprintf(stderr,"Error from tc_multiconnect... trying again\n"); } } while (status != TC_SUCCESS) ; ctr = 1; me = ((int *)&buffer.f1 +1) ; while( 1 ) { while ( (tc_isValid(device)) && (!tc_pending(device)) ) #if __WIN32__ Sleep(1); #else usleep(1000); #endif if (tc_isValid(device)) { tc_rc = tc_read(device,(char *)&buffer,sizeof(buffer)); if (tc_rc != 0) { fprintf(stderr,"client: read msg %d from server:\n",ctr++); fprintf(stderr," i1 = %08x, s1 = %d, c1 = %d, f1 = %f, d1 = %f\n", buffer.i1, buffer.s1, buffer.c1, buffer.f1, buffer.d1 ) ; fprintf(stderr," b1 = %d , b2 = %d, b3 = %d, b4 = %d, b5 = %d\n", buffer.b1, buffer.b2, buffer.b3, buffer.b4, buffer.b5 ) ; fprintf(stderr," me = %08x\n", *me ) ; #if __WIN32__ Sleep(500) ; #else usleep(500000); #endif } } else { fprintf(stderr,"Server side disconnected... exiting\n"); tc_disconnect(device); break ; } } } /* end main */