2 * Copyright (c) 1998-2001, Index Data.
3 * See the file LICENSE for details.
5 * $Id: yaz-my-server.cpp,v 1.10 2002-10-09 12:50:26 adam Exp $
9 #include <yaz/options.h>
10 #include <yaz++/z-server.h>
11 #include <yaz++/pdu-assoc.h>
12 #include <yaz++/socket-manager.h>
14 class MyILL : public Yaz_Facility_ILL {
16 void ill_service (Z_ExtendedServicesRequest *req,
18 Z_ExtendedServicesResponse *res);
21 class MyUpdate : public Yaz_Facility_Update {
23 void update_service (Z_ExtendedServicesRequest *req,
25 Z_ExtendedServicesResponse *res);
26 void update_service0 (Z_ExtendedServicesRequest *req,
28 Z_ExtendedServicesResponse *res);
32 class MyRetrieval : public Yaz_Facility_Retrieval, Yaz_USMARC {
34 int sr_init (Z_InitRequest *initRequest,
35 Z_InitResponse *initResponse);
36 void sr_search (Z_SearchRequest *searchRequest,
37 Z_SearchResponse *searchResponse);
38 void sr_present (Z_PresentRequest *presentRequest,
39 Z_PresentResponse *presentResponse);
40 void sr_record (const char *resultSetName,
43 Z_RecordComposition *comp,
44 Z_NamePlusRecord *namePlusRecord,
49 class MyUrsula : public Yaz_Facility_Ursula {
51 void ursula_service (Z_ExtendedServicesRequest *req,
53 Z_ExtendedServicesResponse *res);
57 class MyServer : public Yaz_Z_Server {
60 MyServer(IYaz_PDU_Observable *the_PDU_Observable);
61 IYaz_PDU_Observer* sessionNotify(IYaz_PDU_Observable *the_PDU_Observable,
68 MyRetrieval m_retrieval;
77 void MyILL::ill_service (Z_ExtendedServicesRequest *req,
79 Z_ExtendedServicesResponse *res)
81 yaz_log (LOG_LOG, "MyServer::ill_service");
84 void MyUpdate::update_service (Z_ExtendedServicesRequest *req,
86 Z_ExtendedServicesResponse *res)
88 yaz_log (LOG_LOG, "MyServer::update_service (v1.1)");
91 void MyUpdate::update_service0 (Z_ExtendedServicesRequest *req,
93 Z_ExtendedServicesResponse *res)
95 yaz_log (LOG_LOG, "MyServer::update_service (v1.0)");
99 void MyUrsula::ursula_service (Z_ExtendedServicesRequest *req,
101 Z_ExtendedServicesResponse *res)
103 yaz_log (LOG_LOG, "MyServer::ursula_service");
106 case Z_UrsPDU_request:
107 yaz_log(LOG_LOG, "request");
108 if (u->u.request->libraryNo)
109 yaz_log (LOG_LOG, "libraryNo: %s", u->u.request->libraryNo);
111 case Z_UrsPDU_update:
112 yaz_log(LOG_LOG, "request");
114 case Z_UrsPDU_reservation:
115 yaz_log(LOG_LOG, "request");
117 case Z_UrsPDU_renewal:
118 yaz_log(LOG_LOG, "request");
121 yaz_log(LOG_LOG, "unknown");
127 int MyRetrieval::sr_init (Z_InitRequest *initRequest,
128 Z_InitResponse *initResponse)
130 yaz_log (LOG_LOG, "MyServer::sr_init");
134 void MyRetrieval::sr_search (Z_SearchRequest *searchRequest,
135 Z_SearchResponse *searchResponse)
137 yaz_log (LOG_LOG, "MyServer::recv_Z_search");
138 if (searchRequest->query->which == Z_Query_type_1)
140 Z_RPNStructure *s = searchRequest->query->u.type_1->RPNStructure;
141 if (s->which == Z_RPNStructure_simple &&
142 s->u.simple->which == Z_Operand_APT &&
143 s->u.simple->u.attributesPlusTerm->term->which == Z_Term_general)
145 Odr_oct *term = s->u.simple->u.attributesPlusTerm->term->u.general;
146 char *str = (char *) odr_malloc (odr_encode(), term->len+1);
148 memcpy (str, term->buf, term->len);
149 str[term->len] = '\0';
150 *searchResponse->resultCount = atoi(str);
155 void MyRetrieval::sr_present (Z_PresentRequest *presentRequest,
156 Z_PresentResponse *presentResponse)
158 yaz_log (LOG_LOG, "MyServer::recv_Z_present");
161 void MyRetrieval::sr_record (const char *resultSetName,
164 Z_RecordComposition *comp,
165 Z_NamePlusRecord *namePlusRecord,
168 yaz_log (LOG_LOG, "MyServer::recv_Z_record");
169 const char *rec = get_record(position);
170 create_databaseRecord (odr_encode(), namePlusRecord, 0, VAL_USMARC, rec,
174 MyServer::~MyServer()
178 IYaz_PDU_Observer *MyServer::sessionNotify(
179 IYaz_PDU_Observable *the_PDU_Observable, int fd)
181 MyServer *new_server;
183 new_server = new MyServer(the_PDU_Observable);
184 new_server->timeout(900);
185 new_server->facility_add(&new_server->m_retrieval, "my sr");
186 new_server->facility_add(&new_server->m_ill, "my ill");
187 new_server->facility_add(&new_server->m_update, "my update");
188 #if HAVE_YAZ_URSULA_H
189 new_server->facility_add(&new_server->m_ursula, "my ursula");
192 new_server->set_APDU_log(get_APDU_log());
197 MyServer::MyServer(IYaz_PDU_Observable *the_PDU_Observable) :
198 Yaz_Z_Server (the_PDU_Observable)
203 void MyServer::timeoutNotify()
205 yaz_log (LOG_LOG, "connection timed out");
209 void MyServer::failNotify()
211 yaz_log (LOG_LOG, "connection closed by client");
215 void MyServer::connectNotify()
219 void usage(char *prog)
221 fprintf (stderr, "%s: [-a log] [-v level] [-T] @:port\n", prog);
225 int main(int argc, char **argv)
230 char *addr = "tcp:@:9999";
233 Yaz_SocketManager mySocketManager;
235 Yaz_PDU_Assoc *my_PDU_Assoc = 0;
240 while ((ret = options("a:v:T", argv, argc, &arg)) != -2)
248 apdu_log = xstrdup(arg);
251 yaz_log_init_level (yaz_log_mask_str(arg));
261 #if YAZ_POSIX_THREADS
263 my_PDU_Assoc = new Yaz_PDU_AssocThread(&mySocketManager);
265 my_PDU_Assoc = new Yaz_PDU_Assoc(&mySocketManager);
267 my_PDU_Assoc = new Yaz_PDU_Assoc(&mySocketManager);
270 z = new MyServer(my_PDU_Assoc);
274 yaz_log (LOG_LOG, "set_APDU_log %s", apdu_log);
275 z->set_APDU_log(apdu_log);
278 while (mySocketManager.processEvent() > 0)