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_drive.c File Reference

#include "thd_iochan.h"

Go to the source code of this file.


Defines

#define COM_LENGTH   1000
#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 = 8099
int afni_verbose = 0
int DontWait = 0
int N_com = 0
int I_com = 0
char * com [1024]

Define Documentation

#define AFNI_CONTINUE_MODE   5
 

Definition at line 230 of file plugout_drive.c.

Referenced by afni_io().

#define AFNI_OPEN_CONTROL_MODE   1
 

Je ne sais pas *

Definition at line 226 of file plugout_drive.c.

Referenced by afni_io().

#define AFNI_OPEN_DATA_MODE   3
 

Definition at line 228 of file plugout_drive.c.

Referenced by afni_io().

#define AFNI_WAIT_CONTROL_MODE   2
 

Definition at line 227 of file plugout_drive.c.

Referenced by afni_io().

#define AFNI_WAIT_DATA_MODE   4
 

Definition at line 229 of file plugout_drive.c.

Referenced by afni_io().

#define COM_LENGTH   1000
 

Definition at line 34 of file plugout_drive.c.

Referenced by afni_io(), and main().

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

Definition at line 236 of file plugout_drive.c.

Referenced by afni_io(), AFNI_plugout_workproc(), and AFNI_process_plugout().

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

Definition at line 237 of file plugout_drive.c.

Referenced by afni_io(), AFNI_plugout_workproc(), and AFNI_process_plugout().

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

Definition at line 238 of file plugout_drive.c.

Referenced by AFNI_process_plugout().

#define POACKSIZE   4
 

Definition at line 234 of file plugout_drive.c.

Referenced by afni_io().


Function Documentation

int afni_io void   
 

Definition at line 241 of file plugout_drive.c.

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

Referenced by main().

00242 {
00243    static int afni_mode = AFNI_OPEN_CONTROL_MODE ;  /* status variable */
00244    static IOCHAN * afni_ioc = NULL ;                /* connection to AFNI */
00245    int ii ;
00246 
00247    /***************************************************************/
00248    /***** Check to see if status is OK before we proceed.     *****/
00249    /***** (if an error occurs below, afni_mode gets set to 0) *****/
00250 
00251    if( afni_mode <= 0 ) return -1 ;
00252 
00253    /***********************************************************************/
00254    /***** First time into this routine?  Open control channel to AFNI *****/
00255 
00256    if( afni_mode == AFNI_OPEN_CONTROL_MODE ){
00257       char afni_iocname[128] ;           /* will hold name of I/O channel */
00258 
00259       /** Note that the control channel is always a
00260           TCP/IP channel to port # 7955 on the AFNI host system **/
00261 
00262       if( strcmp(afni_host,".") == 0 )
00263          sprintf( afni_iocname , "tcp:%s:7955" , "localhost" ); /* make name */
00264       else
00265          sprintf( afni_iocname , "tcp:%s:7955" , afni_host ) ;  /* make name */
00266 
00267       afni_ioc = iochan_init( afni_iocname , "create" ) ;    /* create it */
00268       if( afni_ioc == NULL ){
00269          fprintf(stderr,
00270                  "** Can't create control channel %s to AFNI!\n",afni_iocname) ;
00271          afni_mode = 0 ;
00272          return -1 ;
00273       }
00274       afni_mode = AFNI_WAIT_CONTROL_MODE ; /* waiting for AFNI connection */
00275       if( afni_verbose )
00276          fprintf(stderr,"++ AFNI control channel created\n") ;
00277    }
00278 
00279    /****************************************************/
00280    /**** Check if AFNI control channel is connected ****/
00281 
00282    if( afni_mode == AFNI_WAIT_CONTROL_MODE ){
00283       ii = iochan_writecheck( afni_ioc , 5 ) ;     /* wait at most 5 msec */
00284 
00285       /** the iochan_*check() routines return
00286              -1 for an error,
00287               0 if not ready,
00288              >0 if the I/O channel is ready. **/
00289 
00290       if( ii < 0 ){
00291          fprintf(stderr,"** Control channel to AFNI failed!\a\n") ;
00292          IOCHAN_CLOSE(afni_ioc) ;
00293          afni_mode = 0 ;
00294          return -1 ;
00295       } else if( ii > 0 ){
00296          afni_mode = AFNI_OPEN_DATA_MODE ;        /* prepare to send data */
00297          if( afni_verbose )
00298             fprintf(stderr,"++ AFNI control channel connected\n");
00299       } else {
00300          return 0 ;                                /* try again next time */
00301       }
00302    }
00303 
00304    /**********************************************************/
00305    /**** Send control data to AFNI, and open data channel ****/
00306 
00307    if( afni_mode == AFNI_OPEN_DATA_MODE ){
00308       char afni_iocname[128] ;
00309       char afni_buf[256] ;
00310 
00311       /** decide name of data channel:
00312             use shared memory (shm:) on ".",
00313             use TCP/IP (tcp:) on other computer systems;
00314         * Note that the TCP/IP port number can be
00315            anything that isn't already in use;
00316         * Note that the shm control name (here "test_plugout")
00317            is a string that will be converted to an IPC
00318            key (in function string_to_key in iochan.c).       **/
00319 
00320       if( strcmp(afni_host,".") == 0 )
00321          strcpy( afni_iocname , "shm:test_plugout:1K+1K" ) ;
00322       else
00323          sprintf( afni_iocname , "tcp:%s:%d" , afni_host , afni_port ) ;
00324 
00325       /** write the command to AFNI into the buffer:
00326             * each command ends with a newline character,
00327                 except (possibly) the last command;
00328             * the command buffer is a C string, which ends
00329                 with an ASCII NUL character;
00330             * PONAME means 'use this string for informative messages';
00331             * IOCHAN means 'use this I/O channel from now on'. **/
00332 
00333       if( afni_name[0] == '\0' ) strcpy(afni_name,"ManOfLaMancha") ;
00334 
00335       sprintf( afni_buf , "PONAME %s\n"
00336                           "IOCHAN %s" ,
00337                afni_name , afni_iocname ) ;
00338 
00339       if( afni_verbose )
00340          fprintf(stderr,"++ Sending control information to AFNI\n") ;
00341 
00342       /** note that the ASCII NUL at the end of the buffer is sent **/
00343 
00344       ii = iochan_sendall( afni_ioc , afni_buf , strlen(afni_buf)+1 ) ;
00345 
00346       /** the return value is the number of bytes sent,
00347           or -1 indicating a fatal error transpired.    **/
00348 
00349       if( ii < 0 ){
00350          fprintf(stderr,"** Transmission of control data to AFNI failed!\a\n") ;
00351          IOCHAN_CLOSE(afni_ioc) ;
00352          afni_mode = 0 ;
00353          return -1 ;
00354 
00355       } else {
00356 
00357          /** wait for the acknowledgment from AFNI, then close channel **/
00358 
00359          ii = iochan_recvall( afni_ioc , afni_buf , POACKSIZE ) ;
00360          IOCHAN_CLOSE(afni_ioc) ;
00361 
00362          if( ii < 0 || strncmp(afni_buf,"OK!",3) != 0 ){
00363             fprintf(stderr,"** AFNI didn't like control information!\a\n") ;
00364             afni_mode = 0 ;
00365             return -1 ;
00366          }
00367 
00368          /** now open data channel to AFNI **/
00369 
00370          afni_ioc = iochan_init( afni_iocname , "create" ) ;
00371          if( afni_ioc == NULL ){
00372             fprintf(stderr,
00373                     "** Can't open data channel %s to AFNI!\a\n",afni_iocname) ;
00374             afni_mode = 0 ;
00375             return -1 ;
00376          } else {
00377             afni_mode = AFNI_WAIT_DATA_MODE ;
00378             if( afni_verbose ) fprintf(stderr,"++ AFNI data channel created\n") ;
00379          }
00380       }
00381    }
00382 
00383    /****************************************************/
00384    /***** See if data channel is connected to AFNI *****/
00385 
00386    if( afni_mode == AFNI_WAIT_DATA_MODE ){
00387 
00388       ii = iochan_goodcheck( afni_ioc , 5 ) ;  /* wait at most 5 msec */
00389       if( ii < 0 ){
00390          fprintf(stderr,
00391                  "** AFNI data channel aborted before any data was sent!\a\n") ;
00392          IOCHAN_CLOSE( afni_ioc ) ;
00393          afni_mode = 0 ;
00394          return -1 ;
00395       } else if( ii > 0 ){                     /* ready to go! */
00396          afni_mode = AFNI_CONTINUE_MODE ;
00397          if( afni_verbose ) fprintf(stderr,"++ AFNI data channel is open\n") ;
00398       } else {
00399          return 0 ;                            /* try again next time */
00400       }
00401    }
00402 
00403    /**************************************************************/
00404    /***** The "normal" state of affairs:  AFNI is connected. *****/
00405    /***** See if the user wants to drive AFNI.               *****/
00406 
00407    if( afni_mode == AFNI_CONTINUE_MODE ){
00408       char cmd_buf[COM_LENGTH] , afni_buf[COM_LENGTH+56];
00409 
00410       if( I_com < N_com ){                   /* send the I_com'th command */
00411          strcpy(afni_buf, "DRIVE_AFNI ") ;
00412          strcat(afni_buf, com[I_com]   ) ; strcpy(cmd_buf,com[I_com]) ;
00413          I_com++ ;
00414       } else {
00415          if (DontWait) exit(0);
00416          /* get user input */
00417 
00418          printf("Enter command: ") ; fflush(stdout) ; fgets(cmd_buf,COM_LENGTH,stdin) ;
00419 
00420          /* make command to AFNI */
00421 
00422          strcpy(afni_buf,"DRIVE_AFNI ") ;
00423          strcat(afni_buf,cmd_buf) ;
00424       }
00425 
00426       /* send command to AFNI */
00427 
00428       ii = iochan_sendall( afni_ioc , afni_buf , strlen(afni_buf)+1 ) ;
00429 
00430       if( strcmp(cmd_buf,"QUIT") == 0 ){  /* 28 Jul 2005 */
00431         iochan_sleep(222) ; exit(0) ;
00432       }
00433 
00434       if( ii > 0 ){  /* send was OK; wait for acknowledgment */
00435          ii = iochan_recvall( afni_ioc , afni_buf , POACKSIZE ) ;
00436          if( ii > 0 && afni_verbose )
00437            printf("++ AFNI response string: %s\n",afni_buf) ;
00438       }
00439 
00440       if( ii < 0 ){   /* send or acknowledgment failed */
00441          fprintf(stderr,"** AFNI data channel aborted!\a\n") ;
00442          IOCHAN_CLOSE(afni_ioc) ;
00443          afni_mode = 0 ;
00444          return -1 ;
00445       }
00446       return 0 ;
00447    }
00448 
00449    return 0 ;
00450 }

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 57 of file plugout_drive.c.

References afni_host, afni_io(), afni_name, afni_port, afni_verbose, argc, com, COM_LENGTH, DontWait, iochan_sleep(), and N_com.

00058 {
00059    int narg , ii;
00060 
00061    /***** See if the pitiful user wants help *****/
00062 
00063    if( argc == 2 && strncmp(argv[1],"-help",5) == 0 ){
00064       printf("Usage: plugout_drive [-host name] [-v]\n"
00065              "This program connects to AFNI and sends commands\n"
00066              " that the user specifies interactively or on command line\n"
00067              " over to AFNI to be executed.\n"
00068              "\n"
00069              "Options:\n"
00070              "  -host name  Means to connect to AFNI running on the\n"
00071              "                computer 'name' using TCP/IP.  The default is to\n"
00072              "                connect on the current host using shared memory.\n"
00073              "  -v          Verbose mode.\n"
00074              "  -port pp    Use TCP/IP port number 'pp'.  The default is\n"
00075              "                8099, but if two plugouts are running on the\n"
00076              "                same computer, they must use different ports.\n"
00077              "  -name sss   Use the string 'sss' for the name that AFNI assigns\n"
00078              "                to this plugout.  The default is something stupid.\n"
00079              "  -com 'ACTION DATA'  Execute the following command. For example:\n"
00080              "                       -com 'SET_FUNCTION SomeFunction'\n"
00081              "                       will switch AFNI's function (overlay) to\n"
00082              "                       dataset with prefix SomeFunction. \n"
00083              "                      Make sure ACTION and DATA are together enclosed\n"
00084              "                       in one pair of single quotes.\n"
00085              "                      There are numerous actions listed in AFNI's\n"
00086              "                       README.driver file.\n"
00087              "                      You can use the option -com repeatedly. \n"
00088              "  -quit  Quit after you are done with all the -com commands.\n"
00089              "         The default is for the program to wait for more\n"
00090              "          commands to be typed at the terminal's prompt.\n"
00091              "\n"
00092              "NOTE:\n"
00093              "You will need to turn plugouts on in AFNI using one of the\n"
00094              "following methods: \n"
00095              " 1. Including '-yesplugouts' as an option on AFNI's command line\n"
00096              " 2. From AFNI: Define Datamode->Misc->Start Plugouts\n"
00097              " 3. Set environment variable AFNI_YESPLUGOUTS to YES in .afnirc\n"
00098              "Otherwise, AFNI won't be listening for a plugout connection.\n"
00099              "\n"
00100             ) ;
00101       exit(0) ;
00102    }
00103 
00104    /***** Process command line options *****/
00105 
00106    N_com = 0;
00107    DontWait = 0;
00108    narg = 1 ;
00109    while( narg < argc ){
00110 
00111       /** -host name **/
00112 
00113       if( strncmp(argv[narg],"-host",5) == 0 ){
00114          narg++ ;
00115          if( narg >= argc ){
00116             fprintf(stderr,"** -host needs a following name!\a\n"); exit(1);
00117          }
00118          strcpy( afni_host , argv[narg] ) ;
00119          narg++ ; continue ;
00120       }
00121 
00122       /** -name sss **/
00123 
00124       if( strncmp(argv[narg],"-name",5) == 0 ){
00125          narg++ ;
00126          if( narg >= argc ){
00127             fprintf(stderr,"** -name needs a following string!\a\n"); exit(1);
00128          }
00129          strcpy( afni_name , argv[narg] ) ;
00130          narg++ ; continue ;
00131       }
00132 
00133       /** -v **/
00134 
00135       if( strncmp(argv[narg],"-v",2) == 0 ){
00136          afni_verbose = 1 ;
00137          narg++ ; continue ;
00138       }
00139 
00140       /** -port pp **/
00141 
00142       if( strncmp(argv[narg],"-port",4) == 0 ){
00143          narg++ ;
00144          if( narg >= argc ){
00145             fprintf(stderr,"** -port needs a following argument!\a\n"); exit(1);
00146          }
00147          afni_port = strtol( argv[narg] , NULL , 10 ) ;
00148          if( afni_port <= 1024 ){
00149             fprintf(stderr,"** -port needs an argument > 1024!\a\n"); exit(1);
00150          }
00151          if( strcmp(afni_host,".") == 0 ) strcpy(afni_host,"localhost") ;
00152          narg++ ; continue ;
00153       }
00154 
00155       /*** -com 'command this' */
00156       if( strncmp(argv[narg],"-com",4) == 0 ){
00157          narg++ ;
00158          if( narg >= argc ){
00159             fprintf(stderr,"** -com needs a following argument!\a\n"); exit(1);
00160          }
00161 
00162          if (argv[narg] && strlen(argv[narg]) >= COM_LENGTH) {
00163             fprintf(stderr,"** Command length must be smaller than %d characters.\n", COM_LENGTH);
00164          }
00165 
00166          if (N_com < 1024) {
00167             com[N_com] = argv[narg];
00168             ++N_com;
00169          } else {
00170             fprintf(stderr,"** Only 1024 -com options allowed. Are you nuts?\a\n"); exit(1);
00171          }
00172 
00173          narg++ ; continue ;
00174       }
00175 
00176       /*** -quit */
00177       if( strncmp(argv[narg],"-quit",5) == 0 ){
00178          DontWait = 1 ;
00179          narg++ ; continue ;
00180       }
00181 
00182       /** Je ne sais pas **/
00183 
00184       fprintf(stderr,"** Unrecognized option: %s\a\n",argv[narg]) ;
00185       exit(1) ;
00186    }
00187 
00188    if (DontWait && !N_com) {
00189       fprintf(stderr,"** WARNING: -quit option is meaningless without -com option.\n");
00190       DontWait = 0;
00191    }
00192 
00193    /***** Loop and check in with AFNI every 100 msec *****/
00194 
00195    while( 1 ){
00196       ii = afni_io() ;        /* commune with AFNI  */
00197       if( ii < 0 ) exit(0) ;  /* bad trip? then die */
00198       iochan_sleep(100) ;     /* perchance to dream */
00199    }
00200 
00201 }

Variable Documentation

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

Definition at line 39 of file plugout_drive.c.

Referenced by afni_io(), and main().

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

Definition at line 40 of file plugout_drive.c.

Referenced by afni_io(), and main().

int afni_port = 8099 [static]
 

Definition at line 41 of file plugout_drive.c.

Referenced by afni_io(), and main().

int afni_verbose = 0 [static]
 

Definition at line 42 of file plugout_drive.c.

Referenced by afni_io(), and main().

char* com[1024] [static]
 

Definition at line 46 of file plugout_drive.c.

Referenced by afni_io(), and main().

int DontWait = 0 [static]
 

Definition at line 43 of file plugout_drive.c.

Referenced by main().

int I_com = 0 [static]
 

Definition at line 45 of file plugout_drive.c.

Referenced by afni_io().

int N_com = 0 [static]
 

Definition at line 44 of file plugout_drive.c.

Referenced by afni_io(), and main().

 

Powered by Plone

This site conforms to the following standards: