1 /* This file is part of Pazpar2.
2 Copyright (C) 2006-2012 Index Data
4 Pazpar2 is free software; you can redistribute it and/or modify it under
5 the terms of the GNU General Public License as published by the Free
6 Software Foundation; either version 2, or (at your option) any later
9 Pazpar2 is distributed in the hope that it will be useful, but WITHOUT ANY
10 WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
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(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);
81 spec.tv_sec = tv_sec1 - tv_sec0;
82 if (tv_usec0 > tv_usec1)
84 spec.tv_usec = 1000000 + tv_usec1 - tv_usec0;
88 spec.tv_usec = tv_usec1 - tv_usec0;
90 select(0, 0, 0, 0, &spec);
94 for (conp = &cons; *conp; conp = &(*conp)->next)
95 if ((*conp)->id == id)
101 for (rp = res; rp; rp = rp->ai_next)
103 fd = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
109 fprintf(stderr, "socket: cannot create\n");
112 r = connect(fd, rp->ai_addr, rp->ai_addrlen);
115 fprintf(stderr, "socket: cannot connect\n");
119 *conp = xmalloc(sizeof(**conp));
126 struct con *c = *conp;
139 size_t toread = sz - cnt;
141 if (toread > sizeof(buf))
142 toread = sizeof(buf);
143 r = fread(buf, 1, toread, inf);
146 fprintf(stderr, "fread truncated. toread=%lld r=%lld\n",
147 (long long) toread, (long long) r);
150 if (*request_type == 'r')
151 { /* Only deal with things tha Pazpar2 received */
152 w = write((*conp)->fd, buf, toread);
155 fprintf(stderr, "write truncated\n");
166 static void usage(void)
168 fprintf(stderr, "Usage: pazpar2_play infile host\n"
169 " -v level Set log level\n");
173 int main(int argc, char **argv)
178 const char *file = 0;
179 while ((ret = options("v:", argv, argc, &arg)) != -2)
184 yaz_log_init_level(yaz_log_mask_str(arg));
207 struct addrinfo hints, *res;
209 hints.ai_family = AF_UNSPEC;
210 hints.ai_socktype = SOCK_STREAM;
211 hints.ai_protocol = 0;
212 hints.ai_addrlen = 0;
213 hints.ai_addr = NULL;
214 hints.ai_canonname = NULL;
215 hints.ai_next = NULL;
217 cp = strchr(host, ':');
227 if (getaddrinfo(host, port, &hints, &res))
229 fprintf(stderr, "cannot resolve %s:%s\n", host, port);
233 inf = fopen(file, "rb");
236 fprintf(stderr, "cannot open %s\n", file);
251 * c-file-style: "Stroustrup"
252 * indent-tabs-mode: nil
254 * vim: shiftwidth=4 tabstop=8 expandtab