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