Doxygen Source Code Documentation
plugout_graph.c File Reference
#include "thd_iochan.h"
#include <math.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 = 8777 |
int | afni_verbose = 0 |
Define Documentation
|
Definition at line 143 of file plugout_graph.c. Referenced by afni_io(). |
|
Je ne sais pas * Definition at line 139 of file plugout_graph.c. Referenced by afni_io(). |
|
Definition at line 141 of file plugout_graph.c. Referenced by afni_io(). |
|
Definition at line 140 of file plugout_graph.c. Referenced by afni_io(). |
|
Definition at line 142 of file plugout_graph.c. Referenced by afni_io(). |
|
Definition at line 149 of file plugout_graph.c. |
|
Definition at line 150 of file plugout_graph.c. |
|
Definition at line 151 of file plugout_graph.c. |
|
Definition at line 147 of file plugout_graph.c. |
Function Documentation
|
Definition at line 153 of file plugout_graph.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_error_string(), iochan_goodcheck(), iochan_init(), iochan_sendall(), iochan_set_cutoff(), iochan_writecheck(), and y1.
00154 { 00155 static int afni_mode = AFNI_OPEN_CONTROL_MODE ; /* status variable */ 00156 static IOCHAN * afni_ioc = NULL ; /* connection to AFNI */ 00157 int ii ; 00158 00159 /***************************************************************/ 00160 /***** Check to see if status is OK before we proceed. *****/ 00161 /***** (if an error occurs below, afni_mode gets set to 0) *****/ 00162 00163 if( afni_mode <= 0 ) return -1 ; 00164 00165 /***********************************************************************/ 00166 /***** First time into this routine? Open control channel to AFNI *****/ 00167 00168 if( afni_mode == AFNI_OPEN_CONTROL_MODE ){ 00169 char afni_iocname[128] ; /* will hold name of I/O channel */ 00170 00171 /** Note that the control channel is always a 00172 TCP/IP channel to port # 7957 on the AFNI host system **/ 00173 00174 if( strcmp(afni_host,".") == 0 ) 00175 sprintf( afni_iocname , "tcp:%s:7957" , "localhost" ); /* make name */ 00176 else 00177 sprintf( afni_iocname , "tcp:%s:7957" , afni_host ) ; /* make name */ 00178 00179 afni_ioc = iochan_init( afni_iocname , "create" ) ; /* create it */ 00180 if( afni_ioc == NULL ){ 00181 fprintf(stderr, 00182 "** Can't create control channel %s to AFNI!\n",afni_iocname) ; 00183 afni_mode = 0 ; 00184 return -1 ; 00185 } 00186 afni_mode = AFNI_WAIT_CONTROL_MODE ; /* waiting for AFNI connection */ 00187 if( afni_verbose ) 00188 fprintf(stderr,"++ AFNI control channel created\n") ; 00189 } 00190 00191 /****************************************************/ 00192 /**** Check if AFNI control channel is connected ****/ 00193 00194 if( afni_mode == AFNI_WAIT_CONTROL_MODE ){ 00195 ii = iochan_writecheck( afni_ioc , 5 ) ; /* wait at most 5 msec */ 00196 00197 /** the iochan_*check() routines return 00198 -1 for an error, 00199 0 if not ready, 00200 >0 if the I/O channel is ready. **/ 00201 00202 if( ii < 0 ){ 00203 fprintf(stderr,"** Control channel to AFNI failed!\a\n") ; 00204 iochan_set_cutoff(afni_ioc) ; IOCHAN_CLOSE(afni_ioc) ; 00205 afni_mode = 0 ; 00206 return -1 ; 00207 } else if( ii > 0 ){ 00208 afni_mode = AFNI_OPEN_DATA_MODE ; /* prepare to send data */ 00209 if( afni_verbose ) 00210 fprintf(stderr,"++ AFNI control channel connected\n"); 00211 } else { 00212 return 0 ; /* try again next time */ 00213 } 00214 } 00215 00216 /**********************************************************/ 00217 /**** Send control data to AFNI, and open data channel ****/ 00218 00219 if( afni_mode == AFNI_OPEN_DATA_MODE ){ 00220 char afni_iocname[128] ; 00221 char afni_buf[256] ; 00222 00223 /** decide name of data channel: 00224 use shared memory (shm:) on ".", 00225 use TCP/IP (tcp:) on other computer systems; 00226 * Note that the TCP/IP port number can be 00227 anything that isn't already in use; 00228 * Note that the shm control name (here "test_plugout") 00229 is a string that will be converted to an IPC 00230 key (in function string_to_key in iochan.c). **/ 00231 00232 if( strcmp(afni_host,".") == 0 ) 00233 strcpy( afni_iocname , "shm:test_plugout:1K+1K" ) ; 00234 else 00235 sprintf( afni_iocname , "tcp:%s:%d" , afni_host , afni_port ) ; 00236 00237 /** write the command to AFNI into the buffer: 00238 * each command ends with a newline character, 00239 except (possibly) the last command; 00240 * the command buffer is a C string, which ends 00241 with an ASCII NUL character; 00242 * PONAME means 'use this string for informative messages'; 00243 * IOCHAN means 'use this I/O channel from now on'. **/ 00244 00245 if( afni_name[0] == '\0' ) strcpy(afni_name,"TambourineMan") ; 00246 00247 sprintf( afni_buf , "PONAME %s\n" 00248 "IOCHAN %s\n" 00249 "NO_ACK" , 00250 afni_name , afni_iocname ) ; 00251 00252 if( afni_verbose ) 00253 fprintf(stderr,"++ Sending control information to AFNI\n") ; 00254 00255 /** note that the ASCII NUL at the end of the buffer is sent **/ 00256 00257 ii = iochan_sendall( afni_ioc , afni_buf , strlen(afni_buf)+1 ) ; 00258 00259 /** the return value is the number of bytes sent, 00260 or -1 indicating a fatal error transpired. **/ 00261 00262 if( ii < 0 ){ 00263 fprintf(stderr,"** Transmission of control data to AFNI failed!\a\n") ; 00264 iochan_set_cutoff(afni_ioc) ; IOCHAN_CLOSE(afni_ioc) ; 00265 afni_mode = 0 ; 00266 return -1 ; 00267 00268 } else { 00269 00270 /** close control channel **/ 00271 00272 iochan_set_cutoff(afni_ioc) ; IOCHAN_CLOSE(afni_ioc) ; 00273 00274 /** now open data channel to AFNI **/ 00275 00276 afni_ioc = iochan_init( afni_iocname , "create" ) ; 00277 if( afni_ioc == NULL ){ 00278 fprintf(stderr, 00279 "** Can't open data channel %s to AFNI!\a\n",afni_iocname) ; 00280 afni_mode = 0 ; 00281 return -1 ; 00282 } else { 00283 afni_mode = AFNI_WAIT_DATA_MODE ; 00284 if( afni_verbose ) fprintf(stderr,"++ AFNI data channel created\n") ; 00285 } 00286 } 00287 } 00288 00289 /****************************************************/ 00290 /***** See if data channel is connected to AFNI *****/ 00291 00292 if( afni_mode == AFNI_WAIT_DATA_MODE ){ 00293 00294 ii = iochan_goodcheck( afni_ioc , 5 ) ; /* wait at most 5 msec */ 00295 if( ii < 0 ){ 00296 fprintf(stderr, 00297 "** AFNI data channel aborted before any data was sent!\a\n") ; 00298 iochan_set_cutoff(afni_ioc) ;IOCHAN_CLOSE(afni_ioc) ; 00299 afni_mode = 0 ; 00300 return -1 ; 00301 } else if( ii > 0 ){ /* ready to go! */ 00302 afni_mode = AFNI_CONTINUE_MODE ; 00303 if( afni_verbose ) fprintf(stderr,"++ AFNI data channel is open\n") ; 00304 } else { 00305 return 0 ; /* try again next time */ 00306 } 00307 } 00308 00309 /**************************************************************/ 00310 /***** The "normal" state of affairs: AFNI is connected. *****/ 00311 /***** See if the user wants to drive AFNI. *****/ 00312 00313 if( afni_mode == AFNI_CONTINUE_MODE ){ 00314 static int ncom=-1 , nn ; 00315 static double f=2.0 ; 00316 double y1,y2,y3 , t ; 00317 char afni_buf[256]; 00318 00319 switch( ncom ){ 00320 00321 case -1: /* first time in only */ 00322 strcpy(afni_buf,"DRIVE_AFNI OPEN_GRAPH_XY bob '' 0 80 '' 3 -1.5 1.5") ; 00323 srand48((long)time(NULL)) ; 00324 ncom++ ; 00325 break ; 00326 00327 case 801: 00328 strcpy(afni_buf,"DRIVE_AFNI CLEAR_GRAPH_XY bob") ; 00329 ncom = 0 ; 00330 break ; 00331 00332 default: 00333 t = 0.1*ncom ; ncom++ ; 00334 y1 = cos(t) ; 00335 y2 = cos(f*t) ; 00336 y3 = y2+0.2*sin(t/7.0)+0.2*(drand48()-0.5) ; 00337 sprintf(afni_buf,"DRIVE_AFNI ADDTO_GRAPH_XY bob %g %g %g %g",t,y1,y2,y3) ; 00338 #if 1 00339 f += 0.01*(drand48()-0.5) ; 00340 #endif 00341 break ; 00342 } 00343 00344 /* send command to AFNI */ 00345 00346 nn = strlen(afni_buf)+1 ; 00347 ii = iochan_sendall( afni_ioc , afni_buf , nn ) ; 00348 00349 if( ii < 0 ){ /* send or acknowledgment failed */ 00350 char *sss ; 00351 fprintf(stderr,"** AFNI data channel failed!\a\n") ; 00352 sss = iochan_error_string() ; 00353 if( sss != NULL ) fprintf(stderr,"** %s\n",sss) ; 00354 iochan_set_cutoff(afni_ioc) ; IOCHAN_CLOSE(afni_ioc) ; 00355 afni_mode = 0 ; 00356 return -1 ; 00357 } else if( ii < nn ){ 00358 char *sss ; 00359 fprintf(stderr,"++ AFNI data channel full - can't send data!\n") ; 00360 sss = iochan_error_string() ; 00361 if( sss != NULL ) fprintf(stderr,"++ %s\n",sss) ; 00362 } 00363 return 0 ; 00364 } 00365 00366 return 0 ; 00367 } |
|
\** File : SUMA.c
Input paramters :
Definition at line 28 of file plugout_graph.c. References afni_host, afni_io(), afni_name, afni_port, afni_verbose, argc, and iochan_sleep().
00029 { 00030 int narg , ii ; 00031 00032 /***** See if the pitiful user wants help *****/ 00033 00034 if( argc == 2 && strncmp(argv[1],"-help",5) == 0 ){ 00035 printf("Usage: plugout_graph [-host name] [-v]\n" 00036 "This program connects to AFNI and sends graphing commands.\n" 00037 "Options:\n" 00038 " -host name Means to connect to AFNI running on the\n" 00039 " computer 'name' using TCP/IP. The default is to\n" 00040 " connect on the current host using shared memory.\n" 00041 " -v Verbose mode.\n" 00042 " -port pp Use TCP/IP port number 'pp'. The default is\n" 00043 " 8099, but if two plugouts are running on the\n" 00044 " same computer, they must use different ports.\n" 00045 " -name sss Use the string 'sss' for the name that AFNI assigns\n" 00046 " to this plugout. The default is something stupid.\n" 00047 ) ; 00048 exit(0) ; 00049 } 00050 00051 /***** Process command line options *****/ 00052 00053 narg = 1 ; 00054 while( narg < argc ){ 00055 00056 /** -host name **/ 00057 00058 if( strncmp(argv[narg],"-host",5) == 0 ){ 00059 narg++ ; 00060 if( narg >= argc ){ 00061 fprintf(stderr,"** -host needs a following name!\a\n"); exit(1); 00062 } 00063 strcpy( afni_host , argv[narg] ) ; 00064 narg++ ; continue ; 00065 } 00066 00067 /** -name sss **/ 00068 00069 if( strncmp(argv[narg],"-name",5) == 0 ){ 00070 narg++ ; 00071 if( narg >= argc ){ 00072 fprintf(stderr,"** -name needs a following string!\a\n"); exit(1); 00073 } 00074 strcpy( afni_name , argv[narg] ) ; 00075 narg++ ; continue ; 00076 } 00077 00078 /** -v **/ 00079 00080 if( strncmp(argv[narg],"-v",2) == 0 ){ 00081 afni_verbose = 1 ; 00082 narg++ ; continue ; 00083 } 00084 00085 /** -port pp **/ 00086 00087 if( strncmp(argv[narg],"-port",4) == 0 ){ 00088 narg++ ; 00089 if( narg >= argc ){ 00090 fprintf(stderr,"** -port needs a following argument!\a\n"); exit(1); 00091 } 00092 afni_port = strtol( argv[narg] , NULL , 10 ) ; 00093 if( afni_port <= 1024 ){ 00094 fprintf(stderr,"** -port needs an argument > 1024!\a\n"); exit(1); 00095 } 00096 if( strcmp(afni_host,".") == 0 ) strcpy(afni_host,"localhost") ; 00097 narg++ ; continue ; 00098 } 00099 00100 /** Je ne sais pas **/ 00101 00102 fprintf(stderr,"** Unrecognized option: %s\a\n",argv[narg]) ; 00103 exit(1) ; 00104 } 00105 00106 /***** Loop and check in with AFNI every 33 msec *****/ 00107 00108 while( 1 ){ 00109 ii = afni_io() ; /* commune with AFNI */ 00110 if( ii < 0 ) exit(0) ; /* bad trip? then die */ 00111 iochan_sleep(33) ; /* perchance to dream */ 00112 } 00113 00114 } |
Variable Documentation
|
Definition at line 13 of file plugout_graph.c. |
|
Definition at line 14 of file plugout_graph.c. |
|
Definition at line 15 of file plugout_graph.c. |
|
Definition at line 16 of file plugout_graph.c. Referenced by main(). |