Doxygen Source Code Documentation
Main Page Alphabetical List Data Structures File List Data Fields Globals Search
nicat.c
Go to the documentation of this file.00001 #include "niml.h"
00002
00003
00004
00005 #define NBUF 65000
00006
00007 int main( int argc , char *argv[] )
00008 {
00009 NI_stream ns ;
00010 char lbuf[NBUF] , *reop=NULL ;
00011 int nn , nl=0 , jj , ntot=0 , ct , ech , iarg=1 ;
00012
00013 if( argc < 2 || strcmp(argv[1],"-help") == 0 ){
00014 printf("Usage: nicat [-reopen rr] [-rR] streamspec\n"
00015 "Copies stdin to the NIML stream, which will be opened\n"
00016 "for writing.\n"
00017 "\n"
00018 "-reopen rr == reopen the stream after connection\n"
00019 " to the stream specified by 'rr'\n"
00020 "-r == Copy the stream to stdout instead; the\n"
00021 " 'streamspec' will be opened for reading.\n"
00022 "-R == Read the stream but don't copy to stdout.\n"
00023 "\n"
00024 "Intended for testing other programs that use NIML for\n"
00025 "various services. Example:\n"
00026 " aiv -p 4444 &\n"
00027 " im2niml zork.jpg | nicat tcp:localhost:4444\n"
00028 "Starts aiv listening on TCP/IP port 444, then sends image\n"
00029 "file zork.jpg to that port for display.\n"
00030 ) ;
00031 exit(0) ;
00032 }
00033
00034 if( strcmp(argv[iarg],"-reopen") == 0 ){
00035 reop = argv[++iarg] ; iarg++ ;
00036 }
00037
00038
00039
00040 if( toupper(argv[iarg][1]) != 'R' ){
00041 ns = NI_stream_open( argv[iarg] , "w" ) ;
00042 if( ns == NULL ){
00043 fprintf(stderr,"** NI_stream_open fails\n") ; exit(1) ;
00044 }
00045 while(1){
00046 nn = NI_stream_writecheck( ns , 400 ) ;
00047 if( nn == 1 ){ fprintf(stderr,"!\n") ; break ; }
00048 if( nn < 0 ){ fprintf(stderr,"BAD\n"); exit(1) ; }
00049 fprintf(stderr,"-") ;
00050 }
00051 if( reop ){
00052 fprintf(stderr,"++ reopening") ;
00053 nn = NI_stream_reopen( ns , reop ) ;
00054 if( nn == 0 ) fprintf(stderr,".BAD") ;
00055 else fprintf(stderr,".GOOD") ;
00056 fprintf(stderr,"\n") ;
00057 }
00058 ct = NI_clock_time() ;
00059 while(1){
00060 jj = fread(lbuf,1,NBUF,stdin) ; if( jj <= 0 ) break ;
00061 nn = NI_stream_write( ns , lbuf , jj ) ;
00062 if( nn < 0 ){
00063 fprintf(stderr,"** NI_stream_write fails\n"); break ;
00064 } else if( nn < jj ){
00065 fprintf(stderr,"++ nl=%d: wrote %d/%d bytes\n",nl,nn,jj) ;
00066 }
00067 nl++ ; ntot += jj ;
00068 }
00069 NI_sleep(1) ; NI_stream_close(ns) ;
00070 ct = NI_clock_time()-ct ;
00071 fprintf(stderr,"++ Wrote %d bytes in %d ms: %.3f Mbytes/s\n",
00072 ntot,ct,(9.5367e-7*ntot)/(1.e-3*ct) ) ;
00073 sleep(1) ; exit(0) ;
00074 }
00075
00076
00077
00078 if( argc < iarg+2 ){
00079 fprintf(stderr,"** %s needs argv[%d]\n",argv[1],iarg+1); exit(1);
00080 }
00081
00082 ech = (argv[iarg][1] == 'r') ;
00083
00084 ns = NI_stream_open( argv[iarg+1], (argv[iarg][2]=='\0') ? "r" : "w" ) ;
00085 if( ns == NULL ){
00086 fprintf(stderr,"** NI_stream_open fails\n") ; exit(1) ;
00087 }
00088 while(1){
00089 nn = NI_stream_readcheck( ns , 400 ) ;
00090 if( nn == 1 ){ fprintf(stderr,"!\n") ; break ; }
00091 if( nn < 0 ){ fprintf(stderr,"BAD\n"); exit(1) ; }
00092 fprintf(stderr,"-") ;
00093 }
00094 if( reop ){
00095 fprintf(stderr,"++ reopening") ;
00096 nn = NI_stream_reopen( ns , reop ) ;
00097 if( nn == 0 ) fprintf(stderr,".BAD") ;
00098 else fprintf(stderr,".GOOD") ;
00099 fprintf(stderr,"\n") ;
00100 }
00101 ct = NI_clock_time() ;
00102 while(1){
00103 nn = NI_stream_read( ns , lbuf , NBUF ) ;
00104 if( nn < 0 ){
00105 fprintf(stderr,"\nNI_stream_read fails\n"); break;
00106 } else {
00107 ntot += nn ;
00108 }
00109 if( ech && nn > 0 ){
00110 printf("%.*s",nn,lbuf) ; nl++ ;
00111 }
00112 }
00113 ct = NI_clock_time()-ct ;
00114 fprintf(stderr,"Read %d bytes in %d ms: %.3f Mbytes/s\n",
00115 ntot,ct,(9.5367e-7*ntot)/(1.e-3*ct) ) ;
00116 sleep(1) ; exit(0) ;
00117 }