Skip to content

AFNI/NIfTI Server

Sections
Personal tools
You are here: Home » AFNI » Documentation

Doxygen Source Code Documentation


Main Page   Alphabetical List   Data Structures   File List   Data Fields   Globals   Search  

plugout_ijk.c File Reference

#include "thd_iochan.h"

Go to the source code of this file.


Defines

#define AFNI_OPEN_CONTROL_MODE   1
#define AFNI_WAIT_CONTROL_MODE   2
#define AFNI_OPEN_DATA_MODE   3
#define AFNI_WAIT_DATA_MODE   4
#define AFNI_CONTINUE_MODE   5
#define POACKSIZE   4
#define PO_ACK_BAD(ic)   iochan_sendall( (ic) , "BAD" , POACKSIZE )
#define PO_ACK_OK(ic)   iochan_sendall( (ic) , "OK!" , POACKSIZE )
#define PO_SEND(ic, str)   iochan_sendall( (ic) , (str) , strlen((str))+1 )

Functions

int afni_io (void)
int main (int argc, char *argv[])

Variables

char afni_host [128] = "."
char afni_name [128] = "\0"
int afni_port = 8009
int afni_verbose = 0

Define Documentation

#define AFNI_CONTINUE_MODE   5
 

Definition at line 150 of file plugout_ijk.c.

Referenced by afni_io().

#define AFNI_OPEN_CONTROL_MODE   1
 

Je ne sais pas *

Definition at line 146 of file plugout_ijk.c.

Referenced by afni_io().

#define AFNI_OPEN_DATA_MODE   3
 

Definition at line 148 of file plugout_ijk.c.

Referenced by afni_io().

#define AFNI_WAIT_CONTROL_MODE   2
 

Definition at line 147 of file plugout_ijk.c.

Referenced by afni_io().

#define AFNI_WAIT_DATA_MODE   4
 

Definition at line 149 of file plugout_ijk.c.

Referenced by afni_io().

#define PO_ACK_BAD ic       iochan_sendall( (ic) , "BAD" , POACKSIZE )
 

Definition at line 156 of file plugout_ijk.c.

#define PO_ACK_OK ic       iochan_sendall( (ic) , "OK!" , POACKSIZE )
 

Definition at line 157 of file plugout_ijk.c.

#define PO_SEND ic,
str       iochan_sendall( (ic) , (str) , strlen((str))+1 )
 

Definition at line 158 of file plugout_ijk.c.

#define POACKSIZE   4
 

Definition at line 154 of file plugout_ijk.c.

Referenced by afni_io().


Function Documentation

int afni_io void   
 

Definition at line 160 of file plugout_ijk.c.

References AFNI_CONTINUE_MODE, afni_host, afni_name, AFNI_OPEN_CONTROL_MODE, AFNI_OPEN_DATA_MODE, afni_port, AFNI_WAIT_CONTROL_MODE, AFNI_WAIT_DATA_MODE, IOCHAN_CLOSE, iochan_goodcheck(), iochan_init(), iochan_recvall(), iochan_sendall(), iochan_sleep(), iochan_writecheck(), and POACKSIZE.

00161 {
00162    static int afni_mode = AFNI_OPEN_CONTROL_MODE ;  /* status variable */
00163    static IOCHAN * afni_ioc = NULL ;                /* connection to AFNI */
00164    int ii ;
00165 
00166    /***************************************************************/
00167    /***** Check to see if status is OK before we proceed.     *****/
00168    /***** (if an error occurs below, afni_mode gets set to 0) *****/
00169 
00170    if( afni_mode <= 0 ) return -1 ;
00171 
00172    /***********************************************************************/
00173    /***** First time into this routine?  Open control channel to AFNI *****/
00174 
00175    if( afni_mode == AFNI_OPEN_CONTROL_MODE ){
00176       char afni_iocname[128] ;           /* will hold name of I/O channel */
00177 
00178       /** Note that the control channel is always a
00179           TCP/IP channel to port # 7955 on the AFNI host system **/
00180 
00181       if( strcmp(afni_host,".") == 0 )
00182          sprintf( afni_iocname , "tcp:%s:7955" , "localhost" ); /* make name */
00183       else
00184          sprintf( afni_iocname , "tcp:%s:7955" , afni_host ) ;  /* make name */
00185 
00186       afni_ioc = iochan_init( afni_iocname , "create" ) ;    /* create it */
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 ; /* waiting for AFNI connection */
00194       if( afni_verbose )
00195          fprintf(stderr,"AFNI control channel created\n") ;
00196    }
00197 
00198    /****************************************************/
00199    /**** Check if AFNI control channel is connected ****/
00200 
00201    if( afni_mode == AFNI_WAIT_CONTROL_MODE ){
00202       ii = iochan_writecheck( afni_ioc , 5 ) ;     /* wait at most 5 msec */
00203 
00204       /** the iochan_*check() routines return
00205              -1 for an error,
00206               0 if not ready,
00207              >0 if the I/O channel is ready. **/
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 ;        /* prepare to send data */
00216          if( afni_verbose )
00217             fprintf(stderr,"AFNI control channel connected\n");
00218       } else {
00219          return 0 ;                                /* try again next time */
00220       }
00221    }
00222 
00223    /**********************************************************/
00224    /**** Send control data to AFNI, and open data channel ****/
00225 
00226    if( afni_mode == AFNI_OPEN_DATA_MODE ){
00227       char afni_iocname[128] ;
00228       char afni_buf[256] ;
00229 
00230       /** decide name of data channel:
00231             use shared memory (shm:) on ".",
00232             use TCP/IP (tcp:) on other computer systems;
00233         * Note that the TCP/IP port number can be
00234            anything that isn't already in use;
00235         * Note that the shm control name (here "test_plugout")
00236            is a string that will be converted to an IPC
00237            key (in function string_to_key in iochan.c).       **/
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       /** write the command to AFNI into the buffer:
00245             * each command ends with a newline character,
00246                 except (possibly) the last command;
00247             * the command buffer is a C string, which ends
00248                 with an ASCII NUL character;
00249             * PONAME means 'use this string for informative messages';
00250             * IOCHAN means 'use this I/O channel from now on'. **/
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       /** note that the ASCII NUL at the end of the buffer is sent **/
00262 
00263       ii = iochan_sendall( afni_ioc , afni_buf , strlen(afni_buf)+1 ) ;
00264 
00265       /** the return value is the number of bytes sent,
00266           or -1 indicating a fatal error transpired.    **/
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          /** wait for the acknowledgment from AFNI, then close channel **/
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          /** now open data channel to AFNI **/
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    /***** See if data channel is connected to AFNI *****/
00304 
00305    if( afni_mode == AFNI_WAIT_DATA_MODE ){
00306 
00307       ii = iochan_goodcheck( afni_ioc , 5 ) ;  /* wait at most 5 msec */
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 ){                     /* ready to go! */
00315          afni_mode = AFNI_CONTINUE_MODE ;
00316          if( afni_verbose ) fprintf(stderr,"AFNI data channel is open\n") ;
00317       } else {
00318          return 0 ;                            /* try again next time */
00319       }
00320    }
00321 
00322    /**************************************************************/
00323    /***** The "normal" state of affairs:  AFNI is connected. *****/
00324    /***** See if the user wants to send i,j,k to AFNI.       *****/
00325 
00326    if( afni_mode == AFNI_CONTINUE_MODE ){
00327       char afni_buf[256] ;
00328       int   ix , jy , kz ;
00329 
00330       /* get user input */
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       /* send input to AFNI (essentially unedited) */
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 ){  /* send was OK; wait for acknowledgment */
00347          ii = iochan_recvall( afni_ioc , afni_buf , POACKSIZE ) ;
00348       }
00349 
00350       if( ii < 0 ){   /* send or acknowledgment failed */
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 }

int main int    argc,
char *    argv[]
 

\** File : SUMA.c

Author:
: Ziad Saad Date : Thu Dec 27 16:21:01 EST 2001
Purpose :

Input paramters :

Parameters:
param  Usage : SUMA ( )
Returns :
Returns:
Support :
See also:
OpenGL prog. Guide 3rd edition , varray.c from book's sample code
Side effects :

Definition at line 34 of file plugout_ijk.c.

References afni_host, afni_io(), afni_name, afni_port, afni_verbose, argc, and iochan_sleep().

00035 {
00036    int narg , ii ;
00037 
00038    /***** See if the pitiful user wants help *****/
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    /***** Process command line options *****/
00059 
00060    narg = 1 ;
00061    while( narg < argc ){
00062 
00063       /** -host name **/
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       /** -name sss **/
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       /** -v **/
00086 
00087       if( strncmp(argv[narg],"-v",2) == 0 ){
00088          afni_verbose = 1 ;
00089          narg++ ; continue ;
00090       }
00091 
00092       /** -port pp **/
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       /** Je ne sais pas **/
00108 
00109       fprintf(stderr,"Unrecognized option: %s\a\n",argv[narg]) ;
00110       exit(1) ;
00111    }
00112 
00113    /***** Loop and check in with AFNI every 100 msec *****/
00114 
00115    while( 1 ){
00116       ii = afni_io() ;        /* commune with AFNI  */
00117       if( ii < 0 ) exit(0) ;  /* bad trip? then die */
00118       iochan_sleep(100) ;     /* perchance to dream */
00119    }
00120 
00121 }

Variable Documentation

char afni_host[128] = "." [static]
 

Definition at line 19 of file plugout_ijk.c.

Referenced by afni_io(), and main().

char afni_name[128] = "\0" [static]
 

Definition at line 20 of file plugout_ijk.c.

Referenced by afni_io(), and main().

int afni_port = 8009 [static]
 

Definition at line 21 of file plugout_ijk.c.

Referenced by afni_io(), and main().

int afni_verbose = 0 [static]
 

Definition at line 22 of file plugout_ijk.c.

Referenced by main().

 

Powered by Plone

This site conforms to the following standards: