2 * Copyright (c) 1998-2001, Index Data.
3 * See the file LICENSE for details.
5 * $Id: yaz-my-server.cpp,v 1.21 2007-04-12 15:00:33 adam Exp $
10 #include <yaz/diagbib1.h>
11 #include <yaz/options.h>
12 #include <yazpp/z-server.h>
13 #include <yazpp/pdu-assoc.h>
14 #include <yazpp/socket-manager.h>
15 #include <yaz/oid_db.h>
17 using namespace yazpp_1;
19 class MyILL : public Yaz_Facility_ILL {
21 void ill_service (Z_ExtendedServicesRequest *req,
23 Z_ExtendedServicesResponse *res);
26 class MyUpdate : public Yaz_Facility_Update {
28 void update_service (Z_ExtendedServicesRequest *req,
30 Z_ExtendedServicesResponse *res);
31 void update_service0 (Z_ExtendedServicesRequest *req,
33 Z_ExtendedServicesResponse *res);
37 class MyRetrieval : public Yaz_Facility_Retrieval, Yaz_USMARC {
39 int sr_init (Z_InitRequest *initRequest,
40 Z_InitResponse *initResponse);
41 void sr_search (Z_SearchRequest *searchRequest,
42 Z_SearchResponse *searchResponse);
43 void sr_present (Z_PresentRequest *presentRequest,
44 Z_PresentResponse *presentResponse);
45 void sr_record (const char *resultSetName,
48 Z_RecordComposition *comp,
49 Z_NamePlusRecord *namePlusRecord,
53 class MyServer : public Z_Server {
56 MyServer(IPDU_Observable *the_PDU_Observable);
57 IPDU_Observer* sessionNotify(IPDU_Observable *the_PDU_Observable,
64 MyRetrieval m_retrieval;
70 void MyILL::ill_service (Z_ExtendedServicesRequest *req,
72 Z_ExtendedServicesResponse *res)
74 yaz_log (YLOG_LOG, "MyServer::ill_service");
77 void MyUpdate::update_service (Z_ExtendedServicesRequest *req,
79 Z_ExtendedServicesResponse *res)
81 yaz_log (YLOG_LOG, "MyServer::update_service (v1.1)");
84 void MyUpdate::update_service0 (Z_ExtendedServicesRequest *req,
86 Z_ExtendedServicesResponse *res)
88 yaz_log (YLOG_LOG, "MyServer::update_service (v1.0)");
91 int MyRetrieval::sr_init (Z_InitRequest *initRequest,
92 Z_InitResponse *initResponse)
94 yaz_log (YLOG_LOG, "MyServer::sr_init");
98 void MyRetrieval::sr_search (Z_SearchRequest *searchRequest,
99 Z_SearchResponse *searchResponse)
101 yaz_log (YLOG_LOG, "MyServer::recv_Z_search");
102 if (searchRequest->query->which == Z_Query_type_1)
104 Z_RPNStructure *s = searchRequest->query->u.type_1->RPNStructure;
105 if (s->which == Z_RPNStructure_simple &&
106 s->u.simple->which == Z_Operand_APT &&
107 s->u.simple->u.attributesPlusTerm->term->which == Z_Term_general)
109 Odr_oct *term = s->u.simple->u.attributesPlusTerm->term->u.general;
110 char *str = (char *) odr_malloc (odr_encode(), term->len+1);
112 memcpy (str, term->buf, term->len);
113 str[term->len] = '\0';
114 *searchResponse->resultCount = atoi(str);
119 void MyRetrieval::sr_present (Z_PresentRequest *presentRequest,
120 Z_PresentResponse *presentResponse)
122 yaz_log (YLOG_LOG, "MyServer::recv_Z_present");
125 void MyRetrieval::sr_record (const char *resultSetName,
128 Z_RecordComposition *comp,
129 Z_NamePlusRecord *namePlusRecord,
132 yaz_log (YLOG_LOG, "MyServer::recv_Z_record");
133 const char *rec = get_record(position);
135 create_databaseRecord(odr_encode(), namePlusRecord, 0,
136 OID_STR_USMARC, rec, strlen(rec));
138 create_surrogateDiagnostics(odr_encode(), namePlusRecord, 0,
139 YAZ_BIB1_PRESENT_REQUEST_OUT_OF_RANGE, 0);
142 MyServer::~MyServer()
146 IPDU_Observer *MyServer::sessionNotify(
147 IPDU_Observable *the_PDU_Observable, int fd)
149 MyServer *new_server;
151 new_server = new MyServer(the_PDU_Observable);
152 new_server->timeout(900);
153 new_server->facility_add(&new_server->m_retrieval, "my sr");
154 new_server->facility_add(&new_server->m_ill, "my ill");
155 new_server->facility_add(&new_server->m_update, "my update");
156 new_server->set_APDU_log(get_APDU_log());
161 MyServer::MyServer(IPDU_Observable *the_PDU_Observable) :
162 Z_Server (the_PDU_Observable)
167 void MyServer::timeoutNotify()
169 yaz_log (YLOG_LOG, "connection timed out");
173 void MyServer::failNotify()
175 yaz_log (YLOG_LOG, "connection closed by client");
179 void MyServer::connectNotify()
183 void usage(const char *prog)
185 fprintf (stderr, "%s: [-a log] [-v level] [-T] @:port\n", prog);
189 int main(int argc, char **argv)
194 const char *addr = "tcp:@:9999";
197 SocketManager mySocketManager;
199 PDU_Assoc *my_PDU_Assoc = 0;
204 while ((ret = options("a:v:T", argv, argc, &arg)) != -2)
212 apdu_log = xstrdup(arg);
215 yaz_log_init_level (yaz_log_mask_str(arg));
225 #if YAZ_POSIX_THREADS
227 my_PDU_Assoc = new PDU_AssocThread(&mySocketManager);
229 my_PDU_Assoc = new PDU_Assoc(&mySocketManager);
231 my_PDU_Assoc = new PDU_Assoc(&mySocketManager);
234 z = new MyServer(my_PDU_Assoc);
238 yaz_log (YLOG_LOG, "set_APDU_log %s", apdu_log);
239 z->set_APDU_log(apdu_log);
242 while (mySocketManager.processEvent() > 0)
250 * indent-tabs-mode: nil
252 * vim: shiftwidth=4 tabstop=8 expandtab