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.2 1997-09-04 13:50:31 adam
16 * Demonstration of simple server
28 /* Specifically for NT Services - Shouldn't cause problems on UNIX */
31 Z_GenericRecord *read_grs1(FILE *f, ODR o);
33 bend_initresult *bend_init(bend_initrequest *q)
35 static bend_initresult r;
36 static char *dummy = "Hej fister";
44 bend_searchresult *bend_search(void *handle, bend_searchrequest *q, int *fd)
46 static bend_searchresult r;
54 static int atoin (const char *buf, int n)
60 val = val*10 + (*buf - '0');
66 char *marc_read(FILE *inf)
72 if (fread (length_str, 1, 5, inf) != 5)
74 size = atoin (length_str, 5);
77 if (!(buf = xmalloc (size+1)))
79 if (fread (buf+5, 1, size-5, inf) != (size-5))
84 memcpy (buf, length_str, 5);
89 static char *dummy_database_record (int num)
91 FILE *inf = fopen ("dummy-records", "r");
102 assert(buf = xmalloc(2101));
103 memset(buf, 'A', 2100);
108 buf = marc_read (inf);
118 static Z_GenericRecord *dummy_grs_record (int num, ODR o)
120 FILE *f = fopen("dummy-grs", "r");
122 Z_GenericRecord *r = 0;
127 while (fgets(line, 512, f))
128 if (*line == '#' && sscanf(line, "#%d", &n) == 1 && n == num)
137 bend_fetchresult *bend_fetch(void *handle, bend_fetchrequest *q, int *num)
139 static bend_fetchresult r;
140 static char *bbb = 0;
144 r.basename = "DUMMY";
151 if (q->format == VAL_SUTRS)
155 sprintf(buf, "This is dummy SUTRS record number %d\n", q->number);
156 assert(r.record = bbb = xmalloc(strlen(buf)+1));
160 else if (q->format == VAL_GRS1)
163 r.record = (char*) dummy_grs_record(q->number, q->stream);
170 else if (!(r.record = bbb = dummy_database_record(q->number)))
176 r.len = strlen(r.record);
177 r.format = q->format;
182 bend_deleteresult *bend_delete(void *handle, bend_deleterequest *q, int *num)
188 bend_scanresult *bend_scan(void *handle, bend_scanrequest *q, int *num)
190 static struct scan_entry list[200];
191 static char buf[200][200];
192 static bend_scanresult r;
195 r.term_position = q->term_position;
196 r.num_entries = q->num_entries;
198 for (i = 0; i < r.num_entries; i++)
200 list[i].term = buf[i];
201 sprintf(list[i].term, "term-%d", i+1);
202 list[i].occurrences = rand() % 100000;
210 * silly dummy-scan what reads words from a file.
212 bend_scanresult *bend_scan(void *handle, bend_scanrequest *q, int *num)
214 static bend_scanresult r;
216 static struct scan_entry list[200];
217 static char entries[200][80];
224 r.status = BEND_SCAN_SUCCESS;
225 if (!f && !(f = fopen("dummy-words", "r")))
227 perror("dummy-words");
230 if (q->term->term->which != Z_Term_general)
232 r.errcode = 229; /* unsupported term type */
235 if (q->term->term->u.general->len >= 80)
237 r.errcode = 11; /* term too long */
240 if (q->num_entries > 200)
245 memcpy(term, q->term->term->u.general->buf, q->term->term->u.general->len);
246 term[q->term->term->u.general->len] = '\0';
247 for (p = term; *p; p++)
253 for (i = 0, pos = 0; fscanf(f, " %79[^:]:%d", entries[pos], &hits[pos]) == 2;
254 i++, pos < 199 ? pos++ : (pos = 0))
256 if (!r.num_entries && strcmp(entries[pos], term) >= 0) /* s-point fnd */
258 if ((r.term_position = q->term_position) > i + 1)
260 r.term_position = i + 1;
261 r.status = BEND_SCAN_PARTIAL;
263 for (; r.num_entries < r.term_position; r.num_entries++)
267 po = pos - r.term_position + r.num_entries + 1; /* find pos */
270 list[r.num_entries].term = entries[po];
271 list[r.num_entries].occurrences = hits[po];
274 else if (r.num_entries)
276 list[r.num_entries].term = entries[pos];
277 list[r.num_entries].occurrences = hits[pos];
280 if (r.num_entries >= q->num_entries)
284 r.status = BEND_SCAN_PARTIAL;
290 void bend_close(void *handle)
301 static Args ArgDetails;
303 /* name of the executable */
304 #define SZAPPNAME "server"
306 /* internal name of the service */
307 #define SZSERVICENAME "Z3950 Test Server"
309 /* displayed name of the service */
310 #define SZSERVICEDISPLAYNAME "Z3950 Test Server"
312 /* list of service dependencies - "dep1\0dep2\0\0" */
313 #define SZDEPENDENCIES ""
315 int main(int argc, char **argv)
317 /* Lets setup the Arg structure */
318 ArgDetails.argc = argc;
319 ArgDetails.argv = argv;
323 /* Now setup the service with the service controller */
324 SetupService(argc, argv, &ArgDetails, SZAPPNAME, SZSERVICENAME, SZSERVICEDISPLAYNAME, SZDEPENDENCIES);
328 /* The service controller does the following for us under windows */
329 if (StartAppService(NULL, argc, argv))
330 RunAppService(&ArgDetails);
332 /* Ensure the service has been stopped */
333 StopAppService(NULL);
340 int StartAppService(void *pHandle, int argc, char **argv)
342 /* Initializes the App */
346 void RunAppService(void *pHandle)
348 Args *pArgs = (Args *)pHandle;
350 /* Starts the app running */
351 statserv_main(pArgs->argc, pArgs->argv);
354 void StopAppService(void *pHandle)
357 statserv_closedown();