2 * Copyright (c) 1998-2001, Index Data.
3 * See the file LICENSE for details.
5 * $Log: yaz-my-server.cpp,v $
6 * Revision 1.1 2001-03-27 14:47:45 adam
7 * New server facility scheme.
9 * Revision 1.15 2001/03/26 14:43:49 adam
10 * New threaded PDU association.
12 * Revision 1.14 2000/11/01 14:22:59 adam
13 * Added fd parameter for method IYaz_PDU_Observer::clone.
15 * Revision 1.13 2000/10/11 11:58:16 adam
16 * Moved header files to include/yaz++. Switched to libtool and automake.
17 * Configure script creates yaz++-config script.
19 * Revision 1.12 2000/09/21 21:43:20 adam
20 * Better high-level server API.
22 * Revision 1.11 2000/09/12 16:23:49 adam
23 * Updated server example.
25 * Revision 1.10 2000/09/12 16:04:17 adam
26 * Added comstack method for Yaz_PDU_Assoc..
28 * Revision 1.9 2000/09/12 12:09:53 adam
29 * More work on high-level server.
31 * Revision 1.8 2000/09/08 10:23:42 adam
32 * Added skeleton of yaz-z-server.
34 * Revision 1.7 1999/12/06 13:52:45 adam
35 * Modified for new location of YAZ header files. Experimental threaded
38 * Revision 1.6 1999/04/21 12:09:01 adam
39 * Many improvements. Modified to proxy server to work with "sessions"
42 * Revision 1.5 1999/04/09 11:46:57 adam
43 * Added object Yaz_Z_Assoc. Much more functional client.
45 * Revision 1.4 1999/03/23 14:17:57 adam
46 * More work on timeout handling. Work on yaz-client.
48 * Revision 1.3 1999/02/02 14:01:22 adam
49 * First WIN32 port of YAZ++.
51 * Revision 1.2 1999/01/28 13:08:47 adam
52 * Yaz_PDU_Assoc better encapsulated. Memory leak fix in
53 * yaz-socket-manager.cc.
55 * Revision 1.1.1.1 1999/01/28 09:41:07 adam
56 * First implementation of YAZ++.
61 #include <yaz/options.h>
62 #include <yaz++/yaz-z-server.h>
63 #include <yaz++/yaz-pdu-assoc.h>
64 #include <yaz++/yaz-socket-manager.h>
66 class MyILL : public Yaz_Facility_ILL {
68 int ill_init (Z_InitRequest *initRequest,
69 Z_InitResponse *initResponse);
72 class MyRetrieval : public Yaz_Facility_Retrieval, Yaz_USMARC {
74 int sr_init (Z_InitRequest *initRequest,
75 Z_InitResponse *initResponse);
76 void sr_search (Z_SearchRequest *searchRequest,
77 Z_SearchResponse *searchResponse);
78 void sr_present (Z_PresentRequest *presentRequest,
79 Z_PresentResponse *presentResponse);
80 void sr_record (const char *resultSetName,
83 Z_RecordComposition *comp,
84 Z_NamePlusRecord *namePlusRecord,
88 class MyServer : public Yaz_Z_Server {
90 MyServer(IYaz_PDU_Observable *the_PDU_Observable);
91 IYaz_PDU_Observer* sessionNotify(IYaz_PDU_Observable *the_PDU_Observable,
101 int MyILL::ill_init (Z_InitRequest *initRequest,
102 Z_InitResponse *initResponse)
104 yaz_log (LOG_LOG, "MyILL::ill_init");
108 int MyRetrieval::sr_init (Z_InitRequest *initRequest,
109 Z_InitResponse *initResponse)
111 yaz_log (LOG_LOG, "MyServer::sr_init");
115 void MyRetrieval::sr_search (Z_SearchRequest *searchRequest,
116 Z_SearchResponse *searchResponse)
118 yaz_log (LOG_LOG, "MyServer::recv_Z_search");
119 if (searchRequest->query->which == Z_Query_type_1)
121 Z_RPNStructure *s = searchRequest->query->u.type_1->RPNStructure;
122 if (s->which == Z_RPNStructure_simple &&
123 s->u.simple->which == Z_Operand_APT &&
124 s->u.simple->u.attributesPlusTerm->term->which == Z_Term_general)
126 Odr_oct *term = s->u.simple->u.attributesPlusTerm->term->u.general;
127 char *str = (char *) odr_malloc (odr_encode(), term->len+1);
129 memcpy (str, term->buf, term->len);
130 str[term->len] = '\0';
131 *searchResponse->resultCount = atoi(str);
136 void MyRetrieval::sr_present (Z_PresentRequest *presentRequest,
137 Z_PresentResponse *presentResponse)
139 yaz_log (LOG_LOG, "MyServer::recv_Z_present");
142 void MyRetrieval::sr_record (const char *resultSetName,
145 Z_RecordComposition *comp,
146 Z_NamePlusRecord *namePlusRecord,
149 yaz_log (LOG_LOG, "MyServer::recv_Z_record");
150 const char *rec = get_record(position);
151 create_databaseRecord (namePlusRecord, 0, VAL_USMARC, rec,
155 IYaz_PDU_Observer *MyServer::sessionNotify(
156 IYaz_PDU_Observable *the_PDU_Observable, int fd)
158 MyServer *new_server;
159 MyRetrieval *new_ret = new MyRetrieval;
160 MyILL *new_ill = new MyILL;
162 new_server = new MyServer(the_PDU_Observable);
163 new_server->timeout(900);
164 new_server->facility_add(new_ret, "my sr");
165 new_server->facility_add(new_ill, "my ill");
167 new_server->set_APDU_log(get_APDU_log());
172 MyServer::MyServer(IYaz_PDU_Observable *the_PDU_Observable) :
173 Yaz_Z_Server (the_PDU_Observable)
178 void MyServer::timeoutNotify()
180 yaz_log (LOG_LOG, "connection timed out");
184 void MyServer::failNotify()
186 yaz_log (LOG_LOG, "connection closed by client");
190 void MyServer::connectNotify()
195 void usage(char *prog)
197 fprintf (stderr, "%s: [-a log] [-v level] [-T] @:port\n", prog);
201 int main(int argc, char **argv)
206 char *addr = "tcp:@:9999";
209 Yaz_SocketManager mySocketManager;
211 Yaz_PDU_Assoc *my_PDU_Assoc = 0;
216 while ((ret = options("a:v:T", argv, argc, &arg)) != -2)
224 apdu_log = xstrdup(arg);
227 yaz_log_init_level (yaz_log_mask_str(arg));
238 my_PDU_Assoc = new Yaz_PDU_AssocThread(&mySocketManager);
240 my_PDU_Assoc = new Yaz_PDU_Assoc(&mySocketManager);
242 z = new MyServer(my_PDU_Assoc);
246 yaz_log (LOG_LOG, "set_APDU_log %s", apdu_log);
247 z->set_APDU_log(apdu_log);
250 while (mySocketManager.processEvent() > 0)