client counting (for debugging)
[pazpar2-moved-to-github.git] / src / client.c
1 /* This file is part of Pazpar2.
2    Copyright (C) 2006-2010 Index Data
3
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
7 version.
8
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
12 for more details.
13
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
17
18 */
19
20 /** \file client.c
21     \brief Z39.50 client 
22 */
23
24 #if HAVE_CONFIG_H
25 #include <config.h>
26 #endif
27 #include <pthread.h>
28 #include <stdlib.h>
29 #include <stdio.h>
30 #include <string.h>
31 #if HAVE_SYS_TIME_H
32 #include <sys/time.h>
33 #endif
34 #if HAVE_UNISTD_H
35 #include <unistd.h>
36 #endif
37 #include <signal.h>
38 #include <assert.h>
39
40 #include <yaz/marcdisp.h>
41 #include <yaz/comstack.h>
42 #include <yaz/tcpip.h>
43 #include <yaz/proto.h>
44 #include <yaz/readconf.h>
45 #include <yaz/pquery.h>
46 #include <yaz/otherinfo.h>
47 #include <yaz/yaz-util.h>
48 #include <yaz/nmem.h>
49 #include <yaz/query-charset.h>
50 #include <yaz/querytowrbuf.h>
51 #include <yaz/oid_db.h>
52 #include <yaz/diagbib1.h>
53 #include <yaz/snprintf.h>
54 #include <yaz/rpn2cql.h>
55
56 #define USE_TIMING 0
57 #if USE_TIMING
58 #include <yaz/timing.h>
59 #endif
60
61 #include "ppmutex.h"
62 #include "session.h"
63 #include "parameters.h"
64 #include "client.h"
65 #include "connection.h"
66 #include "settings.h"
67 #include "relevance.h"
68 #include "incref.h"
69
70 /* client counting (1) , disable client counting (0) */
71 #if 1
72 static YAZ_MUTEX g_mutex = 0;
73 static int no_clients = 0;
74
75 static void client_use(int delta)
76 {
77     if (!g_mutex)
78         yaz_mutex_create(&g_mutex);
79     yaz_mutex_enter(g_mutex);
80     no_clients += delta;
81     yaz_mutex_leave(g_mutex);
82     yaz_log(YLOG_LOG, "%s clients=%d", delta > 0 ? "INC" : "DEC", no_clients);
83 }
84 #else
85 #define client_use(x)
86 #endif
87
88 /** \brief Represents client state for a connection to one search target */
89 struct client {
90     struct session_database *database;
91     struct connection *connection;
92     struct session *session;
93     char *pquery; // Current search
94     char *cqlquery; // used for SRU targets only
95     Odr_int hits;
96     int record_offset;
97     int maxrecs;
98     int startrecs;
99     int diagnostic;
100     enum client_state state;
101     struct show_raw *show_raw;
102     struct client *next;     // next client in session or next in free list
103     ZOOM_resultset resultset;
104     YAZ_MUTEX mutex;
105     int ref_count;
106 };
107
108 struct show_raw {
109     int active; // whether this request has been sent to the server
110     int position;
111     int binary;
112     char *syntax;
113     char *esn;
114     void (*error_handler)(void *data, const char *addinfo);
115     void (*record_handler)(void *data, const char *buf, size_t sz);
116     void *data;
117     struct show_raw *next;
118 };
119
120 static const char *client_states[] = {
121     "Client_Connecting",
122     "Client_Idle",
123     "Client_Working",
124     "Client_Error",
125     "Client_Failed",
126     "Client_Disconnected"
127 };
128
129 const char *client_get_state_str(struct client *cl)
130 {
131     return client_states[cl->state];
132 }
133
134 enum client_state client_get_state(struct client *cl)
135 {
136     return cl->state;
137 }
138
139 void client_set_state(struct client *cl, enum client_state st)
140 {
141     cl->state = st;
142     /* no need to check for all client being non-active if this one
143        already is. Note that session_active_clients also LOCKS session */
144 #if 0
145     if (!client_is_active(cl) && cl->session)
146     {
147         int no_active = session_active_clients(cl->session);
148         if (no_active == 0)
149             session_alert_watch(cl->session, SESSION_WATCH_SHOW);
150     }
151 #endif
152 }
153
154 static void client_show_raw_error(struct client *cl, const char *addinfo);
155
156 // Close connection and set state to error
157 void client_fatal(struct client *cl)
158 {
159     yaz_log(YLOG_WARN, "Fatal error from %s", client_get_url(cl));
160     connection_destroy(cl->connection);
161     client_set_state(cl, Client_Error);
162 }
163
164 struct connection *client_get_connection(struct client *cl)
165 {
166     return cl->connection;
167 }
168
169 struct session_database *client_get_database(struct client *cl)
170 {
171     return cl->database;
172 }
173
174 struct session *client_get_session(struct client *cl)
175 {
176     return cl->session;
177 }
178
179 const char *client_get_pquery(struct client *cl)
180 {
181     return cl->pquery;
182 }
183
184 static void client_send_raw_present(struct client *cl);
185 static int nativesyntax_to_type(struct session_database *sdb, char *type,
186                                 ZOOM_record rec);
187
188 static void client_show_immediate(
189     ZOOM_resultset resultset, struct session_database *sdb, int position,
190     void *data,
191     void (*error_handler)(void *data, const char *addinfo),
192     void (*record_handler)(void *data, const char *buf, size_t sz),
193     int binary)
194 {
195     ZOOM_record rec = 0;
196     char type[80];
197     const char *buf;
198     int len;
199
200     if (!resultset)
201     {
202         error_handler(data, "no resultset");
203         return;
204     }
205     rec = ZOOM_resultset_record(resultset, position-1);
206     if (!rec)
207     {
208         error_handler(data, "no record");
209         return;
210     }
211     if (binary)
212         strcpy(type, "raw");
213     else
214         nativesyntax_to_type(sdb, type, rec);
215     buf = ZOOM_record_get(rec, type, &len);
216     if (!buf)
217     {
218         error_handler(data, "no record");
219         return;
220     }
221     record_handler(data, buf, len);
222 }
223
224
225 int client_show_raw_begin(struct client *cl, int position,
226                           const char *syntax, const char *esn,
227                           void *data,
228                           void (*error_handler)(void *data, const char *addinfo),
229                           void (*record_handler)(void *data, const char *buf,
230                                                  size_t sz),
231                           int binary)
232 {
233     if (syntax == 0 && esn == 0)
234         client_show_immediate(cl->resultset, client_get_database(cl),
235                               position, data,
236                               error_handler, record_handler,
237                               binary);
238     else
239     {
240         struct show_raw *rr, **rrp;
241
242         if (!cl->connection)
243             return -1;
244     
245
246         rr = xmalloc(sizeof(*rr));
247         rr->position = position;
248         rr->active = 0;
249         rr->data = data;
250         rr->error_handler = error_handler;
251         rr->record_handler = record_handler;
252         rr->binary = binary;
253         if (syntax)
254             rr->syntax = xstrdup(syntax);
255         else
256             rr->syntax = 0;
257         if (esn)
258             rr->esn = xstrdup(esn);
259         else
260             rr->esn = 0;
261         rr->next = 0;
262         
263         for (rrp = &cl->show_raw; *rrp; rrp = &(*rrp)->next)
264             ;
265         *rrp = rr;
266         
267         if (cl->state == Client_Failed)
268         {
269             client_show_raw_error(cl, "client failed");
270         }
271         else if (cl->state == Client_Disconnected)
272         {
273             client_show_raw_error(cl, "client disconnected");
274         }
275         else
276         {
277             client_send_raw_present(cl);
278         }
279     }
280     return 0;
281 }
282
283 static void client_show_raw_delete(struct show_raw *r)
284 {
285     xfree(r->syntax);
286     xfree(r->esn);
287     xfree(r);
288 }
289
290 void client_show_raw_remove(struct client *cl, void *data)
291 {
292     struct show_raw *rr = data;
293     struct show_raw **rrp = &cl->show_raw;
294     while (*rrp != rr)
295         rrp = &(*rrp)->next;
296     if (*rrp)
297     {
298         *rrp = rr->next;
299         client_show_raw_delete(rr);
300     }
301 }
302
303 void client_show_raw_dequeue(struct client *cl)
304 {
305     struct show_raw *rr = cl->show_raw;
306
307     cl->show_raw = rr->next;
308     client_show_raw_delete(rr);
309 }
310
311 static void client_show_raw_error(struct client *cl, const char *addinfo)
312 {
313     while (cl->show_raw)
314     {
315         cl->show_raw->error_handler(cl->show_raw->data, addinfo);
316         client_show_raw_dequeue(cl);
317     }
318 }
319
320 static void client_send_raw_present(struct client *cl)
321 {
322     struct session_database *sdb = client_get_database(cl);
323     struct connection *co = client_get_connection(cl);
324     ZOOM_resultset set = cl->resultset;
325
326     int offset = cl->show_raw->position;
327     const char *syntax = 0;
328     const char *elements = 0;
329
330     assert(cl->show_raw);
331     assert(set);
332
333     yaz_log(YLOG_DEBUG, "%s: trying to present %d record(s) from %d",
334             client_get_url(cl), 1, offset);
335
336     if (cl->show_raw->syntax)
337         syntax = cl->show_raw->syntax;
338     else
339         syntax = session_setting_oneval(sdb, PZ_REQUESTSYNTAX);
340     ZOOM_resultset_option_set(set, "preferredRecordSyntax", syntax);
341
342     if (cl->show_raw->esn)
343         elements = cl->show_raw->esn;
344     else
345         elements = session_setting_oneval(sdb, PZ_ELEMENTS);
346     if (elements && *elements)
347         ZOOM_resultset_option_set(set, "elementSetName", elements);
348
349     ZOOM_resultset_records(set, 0, offset-1, 1);
350     cl->show_raw->active = 1;
351
352     connection_continue(co);
353 }
354
355 static int nativesyntax_to_type(struct session_database *sdb, char *type,
356                                 ZOOM_record rec)
357 {
358     const char *s = session_setting_oneval(sdb, PZ_NATIVESYNTAX);
359
360     if (s && *s)
361     {
362         if (!strncmp(s, "iso2709", 7))
363         {
364             const char *cp = strchr(s, ';');
365             yaz_snprintf(type, 80, "xml; charset=%s", cp ? cp+1 : "marc-8s");
366         }
367         else if (!strncmp(s, "xml", 3))
368         {
369             strcpy(type, "xml");
370         }
371         else if (!strncmp(s, "txml", 4))
372         {
373             const char *cp = strchr(s, ';');
374             yaz_snprintf(type, 80, "txml; charset=%s", cp ? cp+1 : "marc-8s");
375         }
376         else
377             return -1;
378         return 0;
379     }
380     else  /* attempt to deduce structure */
381     {
382         const char *syntax = ZOOM_record_get(rec, "syntax", NULL);
383         if (syntax)
384         {
385             if (!strcmp(syntax, "XML"))
386             {
387                 strcpy(type, "xml");
388                 return 0;
389             }
390             else if (!strcmp(syntax, "TXML"))
391                 {
392                     strcpy(type, "txml");
393                     return 0;
394                 }
395             else if (!strcmp(syntax, "USmarc") || !strcmp(syntax, "MARC21"))
396             {
397                 strcpy(type, "xml; charset=marc8-s");
398                 return 0;
399             }
400             else return -1;
401         }
402         else return -1;
403     }
404 }
405
406 static void ingest_raw_record(struct client *cl, ZOOM_record rec)
407 {
408     const char *buf;
409     int len;
410     char type[80];
411
412     if (cl->show_raw->binary)
413         strcpy(type, "raw");
414     else
415     {
416         struct session_database *sdb = client_get_database(cl);
417         nativesyntax_to_type(sdb, type, rec);
418     }
419
420     buf = ZOOM_record_get(rec, type, &len);
421     cl->show_raw->record_handler(cl->show_raw->data,  buf, len);
422     client_show_raw_dequeue(cl);
423 }
424
425 void client_search_response(struct client *cl)
426 {
427     struct connection *co = cl->connection;
428     struct session *se = cl->session;
429     ZOOM_connection link = connection_get_link(co);
430     ZOOM_resultset resultset = cl->resultset;
431     const char *error, *addinfo = 0;
432     
433     if (ZOOM_connection_error(link, &error, &addinfo))
434     {
435         cl->hits = 0;
436         client_set_state(cl, Client_Error);
437         yaz_log(YLOG_WARN, "Search error %s (%s): %s",
438                 error, addinfo, client_get_url(cl));
439     }
440     else
441     {
442         cl->record_offset = cl->startrecs;
443         cl->hits = ZOOM_resultset_size(resultset);
444         if (se)
445             se->total_hits += cl->hits;
446     }
447 }
448
449 void client_got_records(struct client *cl)
450 {
451     if (cl->session)
452     {
453         session_alert_watch(cl->session, SESSION_WATCH_SHOW);
454         session_alert_watch(cl->session, SESSION_WATCH_RECORD);
455     }
456 }
457
458 void client_record_response(struct client *cl)
459 {
460     struct connection *co = cl->connection;
461     ZOOM_connection link = connection_get_link(co);
462     ZOOM_resultset resultset = cl->resultset;
463     const char *error, *addinfo;
464
465     if (ZOOM_connection_error(link, &error, &addinfo))
466     {
467         client_set_state(cl, Client_Error);
468         yaz_log(YLOG_WARN, "Search error %s (%s): %s",
469             error, addinfo, client_get_url(cl));
470     }
471     else
472     {
473         ZOOM_record rec = 0;
474         const char *msg, *addinfo;
475         
476         if (cl->show_raw && cl->show_raw->active)
477         {
478             if ((rec = ZOOM_resultset_record(resultset,
479                                              cl->show_raw->position-1)))
480             {
481                 cl->show_raw->active = 0;
482                 ingest_raw_record(cl, rec);
483             }
484             else
485             {
486                 yaz_log(YLOG_WARN, "Expected record, but got NULL, offset=%d",
487                         cl->show_raw->position-1);
488             }
489         }
490         else
491         {
492             int offset = cl->record_offset;
493             if ((rec = ZOOM_resultset_record(resultset, offset)))
494             {
495                 cl->record_offset++;
496                 if (cl->session == 0)
497                     ;
498                 else if (ZOOM_record_error(rec, &msg, &addinfo, 0))
499                 {
500                     yaz_log(YLOG_WARN, "Record error %s (%s): %s (rec #%d)",
501                             msg, addinfo, client_get_url(cl),
502                             cl->record_offset);
503                 }
504                 else
505                 {
506                     struct session_database *sdb = client_get_database(cl);
507                     NMEM nmem = nmem_create();
508                     const char *xmlrec;
509                     char type[80];
510
511                     if (nativesyntax_to_type(sdb, type, rec))
512                         yaz_log(YLOG_WARN, "Failed to determine record type");
513                     xmlrec = ZOOM_record_get(rec, type, NULL);
514                     if (!xmlrec)
515                         yaz_log(YLOG_WARN, "ZOOM_record_get failed from %s",
516                                 client_get_url(cl));
517                     else
518                     {
519                         if (ingest_record(cl, xmlrec, cl->record_offset, nmem))
520                             yaz_log(YLOG_WARN, "Failed to ingest from %s",
521                                     client_get_url(cl));
522                     }
523                     nmem_destroy(nmem);
524                 }
525             }
526             else
527             {
528                 yaz_log(YLOG_WARN, "Expected record, but got NULL, offset=%d",
529                         offset);
530             }
531         }
532     }
533 }
534
535 void client_start_search(struct client *cl)
536 {
537     struct session_database *sdb = client_get_database(cl);
538     struct connection *co = client_get_connection(cl);
539     ZOOM_connection link = connection_get_link(co);
540     ZOOM_resultset rs;
541     char *databaseName = sdb->database->databases[0];
542     const char *opt_piggyback = session_setting_oneval(sdb, PZ_PIGGYBACK);
543     const char *opt_queryenc = session_setting_oneval(sdb, PZ_QUERYENCODING);
544     const char *opt_elements = session_setting_oneval(sdb, PZ_ELEMENTS);
545     const char *opt_requestsyn = session_setting_oneval(sdb, PZ_REQUESTSYNTAX);
546     const char *opt_maxrecs = session_setting_oneval(sdb, PZ_MAXRECS);
547     const char *opt_sru = session_setting_oneval(sdb, PZ_SRU);
548     const char *opt_sort = session_setting_oneval(sdb, PZ_SORT);
549     char maxrecs_str[24], startrecs_str[24];
550
551     assert(link);
552
553     cl->hits = -1;
554     cl->record_offset = 0;
555     cl->diagnostic = 0;
556     client_set_state(cl, Client_Working);
557
558     if (*opt_piggyback)
559         ZOOM_connection_option_set(link, "piggyback", opt_piggyback);
560     else
561         ZOOM_connection_option_set(link, "piggyback", "1");
562     if (*opt_queryenc)
563         ZOOM_connection_option_set(link, "rpnCharset", opt_queryenc);
564     if (*opt_sru && *opt_elements)
565         ZOOM_connection_option_set(link, "schema", opt_elements);
566     else if (*opt_elements)
567         ZOOM_connection_option_set(link, "elementSetName", opt_elements);
568     if (*opt_requestsyn)
569         ZOOM_connection_option_set(link, "preferredRecordSyntax", opt_requestsyn);
570
571     if (!*opt_maxrecs)
572     {
573         sprintf(maxrecs_str, "%d", cl->maxrecs);
574         opt_maxrecs = maxrecs_str;
575     }
576     ZOOM_connection_option_set(link, "count", opt_maxrecs);
577
578
579     if (atoi(opt_maxrecs) > 20)
580         ZOOM_connection_option_set(link, "presentChunk", "20");
581     else
582         ZOOM_connection_option_set(link, "presentChunk", opt_maxrecs);
583
584     sprintf(startrecs_str, "%d", cl->startrecs);
585     ZOOM_connection_option_set(link, "start", startrecs_str);
586
587     if (databaseName)
588         ZOOM_connection_option_set(link, "databaseName", databaseName);
589
590     if (cl->cqlquery)
591     {
592         ZOOM_query q = ZOOM_query_create();
593         yaz_log(YLOG_LOG, "Search %s CQL: %s", sdb->database->url, cl->cqlquery);
594         ZOOM_query_cql(q, cl->cqlquery);
595         if (*opt_sort)
596             ZOOM_query_sortby(q, opt_sort);
597         rs = ZOOM_connection_search(link, q);
598         ZOOM_query_destroy(q);
599     }
600     else
601     {
602         yaz_log(YLOG_LOG, "Search %s PQF: %s", sdb->database->url, cl->pquery);
603         rs = ZOOM_connection_search_pqf(link, cl->pquery);
604     }
605     ZOOM_resultset_destroy(cl->resultset);
606     cl->resultset = rs;
607     connection_continue(co);
608 }
609
610 struct client *client_create(void)
611 {
612     struct client *r = xmalloc(sizeof(*r));
613     r->maxrecs = 100;
614     r->startrecs = 0;
615     r->pquery = 0;
616     r->cqlquery = 0;
617     r->database = 0;
618     r->connection = 0;
619     r->session = 0;
620     r->hits = 0;
621     r->record_offset = 0;
622     r->diagnostic = 0;
623     r->state = Client_Disconnected;
624     r->show_raw = 0;
625     r->resultset = 0;
626     r->next = 0;
627     r->mutex = 0;
628     pazpar2_mutex_create(&r->mutex, "client");
629
630     r->ref_count = 1;
631     client_use(1);
632     
633     return r;
634 }
635
636 void client_incref(struct client *c)
637 {
638     pazpar2_incref(&c->ref_count, c->mutex);
639     yaz_log(YLOG_LOG, "client_incref c=%p %s cnt=%d",
640             c, client_get_url(c), c->ref_count);
641 }
642
643 int client_destroy(struct client *c)
644 {
645     if (c)
646     {
647         yaz_log(YLOG_LOG, "client_destroy c=%p %s cnt=%d",
648                 c, client_get_url(c), c->ref_count);
649         if (!pazpar2_decref(&c->ref_count, c->mutex))
650         {
651             c->next = 0;
652             xfree(c->pquery);
653             c->pquery = 0;
654             xfree(c->cqlquery);
655             c->cqlquery = 0;
656             assert(!c->connection);
657             assert(!c->resultset);
658             
659             yaz_mutex_destroy(&c->mutex);
660             xfree(c);
661             client_use(-1);
662             return 1;
663         }
664     }
665     return 0;
666 }
667
668 void client_set_connection(struct client *cl, struct connection *con)
669 {
670     if (cl->resultset)
671     {
672         ZOOM_resultset_destroy(cl->resultset);
673         cl->resultset = 0;
674     }
675     if (con)
676     {
677         assert(cl->connection == 0);
678         cl->connection = con;
679         client_incref(cl);
680     }
681     else
682     {
683         cl->connection = con;
684         client_destroy(cl);
685     }
686 }
687
688 void client_disconnect(struct client *cl)
689 {
690     if (cl->state != Client_Idle)
691         client_set_state(cl, Client_Disconnected);
692     client_set_connection(cl, 0);
693 }
694
695 // Extract terms from query into null-terminated termlist
696 static void extract_terms(NMEM nmem, struct ccl_rpn_node *query, char **termlist)
697 {
698     int num = 0;
699
700     pull_terms(nmem, query, termlist, &num);
701     termlist[num] = 0;
702 }
703
704 // Initialize CCL map for a target
705 static CCL_bibset prepare_cclmap(struct client *cl)
706 {
707     struct session_database *sdb = client_get_database(cl);
708     struct setting *s;
709     CCL_bibset res;
710
711     if (!sdb->settings)
712         return 0;
713     res = ccl_qual_mk();
714     for (s = sdb->settings[PZ_CCLMAP]; s; s = s->next)
715     {
716         char *p = strchr(s->name + 3, ':');
717         if (!p)
718         {
719             yaz_log(YLOG_WARN, "Malformed cclmap name: %s", s->name);
720             ccl_qual_rm(&res);
721             return 0;
722         }
723         p++;
724         ccl_qual_fitem(res, s->value, p);
725     }
726     return res;
727 }
728
729 // returns a xmalloced CQL query corresponding to the pquery in client
730 static char *make_cqlquery(struct client *cl)
731 {
732     cql_transform_t cqlt = cql_transform_create();
733     Z_RPNQuery *zquery;
734     char *r;
735     WRBUF wrb = wrbuf_alloc();
736     int status;
737     ODR odr_out = odr_createmem(ODR_ENCODE);
738
739     zquery = p_query_rpn(odr_out, cl->pquery);
740     yaz_log(YLOG_LOG, "PQF: %s", cl->pquery);
741     if ((status = cql_transform_rpn2cql_wrbuf(cqlt, wrb, zquery)))
742     {
743         yaz_log(YLOG_WARN, "Failed to generate CQL query, code=%d", status);
744         r = 0;
745     }
746     else
747     {
748         r = xstrdup(wrbuf_cstr(wrb));
749     }     
750     wrbuf_destroy(wrb);
751     odr_destroy(odr_out);
752     cql_transform_close(cqlt);
753     return r;
754 }
755
756 // Parse the query given the settings specific to this client
757 int client_parse_query(struct client *cl, const char *query)
758 {
759     struct session *se = client_get_session(cl);
760     struct session_database *sdb = client_get_database(cl);
761     struct ccl_rpn_node *cn;
762     int cerror, cpos;
763     CCL_bibset ccl_map = prepare_cclmap(cl);
764     const char *sru = session_setting_oneval(sdb, PZ_SRU);
765     const char *pqf_prefix = session_setting_oneval(sdb, PZ_PQF_PREFIX);
766     const char *pqf_strftime = session_setting_oneval(sdb, PZ_PQF_STRFTIME);
767
768     if (!ccl_map)
769         return -1;
770
771     cn = ccl_find_str(ccl_map, query, &cerror, &cpos);
772     ccl_qual_rm(&ccl_map);
773     if (!cn)
774     {
775         client_set_state(cl, Client_Error);
776         yaz_log(YLOG_WARN, "Failed to parse CCL query %s for %s",
777                 query,
778                 client_get_database(cl)->database->url);
779         return -1;
780     }
781     wrbuf_rewind(se->wrbuf);
782     if (*pqf_prefix)
783     {
784         wrbuf_puts(se->wrbuf, pqf_prefix);
785         wrbuf_puts(se->wrbuf, " ");
786     }
787     if (!pqf_strftime || !*pqf_strftime)
788         ccl_pquery(se->wrbuf, cn);
789     else
790     {
791         time_t cur_time = time(0);
792         struct tm *tm =  localtime(&cur_time);
793         char tmp_str[300];
794         const char *cp = tmp_str;
795
796         /* see man strftime(3) for things .. In particular %% gets converted
797          to %.. And That's our original query .. */
798         strftime(tmp_str, sizeof(tmp_str)-1, pqf_strftime, tm);
799         for (; *cp; cp++)
800         {
801             if (cp[0] == '%')
802                 ccl_pquery(se->wrbuf, cn);
803             else
804                 wrbuf_putc(se->wrbuf, cp[0]);
805         }
806     }
807     xfree(cl->pquery);
808     cl->pquery = xstrdup(wrbuf_cstr(se->wrbuf));
809
810     xfree(cl->cqlquery);
811     if (*sru)
812     {
813         if (!(cl->cqlquery = make_cqlquery(cl)))
814             return -1;
815     }
816     else
817         cl->cqlquery = 0;
818
819     if (!se->relevance)
820     {
821         // Initialize relevance structure with query terms
822         char *p[512];
823         extract_terms(se->nmem, cn, p);
824         se->relevance = relevance_create(
825             se->service->relevance_pct,
826             se->nmem, (const char **) p);
827     }
828
829     ccl_rpn_delete(cn);
830     return 0;
831 }
832
833
834 void client_remove_from_session(struct client *c)
835 {
836     struct session *se;
837
838     se = c->session;
839     assert(se);
840     if (se)
841     {
842         struct client **ccp = &se->clients;
843         
844         while (*ccp && *ccp != c)
845             ccp = &(*ccp)->next;
846         assert(*ccp == c);
847         *ccp = c->next;
848         
849         c->database = 0;
850         c->session = 0;
851         c->next = 0;
852     }
853 }
854
855 void client_set_session(struct client *cl, struct session *se)
856 {
857     cl->session = se;
858     cl->next = se->clients;
859     se->clients = cl;
860 }
861
862 int client_is_active(struct client *cl)
863 {
864     if (cl->connection && (cl->state == Client_Connecting ||
865                            cl->state == Client_Working))
866         return 1;
867     return 0;
868 }
869
870 struct client *client_next_in_session(struct client *cl)
871 {
872     if (cl)
873         return cl->next;
874     return 0;
875
876 }
877
878 Odr_int client_get_hits(struct client *cl)
879 {
880     return cl->hits;
881 }
882
883 int client_get_num_records(struct client *cl)
884 {
885     return cl->record_offset;
886 }
887
888 void client_set_diagnostic(struct client *cl, int diagnostic)
889 {
890     cl->diagnostic = diagnostic;
891 }
892
893 int client_get_diagnostic(struct client *cl)
894 {
895     return cl->diagnostic;
896 }
897
898 void client_set_database(struct client *cl, struct session_database *db)
899 {
900     cl->database = db;
901 }
902
903 struct host *client_get_host(struct client *cl)
904 {
905     return client_get_database(cl)->database->host;
906 }
907
908 const char *client_get_url(struct client *cl)
909 {
910     if (cl->database)
911         return client_get_database(cl)->database->url;
912     else
913         return "NOURL";
914 }
915
916 void client_set_maxrecs(struct client *cl, int v)
917 {
918     cl->maxrecs = v;
919 }
920
921 void client_set_startrecs(struct client *cl, int v)
922 {
923     cl->startrecs = v;
924 }
925
926 /*
927  * Local variables:
928  * c-basic-offset: 4
929  * c-file-style: "Stroustrup"
930  * indent-tabs-mode: nil
931  * End:
932  * vim: shiftwidth=4 tabstop=8 expandtab
933  */
934