2 * Copyright (c) 1998-2001, Index Data.
3 * See the file LICENSE for details.
5 * $Log: yaz-my-server.cpp,v $
6 * Revision 1.4 2001-04-05 13:09:44 adam
7 * Removed ursula dependancy.
9 * Revision 1.3 2001/04/04 14:02:49 adam
10 * URSULA / Z-ruth service.
12 * Revision 1.2 2001/03/29 15:14:26 adam
15 * Revision 1.1 2001/03/27 14:47:45 adam
16 * New server facility scheme.
18 * Revision 1.15 2001/03/26 14:43:49 adam
19 * New threaded PDU association.
21 * Revision 1.14 2000/11/01 14:22:59 adam
22 * Added fd parameter for method IYaz_PDU_Observer::clone.
24 * Revision 1.13 2000/10/11 11:58:16 adam
25 * Moved header files to include/yaz++. Switched to libtool and automake.
26 * Configure script creates yaz++-config script.
28 * Revision 1.12 2000/09/21 21:43:20 adam
29 * Better high-level server API.
31 * Revision 1.11 2000/09/12 16:23:49 adam
32 * Updated server example.
34 * Revision 1.10 2000/09/12 16:04:17 adam
35 * Added comstack method for Yaz_PDU_Assoc..
37 * Revision 1.9 2000/09/12 12:09:53 adam
38 * More work on high-level server.
40 * Revision 1.8 2000/09/08 10:23:42 adam
41 * Added skeleton of yaz-z-server.
43 * Revision 1.7 1999/12/06 13:52:45 adam
44 * Modified for new location of YAZ header files. Experimental threaded
47 * Revision 1.6 1999/04/21 12:09:01 adam
48 * Many improvements. Modified to proxy server to work with "sessions"
51 * Revision 1.5 1999/04/09 11:46:57 adam
52 * Added object Yaz_Z_Assoc. Much more functional client.
54 * Revision 1.4 1999/03/23 14:17:57 adam
55 * More work on timeout handling. Work on yaz-client.
57 * Revision 1.3 1999/02/02 14:01:22 adam
58 * First WIN32 port of YAZ++.
60 * Revision 1.2 1999/01/28 13:08:47 adam
61 * Yaz_PDU_Assoc better encapsulated. Memory leak fix in
62 * yaz-socket-manager.cc.
64 * Revision 1.1.1.1 1999/01/28 09:41:07 adam
65 * First implementation of YAZ++.
70 #include <yaz/options.h>
71 #include <yaz++/yaz-z-server.h>
72 #include <yaz++/yaz-pdu-assoc.h>
73 #include <yaz++/yaz-socket-manager.h>
75 class MyILL : public Yaz_Facility_ILL {
77 int ill_init (Z_InitRequest *initRequest,
78 Z_InitResponse *initResponse);
79 void ill_service (Z_ExtendedServicesRequest *req,
81 Z_ExtendedServicesResponse *res);
84 class MyRetrieval : public Yaz_Facility_Retrieval, Yaz_USMARC {
86 int sr_init (Z_InitRequest *initRequest,
87 Z_InitResponse *initResponse);
88 void sr_search (Z_SearchRequest *searchRequest,
89 Z_SearchResponse *searchResponse);
90 void sr_present (Z_PresentRequest *presentRequest,
91 Z_PresentResponse *presentResponse);
92 void sr_record (const char *resultSetName,
95 Z_RecordComposition *comp,
96 Z_NamePlusRecord *namePlusRecord,
100 #if HAVE_YAZ_URSULA_H
101 class MyUrsula : public Yaz_Facility_Ursula {
103 void ursula_service (Z_ExtendedServicesRequest *req,
105 Z_ExtendedServicesResponse *res);
109 class MyServer : public Yaz_Z_Server {
112 MyServer(IYaz_PDU_Observable *the_PDU_Observable);
113 IYaz_PDU_Observer* sessionNotify(IYaz_PDU_Observable *the_PDU_Observable,
116 void timeoutNotify();
117 void connectNotify();
120 MyRetrieval m_retrieval;
122 #if HAVE_YAZ_URSULA_H
128 int MyILL::ill_init (Z_InitRequest *initRequest,
129 Z_InitResponse *initResponse)
131 yaz_log (LOG_LOG, "MyILL::ill_init");
135 void MyILL::ill_service (Z_ExtendedServicesRequest *req,
137 Z_ExtendedServicesResponse *res)
139 yaz_log (LOG_LOG, "MyServer::ill_service");
143 #if HAVE_YAZ_URSULA_H
144 void MyUrsula::ursula_service (Z_ExtendedServicesRequest *req,
146 Z_ExtendedServicesResponse *res)
148 yaz_log (LOG_LOG, "MyServer::ursula_service");
151 case Z_UrsPDU_request:
152 yaz_log(LOG_LOG, "request");
153 if (u->u.request->libraryNo)
154 yaz_log (LOG_LOG, "libraryNo: %s", u->u.request->libraryNo);
156 case Z_UrsPDU_update:
157 yaz_log(LOG_LOG, "request");
159 case Z_UrsPDU_reservation:
160 yaz_log(LOG_LOG, "request");
162 case Z_UrsPDU_renewal:
163 yaz_log(LOG_LOG, "request");
166 yaz_log(LOG_LOG, "unknown");
172 int MyRetrieval::sr_init (Z_InitRequest *initRequest,
173 Z_InitResponse *initResponse)
175 yaz_log (LOG_LOG, "MyServer::sr_init");
179 void MyRetrieval::sr_search (Z_SearchRequest *searchRequest,
180 Z_SearchResponse *searchResponse)
182 yaz_log (LOG_LOG, "MyServer::recv_Z_search");
183 if (searchRequest->query->which == Z_Query_type_1)
185 Z_RPNStructure *s = searchRequest->query->u.type_1->RPNStructure;
186 if (s->which == Z_RPNStructure_simple &&
187 s->u.simple->which == Z_Operand_APT &&
188 s->u.simple->u.attributesPlusTerm->term->which == Z_Term_general)
190 Odr_oct *term = s->u.simple->u.attributesPlusTerm->term->u.general;
191 char *str = (char *) odr_malloc (odr_encode(), term->len+1);
193 memcpy (str, term->buf, term->len);
194 str[term->len] = '\0';
195 *searchResponse->resultCount = atoi(str);
200 void MyRetrieval::sr_present (Z_PresentRequest *presentRequest,
201 Z_PresentResponse *presentResponse)
203 yaz_log (LOG_LOG, "MyServer::recv_Z_present");
206 void MyRetrieval::sr_record (const char *resultSetName,
209 Z_RecordComposition *comp,
210 Z_NamePlusRecord *namePlusRecord,
213 yaz_log (LOG_LOG, "MyServer::recv_Z_record");
214 const char *rec = get_record(position);
215 create_databaseRecord (odr_encode(), namePlusRecord, 0, VAL_USMARC, rec,
219 MyServer::~MyServer()
223 IYaz_PDU_Observer *MyServer::sessionNotify(
224 IYaz_PDU_Observable *the_PDU_Observable, int fd)
226 MyServer *new_server;
228 new_server = new MyServer(the_PDU_Observable);
229 new_server->timeout(900);
230 new_server->facility_add(&new_server->m_retrieval, "my sr");
231 new_server->facility_add(&new_server->m_ill, "my ill");
232 #if HAVE_YAZ_URSULA_H
233 new_server->facility_add(&new_server->m_ursula, "my ill");
236 new_server->set_APDU_log(get_APDU_log());
241 MyServer::MyServer(IYaz_PDU_Observable *the_PDU_Observable) :
242 Yaz_Z_Server (the_PDU_Observable)
247 void MyServer::timeoutNotify()
249 yaz_log (LOG_LOG, "connection timed out");
253 void MyServer::failNotify()
255 yaz_log (LOG_LOG, "connection closed by client");
259 void MyServer::connectNotify()
264 void usage(char *prog)
266 fprintf (stderr, "%s: [-a log] [-v level] [-T] @:port\n", prog);
270 int main(int argc, char **argv)
275 char *addr = "tcp:@:9999";
278 Yaz_SocketManager mySocketManager;
280 Yaz_PDU_Assoc *my_PDU_Assoc = 0;
285 while ((ret = options("a:v:T", argv, argc, &arg)) != -2)
293 apdu_log = xstrdup(arg);
296 yaz_log_init_level (yaz_log_mask_str(arg));
307 my_PDU_Assoc = new Yaz_PDU_AssocThread(&mySocketManager);
309 my_PDU_Assoc = new Yaz_PDU_Assoc(&mySocketManager);
311 z = new MyServer(my_PDU_Assoc);
315 yaz_log (LOG_LOG, "set_APDU_log %s", apdu_log);
316 z->set_APDU_log(apdu_log);
319 while (mySocketManager.processEvent() > 0)