2 * Copyright (c) 1998-2001, Index Data.
3 * See the file LICENSE for details.
5 * $Id: yaz-my-server.cpp,v 1.16 2005-05-17 13:00:56 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 class MyILL : public Yaz_Facility_ILL {
18 void ill_service (Z_ExtendedServicesRequest *req,
20 Z_ExtendedServicesResponse *res);
23 class MyUpdate : public Yaz_Facility_Update {
25 void update_service (Z_ExtendedServicesRequest *req,
27 Z_ExtendedServicesResponse *res);
28 void update_service0 (Z_ExtendedServicesRequest *req,
30 Z_ExtendedServicesResponse *res);
34 class MyRetrieval : public Yaz_Facility_Retrieval, Yaz_USMARC {
36 int sr_init (Z_InitRequest *initRequest,
37 Z_InitResponse *initResponse);
38 void sr_search (Z_SearchRequest *searchRequest,
39 Z_SearchResponse *searchResponse);
40 void sr_present (Z_PresentRequest *presentRequest,
41 Z_PresentResponse *presentResponse);
42 void sr_record (const char *resultSetName,
45 Z_RecordComposition *comp,
46 Z_NamePlusRecord *namePlusRecord,
50 class MyServer : public Yaz_Z_Server {
53 MyServer(IYaz_PDU_Observable *the_PDU_Observable);
54 IYaz_PDU_Observer* sessionNotify(IYaz_PDU_Observable *the_PDU_Observable,
61 MyRetrieval m_retrieval;
67 void MyILL::ill_service (Z_ExtendedServicesRequest *req,
69 Z_ExtendedServicesResponse *res)
71 yaz_log (YLOG_LOG, "MyServer::ill_service");
74 void MyUpdate::update_service (Z_ExtendedServicesRequest *req,
76 Z_ExtendedServicesResponse *res)
78 yaz_log (YLOG_LOG, "MyServer::update_service (v1.1)");
81 void MyUpdate::update_service0 (Z_ExtendedServicesRequest *req,
83 Z_ExtendedServicesResponse *res)
85 yaz_log (YLOG_LOG, "MyServer::update_service (v1.0)");
88 int MyRetrieval::sr_init (Z_InitRequest *initRequest,
89 Z_InitResponse *initResponse)
91 yaz_log (YLOG_LOG, "MyServer::sr_init");
95 void MyRetrieval::sr_search (Z_SearchRequest *searchRequest,
96 Z_SearchResponse *searchResponse)
98 yaz_log (YLOG_LOG, "MyServer::recv_Z_search");
99 if (searchRequest->query->which == Z_Query_type_1)
101 Z_RPNStructure *s = searchRequest->query->u.type_1->RPNStructure;
102 if (s->which == Z_RPNStructure_simple &&
103 s->u.simple->which == Z_Operand_APT &&
104 s->u.simple->u.attributesPlusTerm->term->which == Z_Term_general)
106 Odr_oct *term = s->u.simple->u.attributesPlusTerm->term->u.general;
107 char *str = (char *) odr_malloc (odr_encode(), term->len+1);
109 memcpy (str, term->buf, term->len);
110 str[term->len] = '\0';
111 *searchResponse->resultCount = atoi(str);
116 void MyRetrieval::sr_present (Z_PresentRequest *presentRequest,
117 Z_PresentResponse *presentResponse)
119 yaz_log (YLOG_LOG, "MyServer::recv_Z_present");
122 void MyRetrieval::sr_record (const char *resultSetName,
125 Z_RecordComposition *comp,
126 Z_NamePlusRecord *namePlusRecord,
129 yaz_log (YLOG_LOG, "MyServer::recv_Z_record");
130 const char *rec = get_record(position);
132 create_databaseRecord (odr_encode(), namePlusRecord, 0,
133 VAL_USMARC, rec, strlen(rec));
135 create_surrogateDiagnostics(odr_encode(), namePlusRecord, 0,
136 YAZ_BIB1_PRESENT_REQUEST_OUT_OF_RANGE, 0);
139 MyServer::~MyServer()
143 IYaz_PDU_Observer *MyServer::sessionNotify(
144 IYaz_PDU_Observable *the_PDU_Observable, int fd)
146 MyServer *new_server;
148 new_server = new MyServer(the_PDU_Observable);
149 new_server->timeout(900);
150 new_server->facility_add(&new_server->m_retrieval, "my sr");
151 new_server->facility_add(&new_server->m_ill, "my ill");
152 new_server->facility_add(&new_server->m_update, "my update");
153 new_server->set_APDU_log(get_APDU_log());
158 MyServer::MyServer(IYaz_PDU_Observable *the_PDU_Observable) :
159 Yaz_Z_Server (the_PDU_Observable)
164 void MyServer::timeoutNotify()
166 yaz_log (YLOG_LOG, "connection timed out");
170 void MyServer::failNotify()
172 yaz_log (YLOG_LOG, "connection closed by client");
176 void MyServer::connectNotify()
180 void usage(const char *prog)
182 fprintf (stderr, "%s: [-a log] [-v level] [-T] @:port\n", prog);
186 int main(int argc, char **argv)
191 const char *addr = "tcp:@:9999";
194 Yaz_SocketManager mySocketManager;
196 Yaz_PDU_Assoc *my_PDU_Assoc = 0;
201 while ((ret = options("a:v:T", argv, argc, &arg)) != -2)
209 apdu_log = xstrdup(arg);
212 yaz_log_init_level (yaz_log_mask_str(arg));
222 #if YAZ_POSIX_THREADS
224 my_PDU_Assoc = new Yaz_PDU_AssocThread(&mySocketManager);
226 my_PDU_Assoc = new Yaz_PDU_Assoc(&mySocketManager);
228 my_PDU_Assoc = new Yaz_PDU_Assoc(&mySocketManager);
231 z = new MyServer(my_PDU_Assoc);
235 yaz_log (YLOG_LOG, "set_APDU_log %s", apdu_log);
236 z->set_APDU_log(apdu_log);
239 while (mySocketManager.processEvent() > 0)