2 * Copyright (c) 1998-2001, Index Data.
3 * See the file LICENSE for details.
5 * $Id: yaz-my-server.cpp,v 1.15 2005-01-17 09:55:58 adam Exp $
10 #include <yaz/options.h>
11 #include <yaz++/z-server.h>
12 #include <yaz++/pdu-assoc.h>
13 #include <yaz++/socket-manager.h>
15 class MyILL : public Yaz_Facility_ILL {
17 void ill_service (Z_ExtendedServicesRequest *req,
19 Z_ExtendedServicesResponse *res);
22 class MyUpdate : public Yaz_Facility_Update {
24 void update_service (Z_ExtendedServicesRequest *req,
26 Z_ExtendedServicesResponse *res);
27 void update_service0 (Z_ExtendedServicesRequest *req,
29 Z_ExtendedServicesResponse *res);
33 class MyRetrieval : public Yaz_Facility_Retrieval, Yaz_USMARC {
35 int sr_init (Z_InitRequest *initRequest,
36 Z_InitResponse *initResponse);
37 void sr_search (Z_SearchRequest *searchRequest,
38 Z_SearchResponse *searchResponse);
39 void sr_present (Z_PresentRequest *presentRequest,
40 Z_PresentResponse *presentResponse);
41 void sr_record (const char *resultSetName,
44 Z_RecordComposition *comp,
45 Z_NamePlusRecord *namePlusRecord,
49 class MyServer : public Yaz_Z_Server {
52 MyServer(IYaz_PDU_Observable *the_PDU_Observable);
53 IYaz_PDU_Observer* sessionNotify(IYaz_PDU_Observable *the_PDU_Observable,
60 MyRetrieval m_retrieval;
66 void MyILL::ill_service (Z_ExtendedServicesRequest *req,
68 Z_ExtendedServicesResponse *res)
70 yaz_log (YLOG_LOG, "MyServer::ill_service");
73 void MyUpdate::update_service (Z_ExtendedServicesRequest *req,
75 Z_ExtendedServicesResponse *res)
77 yaz_log (YLOG_LOG, "MyServer::update_service (v1.1)");
80 void MyUpdate::update_service0 (Z_ExtendedServicesRequest *req,
82 Z_ExtendedServicesResponse *res)
84 yaz_log (YLOG_LOG, "MyServer::update_service (v1.0)");
87 int MyRetrieval::sr_init (Z_InitRequest *initRequest,
88 Z_InitResponse *initResponse)
90 yaz_log (YLOG_LOG, "MyServer::sr_init");
94 void MyRetrieval::sr_search (Z_SearchRequest *searchRequest,
95 Z_SearchResponse *searchResponse)
97 yaz_log (YLOG_LOG, "MyServer::recv_Z_search");
98 if (searchRequest->query->which == Z_Query_type_1)
100 Z_RPNStructure *s = searchRequest->query->u.type_1->RPNStructure;
101 if (s->which == Z_RPNStructure_simple &&
102 s->u.simple->which == Z_Operand_APT &&
103 s->u.simple->u.attributesPlusTerm->term->which == Z_Term_general)
105 Odr_oct *term = s->u.simple->u.attributesPlusTerm->term->u.general;
106 char *str = (char *) odr_malloc (odr_encode(), term->len+1);
108 memcpy (str, term->buf, term->len);
109 str[term->len] = '\0';
110 *searchResponse->resultCount = atoi(str);
115 void MyRetrieval::sr_present (Z_PresentRequest *presentRequest,
116 Z_PresentResponse *presentResponse)
118 yaz_log (YLOG_LOG, "MyServer::recv_Z_present");
121 void MyRetrieval::sr_record (const char *resultSetName,
124 Z_RecordComposition *comp,
125 Z_NamePlusRecord *namePlusRecord,
128 yaz_log (YLOG_LOG, "MyServer::recv_Z_record");
129 const char *rec = get_record(position);
130 create_databaseRecord (odr_encode(), namePlusRecord, 0, VAL_USMARC, rec,
134 MyServer::~MyServer()
138 IYaz_PDU_Observer *MyServer::sessionNotify(
139 IYaz_PDU_Observable *the_PDU_Observable, int fd)
141 MyServer *new_server;
143 new_server = new MyServer(the_PDU_Observable);
144 new_server->timeout(900);
145 new_server->facility_add(&new_server->m_retrieval, "my sr");
146 new_server->facility_add(&new_server->m_ill, "my ill");
147 new_server->facility_add(&new_server->m_update, "my update");
148 new_server->set_APDU_log(get_APDU_log());
153 MyServer::MyServer(IYaz_PDU_Observable *the_PDU_Observable) :
154 Yaz_Z_Server (the_PDU_Observable)
159 void MyServer::timeoutNotify()
161 yaz_log (YLOG_LOG, "connection timed out");
165 void MyServer::failNotify()
167 yaz_log (YLOG_LOG, "connection closed by client");
171 void MyServer::connectNotify()
175 void usage(const char *prog)
177 fprintf (stderr, "%s: [-a log] [-v level] [-T] @:port\n", prog);
181 int main(int argc, char **argv)
186 const char *addr = "tcp:@:9999";
189 Yaz_SocketManager mySocketManager;
191 Yaz_PDU_Assoc *my_PDU_Assoc = 0;
196 while ((ret = options("a:v:T", argv, argc, &arg)) != -2)
204 apdu_log = xstrdup(arg);
207 yaz_log_init_level (yaz_log_mask_str(arg));
217 #if YAZ_POSIX_THREADS
219 my_PDU_Assoc = new Yaz_PDU_AssocThread(&mySocketManager);
221 my_PDU_Assoc = new Yaz_PDU_Assoc(&mySocketManager);
223 my_PDU_Assoc = new Yaz_PDU_Assoc(&mySocketManager);
226 z = new MyServer(my_PDU_Assoc);
230 yaz_log (YLOG_LOG, "set_APDU_log %s", apdu_log);
231 z->set_APDU_log(apdu_log);
234 while (mySocketManager.processEvent() > 0)