2 * Copyright (c) 1998-2001, Index Data.
3 * See the file LICENSE for details.
5 * $Id: yaz-my-server.cpp,v 1.17 2005-06-02 06:40:21 adam Exp $
10 #include <yaz/diagbib1.h>
11 #include <yaz/options.h>
12 #include <yaz++/z-server.h>
13 #include <yaz++/pdu-assoc.h>
14 #include <yaz++/socket-manager.h>
16 using namespace yazpp_1;
18 class MyILL : public Yaz_Facility_ILL {
20 void ill_service (Z_ExtendedServicesRequest *req,
22 Z_ExtendedServicesResponse *res);
25 class MyUpdate : public Yaz_Facility_Update {
27 void update_service (Z_ExtendedServicesRequest *req,
29 Z_ExtendedServicesResponse *res);
30 void update_service0 (Z_ExtendedServicesRequest *req,
32 Z_ExtendedServicesResponse *res);
36 class MyRetrieval : public Yaz_Facility_Retrieval, Yaz_USMARC {
38 int sr_init (Z_InitRequest *initRequest,
39 Z_InitResponse *initResponse);
40 void sr_search (Z_SearchRequest *searchRequest,
41 Z_SearchResponse *searchResponse);
42 void sr_present (Z_PresentRequest *presentRequest,
43 Z_PresentResponse *presentResponse);
44 void sr_record (const char *resultSetName,
47 Z_RecordComposition *comp,
48 Z_NamePlusRecord *namePlusRecord,
52 class MyServer : public Yaz_Z_Server {
55 MyServer(IYaz_PDU_Observable *the_PDU_Observable);
56 IYaz_PDU_Observer* sessionNotify(IYaz_PDU_Observable *the_PDU_Observable,
63 MyRetrieval m_retrieval;
69 void MyILL::ill_service (Z_ExtendedServicesRequest *req,
71 Z_ExtendedServicesResponse *res)
73 yaz_log (YLOG_LOG, "MyServer::ill_service");
76 void MyUpdate::update_service (Z_ExtendedServicesRequest *req,
78 Z_ExtendedServicesResponse *res)
80 yaz_log (YLOG_LOG, "MyServer::update_service (v1.1)");
83 void MyUpdate::update_service0 (Z_ExtendedServicesRequest *req,
85 Z_ExtendedServicesResponse *res)
87 yaz_log (YLOG_LOG, "MyServer::update_service (v1.0)");
90 int MyRetrieval::sr_init (Z_InitRequest *initRequest,
91 Z_InitResponse *initResponse)
93 yaz_log (YLOG_LOG, "MyServer::sr_init");
97 void MyRetrieval::sr_search (Z_SearchRequest *searchRequest,
98 Z_SearchResponse *searchResponse)
100 yaz_log (YLOG_LOG, "MyServer::recv_Z_search");
101 if (searchRequest->query->which == Z_Query_type_1)
103 Z_RPNStructure *s = searchRequest->query->u.type_1->RPNStructure;
104 if (s->which == Z_RPNStructure_simple &&
105 s->u.simple->which == Z_Operand_APT &&
106 s->u.simple->u.attributesPlusTerm->term->which == Z_Term_general)
108 Odr_oct *term = s->u.simple->u.attributesPlusTerm->term->u.general;
109 char *str = (char *) odr_malloc (odr_encode(), term->len+1);
111 memcpy (str, term->buf, term->len);
112 str[term->len] = '\0';
113 *searchResponse->resultCount = atoi(str);
118 void MyRetrieval::sr_present (Z_PresentRequest *presentRequest,
119 Z_PresentResponse *presentResponse)
121 yaz_log (YLOG_LOG, "MyServer::recv_Z_present");
124 void MyRetrieval::sr_record (const char *resultSetName,
127 Z_RecordComposition *comp,
128 Z_NamePlusRecord *namePlusRecord,
131 yaz_log (YLOG_LOG, "MyServer::recv_Z_record");
132 const char *rec = get_record(position);
134 create_databaseRecord (odr_encode(), namePlusRecord, 0,
135 VAL_USMARC, rec, strlen(rec));
137 create_surrogateDiagnostics(odr_encode(), namePlusRecord, 0,
138 YAZ_BIB1_PRESENT_REQUEST_OUT_OF_RANGE, 0);
141 MyServer::~MyServer()
145 IYaz_PDU_Observer *MyServer::sessionNotify(
146 IYaz_PDU_Observable *the_PDU_Observable, int fd)
148 MyServer *new_server;
150 new_server = new MyServer(the_PDU_Observable);
151 new_server->timeout(900);
152 new_server->facility_add(&new_server->m_retrieval, "my sr");
153 new_server->facility_add(&new_server->m_ill, "my ill");
154 new_server->facility_add(&new_server->m_update, "my update");
155 new_server->set_APDU_log(get_APDU_log());
160 MyServer::MyServer(IYaz_PDU_Observable *the_PDU_Observable) :
161 Yaz_Z_Server (the_PDU_Observable)
166 void MyServer::timeoutNotify()
168 yaz_log (YLOG_LOG, "connection timed out");
172 void MyServer::failNotify()
174 yaz_log (YLOG_LOG, "connection closed by client");
178 void MyServer::connectNotify()
182 void usage(const char *prog)
184 fprintf (stderr, "%s: [-a log] [-v level] [-T] @:port\n", prog);
188 int main(int argc, char **argv)
193 const char *addr = "tcp:@:9999";
196 Yaz_SocketManager mySocketManager;
198 Yaz_PDU_Assoc *my_PDU_Assoc = 0;
203 while ((ret = options("a:v:T", argv, argc, &arg)) != -2)
211 apdu_log = xstrdup(arg);
214 yaz_log_init_level (yaz_log_mask_str(arg));
224 #if YAZ_POSIX_THREADS
226 my_PDU_Assoc = new Yaz_PDU_AssocThread(&mySocketManager);
228 my_PDU_Assoc = new Yaz_PDU_Assoc(&mySocketManager);
230 my_PDU_Assoc = new Yaz_PDU_Assoc(&mySocketManager);
233 z = new MyServer(my_PDU_Assoc);
237 yaz_log (YLOG_LOG, "set_APDU_log %s", apdu_log);
238 z->set_APDU_log(apdu_log);
241 while (mySocketManager.processEvent() > 0)