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  

ep_afni.c

Go to the documentation of this file.
00001 /*****************************************************************************
00002    Major portions of this software are copyrighted by the Medical College
00003    of Wisconsin, 1994-2000, and are released under the Gnu General Public
00004    License, Version 2.  See the file README.Copyright for details.
00005 ******************************************************************************/
00006 
00007 #include "def_epi.h"
00008 #include "ep_afni.h"
00009 
00010 EXT struct im_info imX[] ;
00011 
00012 /*****************************************************************************/
00013 
00014 void AFNI_exit(void)                   /* Function to be called to make sure */
00015 {                                      /* the AFNI data channels get closed. */
00016    iochan_close(AFNI_ioc) ;
00017    return ;
00018 }
00019 
00020 /*****************************************************************************
00021   Do I/O startup stuff; nim images are in the imX buffer at this time.
00022 
00023   At any given moment, this routine is in one of a number of modes
00024   (the AFNI_mode variable).
00025   The first time in, AFNI_mode == AFNI_OPEN_CONTROL_MODE.  In each mode,
00026   certain tasks must be accomplished and this program must be synchronized
00027   with AFNI.  When the necessary deeds are done, the routine advances to
00028   the next mode.  If the deeds cannot be done when this routine is called,
00029   then it will stay in the same mode, and the next time it is called it
00030   will try to do them again.  This routine should be called repeatedly
00031   until it progresses to the last mode (AFNI_CONTINUE_MODE), which is for
00032   normal transmission of images (one at a time) to AFNI.  This operation
00033   is handled in the separate routine AFNI_send_image (infra).
00034 
00035   If an error occurs, so that this program can no longer talk to AFNI, then
00036   AFNI_mode is set to 0, which means "do nothing further".  The rest of
00037   the data acquisition software will continue, but these routines will
00038   be stopped dead.
00039 ******************************************************************************/
00040 
00041 void AFNI_start_io( int nim )
00042 {
00043    int ii , jj ;
00044 
00045    /***** Check for illegal conditions *****/
00046 
00047    if( AFNI_mode <= 0 || AFNI_mode == AFNI_CONTINUE_MODE ) return ;
00048 
00049    /***** If we are at the first time in,
00050           try to open a control socket to talk to AFNI *****/
00051 
00052    if( AFNI_mode == AFNI_OPEN_CONTROL_MODE ){
00053 
00054       sprintf( AFNI_iochan , "tcp:%s:%d" , AFNI_host , AFNI_CONTROL_PORT ) ;
00055 
00056       if( AFNI_verbose )
00057          fprintf(stderr,"Opening control channel %s to AFNI.\n",AFNI_iochan) ;
00058 
00059       AFNI_ioc = iochan_init( AFNI_iochan , "w" ) ;
00060 
00061       if( AFNI_ioc == NULL ){
00062          fprintf(stderr,"Can't open control channel %s to AFNI!\a\n",AFNI_iochan) ;
00063          AFNI_mode = 0 ;                       /* disable AFNI */
00064          return ;
00065       } else {
00066          if( AFNI_verbose ) fprintf(stderr,"Entering AFNI_WAIT_CONTROL_MODE.\n") ;
00067          AFNI_mode = AFNI_WAIT_CONTROL_MODE ;  /* begin waiting for AFNI connection */
00068       }
00069    }
00070 
00071    /***** Check if the control socket is connected to AFNI *****/
00072 
00073    if( AFNI_mode == AFNI_WAIT_CONTROL_MODE ){
00074 
00075       ii = iochan_writecheck( AFNI_ioc , 1 ) ;  /* Check; wait at most 1 msec */
00076 
00077       /** if ii == 0, then the channel is still pending,
00078           so do nothing; otherwise, take some action.    **/
00079 
00080       if( ii < 0 ){
00081          fprintf(stderr,"Control channel to AFNI failed!\a\n") ;
00082          IOCHAN_CLOSE(AFNI_ioc) ;
00083          AFNI_mode = 0 ;                    /* disable AFNI */
00084          return ;
00085       } else if( ii > 0 ){
00086          if( AFNI_verbose ) fprintf(stderr,"Control channel connected to AFNI."
00087                                            "  Entering AFNI_OPEN_DATA_MODE.\n") ;
00088          AFNI_mode = AFNI_OPEN_DATA_MODE ;  /* prepare to send data to AFNI */
00089       }
00090    }
00091 
00092    /***** Send the control information, which says
00093           how we will talk to AFNI in the future (shmem or TCP/IP),
00094           then close the control channel and open this new data channel *****/
00095 
00096    if( AFNI_mode == AFNI_OPEN_DATA_MODE ){
00097 
00098       /* decide name of data channel: it can be TCP/IP or shared memory */
00099 
00100       if( AFNI_use_tcp ) sprintf(AFNI_iochan,"tcp:%s:%d",AFNI_host,AFNI_TCP_PORT) ;
00101       else               strcpy(AFNI_iochan,"shm:eps:1M") ;
00102 
00103       strcpy(AFNI_buf,AFNI_iochan) ;     /* tell AFNI where to read data */
00104       if( AFNI_infocom[0] != '\0' ){
00105          strcat(AFNI_buf,"\n") ;
00106          strcat(AFNI_buf,AFNI_infocom) ; /* tell it where to get 3T info */
00107       }
00108 
00109       if( AFNI_verbose )
00110          fprintf(stderr,"Sending control information to AFNI:\n%s\n",AFNI_buf) ;
00111 
00112       ii = iochan_sendall( AFNI_ioc , AFNI_buf , strlen(AFNI_buf)+1 ) ;
00113 
00114       /** A negative return is bad news **/
00115 
00116       if( ii < 0 ){
00117          fprintf(stderr,"Transmission of control data to AFNI failed!\a\n") ;
00118          IOCHAN_CLOSE(AFNI_ioc) ;
00119          AFNI_mode = 0 ;
00120          return ;
00121       } else {
00122          while( ! iochan_clearcheck(AFNI_ioc,2) ) /* wait for control data to clear */
00123             iochan_sleep(2) ;
00124          IOCHAN_CLOSE(AFNI_ioc) ;                 /* close control channel */
00125 
00126          if( AFNI_verbose )
00127             fprintf(stderr,"Opening data channel %s to AFNI.\n",AFNI_iochan) ;
00128 
00129          AFNI_ioc = iochan_init( AFNI_iochan , "w" ) ; /* open data channel */
00130          if( AFNI_ioc == NULL ){
00131             fprintf(stderr,"Can't open data channel %s to AFNI!\a\n",AFNI_iochan) ;
00132             AFNI_mode = 0 ;
00133             return ;
00134          } else {
00135             if( AFNI_verbose ) fprintf(stderr,"Entering AFNI_CATCHUP_MODE.\n") ;
00136             AFNI_mode = AFNI_CATCHUP_MODE ;
00137          }
00138       }
00139    }
00140 
00141    /***** Wait for the data channel to be connected to AFNI,
00142           and then send any images that are reconstructed and ready to go *****/
00143 
00144    if( AFNI_mode == AFNI_CATCHUP_MODE ){
00145 
00146       ii = iochan_writecheck( AFNI_ioc , 1 ) ;  /* wait at most 1 msec */
00147       if( ii < 0 ){
00148          fprintf(stderr,"AFNI data channel aborted before any data was sent!\a\n") ;
00149          IOCHAN_CLOSE( AFNI_ioc ) ;
00150          AFNI_mode = 0 ;
00151          return ;
00152       } else if( ii > 0 ){                      /* can now send data to AFNI! */
00153          if( AFNI_verbose )
00154             fprintf(stderr,"AFNI data channel %s is connected.\n"
00155                            "Entering AFNI_CONTINUE_MODE.\n" , AFNI_iochan) ;
00156          AFNI_mode = AFNI_CONTINUE_MODE ;
00157 
00158          /* if there are any images already accumulated, send them now! */
00159 
00160          if( nim > 0 && AFNI_verbose )
00161             fprintf(stderr,"Playing AFNI catchup with %d images.\n",nim) ;
00162 
00163          for( ii=0 ; ii < nim ; ii++ ) AFNI_send_image( ii ) ;
00164       }
00165    }
00166 
00167    return ;
00168 }
00169 
00170 /*******************************************************************************/
00171 
00172 /** send image in imX[nim] to AFNI **/
00173 
00174 void AFNI_send_image( int nim )
00175 {
00176    int lx = imX[nim].x , ly = imX[nim].y , nbytes = 2*lx*ly , soff , jj ;
00177 
00178    if( AFNI_mode != AFNI_CONTINUE_MODE ) return ;
00179 
00180    if ( (lx*ly) == 65536 ) soff = OFFSET;  /* for Signa */
00181    else                    soff = 0;
00182 
00183    /** before very 1st image, send data type and matrix size **/
00184 
00185    if( nim == 0 ){
00186       if( AFNI_verbose ) fprintf(stderr,"Sending 1st info+image to AFNI.\n") ;
00187       sprintf( AFNI_buf , "DATUM short\nXYMATRIX %d %d\n" , lx,ly ) ;
00188       iochan_sendall( AFNI_ioc , AFNI_buf , strlen(AFNI_buf)+1 ) ;
00189    } else if( AFNI_verbose ){
00190       fprintf(stderr,"Sending image %d to AFNI.\n",nim+1) ;
00191    }
00192 
00193    jj = iochan_sendall( AFNI_ioc , imX[nim].arr + soff , nbytes ) ;
00194 
00195    /** if the data channel failed, stop **/
00196 
00197    if( jj < 0 ){
00198       fprintf(stderr,"Image transmission to AFNI fails at #%d.\a\n",nim) ;
00199       IOCHAN_CLOSE(AFNI_ioc) ;
00200       AFNI_mode = 0 ;
00201    }
00202 
00203    return ;
00204 }
 

Powered by Plone

This site conforms to the following standards: