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 static boolean last_is_segmented = FALSE;
00065
00066 #ifdef FORK_ADINNET
00067 static pid_t 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
00107 jlog("Stat: adin_tcpip: keep connection for next segment\n");
00108 } else {
00109 #ifdef FORK_ADINNET
00110
00111
00112
00113 for (;;) {
00114
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
00121 child = fork();
00122 if (child < 0) {
00123 jlog("Error: adin_tcpip: fork failed\n");
00124 return FALSE;
00125 }
00126
00127 if (child == 0) {
00128 break;
00129 } else {
00130 jlog("Stat: adin_tcpip: forked process [%d] handles this request\n", child);
00131 }
00132 }
00133 #else
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
00140 }
00141 return TRUE;
00142 }
00143
00155 boolean
00156 adin_tcpip_end()
00157 {
00158 if (!last_is_segmented) {
00159
00160 close_socket(adinnet_asd);
00161 #ifdef FORK_ADINNET
00162
00163 jlog("Stat: adin_tcpip: connection end, child process now exit\n");
00164 exit(0);
00165 #else
00166
00167 jlog("Stat: adin_tcpip: connection end\n");
00168 #endif
00169 }
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
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) {
00202 jlog("Error: adin_tcpip: failed to poll socket\n");
00203 return -2;
00204 }
00205 if (status > 0) {
00206
00207 ret = rd(adinnet_asd, (char *)buf, &cnt, sampnum * sizeof(SP16));
00208 if (ret == 0) {
00209
00210 last_is_segmented = TRUE;
00211 return -1;
00212 }
00213 if (ret < 0) {
00214
00215 last_is_segmented = FALSE;
00216 return -1;
00217 }
00218 } else {
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
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
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
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