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