2 * Copyright (c) 1998-2001, Index Data.
3 * See the file LICENSE for details.
5 * $Log: yaz-my-server.cpp,v $
6 * Revision 1.2 2001-03-29 15:14:26 adam
9 * Revision 1.1 2001/03/27 14:47:45 adam
10 * New server facility scheme.
12 * Revision 1.15 2001/03/26 14:43:49 adam
13 * New threaded PDU association.
15 * Revision 1.14 2000/11/01 14:22:59 adam
16 * Added fd parameter for method IYaz_PDU_Observer::clone.
18 * Revision 1.13 2000/10/11 11:58:16 adam
19 * Moved header files to include/yaz++. Switched to libtool and automake.
20 * Configure script creates yaz++-config script.
22 * Revision 1.12 2000/09/21 21:43:20 adam
23 * Better high-level server API.
25 * Revision 1.11 2000/09/12 16:23:49 adam
26 * Updated server example.
28 * Revision 1.10 2000/09/12 16:04:17 adam
29 * Added comstack method for Yaz_PDU_Assoc..
31 * Revision 1.9 2000/09/12 12:09:53 adam
32 * More work on high-level server.
34 * Revision 1.8 2000/09/08 10:23:42 adam
35 * Added skeleton of yaz-z-server.
37 * Revision 1.7 1999/12/06 13:52:45 adam
38 * Modified for new location of YAZ header files. Experimental threaded
41 * Revision 1.6 1999/04/21 12:09:01 adam
42 * Many improvements. Modified to proxy server to work with "sessions"
45 * Revision 1.5 1999/04/09 11:46:57 adam
46 * Added object Yaz_Z_Assoc. Much more functional client.
48 * Revision 1.4 1999/03/23 14:17:57 adam
49 * More work on timeout handling. Work on yaz-client.
51 * Revision 1.3 1999/02/02 14:01:22 adam
52 * First WIN32 port of YAZ++.
54 * Revision 1.2 1999/01/28 13:08:47 adam
55 * Yaz_PDU_Assoc better encapsulated. Memory leak fix in
56 * yaz-socket-manager.cc.
58 * Revision 1.1.1.1 1999/01/28 09:41:07 adam
59 * First implementation of YAZ++.
64 #include <yaz/options.h>
65 #include <yaz++/yaz-z-server.h>
66 #include <yaz++/yaz-pdu-assoc.h>
67 #include <yaz++/yaz-socket-manager.h>
69 class MyILL : public Yaz_Facility_ILL {
71 int ill_init (Z_InitRequest *initRequest,
72 Z_InitResponse *initResponse);
75 class MyRetrieval : public Yaz_Facility_Retrieval, Yaz_USMARC {
77 int sr_init (Z_InitRequest *initRequest,
78 Z_InitResponse *initResponse);
79 void sr_search (Z_SearchRequest *searchRequest,
80 Z_SearchResponse *searchResponse);
81 void sr_present (Z_PresentRequest *presentRequest,
82 Z_PresentResponse *presentResponse);
83 void sr_record (const char *resultSetName,
86 Z_RecordComposition *comp,
87 Z_NamePlusRecord *namePlusRecord,
91 class MyServer : public Yaz_Z_Server {
94 MyServer(IYaz_PDU_Observable *the_PDU_Observable);
95 IYaz_PDU_Observer* sessionNotify(IYaz_PDU_Observable *the_PDU_Observable,
102 MyRetrieval *m_retrieval;
107 int MyILL::ill_init (Z_InitRequest *initRequest,
108 Z_InitResponse *initResponse)
110 yaz_log (LOG_LOG, "MyILL::ill_init");
114 int MyRetrieval::sr_init (Z_InitRequest *initRequest,
115 Z_InitResponse *initResponse)
117 yaz_log (LOG_LOG, "MyServer::sr_init");
121 void MyRetrieval::sr_search (Z_SearchRequest *searchRequest,
122 Z_SearchResponse *searchResponse)
124 yaz_log (LOG_LOG, "MyServer::recv_Z_search");
125 if (searchRequest->query->which == Z_Query_type_1)
127 Z_RPNStructure *s = searchRequest->query->u.type_1->RPNStructure;
128 if (s->which == Z_RPNStructure_simple &&
129 s->u.simple->which == Z_Operand_APT &&
130 s->u.simple->u.attributesPlusTerm->term->which == Z_Term_general)
132 Odr_oct *term = s->u.simple->u.attributesPlusTerm->term->u.general;
133 char *str = (char *) odr_malloc (odr_encode(), term->len+1);
135 memcpy (str, term->buf, term->len);
136 str[term->len] = '\0';
137 *searchResponse->resultCount = atoi(str);
142 void MyRetrieval::sr_present (Z_PresentRequest *presentRequest,
143 Z_PresentResponse *presentResponse)
145 yaz_log (LOG_LOG, "MyServer::recv_Z_present");
148 void MyRetrieval::sr_record (const char *resultSetName,
151 Z_RecordComposition *comp,
152 Z_NamePlusRecord *namePlusRecord,
155 yaz_log (LOG_LOG, "MyServer::recv_Z_record");
156 const char *rec = get_record(position);
157 create_databaseRecord (namePlusRecord, 0, VAL_USMARC, rec,
161 MyServer::~MyServer()
167 IYaz_PDU_Observer *MyServer::sessionNotify(
168 IYaz_PDU_Observable *the_PDU_Observable, int fd)
170 MyServer *new_server;
172 new_server = new MyServer(the_PDU_Observable);
173 new_server->timeout(900);
174 new_server->m_retrieval = new MyRetrieval;
175 new_server->m_ill = new MyILL;
176 new_server->facility_add(new_server->m_retrieval, "my sr");
177 new_server->facility_add(new_server->m_ill, "my ill");
179 new_server->set_APDU_log(get_APDU_log());
184 MyServer::MyServer(IYaz_PDU_Observable *the_PDU_Observable) :
185 Yaz_Z_Server (the_PDU_Observable)
192 void MyServer::timeoutNotify()
194 yaz_log (LOG_LOG, "connection timed out");
198 void MyServer::failNotify()
200 yaz_log (LOG_LOG, "connection closed by client");
204 void MyServer::connectNotify()
209 void usage(char *prog)
211 fprintf (stderr, "%s: [-a log] [-v level] [-T] @:port\n", prog);
215 int main(int argc, char **argv)
220 char *addr = "tcp:@:9999";
223 Yaz_SocketManager mySocketManager;
225 Yaz_PDU_Assoc *my_PDU_Assoc = 0;
230 while ((ret = options("a:v:T", argv, argc, &arg)) != -2)
238 apdu_log = xstrdup(arg);
241 yaz_log_init_level (yaz_log_mask_str(arg));
252 my_PDU_Assoc = new Yaz_PDU_AssocThread(&mySocketManager);
254 my_PDU_Assoc = new Yaz_PDU_Assoc(&mySocketManager);
256 z = new MyServer(my_PDU_Assoc);
260 yaz_log (LOG_LOG, "set_APDU_log %s", apdu_log);
261 z->set_APDU_log(apdu_log);
264 while (mySocketManager.processEvent() > 0)