00001
00051
00052
00053
00054
00055
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;
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
00099 boolean
00100 adin_tcpip_begin()
00101 {
00102 #ifdef FORK_ADINNET
00103
00104
00105
00106 for (;;) {
00107
00108 jlog("Stat: adin_tcpip: waiting connection...\n");
00109 if ((adinnet_asd = accept_from(adinnet_sd)) < 0) {
00110 return FALSE;
00111 }
00112 jlog("Stat: adin_tcpip: connected\n");
00113
00114 child = fork();
00115 if (child < 0) {
00116 jlog("Error: adin_tcpip: fork failed\n");
00117 return FALSE;
00118 }
00119
00120 if (child == 0) {
00121 break;
00122 } else {
00123 jlog("Stat: adin_tcpip: forked process [%d] handles this request\n", child);
00124 }
00125 }
00126 #else
00127 jlog("Stat: adin_tcpip: waiting connection...\n");
00128 if ((adinnet_asd = accept_from(adinnet_sd)) < 0) {
00129 return FALSE;
00130 }
00131 jlog("Stat: adin_tcpip: connected\n");
00132 #endif
00133
00134 return TRUE;
00135 }
00136
00148 boolean
00149 adin_tcpip_end()
00150 {
00151
00152 close_socket(adinnet_asd);
00153 #ifdef FORK_ADINNET
00154
00155 jlog("Stat: adin_tcpip: connection end, child process now exit\n");
00156 exit(0);
00157 #else
00158
00159 jlog("Stat: adin_tcpip: connection end\n");
00160 #endif
00161
00162 return TRUE;
00163 }
00164
00179 int
00180 adin_tcpip_read(SP16 *buf, int sampnum)
00181 {
00182 int cnt, ret;
00183 fd_set rfds;
00184 struct timeval tv;
00185 int status;
00186
00187
00188 FD_ZERO(&rfds);
00189 FD_SET(adinnet_asd, &rfds);
00190 tv.tv_sec = 0;
00191 tv.tv_usec = 10000;
00192 status = select(adinnet_asd+1, &rfds, NULL, NULL, &tv);
00193 if (status < 0) {
00194 jlog("Error: adin_tcpip: failed to poll socket\n");
00195 return -2;
00196 }
00197 if (status > 0) {
00198
00199 ret = rd(adinnet_asd, (char *)buf, &cnt, sampnum * sizeof(SP16));
00200 if (ret == 0) {
00201
00202 return -3;
00203 }
00204 if (ret < 0) {
00205
00206 return -1;
00207 }
00208 } else {
00209 cnt = 0;
00210 }
00211 cnt /= sizeof(SP16);
00212 #ifdef WORDS_BIGENDIAN
00213 swap_sample_bytes(buf, cnt);
00214 #endif
00215 return cnt;
00216 }
00217
00223 boolean
00224 adin_tcpip_send_pause()
00225 {
00226 int count;
00227 char com;
00228
00229 com = '0';
00230 count = wt(adinnet_asd, &com, 1);
00231 if (count < 0) jlog("Warning: adin_tcpip: cannot send pause command to client\n");
00232 jlog("Stat: adin_tcpip: sent pause command to client\n");
00233 return TRUE;
00234 }
00235
00241 boolean
00242 adin_tcpip_send_resume()
00243 {
00244 int count;
00245 char com;
00246 int cnt, ret;
00247 fd_set rfds;
00248 struct timeval tv;
00249 int status;
00250 static char *tmpbuf = NULL;
00251
00252
00253 count = 0;
00254 do {
00255 FD_ZERO(&rfds);
00256 FD_SET(adinnet_asd, &rfds);
00257 tv.tv_sec = 0;
00258 tv.tv_usec = 0;
00259 status = select(adinnet_asd+1, &rfds, NULL, NULL, &tv);
00260 if (status < 0) {
00261 jlog("Error: adin_tcpip: failed to poll socket\n");
00262 return FALSE;
00263 }
00264 if (status > 0) {
00265 if (tmpbuf == NULL) tmpbuf = (char *)mymalloc(MAXSPEECHLEN);
00266 ret = rd(adinnet_asd, tmpbuf, &cnt, MAXSPEECHLEN * sizeof(SP16));
00267 }
00268 if (cnt > 0) count += cnt;
00269 } while (status != 0);
00270 if (count > 0) {
00271 jlog("Stat: %d samples transfered while pause are flushed\n", count);
00272 }
00273
00274
00275 com = '1';
00276 count = wt(adinnet_asd, &com, 1);
00277 if (count < 0) jlog("Warning: adin_tcpip: cannot send resume command to client\n");
00278 jlog("Stat: adin_tcpip: sent resume command to client\n");
00279
00280
00281 return TRUE;
00282 }
00283
00289 boolean
00290 adin_tcpip_send_terminate()
00291 {
00292 int count;
00293 char com;
00294
00295 com = '2';
00296 count = wt(adinnet_asd, &com, 1);
00297 if (count < 0) jlog("Warning: adin_tcpip: cannot send terminate command to client\n");
00298 jlog("Stat: adin_tcpip: sent terminate command to client\n");
00299 return TRUE;
00300 }
00301