Doxygen Source Code Documentation
        
Main Page   Alphabetical List   Data Structures   File List   Data Fields   Globals   Search   
plugout_graph.c
Go to the documentation of this file.00001 
00002 
00003 
00004 
00005 
00006 
00007 #include "thd_iochan.h"
00008 #include <math.h>
00009 
00010 
00011 
00012 
00013 static char afni_host[128] = "." ;
00014 static char afni_name[128] = "\0" ;
00015 static int  afni_port      = 8777 ;
00016 static int  afni_verbose   = 0 ;  
00017 
00018 
00019 
00020 int afni_io(void) ;
00021 
00022 
00023 
00024 
00025 
00026 
00027 
00028 int main( int argc , char * argv[] )
00029 {
00030    int narg , ii ;
00031 
00032    
00033 
00034    if( argc == 2 && strncmp(argv[1],"-help",5) == 0 ){
00035       printf("Usage: plugout_graph [-host name] [-v]\n"
00036              "This program connects to AFNI and sends graphing commands.\n"
00037              "Options:\n"
00038              "  -host name  Means to connect to AFNI running on the\n"
00039              "                computer 'name' using TCP/IP.  The default is to\n"
00040              "                connect on the current host using shared memory.\n"
00041              "  -v          Verbose mode.\n"
00042              "  -port pp    Use TCP/IP port number 'pp'.  The default is\n"
00043              "                8099, but if two plugouts are running on the\n"
00044              "                same computer, they must use different ports.\n"
00045              "  -name sss   Use the string 'sss' for the name that AFNI assigns\n"
00046              "                to this plugout.  The default is something stupid.\n"
00047             ) ;
00048       exit(0) ;
00049    }
00050 
00051    
00052 
00053    narg = 1 ;
00054    while( narg < argc ){
00055 
00056 
00057 
00058       if( strncmp(argv[narg],"-host",5) == 0 ){
00059          narg++ ;
00060          if( narg >= argc ){
00061             fprintf(stderr,"** -host needs a following name!\a\n"); exit(1);
00062          }
00063          strcpy( afni_host , argv[narg] ) ;
00064          narg++ ; continue ;
00065       }
00066 
00067 
00068 
00069       if( strncmp(argv[narg],"-name",5) == 0 ){
00070          narg++ ;
00071          if( narg >= argc ){
00072             fprintf(stderr,"** -name needs a following string!\a\n"); exit(1);
00073          }
00074          strcpy( afni_name , argv[narg] ) ;
00075          narg++ ; continue ;
00076       }
00077 
00078 
00079 
00080       if( strncmp(argv[narg],"-v",2) == 0 ){
00081          afni_verbose = 1 ;
00082          narg++ ; continue ;
00083       }
00084 
00085 
00086 
00087       if( strncmp(argv[narg],"-port",4) == 0 ){
00088          narg++ ;
00089          if( narg >= argc ){
00090             fprintf(stderr,"** -port needs a following argument!\a\n"); exit(1);
00091          }
00092          afni_port = strtol( argv[narg] , NULL , 10 ) ;
00093          if( afni_port <= 1024 ){
00094             fprintf(stderr,"** -port needs an argument > 1024!\a\n"); exit(1);
00095          }
00096          if( strcmp(afni_host,".") == 0 ) strcpy(afni_host,"localhost") ;
00097          narg++ ; continue ;
00098       }
00099 
00100 
00101 
00102       fprintf(stderr,"** Unrecognized option: %s\a\n",argv[narg]) ;
00103       exit(1) ;
00104    }
00105 
00106    
00107 
00108    while( 1 ){
00109       ii = afni_io() ;        
00110       if( ii < 0 ) exit(0) ;  
00111       iochan_sleep(33) ;      
00112    }
00113 
00114 }
00115 
00116 
00117 
00118 
00119 
00120 
00121 
00122 
00123 
00124 
00125 
00126 
00127 
00128 
00129 
00130 
00131 
00132 
00133 
00134 
00135 
00136 
00137 
00138 
00139 #define AFNI_OPEN_CONTROL_MODE  1  
00140 #define AFNI_WAIT_CONTROL_MODE  2  
00141 #define AFNI_OPEN_DATA_MODE     3  
00142 #define AFNI_WAIT_DATA_MODE     4  
00143 #define AFNI_CONTINUE_MODE      5  
00144 
00145 
00146 
00147 #define POACKSIZE       4  
00148 
00149 #define PO_ACK_BAD(ic)  iochan_sendall( (ic) , "BAD" , POACKSIZE )
00150 #define PO_ACK_OK(ic)   iochan_sendall( (ic) , "OK!" , POACKSIZE )
00151 #define PO_SEND(ic,str) iochan_sendall( (ic) , (str) , strlen((str))+1 )
00152 
00153 int afni_io(void)
00154 {
00155    static int afni_mode = AFNI_OPEN_CONTROL_MODE ;  
00156    static IOCHAN * afni_ioc = NULL ;                
00157    int ii ;
00158 
00159    
00160    
00161    
00162 
00163    if( afni_mode <= 0 ) return -1 ;
00164 
00165    
00166    
00167 
00168    if( afni_mode == AFNI_OPEN_CONTROL_MODE ){
00169       char afni_iocname[128] ;           
00170 
00171 
00172 
00173 
00174       if( strcmp(afni_host,".") == 0 )
00175          sprintf( afni_iocname , "tcp:%s:7957" , "localhost" ); 
00176       else
00177          sprintf( afni_iocname , "tcp:%s:7957" , afni_host ) ;  
00178 
00179       afni_ioc = iochan_init( afni_iocname , "create" ) ;    
00180       if( afni_ioc == NULL ){
00181          fprintf(stderr,
00182                  "** Can't create control channel %s to AFNI!\n",afni_iocname) ;
00183          afni_mode = 0 ;
00184          return -1 ;
00185       }
00186       afni_mode = AFNI_WAIT_CONTROL_MODE ; 
00187       if( afni_verbose )
00188          fprintf(stderr,"++ AFNI control channel created\n") ;
00189    }
00190 
00191    
00192    
00193 
00194    if( afni_mode == AFNI_WAIT_CONTROL_MODE ){
00195       ii = iochan_writecheck( afni_ioc , 5 ) ;     
00196 
00197 
00198 
00199 
00200 
00201 
00202       if( ii < 0 ){
00203          fprintf(stderr,"** Control channel to AFNI failed!\a\n") ;
00204          iochan_set_cutoff(afni_ioc) ; IOCHAN_CLOSE(afni_ioc) ;
00205          afni_mode = 0 ;
00206          return -1 ;
00207       } else if( ii > 0 ){
00208          afni_mode = AFNI_OPEN_DATA_MODE ;        
00209          if( afni_verbose )
00210             fprintf(stderr,"++ AFNI control channel connected\n");
00211       } else {
00212          return 0 ;                                
00213       }
00214    }
00215 
00216    
00217    
00218 
00219    if( afni_mode == AFNI_OPEN_DATA_MODE ){
00220       char afni_iocname[128] ;
00221       char afni_buf[256] ;
00222 
00223 
00224 
00225 
00226 
00227 
00228 
00229 
00230 
00231 
00232       if( strcmp(afni_host,".") == 0 )
00233          strcpy( afni_iocname , "shm:test_plugout:1K+1K" ) ;
00234       else
00235          sprintf( afni_iocname , "tcp:%s:%d" , afni_host , afni_port ) ;
00236 
00237 
00238 
00239 
00240 
00241 
00242 
00243 
00244 
00245       if( afni_name[0] == '\0' ) strcpy(afni_name,"TambourineMan") ;
00246 
00247       sprintf( afni_buf , "PONAME %s\n"
00248                           "IOCHAN %s\n"
00249                           "NO_ACK"      ,
00250                afni_name , afni_iocname ) ;
00251 
00252       if( afni_verbose )
00253          fprintf(stderr,"++ Sending control information to AFNI\n") ;
00254 
00255 
00256 
00257       ii = iochan_sendall( afni_ioc , afni_buf , strlen(afni_buf)+1 ) ;
00258 
00259 
00260 
00261 
00262       if( ii < 0 ){
00263          fprintf(stderr,"** Transmission of control data to AFNI failed!\a\n") ;
00264          iochan_set_cutoff(afni_ioc) ; IOCHAN_CLOSE(afni_ioc) ;
00265          afni_mode = 0 ;
00266          return -1 ;
00267 
00268       } else {
00269 
00270 
00271 
00272          iochan_set_cutoff(afni_ioc) ; IOCHAN_CLOSE(afni_ioc) ;
00273 
00274 
00275 
00276          afni_ioc = iochan_init( afni_iocname , "create" ) ;
00277          if( afni_ioc == NULL ){
00278             fprintf(stderr,
00279                     "** Can't open data channel %s to AFNI!\a\n",afni_iocname) ;
00280             afni_mode = 0 ;
00281             return -1 ;
00282          } else {
00283             afni_mode = AFNI_WAIT_DATA_MODE ;
00284             if( afni_verbose ) fprintf(stderr,"++ AFNI data channel created\n") ;
00285          }
00286       }
00287    }
00288 
00289    
00290    
00291 
00292    if( afni_mode == AFNI_WAIT_DATA_MODE ){
00293 
00294       ii = iochan_goodcheck( afni_ioc , 5 ) ;  
00295       if( ii < 0 ){
00296          fprintf(stderr,
00297                  "** AFNI data channel aborted before any data was sent!\a\n") ;
00298          iochan_set_cutoff(afni_ioc) ;IOCHAN_CLOSE(afni_ioc) ;
00299          afni_mode = 0 ;
00300          return -1 ;
00301       } else if( ii > 0 ){                     
00302          afni_mode = AFNI_CONTINUE_MODE ;
00303          if( afni_verbose ) fprintf(stderr,"++ AFNI data channel is open\n") ;
00304       } else {
00305          return 0 ;                            
00306       }
00307    }
00308 
00309    
00310    
00311    
00312 
00313    if( afni_mode == AFNI_CONTINUE_MODE ){
00314       static int ncom=-1 , nn ;
00315       static double f=2.0 ;
00316       double y1,y2,y3 , t ;
00317       char afni_buf[256];
00318 
00319       switch( ncom ){
00320 
00321          case -1:   
00322             strcpy(afni_buf,"DRIVE_AFNI OPEN_GRAPH_XY bob '' 0 80 '' 3 -1.5 1.5") ;
00323             srand48((long)time(NULL)) ;
00324             ncom++ ;
00325          break ;
00326 
00327          case 801:
00328             strcpy(afni_buf,"DRIVE_AFNI CLEAR_GRAPH_XY bob") ;
00329             ncom = 0 ;
00330          break ;
00331 
00332          default:
00333             t  = 0.1*ncom ; ncom++ ;
00334             y1 = cos(t) ;
00335             y2 = cos(f*t) ;
00336             y3 = y2+0.2*sin(t/7.0)+0.2*(drand48()-0.5) ;
00337             sprintf(afni_buf,"DRIVE_AFNI ADDTO_GRAPH_XY bob %g %g %g %g",t,y1,y2,y3) ;
00338 #if 1
00339             f += 0.01*(drand48()-0.5) ;
00340 #endif
00341          break ;
00342       }
00343 
00344       
00345 
00346       nn = strlen(afni_buf)+1 ;
00347       ii = iochan_sendall( afni_ioc , afni_buf , nn ) ;
00348 
00349       if( ii < 0 ){   
00350          char *sss ;
00351          fprintf(stderr,"** AFNI data channel failed!\a\n") ;
00352          sss = iochan_error_string() ;
00353          if( sss != NULL ) fprintf(stderr,"** %s\n",sss) ;
00354          iochan_set_cutoff(afni_ioc) ; IOCHAN_CLOSE(afni_ioc) ;
00355          afni_mode = 0 ;
00356          return -1 ;
00357       } else if( ii < nn ){
00358          char *sss ;
00359          fprintf(stderr,"++ AFNI data channel full - can't send data!\n") ;
00360          sss = iochan_error_string() ;
00361          if( sss != NULL ) fprintf(stderr,"++ %s\n",sss) ;
00362       }
00363       return 0 ;
00364    }
00365 
00366    return 0 ;
00367 }