1 /* This file is part of the yazpp toolkit.
2 * Copyright (C) 1998-2011 Index Data and Mike Taylor
3 * See the file LICENSE for details.
8 #include <yaz/diagbib1.h>
9 #include <yaz/options.h>
10 #include <yazpp/z-server.h>
11 #include <yazpp/pdu-assoc.h>
12 #include <yazpp/socket-manager.h>
13 #include <yaz/oid_db.h>
15 using namespace yazpp_1;
17 class MyILL : public Yaz_Facility_ILL {
19 void ill_service (Z_ExtendedServicesRequest *req,
21 Z_ExtendedServicesResponse *res);
24 class MyUpdate : public Yaz_Facility_Update {
26 void update_service (Z_ExtendedServicesRequest *req,
28 Z_ExtendedServicesResponse *res);
29 void update_service0 (Z_ExtendedServicesRequest *req,
31 Z_ExtendedServicesResponse *res);
35 class MyRetrieval : public Yaz_Facility_Retrieval, Yaz_USMARC {
37 int sr_init (Z_InitRequest *initRequest,
38 Z_InitResponse *initResponse);
39 void sr_search (Z_SearchRequest *searchRequest,
40 Z_SearchResponse *searchResponse);
41 void sr_present (Z_PresentRequest *presentRequest,
42 Z_PresentResponse *presentResponse);
43 void sr_record (const char *resultSetName,
46 Z_RecordComposition *comp,
47 Z_NamePlusRecord *namePlusRecord,
51 class MyServer : public Z_Server {
54 MyServer(IPDU_Observable *the_PDU_Observable);
55 IPDU_Observer* sessionNotify(IPDU_Observable *the_PDU_Observable,
62 MyRetrieval m_retrieval;
68 void MyILL::ill_service (Z_ExtendedServicesRequest *req,
70 Z_ExtendedServicesResponse *res)
72 yaz_log (YLOG_LOG, "MyServer::ill_service");
75 void MyUpdate::update_service (Z_ExtendedServicesRequest *req,
77 Z_ExtendedServicesResponse *res)
79 yaz_log (YLOG_LOG, "MyServer::update_service (v1.1)");
82 void MyUpdate::update_service0 (Z_ExtendedServicesRequest *req,
84 Z_ExtendedServicesResponse *res)
86 yaz_log (YLOG_LOG, "MyServer::update_service (v1.0)");
89 int MyRetrieval::sr_init (Z_InitRequest *initRequest,
90 Z_InitResponse *initResponse)
92 yaz_log (YLOG_LOG, "MyServer::sr_init");
96 void MyRetrieval::sr_search (Z_SearchRequest *searchRequest,
97 Z_SearchResponse *searchResponse)
99 yaz_log (YLOG_LOG, "MyServer::recv_Z_search");
100 if (searchRequest->query->which == Z_Query_type_1)
102 Z_RPNStructure *s = searchRequest->query->u.type_1->RPNStructure;
103 if (s->which == Z_RPNStructure_simple &&
104 s->u.simple->which == Z_Operand_APT &&
105 s->u.simple->u.attributesPlusTerm->term->which == Z_Term_general)
107 Odr_oct *term = s->u.simple->u.attributesPlusTerm->term->u.general;
108 char *str = (char *) odr_malloc (odr_encode(), term->len+1);
110 memcpy (str, term->buf, term->len);
111 str[term->len] = '\0';
112 *searchResponse->resultCount = atoi(str);
117 void MyRetrieval::sr_present (Z_PresentRequest *presentRequest,
118 Z_PresentResponse *presentResponse)
120 yaz_log (YLOG_LOG, "MyServer::recv_Z_present");
123 void MyRetrieval::sr_record (const char *resultSetName,
126 Z_RecordComposition *comp,
127 Z_NamePlusRecord *namePlusRecord,
130 yaz_log (YLOG_LOG, "MyServer::recv_Z_record");
131 const char *rec = get_record(position);
133 create_databaseRecord(odr_encode(), namePlusRecord, 0,
134 yaz_oid_recsyn_usmarc, rec, strlen(rec));
136 create_surrogateDiagnostics(odr_encode(), namePlusRecord, 0,
137 YAZ_BIB1_PRESENT_REQUEST_OUT_OF_RANGE, 0);
140 MyServer::~MyServer()
144 IPDU_Observer *MyServer::sessionNotify(
145 IPDU_Observable *the_PDU_Observable, int fd)
147 MyServer *new_server;
149 new_server = new MyServer(the_PDU_Observable);
150 new_server->timeout(900);
151 new_server->facility_add(&new_server->m_retrieval, "my sr");
152 new_server->facility_add(&new_server->m_ill, "my ill");
153 new_server->facility_add(&new_server->m_update, "my update");
154 new_server->set_APDU_log(get_APDU_log());
159 MyServer::MyServer(IPDU_Observable *the_PDU_Observable) :
160 Z_Server (the_PDU_Observable)
165 void MyServer::timeoutNotify()
167 yaz_log (YLOG_LOG, "connection timed out");
171 void MyServer::failNotify()
173 yaz_log (YLOG_LOG, "connection closed by client");
177 void MyServer::connectNotify()
181 void usage(const char *prog)
183 fprintf (stderr, "%s: [-a log] [-v level] [-T] @:port\n", prog);
187 int main(int argc, char **argv)
192 const char *addr = "tcp:@:9999";
195 SocketManager mySocketManager;
197 PDU_Assoc *my_PDU_Assoc = 0;
202 while ((ret = options("a:v:T", argv, argc, &arg)) != -2)
210 apdu_log = xstrdup(arg);
213 yaz_log_init_level (yaz_log_mask_str(arg));
223 #if YAZ_POSIX_THREADS
225 my_PDU_Assoc = new PDU_AssocThread(&mySocketManager);
227 my_PDU_Assoc = new PDU_Assoc(&mySocketManager);
229 my_PDU_Assoc = new PDU_Assoc(&mySocketManager);
232 z = new MyServer(my_PDU_Assoc);
236 yaz_log (YLOG_LOG, "set_APDU_log %s", apdu_log);
237 z->set_APDU_log(apdu_log);
240 while (mySocketManager.processEvent() > 0)
248 * c-file-style: "Stroustrup"
249 * indent-tabs-mode: nil
251 * vim: shiftwidth=4 tabstop=8 expandtab