26 #include <sys/select.h>
27 #include <sys/types.h>
28 #include <sys/socket.h>
33 #include <yaz/options.h>
35 #include <yaz/xmalloc.h>
44 static int run(
int verbose, FILE *inf,
struct addrinfo *res)
46 long long tv_sec0 = 0;
47 long long tv_usec0 = 0;
57 char request_type[100];
64 for (i = 0; c !=
'\n' && i < (
sizeof(req)-2); i++)
70 r = sscanf(req,
"%s %lld %lld %lld %d", request_type,
71 &tv_sec1, &tv_usec1, &
id, &sz);
74 fprintf(stderr,
"bad line %s\n", req);
78 fprintf(stderr,
"read line: %s\n", req);
83 spec.tv_sec = tv_sec1 - tv_sec0;
84 if (tv_usec0 > tv_usec1)
86 spec.tv_usec = 1000000 + tv_usec1 - tv_usec0;
90 spec.tv_usec = tv_usec1 - tv_usec0;
92 select(0, 0, 0, 0, &spec);
96 for (conp = &cons; *conp; conp = &(*conp)->
next)
97 if ((*conp)->id ==
id)
103 for (rp = res; rp; rp = rp->ai_next)
105 fd = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
111 fprintf(stderr,
"socket: cannot create\n");
114 r = connect(fd, rp->ai_addr, rp->ai_addrlen);
117 fprintf(stderr,
"socket: cannot connect\n");
121 *conp = xmalloc(
sizeof(**conp));
128 struct con *c = *conp;
141 size_t toread = sz - cnt;
143 if (toread >
sizeof(buf))
144 toread =
sizeof(buf);
145 r = fread(buf, 1, toread, inf);
148 fprintf(stderr,
"fread truncated. toread=%lld r=%lld\n",
149 (
long long) toread, (
long long) r);
154 fprintf(stderr,
"read %ld bytes\n---\n", (
long) r);
155 fwrite(buf, 1, r, stderr);
156 fprintf(stderr,
"\n----\n");
158 if (*request_type ==
'r')
160 w = write((*conp)->fd, buf, toread);
163 fprintf(stderr,
"write truncated\n");
176 fprintf(stderr,
"Usage: pazpar2_play infile host\n"
177 " -v level Set log level\n");
181 int main(
int argc,
char **argv)
187 const char *file = 0;
188 while ((ret = options(
"v:", argv, argc, &arg)) != -2)
193 yaz_log_init_level(yaz_log_mask_str(arg));
216 struct addrinfo hints, *res;
218 hints.ai_family = AF_UNSPEC;
219 hints.ai_socktype = SOCK_STREAM;
220 hints.ai_protocol = 0;
221 hints.ai_addrlen = 0;
222 hints.ai_addr = NULL;
223 hints.ai_canonname = NULL;
224 hints.ai_next = NULL;
226 cp = strchr(host,
':');
236 if (getaddrinfo(host, port, &hints, &res))
238 fprintf(stderr,
"cannot resolve %s:%s\n", host, port);
242 inf = fopen(file,
"rb");
245 fprintf(stderr,
"cannot open %s\n", file);
248 run(verbose, inf, res);
int main(int argc, char **argv)
static int run(int verbose, FILE *inf, struct addrinfo *res)