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