2 * Copyright (c) 1995-1997, Index Data.
3 * See the file LICENSE for details.
4 * Sebastian Hammer, Adam Dickmeiss
6 * NT Service interface by
7 * Chas Woodfield, Fretwell Downing Datasystems.
10 * Revision 1.4 1997-09-17 12:10:43 adam
13 * Revision 1.3 1997/09/09 10:10:20 adam
14 * Another MSV5.0 port. Changed projects to include proper
15 * library/include paths.
16 * Server starts server in test-mode when no options are given.
18 * Revision 1.2 1997/09/04 13:50:31 adam
24 * Demonstration of simple server
36 /* Specifically for NT Services - Shouldn't cause problems on UNIX */
39 Z_GenericRecord *read_grs1(FILE *f, ODR o);
41 bend_initresult *bend_init(bend_initrequest *q)
43 bend_initresult *r = odr_malloc (q->stream, sizeof(*r));
44 static char *dummy = "Hej fister";
52 bend_searchresult *bend_search(void *handle, bend_searchrequest *q, int *fd)
54 bend_searchresult *r = odr_malloc (q->stream, sizeof(*r));
58 r->hits = rand() % 22;
63 static int atoin (const char *buf, int n)
69 val = val*10 + (*buf - '0');
75 char *marc_read(FILE *inf)
81 if (fread (length_str, 1, 5, inf) != 5)
83 size = atoin (length_str, 5);
86 if (!(buf = xmalloc (size+1)))
88 if (fread (buf+5, 1, size-5, inf) != (size-5))
93 memcpy (buf, length_str, 5);
98 static char *dummy_database_record (int num)
100 FILE *inf = fopen ("dummy-records", "r");
111 assert(buf = xmalloc(2101));
112 memset(buf, 'A', 2100);
117 buf = marc_read (inf);
127 static Z_GenericRecord *dummy_grs_record (int num, ODR o)
129 FILE *f = fopen("dummy-grs", "r");
131 Z_GenericRecord *r = 0;
136 while (fgets(line, 512, f))
137 if (*line == '#' && sscanf(line, "#%d", &n) == 1 && n == num)
146 bend_fetchresult *bend_fetch(void *handle, bend_fetchrequest *q, int *num)
148 bend_fetchresult *r = odr_malloc (q->stream, sizeof(*r));
149 static char *bbb = 0;
153 r->basename = "DUMMY";
159 r->format = q->format;
160 if (q->format == VAL_SUTRS)
164 sprintf(buf, "This is dummy SUTRS record number %d\n", q->number);
165 assert(r->record = bbb = xmalloc(strlen(buf)+1));
167 r->len = strlen(buf);
169 else if (q->format == VAL_GRS1)
172 r->record = (char*) dummy_grs_record(q->number, q->stream);
179 else if (!(r->record = bbb = dummy_database_record(q->number)))
182 r->format = VAL_USMARC;
186 r->len = strlen(r->record);
191 bend_deleteresult *bend_delete(void *handle, bend_deleterequest *q, int *num)
197 bend_scanresult *bend_scan(void *handle, bend_scanrequest *q, int *num)
199 static struct scan_entry list[200];
200 static char buf[200][200];
201 static bend_scanresult r;
204 r.term_position = q->term_position;
205 r.num_entries = q->num_entries;
207 for (i = 0; i < r.num_entries; i++)
209 list[i].term = buf[i];
210 sprintf(list[i].term, "term-%d", i+1);
211 list[i].occurrences = rand() % 100000;
219 * silly dummy-scan what reads words from a file.
221 bend_scanresult *bend_scan(void *handle, bend_scanrequest *q, int *num)
223 bend_scanresult *r = odr_malloc (q->stream, sizeof(*r));
225 static struct scan_entry list[200];
226 static char entries[200][80];
233 r->status = BEND_SCAN_SUCCESS;
234 if (!f && !(f = fopen("dummy-words", "r")))
236 perror("dummy-words");
239 if (q->term->term->which != Z_Term_general)
241 r->errcode = 229; /* unsupported term type */
244 if (q->term->term->u.general->len >= 80)
246 r->errcode = 11; /* term too long */
249 if (q->num_entries > 200)
254 memcpy(term, q->term->term->u.general->buf, q->term->term->u.general->len);
255 term[q->term->term->u.general->len] = '\0';
256 for (p = term; *p; p++)
262 for (i = 0, pos = 0; fscanf(f, " %79[^:]:%d", entries[pos], &hits[pos]) == 2;
263 i++, pos < 199 ? pos++ : (pos = 0))
265 if (!r->num_entries && strcmp(entries[pos], term) >= 0) /* s-point fnd */
267 if ((r->term_position = q->term_position) > i + 1)
269 r->term_position = i + 1;
270 r->status = BEND_SCAN_PARTIAL;
272 for (; r->num_entries < r->term_position; r->num_entries++)
276 po = pos - r->term_position + r->num_entries + 1; /* find pos */
279 list[r->num_entries].term = entries[po];
280 list[r->num_entries].occurrences = hits[po];
283 else if (r->num_entries)
285 list[r->num_entries].term = entries[pos];
286 list[r->num_entries].occurrences = hits[pos];
289 if (r->num_entries >= q->num_entries)
293 r->status = BEND_SCAN_PARTIAL;
299 void bend_close(void *handle)
306 int main(int argc, char **argv)
308 statserv_main(argc, argv);
309 statserv_closedown();
313 /* Windows version with Service support */
321 static Args ArgDetails;
323 /* name of the executable */
324 #define SZAPPNAME "server"
326 /* internal name of the service */
327 #define SZSERVICENAME "Z3950 Test Server"
329 /* displayed name of the service */
330 #define SZSERVICEDISPLAYNAME "Z3950 Test Server"
332 /* list of service dependencies - "dep1\0dep2\0\0" */
333 #define SZDEPENDENCIES ""
335 int main(int argc, char **argv)
337 /* Lets setup the Arg structure */
338 ArgDetails.argc = argc;
339 ArgDetails.argv = argv;
341 /* Now setup the service with the service controller */
342 SetupService(argc, argv, &ArgDetails, SZAPPNAME, SZSERVICENAME, SZSERVICEDISPLAYNAME, SZDEPENDENCIES);
346 int StartAppService(void *pHandle, int argc, char **argv)
348 /* Initializes the App */
352 void RunAppService(void *pHandle)
354 Args *pArgs = (Args *)pHandle;
356 /* Starts the app running */
357 statserv_main(pArgs->argc, pArgs->argv);
360 void StopAppService(void *pHandle)
363 statserv_closedown();