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.3 1997-09-09 10:10:20 adam
11 * Another MSV5.0 port. Changed projects to include proper
12 * library/include paths.
13 * Server starts server in test-mode when no options are given.
15 * Revision 1.2 1997/09/04 13:50:31 adam
21 * Demonstration of simple server
33 /* Specifically for NT Services - Shouldn't cause problems on UNIX */
36 Z_GenericRecord *read_grs1(FILE *f, ODR o);
38 bend_initresult *bend_init(bend_initrequest *q)
40 static bend_initresult r;
41 static char *dummy = "Hej fister";
49 bend_searchresult *bend_search(void *handle, bend_searchrequest *q, int *fd)
51 static bend_searchresult r;
60 static int atoin (const char *buf, int n)
66 val = val*10 + (*buf - '0');
72 char *marc_read(FILE *inf)
78 if (fread (length_str, 1, 5, inf) != 5)
80 size = atoin (length_str, 5);
83 if (!(buf = xmalloc (size+1)))
85 if (fread (buf+5, 1, size-5, inf) != (size-5))
90 memcpy (buf, length_str, 5);
95 static char *dummy_database_record (int num)
97 FILE *inf = fopen ("dummy-records", "r");
108 assert(buf = xmalloc(2101));
109 memset(buf, 'A', 2100);
114 buf = marc_read (inf);
124 static Z_GenericRecord *dummy_grs_record (int num, ODR o)
126 FILE *f = fopen("dummy-grs", "r");
128 Z_GenericRecord *r = 0;
133 while (fgets(line, 512, f))
134 if (*line == '#' && sscanf(line, "#%d", &n) == 1 && n == num)
143 bend_fetchresult *bend_fetch(void *handle, bend_fetchrequest *q, int *num)
145 static bend_fetchresult r;
146 static char *bbb = 0;
150 r.basename = "DUMMY";
156 r.format = q->format;
157 if (q->format == VAL_SUTRS)
161 sprintf(buf, "This is dummy SUTRS record number %d\n", q->number);
162 assert(r.record = bbb = xmalloc(strlen(buf)+1));
166 else if (q->format == VAL_GRS1)
169 r.record = (char*) dummy_grs_record(q->number, q->stream);
176 else if (!(r.record = bbb = dummy_database_record(q->number)))
179 r.format = VAL_USMARC;
183 r.len = strlen(r.record);
188 bend_deleteresult *bend_delete(void *handle, bend_deleterequest *q, int *num)
194 bend_scanresult *bend_scan(void *handle, bend_scanrequest *q, int *num)
196 static struct scan_entry list[200];
197 static char buf[200][200];
198 static bend_scanresult r;
201 r.term_position = q->term_position;
202 r.num_entries = q->num_entries;
204 for (i = 0; i < r.num_entries; i++)
206 list[i].term = buf[i];
207 sprintf(list[i].term, "term-%d", i+1);
208 list[i].occurrences = rand() % 100000;
216 * silly dummy-scan what reads words from a file.
218 bend_scanresult *bend_scan(void *handle, bend_scanrequest *q, int *num)
220 static bend_scanresult r;
222 static struct scan_entry list[200];
223 static char entries[200][80];
230 r.status = BEND_SCAN_SUCCESS;
231 if (!f && !(f = fopen("dummy-words", "r")))
233 perror("dummy-words");
236 if (q->term->term->which != Z_Term_general)
238 r.errcode = 229; /* unsupported term type */
241 if (q->term->term->u.general->len >= 80)
243 r.errcode = 11; /* term too long */
246 if (q->num_entries > 200)
251 memcpy(term, q->term->term->u.general->buf, q->term->term->u.general->len);
252 term[q->term->term->u.general->len] = '\0';
253 for (p = term; *p; p++)
259 for (i = 0, pos = 0; fscanf(f, " %79[^:]:%d", entries[pos], &hits[pos]) == 2;
260 i++, pos < 199 ? pos++ : (pos = 0))
262 if (!r.num_entries && strcmp(entries[pos], term) >= 0) /* s-point fnd */
264 if ((r.term_position = q->term_position) > i + 1)
266 r.term_position = i + 1;
267 r.status = BEND_SCAN_PARTIAL;
269 for (; r.num_entries < r.term_position; r.num_entries++)
273 po = pos - r.term_position + r.num_entries + 1; /* find pos */
276 list[r.num_entries].term = entries[po];
277 list[r.num_entries].occurrences = hits[po];
280 else if (r.num_entries)
282 list[r.num_entries].term = entries[pos];
283 list[r.num_entries].occurrences = hits[pos];
286 if (r.num_entries >= q->num_entries)
290 r.status = BEND_SCAN_PARTIAL;
296 void bend_close(void *handle)
307 static Args ArgDetails;
309 /* name of the executable */
310 #define SZAPPNAME "server"
312 /* internal name of the service */
313 #define SZSERVICENAME "Z3950 Test Server"
315 /* displayed name of the service */
316 #define SZSERVICEDISPLAYNAME "Z3950 Test Server"
318 /* list of service dependencies - "dep1\0dep2\0\0" */
319 #define SZDEPENDENCIES ""
321 int main(int argc, char **argv)
323 /* Lets setup the Arg structure */
324 ArgDetails.argc = argc;
325 ArgDetails.argv = argv;
329 /* Now setup the service with the service controller */
330 SetupService(argc, argv, &ArgDetails, SZAPPNAME, SZSERVICENAME, SZSERVICEDISPLAYNAME, SZDEPENDENCIES);
334 /* The service controller does the following for us under windows */
335 if (StartAppService(NULL, argc, argv))
336 RunAppService(&ArgDetails);
338 /* Ensure the service has been stopped */
339 StopAppService(NULL);
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();