2 /* little dummy-server */
13 /* Specifically for NT Services - Shouldn't cause problems on UNIX */
16 Z_GenericRecord *read_grs1(FILE *f, ODR o);
18 bend_initresult *bend_init(bend_initrequest *q)
20 static bend_initresult r;
21 static char *dummy = "Hej fister";
29 bend_searchresult *bend_search(void *handle, bend_searchrequest *q, int *fd)
31 static bend_searchresult r;
39 static int atoin (const char *buf, int n)
45 val = val*10 + (*buf - '0');
51 char *marc_read(FILE *inf)
57 if (fread (length_str, 1, 5, inf) != 5)
59 size = atoin (length_str, 5);
62 if (!(buf = xmalloc (size+1)))
64 if (fread (buf+5, 1, size-5, inf) != (size-5))
69 memcpy (buf, length_str, 5);
74 static char *dummy_database_record (int num)
76 FILE *inf = fopen ("dummy-records", "r");
87 assert(buf = xmalloc(2101));
88 memset(buf, 'A', 2100);
93 buf = marc_read (inf);
103 static Z_GenericRecord *dummy_grs_record (int num, ODR o)
105 FILE *f = fopen("dummy-grs", "r");
107 Z_GenericRecord *r = 0;
112 while (fgets(line, 512, f))
113 if (*line == '#' && sscanf(line, "#%d", &n) == 1 && n == num)
122 bend_fetchresult *bend_fetch(void *handle, bend_fetchrequest *q, int *num)
124 static bend_fetchresult r;
125 static char *bbb = 0;
129 r.basename = "DUMMY";
136 if (q->format == VAL_SUTRS)
140 sprintf(buf, "This is dummy SUTRS record number %d\n", q->number);
141 assert(r.record = bbb = xmalloc(strlen(buf)+1));
146 else if (q->format == VAL_GRS1)
148 Z_GenericRecord *rec = odr_malloc(q->stream, sizeof(*rec));
149 Z_TaggedElement *t1 = odr_malloc(q->stream, sizeof(*t1));
150 Z_StringOrNumeric *s1 = odr_malloc(q->stream, sizeof(*s1));
151 Z_ElementData *c1 = odr_malloc(q->stream, sizeof(*c1));
153 rec->elements = odr_malloc(q->stream, sizeof(Z_TaggedElement*)*10);
154 rec->num_elements = 1;
155 rec->elements[0] = t1 ;
156 t1->tagType = odr_malloc(q->stream, sizeof(int));
159 s1->which = Z_StringOrNumeric_string;
160 s1->u.string = "title";
161 t1->tagOccurrence = 0;
163 c1->which = Z_ElementData_string;
164 c1->u.string = "The Bad Seed and The Ugly Duckling";
166 t1->appliedVariant = 0;
167 r.record = (char*) rec;
171 else if (q->format == VAL_GRS1)
174 r.record = (char*) dummy_grs_record(q->number, q->stream);
181 else if (!(r.record = bbb = dummy_database_record(q->number)))
187 r.len = strlen(r.record);
188 r.format = q->format;
193 bend_deleteresult *bend_delete(void *handle, bend_deleterequest *q, int *num)
199 bend_scanresult *bend_scan(void *handle, bend_scanrequest *q, int *num)
201 static struct scan_entry list[200];
202 static char buf[200][200];
203 static bend_scanresult r;
206 r.term_position = q->term_position;
207 r.num_entries = q->num_entries;
209 for (i = 0; i < r.num_entries; i++)
211 list[i].term = buf[i];
212 sprintf(list[i].term, "term-%d", i+1);
213 list[i].occurrences = rand() % 100000;
221 * silly dummy-scan what reads words from a file.
223 bend_scanresult *bend_scan(void *handle, bend_scanrequest *q, int *num)
225 static bend_scanresult r;
227 static struct scan_entry list[200];
228 static char entries[200][80];
235 r.status = BEND_SCAN_SUCCESS;
236 if (!f && !(f = fopen("dummy-words", "r")))
238 perror("dummy-words");
241 if (q->term->term->which != Z_Term_general)
243 r.errcode = 229; /* unsupported term type */
246 if (q->term->term->u.general->len >= 80)
248 r.errcode = 11; /* term too long */
251 if (q->num_entries > 200)
256 memcpy(term, q->term->term->u.general->buf, q->term->term->u.general->len);
257 term[q->term->term->u.general->len] = '\0';
258 for (p = term; *p; p++)
264 for (i = 0, pos = 0; fscanf(f, " %79[^:]:%d", entries[pos], &hits[pos]) == 2;
265 i++, pos < 199 ? pos++ : (pos = 0))
267 if (!r.num_entries && strcmp(entries[pos], term) >= 0) /* s-point fnd */
269 if ((r.term_position = q->term_position) > i + 1)
271 r.term_position = i + 1;
272 r.status = BEND_SCAN_PARTIAL;
274 for (; r.num_entries < r.term_position; r.num_entries++)
278 po = pos - r.term_position + r.num_entries + 1; /* find pos */
281 list[r.num_entries].term = entries[po];
282 list[r.num_entries].occurrences = hits[po];
285 else if (r.num_entries)
287 list[r.num_entries].term = entries[pos];
288 list[r.num_entries].occurrences = hits[pos];
291 if (r.num_entries >= q->num_entries)
295 r.status = BEND_SCAN_PARTIAL;
301 void bend_close(void *handle)
312 static Args ArgDetails;
314 /* name of the executable */
315 #define SZAPPNAME "server"
317 /* internal name of the service */
318 #define SZSERVICENAME "Z3950 Test Server"
320 /* displayed name of the service */
321 #define SZSERVICEDISPLAYNAME "Z3950 Test Server"
323 /* list of service dependencies - "dep1\0dep2\0\0" */
324 #define SZDEPENDENCIES ""
326 int main(int argc, char **argv)
328 /* Lets setup the Arg structure */
329 ArgDetails.argc = argc;
330 ArgDetails.argv = argv;
334 /* Now setup the service with the service controller */
335 SetupService(argc, argv, &ArgDetails, SZAPPNAME, SZSERVICENAME, SZSERVICEDISPLAYNAME, SZDEPENDENCIES);
339 /* The service controller does the following for us under windows */
340 if (StartAppService(NULL, argc, argv))
343 /* Ensure the service has been stopped */
344 StopAppService(NULL);
351 int StartAppService(void *pHandle, int argc, char **argv)
353 /* Initializes the App */
357 void RunAppService(void *pHandle)
359 Args *pArgs = (Args *)pHandle;
361 /* Starts the app running */
362 statserv_main(pArgs->argc, pArgs->argv);
365 void StopAppService(void *pHandle)
368 statserv_closedown();