libsent/src/adin/adin_tcpip.c

Go to the documentation of this file.
00001 
00051 /*
00052  * Copyright (c) 1991-2007 Kawahara Lab., Kyoto University
00053  * Copyright (c) 2000-2005 Shikano Lab., Nara Institute of Science and Technology
00054  * Copyright (c) 2005-2007 Julius project team, Nagoya Institute of Technology
00055  * All rights reserved
00056  */
00057 
00058 #include <sent/stddefs.h>
00059 #include <sent/adin.h>
00060 #include <sent/tcpip.h>
00061 
00062 static int adinnet_sd = -1;     
00063 static int adinnet_asd = -1;    
00064 static boolean last_is_segmented = FALSE; 
00065 
00066 #ifdef FORK_ADINNET
00067 static pid_t child;             /* child process ID (0 if myself is child) */
00068 #endif
00069 
00078 boolean
00079 adin_tcpip_standby(int freq, void *port_str)
00080 {
00081   int port;
00082 
00083   port = atoi((char *)port_str);
00084 
00085   if ((adinnet_sd = ready_as_server(port)) < 0) {
00086     jlog("Error: adin_tcpip: cannot ready for server\n");
00087     return FALSE;
00088   }
00089 
00090   last_is_segmented = FALSE;
00091 
00092   jlog("Stat: adin_tcpip: ready for server\n");
00093 
00094   return TRUE;
00095 }
00096 
00102 boolean
00103 adin_tcpip_begin()
00104 {
00105   if (last_is_segmented) {
00106     /* just wait for the next segment to be received */
00107     jlog("Stat: adin_tcpip: keep connection for next segment\n");
00108   } else {
00109 #ifdef FORK_ADINNET
00110     /***********************************/
00111     /*** server infinite loop here!! ***/
00112     /***********************************/
00113     for (;;) {
00114       /* wait connection */
00115       jlog("Stat: adin_tcpip: waiting connection...\n");
00116       if ((adinnet_asd = accept_from(adinnet_sd)) < 0) {
00117         return FALSE;
00118       }
00119       jlog("Stat: adin_tcpip: connected\n");
00120       /* fork self */
00121       child = fork();
00122       if (child < 0) {          /* error */
00123         jlog("Error: adin_tcpip: fork failed\n");
00124         return FALSE;
00125       }
00126       /* child thread should handle this request */
00127       if (child == 0) {         /* child thread */
00128         break;                  /* proceed */
00129       } else {                  /* parent thread */
00130         jlog("Stat: adin_tcpip: forked process [%d] handles this request\n", child);
00131       }
00132     }
00133 #else  /* ~FORK_ADINNET */
00134     jlog("Stat: adin_tcpip: waiting connection...\n");
00135     if ((adinnet_asd = accept_from(adinnet_sd)) < 0) {
00136       return FALSE;
00137     }
00138     jlog("Stat: adin_tcpip: connected\n");
00139 #endif /* FORK_ADINNET */
00140   }
00141   return TRUE;
00142 }
00143 
00155 boolean
00156 adin_tcpip_end()
00157 {
00158   if (!last_is_segmented) {
00159     /* end of connection */
00160     close_socket(adinnet_asd);
00161 #ifdef FORK_ADINNET
00162     /* terminate this child process here */
00163     jlog("Stat: adin_tcpip: connection end, child process now exit\n");
00164     exit(0);
00165 #else
00166     /* wait for the next connection */
00167     jlog("Stat: adin_tcpip: connection end\n");
00168 #endif
00169   } /* else, end of segment: wait for next input in current socket */
00170   return TRUE;
00171 }
00172 
00187 int
00188 adin_tcpip_read(SP16 *buf, int sampnum)
00189 {
00190   int cnt, ret;
00191   fd_set rfds;
00192   struct timeval tv;
00193   int status;
00194   
00195   /* check if some commands are waiting in queue */
00196   FD_ZERO(&rfds);
00197   FD_SET(adinnet_asd, &rfds);
00198   tv.tv_sec = 0;
00199   tv.tv_usec = 1;
00200   status = select(adinnet_asd+1, &rfds, NULL, NULL, &tv);
00201   if (status < 0) {             /* error */
00202     jlog("Error: adin_tcpip: failed to poll socket\n");
00203     return -2;                  /* error return */
00204   }
00205   if (status > 0) {             /* there are some data */
00206     /* read one data segment, leave rest even if any for avoid blocking */
00207     ret = rd(adinnet_asd, (char *)buf, &cnt, sampnum * sizeof(SP16));
00208     if (ret == 0) {
00209       /* end of segment mark */
00210       last_is_segmented = TRUE;
00211       return -1;
00212     }
00213     if (ret < 0) {
00214       /* end of input, mark */
00215       last_is_segmented = FALSE;
00216       return -1;
00217     }
00218   } else {                      /* no data */
00219     cnt = 0;
00220   }
00221   cnt /= sizeof(SP16);
00222 #ifdef WORDS_BIGENDIAN
00223   swap_sample_bytes(buf, cnt);
00224 #endif
00225   return cnt;
00226 }
00227 
00233 boolean
00234 adin_tcpip_send_pause()
00235 {
00236    int count;
00237    char com;
00238    /* send stop command to adinnet client */
00239    com = '0';
00240    count = wt(adinnet_asd, &com, 1);
00241    if (count < 0) jlog("Warning: adin_tcpip: cannot send pause command to client\n");
00242    jlog("Stat: adin_tcpip: sent pause command to client\n");
00243    return TRUE;
00244 }
00245 
00251 boolean
00252 adin_tcpip_send_resume()
00253 {
00254   int count;
00255   char com;
00256   /* send resume command to adinnet client */
00257   com = '1';
00258   count = wt(adinnet_asd, &com, 1);
00259   if (count < 0) jlog("Warning: adin_tcpip: cannot send resume command to client\n");
00260   jlog("Stat: adin_tcpip: sent resume command to client\n");
00261   return TRUE;
00262 }
00263 
00269 boolean
00270 adin_tcpip_send_terminate()
00271 {
00272    int count;
00273    char com;
00274    /* send terminate command to adinnet client */
00275    com = '2';
00276    count = wt(adinnet_asd, &com, 1);
00277    if (count < 0) jlog("Warning: adin_tcpip: cannot send terminate command to client\n");
00278    jlog("Stat: adin_tcpip: sent terminate command to client\n");
00279    return TRUE;
00280 }
00281 

Generated on Tue Dec 18 15:59:54 2007 for Julius by  doxygen 1.5.4