2 * Copyright (c) 1998-2001, Index Data.
3 * See the file LICENSE for details.
5 * $Log: yaz-my-server.cpp,v $
6 * Revision 1.5 2001-05-03 12:39:39 adam
7 * Added Update server service.
9 * Revision 1.4 2001/04/05 13:09:44 adam
10 * Removed ursula dependancy.
12 * Revision 1.3 2001/04/04 14:02:49 adam
13 * URSULA / Z-ruth service.
15 * Revision 1.2 2001/03/29 15:14:26 adam
18 * Revision 1.1 2001/03/27 14:47:45 adam
19 * New server facility scheme.
21 * Revision 1.15 2001/03/26 14:43:49 adam
22 * New threaded PDU association.
24 * Revision 1.14 2000/11/01 14:22:59 adam
25 * Added fd parameter for method IYaz_PDU_Observer::clone.
27 * Revision 1.13 2000/10/11 11:58:16 adam
28 * Moved header files to include/yaz++. Switched to libtool and automake.
29 * Configure script creates yaz++-config script.
31 * Revision 1.12 2000/09/21 21:43:20 adam
32 * Better high-level server API.
34 * Revision 1.11 2000/09/12 16:23:49 adam
35 * Updated server example.
37 * Revision 1.10 2000/09/12 16:04:17 adam
38 * Added comstack method for Yaz_PDU_Assoc..
40 * Revision 1.9 2000/09/12 12:09:53 adam
41 * More work on high-level server.
43 * Revision 1.8 2000/09/08 10:23:42 adam
44 * Added skeleton of yaz-z-server.
46 * Revision 1.7 1999/12/06 13:52:45 adam
47 * Modified for new location of YAZ header files. Experimental threaded
50 * Revision 1.6 1999/04/21 12:09:01 adam
51 * Many improvements. Modified to proxy server to work with "sessions"
54 * Revision 1.5 1999/04/09 11:46:57 adam
55 * Added object Yaz_Z_Assoc. Much more functional client.
57 * Revision 1.4 1999/03/23 14:17:57 adam
58 * More work on timeout handling. Work on yaz-client.
60 * Revision 1.3 1999/02/02 14:01:22 adam
61 * First WIN32 port of YAZ++.
63 * Revision 1.2 1999/01/28 13:08:47 adam
64 * Yaz_PDU_Assoc better encapsulated. Memory leak fix in
65 * yaz-socket-manager.cc.
67 * Revision 1.1.1.1 1999/01/28 09:41:07 adam
68 * First implementation of YAZ++.
73 #include <yaz/options.h>
74 #include <yaz++/yaz-z-server.h>
75 #include <yaz++/yaz-pdu-assoc.h>
76 #include <yaz++/yaz-socket-manager.h>
78 class MyILL : public Yaz_Facility_ILL {
80 void ill_service (Z_ExtendedServicesRequest *req,
82 Z_ExtendedServicesResponse *res);
85 class MyUpdate : public Yaz_Facility_Update {
87 void update_service (Z_ExtendedServicesRequest *req,
89 Z_ExtendedServicesResponse *res);
93 class MyRetrieval : public Yaz_Facility_Retrieval, Yaz_USMARC {
95 int sr_init (Z_InitRequest *initRequest,
96 Z_InitResponse *initResponse);
97 void sr_search (Z_SearchRequest *searchRequest,
98 Z_SearchResponse *searchResponse);
99 void sr_present (Z_PresentRequest *presentRequest,
100 Z_PresentResponse *presentResponse);
101 void sr_record (const char *resultSetName,
104 Z_RecordComposition *comp,
105 Z_NamePlusRecord *namePlusRecord,
109 #if HAVE_YAZ_URSULA_H
110 class MyUrsula : public Yaz_Facility_Ursula {
112 void ursula_service (Z_ExtendedServicesRequest *req,
114 Z_ExtendedServicesResponse *res);
118 class MyServer : public Yaz_Z_Server {
121 MyServer(IYaz_PDU_Observable *the_PDU_Observable);
122 IYaz_PDU_Observer* sessionNotify(IYaz_PDU_Observable *the_PDU_Observable,
125 void timeoutNotify();
126 void connectNotify();
129 MyRetrieval m_retrieval;
132 #if HAVE_YAZ_URSULA_H
138 void MyILL::ill_service (Z_ExtendedServicesRequest *req,
140 Z_ExtendedServicesResponse *res)
142 yaz_log (LOG_LOG, "MyServer::ill_service");
145 void MyUpdate::update_service (Z_ExtendedServicesRequest *req,
147 Z_ExtendedServicesResponse *res)
149 yaz_log (LOG_LOG, "MyServer::update_service");
153 #if HAVE_YAZ_URSULA_H
154 void MyUrsula::ursula_service (Z_ExtendedServicesRequest *req,
156 Z_ExtendedServicesResponse *res)
158 yaz_log (LOG_LOG, "MyServer::ursula_service");
161 case Z_UrsPDU_request:
162 yaz_log(LOG_LOG, "request");
163 if (u->u.request->libraryNo)
164 yaz_log (LOG_LOG, "libraryNo: %s", u->u.request->libraryNo);
166 case Z_UrsPDU_update:
167 yaz_log(LOG_LOG, "request");
169 case Z_UrsPDU_reservation:
170 yaz_log(LOG_LOG, "request");
172 case Z_UrsPDU_renewal:
173 yaz_log(LOG_LOG, "request");
176 yaz_log(LOG_LOG, "unknown");
182 int MyRetrieval::sr_init (Z_InitRequest *initRequest,
183 Z_InitResponse *initResponse)
185 yaz_log (LOG_LOG, "MyServer::sr_init");
189 void MyRetrieval::sr_search (Z_SearchRequest *searchRequest,
190 Z_SearchResponse *searchResponse)
192 yaz_log (LOG_LOG, "MyServer::recv_Z_search");
193 if (searchRequest->query->which == Z_Query_type_1)
195 Z_RPNStructure *s = searchRequest->query->u.type_1->RPNStructure;
196 if (s->which == Z_RPNStructure_simple &&
197 s->u.simple->which == Z_Operand_APT &&
198 s->u.simple->u.attributesPlusTerm->term->which == Z_Term_general)
200 Odr_oct *term = s->u.simple->u.attributesPlusTerm->term->u.general;
201 char *str = (char *) odr_malloc (odr_encode(), term->len+1);
203 memcpy (str, term->buf, term->len);
204 str[term->len] = '\0';
205 *searchResponse->resultCount = atoi(str);
210 void MyRetrieval::sr_present (Z_PresentRequest *presentRequest,
211 Z_PresentResponse *presentResponse)
213 yaz_log (LOG_LOG, "MyServer::recv_Z_present");
216 void MyRetrieval::sr_record (const char *resultSetName,
219 Z_RecordComposition *comp,
220 Z_NamePlusRecord *namePlusRecord,
223 yaz_log (LOG_LOG, "MyServer::recv_Z_record");
224 const char *rec = get_record(position);
225 create_databaseRecord (odr_encode(), namePlusRecord, 0, VAL_USMARC, rec,
229 MyServer::~MyServer()
233 IYaz_PDU_Observer *MyServer::sessionNotify(
234 IYaz_PDU_Observable *the_PDU_Observable, int fd)
236 MyServer *new_server;
238 new_server = new MyServer(the_PDU_Observable);
239 new_server->timeout(900);
240 new_server->facility_add(&new_server->m_retrieval, "my sr");
241 new_server->facility_add(&new_server->m_ill, "my ill");
242 new_server->facility_add(&new_server->m_update, "my update");
243 #if HAVE_YAZ_URSULA_H
244 new_server->facility_add(&new_server->m_ursula, "my ursula");
247 new_server->set_APDU_log(get_APDU_log());
252 MyServer::MyServer(IYaz_PDU_Observable *the_PDU_Observable) :
253 Yaz_Z_Server (the_PDU_Observable)
258 void MyServer::timeoutNotify()
260 yaz_log (LOG_LOG, "connection timed out");
264 void MyServer::failNotify()
266 yaz_log (LOG_LOG, "connection closed by client");
270 void MyServer::connectNotify()
275 void usage(char *prog)
277 fprintf (stderr, "%s: [-a log] [-v level] [-T] @:port\n", prog);
281 int main(int argc, char **argv)
286 char *addr = "tcp:@:9999";
289 Yaz_SocketManager mySocketManager;
291 Yaz_PDU_Assoc *my_PDU_Assoc = 0;
296 while ((ret = options("a:v:T", argv, argc, &arg)) != -2)
304 apdu_log = xstrdup(arg);
307 yaz_log_init_level (yaz_log_mask_str(arg));
318 my_PDU_Assoc = new Yaz_PDU_AssocThread(&mySocketManager);
320 my_PDU_Assoc = new Yaz_PDU_Assoc(&mySocketManager);
322 z = new MyServer(my_PDU_Assoc);
326 yaz_log (LOG_LOG, "set_APDU_log %s", apdu_log);
327 z->set_APDU_log(apdu_log);
330 while (mySocketManager.processEvent() > 0)