1 /* This file is part of the yazpp toolkit.
2 * Copyright (C) 1998-2012 Index Data and Mike Taylor
3 * See the file LICENSE for details.
11 #include <yaz/diagbib1.h>
12 #include <yaz/options.h>
13 #include <yazpp/z-server.h>
14 #include <yazpp/pdu-assoc.h>
15 #include <yazpp/socket-manager.h>
16 #include <yaz/oid_db.h>
18 using namespace yazpp_1;
20 class MyILL : public Yaz_Facility_ILL {
22 void ill_service (Z_ExtendedServicesRequest *req,
24 Z_ExtendedServicesResponse *res);
27 class MyUpdate : public Yaz_Facility_Update {
29 void update_service (Z_ExtendedServicesRequest *req,
31 Z_ExtendedServicesResponse *res);
32 void update_service0 (Z_ExtendedServicesRequest *req,
34 Z_ExtendedServicesResponse *res);
38 class MyRetrieval : public Yaz_Facility_Retrieval, Yaz_USMARC {
40 int sr_init (Z_InitRequest *initRequest,
41 Z_InitResponse *initResponse);
42 void sr_search (Z_SearchRequest *searchRequest,
43 Z_SearchResponse *searchResponse);
44 void sr_present (Z_PresentRequest *presentRequest,
45 Z_PresentResponse *presentResponse);
46 void sr_record (const char *resultSetName,
49 Z_RecordComposition *comp,
50 Z_NamePlusRecord *namePlusRecord,
54 class MyServer : public Z_Server {
57 MyServer(IPDU_Observable *the_PDU_Observable);
58 IPDU_Observer* sessionNotify(IPDU_Observable *the_PDU_Observable,
65 MyRetrieval m_retrieval;
71 void MyILL::ill_service (Z_ExtendedServicesRequest *req,
73 Z_ExtendedServicesResponse *res)
75 yaz_log (YLOG_LOG, "MyServer::ill_service");
78 void MyUpdate::update_service (Z_ExtendedServicesRequest *req,
80 Z_ExtendedServicesResponse *res)
82 yaz_log (YLOG_LOG, "MyServer::update_service (v1.1)");
85 void MyUpdate::update_service0 (Z_ExtendedServicesRequest *req,
87 Z_ExtendedServicesResponse *res)
89 yaz_log (YLOG_LOG, "MyServer::update_service (v1.0)");
92 int MyRetrieval::sr_init (Z_InitRequest *initRequest,
93 Z_InitResponse *initResponse)
95 yaz_log (YLOG_LOG, "MyServer::sr_init");
99 void MyRetrieval::sr_search (Z_SearchRequest *searchRequest,
100 Z_SearchResponse *searchResponse)
102 yaz_log (YLOG_LOG, "MyServer::recv_Z_search");
103 if (searchRequest->query->which == Z_Query_type_1)
105 Z_RPNStructure *s = searchRequest->query->u.type_1->RPNStructure;
106 if (s->which == Z_RPNStructure_simple &&
107 s->u.simple->which == Z_Operand_APT &&
108 s->u.simple->u.attributesPlusTerm->term->which == Z_Term_general)
110 Odr_oct *term = s->u.simple->u.attributesPlusTerm->term->u.general;
111 char *str = (char *) odr_malloc (odr_encode(), term->len+1);
113 memcpy (str, term->buf, term->len);
114 str[term->len] = '\0';
115 *searchResponse->resultCount = atoi(str);
120 void MyRetrieval::sr_present (Z_PresentRequest *presentRequest,
121 Z_PresentResponse *presentResponse)
123 yaz_log (YLOG_LOG, "MyServer::recv_Z_present");
126 void MyRetrieval::sr_record (const char *resultSetName,
129 Z_RecordComposition *comp,
130 Z_NamePlusRecord *namePlusRecord,
133 yaz_log (YLOG_LOG, "MyServer::recv_Z_record");
134 const char *rec = get_record(position);
136 create_databaseRecord(odr_encode(), namePlusRecord, 0,
137 yaz_oid_recsyn_usmarc, rec, strlen(rec));
139 create_surrogateDiagnostics(odr_encode(), namePlusRecord, 0,
140 YAZ_BIB1_PRESENT_REQUEST_OUT_OF_RANGE, 0);
143 MyServer::~MyServer()
147 IPDU_Observer *MyServer::sessionNotify(
148 IPDU_Observable *the_PDU_Observable, int fd)
150 MyServer *new_server;
152 new_server = new MyServer(the_PDU_Observable);
153 new_server->timeout(900);
154 new_server->facility_add(&new_server->m_retrieval, "my sr");
155 new_server->facility_add(&new_server->m_ill, "my ill");
156 new_server->facility_add(&new_server->m_update, "my update");
157 new_server->set_APDU_log(get_APDU_log());
162 MyServer::MyServer(IPDU_Observable *the_PDU_Observable) :
163 Z_Server (the_PDU_Observable)
168 void MyServer::timeoutNotify()
170 yaz_log (YLOG_LOG, "connection timed out");
174 void MyServer::failNotify()
176 yaz_log (YLOG_LOG, "connection closed by client");
180 void MyServer::connectNotify()
184 void usage(const char *prog)
186 fprintf (stderr, "%s: [-a log] [-v level] [-T] @:port\n", prog);
190 int main(int argc, char **argv)
195 const char *addr = "tcp:@:9999";
198 SocketManager mySocketManager;
200 PDU_Assoc *my_PDU_Assoc = 0;
205 while ((ret = options("a:v:T", argv, argc, &arg)) != -2)
213 apdu_log = xstrdup(arg);
216 yaz_log_init_level (yaz_log_mask_str(arg));
226 #if YAZ_POSIX_THREADS
228 my_PDU_Assoc = new PDU_AssocThread(&mySocketManager);
230 my_PDU_Assoc = new PDU_Assoc(&mySocketManager);
232 my_PDU_Assoc = new PDU_Assoc(&mySocketManager);
235 z = new MyServer(my_PDU_Assoc);
239 yaz_log (YLOG_LOG, "set_APDU_log %s", apdu_log);
240 z->set_APDU_log(apdu_log);
243 while (mySocketManager.processEvent() > 0)
251 * c-file-style: "Stroustrup"
252 * indent-tabs-mode: nil
254 * vim: shiftwidth=4 tabstop=8 expandtab