Julius 4.2
libsent/src/adin/adin_tcpip.c
説明を見る。
00001 
00051 /*
00052  * Copyright (c) 1991-2011 Kawahara Lab., Kyoto University
00053  * Copyright (c) 2000-2005 Shikano Lab., Nara Institute of Science and Technology
00054  * Copyright (c) 2005-2011 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 
00065 #ifdef FORK_ADINNET
00066 static pid_t child;             /* child process ID (0 if myself is child) */
00067 #endif
00068 
00077 boolean
00078 adin_tcpip_standby(int freq, void *port_str)
00079 {
00080   int port;
00081 
00082   port = atoi((char *)port_str);
00083 
00084   if ((adinnet_sd = ready_as_server(port)) < 0) {
00085     jlog("Error: adin_tcpip: cannot ready for server\n");
00086     return FALSE;
00087   }
00088 
00089   jlog("Stat: adin_tcpip: ready for server\n");
00090 
00091   return TRUE;
00092 }
00093 
00101 boolean
00102 adin_tcpip_begin(char *pathname)
00103 {
00104 #ifdef FORK_ADINNET
00105     /***********************************/
00106     /*** server infinite loop here!! ***/
00107     /***********************************/
00108     for (;;) {
00109       /* wait connection */
00110       jlog("Stat: adin_tcpip: waiting connection...\n");
00111       if ((adinnet_asd = accept_from(adinnet_sd)) < 0) {
00112         return FALSE;
00113       }
00114       jlog("Stat: adin_tcpip: connected\n");
00115       /* fork self */
00116       child = fork();
00117       if (child < 0) {          /* error */
00118         jlog("Error: adin_tcpip: fork failed\n");
00119         return FALSE;
00120       }
00121       /* child thread should handle this request */
00122       if (child == 0) {         /* child thread */
00123         break;                  /* proceed */
00124       } else {                  /* parent thread */
00125         jlog("Stat: adin_tcpip: forked process [%d] handles this request\n", child);
00126       }
00127     }
00128 #else  /* ~FORK_ADINNET */
00129     jlog("Stat: adin_tcpip: waiting connection...\n");
00130     if ((adinnet_asd = accept_from(adinnet_sd)) < 0) {
00131       return FALSE;
00132     }
00133     jlog("Stat: adin_tcpip: connected\n");
00134 #endif /* FORK_ADINNET */
00135 
00136   return TRUE;
00137 }
00138 
00150 boolean
00151 adin_tcpip_end()
00152 {
00153     /* end of connection */
00154     close_socket(adinnet_asd);
00155 #ifdef FORK_ADINNET
00156     /* terminate this child process here */
00157     jlog("Stat: adin_tcpip: connection end, child process now exit\n");
00158     exit(0);
00159 #else
00160     /* wait for the next connection */
00161     jlog("Stat: adin_tcpip: connection end\n");
00162 #endif
00163 
00164   return TRUE;
00165 }
00166 
00181 int
00182 adin_tcpip_read(SP16 *buf, int sampnum)
00183 {
00184   int cnt, ret;
00185   fd_set rfds;
00186   struct timeval tv;
00187   int status;
00188 
00189   /* check if some commands are waiting in queue */
00190   FD_ZERO(&rfds);
00191   FD_SET(adinnet_asd, &rfds);
00192   tv.tv_sec = 0;
00193   tv.tv_usec = 10000;           /* 10msec */
00194   status = select(adinnet_asd+1, &rfds, NULL, NULL, &tv);
00195   if (status < 0) {             /* error */
00196     jlog("Error: adin_tcpip: failed to poll socket\n");
00197     return -2;                  /* error return */
00198   }
00199   if (status > 0) {             /* there are some data */
00200     /* read one data segment, leave rest even if any for avoid blocking */
00201     ret = rd(adinnet_asd, (char *)buf, &cnt, sampnum * sizeof(SP16));
00202     if (ret == 0) {
00203       /* end of segment mark */
00204       return -3;
00205     }
00206     if (ret < 0) {
00207       /* end of input, mark */
00208       return -1;
00209     }
00210   } else {                      /* time out, no data */
00211     cnt = 0;
00212   }
00213   cnt /= sizeof(SP16);
00214 #ifdef WORDS_BIGENDIAN
00215   swap_sample_bytes(buf, cnt);
00216 #endif
00217   return cnt;
00218 }
00219 
00225 boolean
00226 adin_tcpip_send_pause()
00227 {
00228    int count;
00229    char com;
00230    /* send stop command to adinnet client */
00231    com = '0';
00232    count = wt(adinnet_asd, &com, 1);
00233    if (count < 0) jlog("Warning: adin_tcpip: cannot send pause command to client\n");
00234    jlog("Stat: adin_tcpip: sent pause command to client\n");
00235    return TRUE;
00236 }
00237 
00243 boolean
00244 adin_tcpip_send_resume()
00245 {
00246   int count;
00247   char com;
00248   int cnt, ret;
00249   fd_set rfds;
00250   struct timeval tv;
00251   int status;
00252   static char *tmpbuf = NULL;
00253 
00254   /* check if some commands are waiting in queue */
00255   count = 0;
00256   do {
00257     FD_ZERO(&rfds);
00258     FD_SET(adinnet_asd, &rfds);
00259     tv.tv_sec = 0;
00260     tv.tv_usec = 0;
00261     status = select(adinnet_asd+1, &rfds, NULL, NULL, &tv);
00262     if (status < 0) {           /* error */
00263       jlog("Error: adin_tcpip: failed to poll socket\n");
00264       return FALSE;                     /* error return */
00265     }
00266     if (status > 0) {           /* there are some data */
00267       if (tmpbuf == NULL) tmpbuf = (char *)mymalloc(MAXSPEECHLEN);
00268       ret = rd(adinnet_asd, tmpbuf, &cnt, MAXSPEECHLEN * sizeof(SP16));
00269     }
00270     if (cnt > 0) count += cnt;
00271   } while (status != 0);
00272   if (count > 0) {
00273     jlog("Stat: %d samples transfered while pause are flushed\n", count);
00274   }
00275     
00276   /* send resume command to adinnet client */
00277   com = '1';
00278   count = wt(adinnet_asd, &com, 1);
00279   if (count < 0) jlog("Warning: adin_tcpip: cannot send resume command to client\n");
00280   jlog("Stat: adin_tcpip: sent resume command to client\n");
00281 
00282   /* flush current buffer */
00283   return TRUE;
00284 }
00285 
00291 boolean
00292 adin_tcpip_send_terminate()
00293 {
00294    int count;
00295    char com;
00296    /* send terminate command to adinnet client */
00297    com = '2';
00298    count = wt(adinnet_asd, &com, 1);
00299    if (count < 0) jlog("Warning: adin_tcpip: cannot send terminate command to client\n");
00300    jlog("Stat: adin_tcpip: sent terminate command to client\n");
00301    return TRUE;
00302 }
00303 
00311 char *
00312 adin_tcpip_input_name()
00313 {
00314   return("network socket");
00315 }