2 * Copyright (c) 1998-2001, Index Data.
3 * See the file LICENSE for details.
5 * $Id: yaz-my-server.cpp,v 1.11 2002-10-28 12:16:09 adam Exp $
9 #include <yaz/options.h>
10 #include <yaz++/z-server.h>
11 #include <yaz++/pdu-assoc.h>
12 #include <yaz++/socket-manager.h>
14 class MyILL : public Yaz_Facility_ILL {
16 void ill_service (Z_ExtendedServicesRequest *req,
18 Z_ExtendedServicesResponse *res);
21 class MyUpdate : public Yaz_Facility_Update {
23 void update_service (Z_ExtendedServicesRequest *req,
25 Z_ExtendedServicesResponse *res);
26 void update_service0 (Z_ExtendedServicesRequest *req,
28 Z_ExtendedServicesResponse *res);
32 class MyRetrieval : public Yaz_Facility_Retrieval, Yaz_USMARC {
34 int sr_init (Z_InitRequest *initRequest,
35 Z_InitResponse *initResponse);
36 void sr_search (Z_SearchRequest *searchRequest,
37 Z_SearchResponse *searchResponse);
38 void sr_present (Z_PresentRequest *presentRequest,
39 Z_PresentResponse *presentResponse);
40 void sr_record (const char *resultSetName,
43 Z_RecordComposition *comp,
44 Z_NamePlusRecord *namePlusRecord,
48 class MyServer : public Yaz_Z_Server {
51 MyServer(IYaz_PDU_Observable *the_PDU_Observable);
52 IYaz_PDU_Observer* sessionNotify(IYaz_PDU_Observable *the_PDU_Observable,
59 MyRetrieval m_retrieval;
65 void MyILL::ill_service (Z_ExtendedServicesRequest *req,
67 Z_ExtendedServicesResponse *res)
69 yaz_log (LOG_LOG, "MyServer::ill_service");
72 void MyUpdate::update_service (Z_ExtendedServicesRequest *req,
74 Z_ExtendedServicesResponse *res)
76 yaz_log (LOG_LOG, "MyServer::update_service (v1.1)");
79 void MyUpdate::update_service0 (Z_ExtendedServicesRequest *req,
81 Z_ExtendedServicesResponse *res)
83 yaz_log (LOG_LOG, "MyServer::update_service (v1.0)");
86 int MyRetrieval::sr_init (Z_InitRequest *initRequest,
87 Z_InitResponse *initResponse)
89 yaz_log (LOG_LOG, "MyServer::sr_init");
93 void MyRetrieval::sr_search (Z_SearchRequest *searchRequest,
94 Z_SearchResponse *searchResponse)
96 yaz_log (LOG_LOG, "MyServer::recv_Z_search");
97 if (searchRequest->query->which == Z_Query_type_1)
99 Z_RPNStructure *s = searchRequest->query->u.type_1->RPNStructure;
100 if (s->which == Z_RPNStructure_simple &&
101 s->u.simple->which == Z_Operand_APT &&
102 s->u.simple->u.attributesPlusTerm->term->which == Z_Term_general)
104 Odr_oct *term = s->u.simple->u.attributesPlusTerm->term->u.general;
105 char *str = (char *) odr_malloc (odr_encode(), term->len+1);
107 memcpy (str, term->buf, term->len);
108 str[term->len] = '\0';
109 *searchResponse->resultCount = atoi(str);
114 void MyRetrieval::sr_present (Z_PresentRequest *presentRequest,
115 Z_PresentResponse *presentResponse)
117 yaz_log (LOG_LOG, "MyServer::recv_Z_present");
120 void MyRetrieval::sr_record (const char *resultSetName,
123 Z_RecordComposition *comp,
124 Z_NamePlusRecord *namePlusRecord,
127 yaz_log (LOG_LOG, "MyServer::recv_Z_record");
128 const char *rec = get_record(position);
129 create_databaseRecord (odr_encode(), namePlusRecord, 0, VAL_USMARC, rec,
133 MyServer::~MyServer()
137 IYaz_PDU_Observer *MyServer::sessionNotify(
138 IYaz_PDU_Observable *the_PDU_Observable, int fd)
140 MyServer *new_server;
142 new_server = new MyServer(the_PDU_Observable);
143 new_server->timeout(900);
144 new_server->facility_add(&new_server->m_retrieval, "my sr");
145 new_server->facility_add(&new_server->m_ill, "my ill");
146 new_server->facility_add(&new_server->m_update, "my update");
147 new_server->set_APDU_log(get_APDU_log());
152 MyServer::MyServer(IYaz_PDU_Observable *the_PDU_Observable) :
153 Yaz_Z_Server (the_PDU_Observable)
158 void MyServer::timeoutNotify()
160 yaz_log (LOG_LOG, "connection timed out");
164 void MyServer::failNotify()
166 yaz_log (LOG_LOG, "connection closed by client");
170 void MyServer::connectNotify()
174 void usage(char *prog)
176 fprintf (stderr, "%s: [-a log] [-v level] [-T] @:port\n", prog);
180 int main(int argc, char **argv)
185 char *addr = "tcp:@:9999";
188 Yaz_SocketManager mySocketManager;
190 Yaz_PDU_Assoc *my_PDU_Assoc = 0;
195 while ((ret = options("a:v:T", argv, argc, &arg)) != -2)
203 apdu_log = xstrdup(arg);
206 yaz_log_init_level (yaz_log_mask_str(arg));
216 #if YAZ_POSIX_THREADS
218 my_PDU_Assoc = new Yaz_PDU_AssocThread(&mySocketManager);
220 my_PDU_Assoc = new Yaz_PDU_Assoc(&mySocketManager);
222 my_PDU_Assoc = new Yaz_PDU_Assoc(&mySocketManager);
225 z = new MyServer(my_PDU_Assoc);
229 yaz_log (LOG_LOG, "set_APDU_log %s", apdu_log);
230 z->set_APDU_log(apdu_log);
233 while (mySocketManager.processEvent() > 0)