Use AM_LDFLAGS instead of LDFLAGS
[yazpp-moved-to-github.git] / src / yaz-my-server.cpp
1 /*
2  * Copyright (c) 1998-2001, Index Data.
3  * See the file LICENSE for details.
4  * 
5  * $Id: yaz-my-server.cpp,v 1.12 2003-10-16 11:43:37 adam Exp $
6  */
7
8 #include <yaz/log.h>
9 #include <yaz/options.h>
10 #include <yaz++/z-server.h>
11 #include <yaz++/pdu-assoc.h>
12 #include <yaz++/socket-manager.h>
13
14 class MyILL : public Yaz_Facility_ILL {
15 public:
16     void ill_service (Z_ExtendedServicesRequest *req,
17                       Z_ItemOrder *io,
18                       Z_ExtendedServicesResponse *res);
19 };
20
21 class MyUpdate : public Yaz_Facility_Update {
22 public:
23     void update_service (Z_ExtendedServicesRequest *req,
24                          Z_IUUpdate *io,
25                          Z_ExtendedServicesResponse *res);
26     void update_service0 (Z_ExtendedServicesRequest *req,
27                          Z_IU0Update *io,
28                          Z_ExtendedServicesResponse *res);
29 };
30
31
32 class MyRetrieval : public Yaz_Facility_Retrieval, Yaz_USMARC {
33 public:
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,
41                     int position,
42                     int *format,
43                     Z_RecordComposition *comp,
44                     Z_NamePlusRecord *namePlusRecord,
45                     Z_Records *records);
46 };
47
48 class MyServer : public Yaz_Z_Server {
49 public:
50     ~MyServer();
51     MyServer(IYaz_PDU_Observable *the_PDU_Observable);
52     IYaz_PDU_Observer* sessionNotify(IYaz_PDU_Observable *the_PDU_Observable,
53                                      int fd);
54     void failNotify();
55     void timeoutNotify();
56     void connectNotify();
57
58 private:
59     MyRetrieval m_retrieval;
60     MyILL       m_ill;
61     MyUpdate    m_update;
62     int m_no;
63 };
64
65 void MyILL::ill_service (Z_ExtendedServicesRequest *req,
66                          Z_ItemOrder *io,
67                          Z_ExtendedServicesResponse *res)
68 {
69     yaz_log (LOG_LOG, "MyServer::ill_service");
70 }
71
72 void MyUpdate::update_service (Z_ExtendedServicesRequest *req,
73                            Z_IUUpdate *io,
74                            Z_ExtendedServicesResponse *res)
75 {
76     yaz_log (LOG_LOG, "MyServer::update_service (v1.1)");
77 }
78
79 void MyUpdate::update_service0 (Z_ExtendedServicesRequest *req,
80                            Z_IU0Update *io,
81                                 Z_ExtendedServicesResponse *res)
82 {
83     yaz_log (LOG_LOG, "MyServer::update_service (v1.0)");
84 }
85
86 int MyRetrieval::sr_init (Z_InitRequest *initRequest,
87                        Z_InitResponse *initResponse)
88 {
89     yaz_log (LOG_LOG, "MyServer::sr_init");
90     return 1;
91 }
92
93 void MyRetrieval::sr_search (Z_SearchRequest *searchRequest,
94                              Z_SearchResponse *searchResponse)
95 {
96     yaz_log (LOG_LOG, "MyServer::recv_Z_search");
97     if (searchRequest->query->which == Z_Query_type_1)
98     {
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)
103         {
104             Odr_oct *term = s->u.simple->u.attributesPlusTerm->term->u.general;
105             char *str = (char *) odr_malloc (odr_encode(), term->len+1);
106             if (term->len)
107                 memcpy (str, term->buf, term->len);
108             str[term->len] = '\0';
109             *searchResponse->resultCount = atoi(str);
110         }
111     }
112 }
113
114 void MyRetrieval::sr_present (Z_PresentRequest *presentRequest,
115                                Z_PresentResponse *presentResponse)
116 {
117     yaz_log (LOG_LOG, "MyServer::recv_Z_present");
118 }
119
120 void MyRetrieval::sr_record (const char *resultSetName,
121                              int position,
122                              int *format,
123                              Z_RecordComposition *comp,
124                              Z_NamePlusRecord *namePlusRecord,
125                              Z_Records *records)
126 {
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,
130                            strlen(rec));
131 }
132
133 MyServer::~MyServer()
134 {
135 }
136
137 IYaz_PDU_Observer *MyServer::sessionNotify(
138     IYaz_PDU_Observable *the_PDU_Observable, int fd)
139 {
140     MyServer *new_server;
141     m_no++;
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());
148
149     return new_server;
150 }
151
152 MyServer::MyServer(IYaz_PDU_Observable *the_PDU_Observable) :
153     Yaz_Z_Server (the_PDU_Observable)
154 {
155     m_no = 0;
156 }
157
158 void MyServer::timeoutNotify()
159 {
160     yaz_log (LOG_LOG, "connection timed out");
161     delete this;
162 }
163
164 void MyServer::failNotify()
165 {
166     yaz_log (LOG_LOG, "connection closed by client");
167     delete this;
168 }
169
170 void MyServer::connectNotify()
171 {
172 }
173
174 void usage(const char *prog)
175 {
176     fprintf (stderr, "%s: [-a log] [-v level] [-T] @:port\n", prog);
177     exit (1);
178 }
179
180 int main(int argc, char **argv)
181 {
182     int thread_flag = 0;
183     char *arg;
184     char *prog = *argv;
185     const char *addr = "tcp:@:9999";
186     char *apdu_log = 0;
187     
188     Yaz_SocketManager mySocketManager;
189     
190     Yaz_PDU_Assoc *my_PDU_Assoc = 0;
191     
192     MyServer *z = 0;
193     int ret;
194     
195     while ((ret = options("a:v:T", argv, argc, &arg)) != -2)
196     {
197         switch (ret)
198         {
199         case 0:
200             addr = xstrdup(arg);
201             break;
202         case 'a':
203             apdu_log = xstrdup(arg);
204             break;
205         case 'v':
206             yaz_log_init_level (yaz_log_mask_str(arg));
207             break;
208         case 'T':
209             thread_flag = 1;
210             break;
211         default:
212             usage(prog);
213             return 1;
214         }
215     }
216 #if YAZ_POSIX_THREADS
217     if (thread_flag)
218         my_PDU_Assoc = new Yaz_PDU_AssocThread(&mySocketManager);
219     else
220         my_PDU_Assoc = new Yaz_PDU_Assoc(&mySocketManager);
221 #else
222     my_PDU_Assoc = new Yaz_PDU_Assoc(&mySocketManager);
223 #endif
224     
225     z = new MyServer(my_PDU_Assoc);
226     z->server(addr);
227     if (apdu_log)
228     {
229         yaz_log (LOG_LOG, "set_APDU_log %s", apdu_log);
230         z->set_APDU_log(apdu_log);
231     }
232
233     while (mySocketManager.processEvent() > 0)
234         ;
235     delete z;
236     return 0;
237 }