Main Page | Modules | Data Structures | Directories | File List | Data Fields | Globals | Related Pages

adin_tcpip.c

Go to the documentation of this file.
00001 
00050 /*
00051  * Copyright (c) 1991-2006 Kawahara Lab., Kyoto University
00052  * Copyright (c) 2000-2005 Shikano Lab., Nara Institute of Science and Technology
00053  * Copyright (c) 2005-2006 Julius project team, Nagoya Institute of Technology, Nagoya Institute of Technology
00054  * All rights reserved
00055  */
00056 
00057 #include <sent/stddefs.h>
00058 #include <sent/adin.h>
00059 #include <sent/tcpip.h>
00060 
00061 static int adinnet_sd = -1;     
00062 static int adinnet_asd = -1;    
00063 static boolean last_is_segmented = FALSE; 
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     j_printerr("adin_tcpip_standby: cannot ready for server\n");
00086     return FALSE;
00087   }
00088 
00089   last_is_segmented = FALSE;
00090 
00091   return TRUE;
00092 }
00093 
00099 boolean
00100 adin_tcpip_begin()
00101 {
00102   if (last_is_segmented) {
00103     /* just wait for the next segment to be received */
00104   } else {
00105 #ifdef FORK_ADINNET
00106     /***********************************/
00107     /*** server infinite loop here!! ***/
00108     /***********************************/
00109     for (;;) {
00110       /* wait connection */
00111       j_printerr("waiting connection...\n");
00112       adinnet_asd = accept_from(adinnet_sd);
00113       /* fork self */
00114       child = fork();
00115       if (child < 0) {          /* error */
00116         j_printerr("adin_tcpip_standby: fork failed\n");
00117         return FALSE;
00118       }
00119       /* child thread should handle this request */
00120       if (child == 0) {         /* child thread */
00121         break;                  /* proceed */
00122       } else {                  /* parent thread */
00123         j_printerr("forked process [%d] handles this request\n", child);
00124       }
00125     }
00126 #else  /* ~FORK_ADINNET */
00127     j_printerr("waiting connection...\n");
00128     adinnet_asd = accept_from(adinnet_sd);
00129 #endif /* FORK_ADINNET */
00130   }
00131   return TRUE;
00132 }
00133 
00145 boolean
00146 adin_tcpip_end()
00147 {
00148   if (!last_is_segmented) {
00149     /* end of connection */
00150     close_socket(adinnet_asd);
00151 #ifdef FORK_ADINNET
00152     /* terminate this child process here */
00153     j_error("connection end\n");
00154 #else
00155     /* wait for the next connection */
00156     j_printerr("connection end\n");
00157 #endif
00158   } /* else, end of segment: wait for next input in current socket */
00159   return TRUE;
00160 }
00161 
00176 int
00177 adin_tcpip_read(SP16 *buf, int sampnum)
00178 {
00179   int cnt, ret;
00180   fd_set rfds;
00181   struct timeval tv;
00182   int status;
00183   
00184   /* check if some commands are waiting in queue */
00185   FD_ZERO(&rfds);
00186   FD_SET(adinnet_asd, &rfds);
00187   tv.tv_sec = 0;
00188   tv.tv_usec = 1;
00189   status = select(adinnet_asd+1, &rfds, NULL, NULL, &tv);
00190   if (status < 0) {             /* error */
00191     j_printerr("adin_tcpip_read: cannot poll\n");
00192     return -2;                  /* error return */
00193   }
00194   if (status > 0) {             /* there are some data */
00195     /* read one data segment, leave rest even if any for avoid blocking */
00196     ret = rd(adinnet_asd, (char *)buf, &cnt, sampnum * sizeof(SP16));
00197     if (ret == 0) {
00198       /* end of segment mark */
00199       last_is_segmented = TRUE;
00200       return -1;
00201     }
00202     if (ret < 0) {
00203       /* end of input, mark */
00204       last_is_segmented = FALSE;
00205       return -1;
00206     }
00207   } else {                      /* no data */
00208     cnt = 0;
00209   }
00210   cnt /= sizeof(SP16);
00211 #ifdef WORDS_BIGENDIAN
00212   swap_sample_bytes(buf, cnt);
00213 #endif
00214   return cnt;
00215 }
00216 
00222 boolean
00223 adin_tcpip_send_pause()
00224 {
00225    int count;
00226    char com;
00227    /* send stop command to adinnet client */
00228    com = '0';
00229    count = wt(adinnet_asd, &com, 1);
00230    if (count < 0) j_printerr("adin_tcpip_send_pause: cannot send command to client\n");
00231    return TRUE;
00232 }
00233 
00239 boolean
00240 adin_tcpip_send_resume()
00241 {
00242   int count;
00243   char com;
00244   /* send resume command to adinnet client */
00245   com = '1';
00246   count = wt(adinnet_asd, &com, 1);
00247   if (count < 0) j_printerr("adin_tcpip_send_resume: cannot send command to client\n");
00248   return TRUE;
00249 }
00250 
00256 boolean
00257 adin_tcpip_send_terminate()
00258 {
00259    int count;
00260    char com;
00261    /* send terminate command to adinnet client */
00262    com = '2';
00263    count = wt(adinnet_asd, &com, 1);
00264    if (count < 0) j_printerr("adin_tcpip_send_terminate: cannot send command to client\n");
00265    return TRUE;
00266 }
00267 

Generated on Tue Mar 28 16:01:39 2006 for Julius by  doxygen 1.4.2