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 
00003 
00004 
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)                   
00015 {                                      
00016    iochan_close(AFNI_ioc) ;
00017    return ;
00018 }
00019 
00020 
00021 
00022 
00023 
00024 
00025 
00026 
00027 
00028 
00029 
00030 
00031 
00032 
00033 
00034 
00035 
00036 
00037 
00038 
00039 
00040 
00041 void AFNI_start_io( int nim )
00042 {
00043    int ii , jj ;
00044 
00045    
00046 
00047    if( AFNI_mode <= 0 || AFNI_mode == AFNI_CONTINUE_MODE ) return ;
00048 
00049    
00050 
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 ;                       
00064          return ;
00065       } else {
00066          if( AFNI_verbose ) fprintf(stderr,"Entering AFNI_WAIT_CONTROL_MODE.\n") ;
00067          AFNI_mode = AFNI_WAIT_CONTROL_MODE ;  
00068       }
00069    }
00070 
00071    
00072 
00073    if( AFNI_mode == AFNI_WAIT_CONTROL_MODE ){
00074 
00075       ii = iochan_writecheck( AFNI_ioc , 1 ) ;  
00076 
00077 
00078 
00079 
00080       if( ii < 0 ){
00081          fprintf(stderr,"Control channel to AFNI failed!\a\n") ;
00082          IOCHAN_CLOSE(AFNI_ioc) ;
00083          AFNI_mode = 0 ;                    
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 ;  
00089       }
00090    }
00091 
00092    
00093 
00094 
00095 
00096    if( AFNI_mode == AFNI_OPEN_DATA_MODE ){
00097 
00098       
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) ;     
00104       if( AFNI_infocom[0] != '\0' ){
00105          strcat(AFNI_buf,"\n") ;
00106          strcat(AFNI_buf,AFNI_infocom) ; 
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 
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) ) 
00123             iochan_sleep(2) ;
00124          IOCHAN_CLOSE(AFNI_ioc) ;                 
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" ) ; 
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    
00142 
00143 
00144    if( AFNI_mode == AFNI_CATCHUP_MODE ){
00145 
00146       ii = iochan_writecheck( AFNI_ioc , 1 ) ;  
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 ){                      
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          
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 
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;  
00181    else                    soff = 0;
00182 
00183 
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 
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 }