Doxygen Source Code Documentation
Main Page Alphabetical List Data Structures File List Data Fields Globals Search
plugout_surf.c
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007 #include "thd_iochan.h"
00008
00009
00010
00011
00012 static char afni_host[128] = "." ;
00013 static char afni_name[128] = "\0" ;
00014 static int afni_port = 8019 ;
00015 static int afni_verbose = 0 ;
00016
00017
00018
00019 int afni_io(void) ;
00020
00021 int got_key(char *) ;
00022 void setup_key(void) ;
00023 void restore_key(void) ;
00024
00025
00026
00027
00028
00029
00030
00031 int main( int argc , char * argv[] )
00032 {
00033 int narg , ii ;
00034
00035
00036
00037 if( argc == 2 && strncmp(argv[1],"-help",5) == 0 ){
00038 printf("Usage: plugout_surf [-host name] [-v]\n"
00039 "This program connects to AFNI and sends/gets surface\n"
00040 "node IDs to control/know the AFNI viewpoint.\n\n"
00041 "Options:\n"
00042 " -host name Means to connect to AFNI running on the\n"
00043 " computer 'name' using TCP/IP. The default is to\n"
00044 " connect on the current host using shared memory.\n"
00045 " -v Verbose mode.\n"
00046 " -port pp Use TCP/IP port number 'pp'. The default is\n"
00047 " 8019, but if two plugouts are running on the\n"
00048 " same computer, they must use different ports.\n"
00049 " -name sss Use the string 'sss' for the name that AFNI assigns\n"
00050 " to this plugout. The default is something stupid.\n"
00051 ) ;
00052 exit(0) ;
00053 }
00054
00055
00056
00057 narg = 1 ;
00058 while( narg < argc ){
00059
00060
00061
00062 if( strncmp(argv[narg],"-host",5) == 0 ){
00063 narg++ ;
00064 if( narg >= argc ){
00065 fprintf(stderr,"-host needs a following name!\a\n"); exit(1);
00066 }
00067 strcpy( afni_host , argv[narg] ) ;
00068 narg++ ; continue ;
00069 }
00070
00071
00072
00073 if( strncmp(argv[narg],"-name",5) == 0 ){
00074 narg++ ;
00075 if( narg >= argc ){
00076 fprintf(stderr,"-name needs a following string!\a\n"); exit(1);
00077 }
00078 strcpy( afni_name , argv[narg] ) ;
00079 narg++ ; continue ;
00080 }
00081
00082
00083
00084 if( strncmp(argv[narg],"-v",2) == 0 ){
00085 afni_verbose = 1 ;
00086 narg++ ; continue ;
00087 }
00088
00089
00090
00091 if( strncmp(argv[narg],"-port",4) == 0 ){
00092 narg++ ;
00093 if( narg >= argc ){
00094 fprintf(stderr,"-port needs a following argument!\a\n"); exit(1);
00095 }
00096 afni_port = strtol( argv[narg] , NULL , 10 ) ;
00097 if( afni_port <= 0 ){
00098 fprintf(stderr,"-port needs a positive argument!\a\n"); exit(1);
00099 }
00100 if( strcmp(afni_host,".") == 0 ) strcpy(afni_host,"localhost") ;
00101 narg++ ; continue ;
00102 }
00103
00104
00105
00106 fprintf(stderr,"Unrecognized option: %s\a\n",argv[narg]) ;
00107 exit(1) ;
00108 }
00109
00110
00111
00112 while( 1 ){
00113 ii = afni_io() ;
00114 if( ii < 0 ) break ;
00115 iochan_sleep(100) ;
00116 }
00117
00118 restore_key() ; exit(0) ;
00119 }
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144 #define AFNI_OPEN_CONTROL_MODE 1
00145 #define AFNI_WAIT_CONTROL_MODE 2
00146 #define AFNI_OPEN_DATA_MODE 3
00147 #define AFNI_WAIT_DATA_MODE 4
00148 #define AFNI_CONTINUE_MODE 5
00149
00150
00151
00152 #define POACKSIZE 4
00153
00154 #define PO_ACK_BAD(ic) iochan_sendall( (ic) , "BAD" , POACKSIZE )
00155 #define PO_ACK_OK(ic) iochan_sendall( (ic) , "OK!" , POACKSIZE )
00156 #define PO_SEND(ic,str) iochan_sendall( (ic) , (str) , strlen((str))+1 )
00157
00158 int afni_io(void)
00159 {
00160 static int afni_mode = AFNI_OPEN_CONTROL_MODE ;
00161 static IOCHAN * afni_ioc = NULL ;
00162 int ii ;
00163 int need_setup_key=1 ;
00164
00165
00166
00167
00168
00169 if( afni_mode <= 0 ) return -1 ;
00170
00171
00172
00173
00174 if( afni_mode == AFNI_OPEN_CONTROL_MODE ){
00175 char afni_iocname[128] ;
00176
00177
00178
00179
00180 if( strcmp(afni_host,".") == 0 )
00181 sprintf( afni_iocname , "tcp:%s:7955" , "localhost" );
00182 else
00183 sprintf( afni_iocname , "tcp:%s:7955" , afni_host ) ;
00184
00185 afni_ioc = iochan_init( afni_iocname , "create" ) ;
00186 if( afni_ioc == NULL ){
00187 fprintf(stderr,
00188 "Can't create control channel %s to AFNI!\n",afni_iocname) ;
00189 afni_mode = 0 ;
00190 return -1 ;
00191 }
00192 afni_mode = AFNI_WAIT_CONTROL_MODE ;
00193 if( afni_verbose )
00194 fprintf(stderr,"AFNI control channel created\n") ;
00195 }
00196
00197
00198
00199
00200 if( afni_mode == AFNI_WAIT_CONTROL_MODE ){
00201 ii = iochan_writecheck( afni_ioc , 5 ) ;
00202
00203
00204
00205
00206
00207
00208 if( ii < 0 ){
00209 fprintf(stderr,"Control channel to AFNI failed!\a\n") ;
00210 IOCHAN_CLOSE(afni_ioc) ;
00211 afni_mode = 0 ;
00212 return -1 ;
00213 } else if( ii > 0 ){
00214 afni_mode = AFNI_OPEN_DATA_MODE ;
00215 if( afni_verbose )
00216 fprintf(stderr,"AFNI control channel connected\n");
00217 } else {
00218 return 0 ;
00219 }
00220 }
00221
00222
00223
00224
00225 if( afni_mode == AFNI_OPEN_DATA_MODE ){
00226 char afni_iocname[128] ;
00227 char afni_buf[256] ;
00228
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238 if( strcmp(afni_host,".") == 0 )
00239 strcpy( afni_iocname , "shm:surf_plugout:1K+1K" ) ;
00240 else
00241 sprintf( afni_iocname , "tcp:%s:%d" , afni_host , afni_port ) ;
00242
00243
00244
00245
00246
00247
00248
00249
00250
00251 if( afni_name[0] == '\0' ) strcpy(afni_name,"SurfaceHack") ;
00252
00253 sprintf( afni_buf , "SURFID_DELTA\n"
00254 "NO_ACK\n"
00255 "PONAME %s\n"
00256 "IOCHAN %s" ,
00257 afni_name , afni_iocname ) ;
00258
00259 if( afni_verbose )
00260 fprintf(stderr,"Sending control information to AFNI\n") ;
00261
00262
00263
00264 ii = iochan_sendall( afni_ioc , afni_buf , strlen(afni_buf)+1 ) ;
00265
00266
00267
00268
00269 if( ii < 0 ){
00270 fprintf(stderr,"Transmission of control data to AFNI failed!\a\n") ;
00271 IOCHAN_CLOSE(afni_ioc) ;
00272 afni_mode = 0 ;
00273 return -1 ;
00274
00275 } else {
00276
00277
00278
00279 IOCHAN_CLOSE(afni_ioc) ;
00280
00281
00282
00283 afni_ioc = iochan_init( afni_iocname , "create" ) ;
00284 if( afni_ioc == NULL ){
00285 fprintf(stderr,
00286 "Can't open data channel %s to AFNI!\a\n",afni_iocname) ;
00287 afni_mode = 0 ;
00288 return -1 ;
00289 } else {
00290 afni_mode = AFNI_WAIT_DATA_MODE ;
00291 if( afni_verbose ) fprintf(stderr,"AFNI data channel created\n") ;
00292 }
00293 }
00294 }
00295
00296
00297
00298
00299 if( afni_mode == AFNI_WAIT_DATA_MODE ){
00300
00301 ii = iochan_goodcheck( afni_ioc , 5 ) ;
00302 if( ii < 0 ){
00303 fprintf(stderr,
00304 "AFNI data channel aborted before any data was sent!\a\n") ;
00305 IOCHAN_CLOSE( afni_ioc ) ;
00306 afni_mode = 0 ;
00307 return -1 ;
00308 } else if( ii > 0 ){
00309 afni_mode = AFNI_CONTINUE_MODE ;
00310 if( afni_verbose ) fprintf(stderr,"AFNI data channel is open\n") ;
00311 printf("\rEnter node ID: "); fflush(stdout);
00312 } else {
00313 return 0 ;
00314 }
00315 }
00316
00317
00318
00319
00320
00321
00322 #define BUF 256
00323
00324 if( afni_mode == AFNI_CONTINUE_MODE ){
00325 static char kbuf[BUF] ;
00326 static int nkey=0 ;
00327 char afni_buf[BUF] ;
00328 int id ;
00329
00330 if( need_setup_key ){ setup_key(); need_setup_key = 0; }
00331
00332
00333
00334 ii = iochan_readcheck( afni_ioc , 0 ) ;
00335
00336
00337
00338
00339
00340 if( ii < 0 ){
00341
00342 fprintf(stderr,"\nAFNI data channel aborted!\a\n") ;
00343 IOCHAN_CLOSE(afni_ioc) ;
00344 afni_mode = 0 ;
00345 return -1 ;
00346
00347 } else if( ii > 0 ){
00348
00349 ii = iochan_recv( afni_ioc , afni_buf , BUF ) ;
00350
00351 if( ii <= 0 ){
00352 fprintf(stderr,"\nAFNI data channel recv failed!\a\n") ;
00353 IOCHAN_CLOSE(afni_ioc) ;
00354 afni_mode = 0 ;
00355 return -1 ;
00356 }
00357
00358
00359
00360
00361 #if 1
00362 printf("AFNI sent: %s\n",afni_buf) ;
00363 #else
00364 ii = sscanf( afni_buf , "SURFID %d" , &id ) ;
00365 if( ii < 1 ){
00366 fprintf(stderr,"AFNI sent bad data: %s\a\n",afni_buf) ;
00367 } else {
00368 fprintf(stderr,"AFNI sent id: %d\n",id) ;
00369 }
00370 #endif
00371
00372
00373
00374
00375 printf("\rEnter node ID: "); fflush(stdout); nkey = 0;
00376
00377 }
00378
00379
00380
00381 while( got_key(kbuf+nkey) ){
00382
00383 if( isdigit(kbuf[nkey]) ){
00384
00385 printf("%c",kbuf[nkey++]); fflush(stdout);
00386
00387 } else if( nkey > 0 && kbuf[nkey] == '\n' ){
00388
00389 printf("\n"); fflush(stdout); kbuf[nkey++]='\0'; break;
00390
00391 }
00392
00393 if( nkey == BUF ){
00394 fprintf(stderr,"\nBad boy.\n\a"); exit(1);
00395 }
00396 }
00397
00398
00399
00400
00401 if( nkey == 0 || kbuf[nkey-1] != '\0' ) return 0 ;
00402
00403 id = strtol( kbuf , NULL , 10 ) ;
00404 sprintf(afni_buf,"SURFID %d",id) ;
00405 ii = iochan_sendall( afni_ioc , afni_buf , strlen(afni_buf)+1 ) ;
00406
00407 if( ii < 0 ){
00408 fprintf(stderr,"AFNI data channel aborted!\a\n") ;
00409 IOCHAN_CLOSE(afni_ioc) ;
00410 afni_mode = 0 ;
00411 return -1 ;
00412 }
00413
00414 printf("\rEnter node ID: "); fflush(stdout); nkey = 0;
00415 return 0 ;
00416 }
00417
00418 return 0 ;
00419 }
00420
00421
00422
00423 #include <errno.h>
00424 #include <stdlib.h>
00425 #include <strings.h>
00426 #include <curses.h>
00427
00428
00429
00430 void setup_key(void)
00431 {
00432 stdscr = initscr();
00433 noecho();
00434 cbreak();
00435 nodelay(stdscr, TRUE);
00436 }
00437
00438
00439
00440 void restore_key(void)
00441 {
00442 nodelay(stdscr, FALSE);
00443 nocbreak();
00444 echo();
00445 endwin();
00446 }
00447
00448
00449
00450
00451
00452
00453 int got_key(char *char_pressed)
00454 {
00455 int return_code = 0;
00456 *char_pressed = getch();
00457 if (*char_pressed != ERR) return_code = TRUE;
00458 return(return_code);
00459 }