2 * Copyright (c) 2005, Index Data.
3 * See the file LICENSE for details.
5 * $Id: t-server.cpp,v 1.9 2006-03-30 14:19:19 adam Exp $
11 #include <yaz/diagbib1.h>
12 #include <yaz/options.h>
13 #include "msg-thread.h"
14 #include <yazpp/z-assoc.h>
15 #include <yazpp/pdu-assoc.h>
16 #include <yazpp/gdu.h>
17 #include <yazpp/gduqueue.h>
18 #include <yazpp/socket-manager.h>
24 using namespace yazpp_1;
28 class Auth_Msg : public IMsg_Thread {
34 IMsg_Thread *handle();
36 Auth_Msg(GDU *gdu, MyServer *front);
40 Auth_Msg::Auth_Msg(GDU *gdu, MyServer *front)
54 IMsg_Thread *Auth_Msg::handle()
56 ODR odr = odr_createmem(ODR_ENCODE);
57 yaz_log(YLOG_LOG, "Auth_Msg:handle begin");
58 Z_GDU *z_gdu = m_gdu->get();
59 if (z_gdu->which == Z_GDU_Z3950)
62 switch(z_gdu->u.z3950->which)
64 case Z_APDU_initRequest:
65 apdu = zget_APDU(odr, Z_APDU_initResponse);
66 ODR_MASK_SET(apdu->u.initResponse->options, Z_Options_triggerResourceCtrl);
67 ODR_MASK_SET(apdu->u.initResponse->options, Z_Options_search);
68 ODR_MASK_SET(apdu->u.initResponse->options, Z_Options_present);
70 case Z_APDU_searchRequest:
74 apdu = zget_APDU(odr, Z_APDU_searchResponse);
76 case Z_APDU_triggerResourceControlRequest:
79 apdu = zget_APDU(odr, Z_APDU_close);
84 m_output = new GDU(apdu);
86 yaz_log(YLOG_LOG, "Auth_Msg:handle end");
91 class MyServer : public Z_Assoc {
94 MyServer(IPDU_Observable *the_PDU_Observable,
95 Msg_Thread *m_my_thread
97 IPDU_Observer* sessionNotify(IPDU_Observable *the_PDU_Observable,
100 void recv_GDU(Z_GDU *apdu, int len);
103 void timeoutNotify();
104 void connectNotify();
109 yazpp_1::GDUQueue m_in_queue;
110 Msg_Thread *m_my_thread;
113 void Auth_Msg::result()
115 m_front->m_no_requests--;
116 if (!m_front->m_delete_flag)
121 m_front->send_GDU(m_output->get(), &len);
126 m_front->m_delete_flag = 1;
129 if (m_front->m_delete_flag && m_front->m_no_requests == 0)
134 MyServer::MyServer(IPDU_Observable *the_PDU_Observable,
135 Msg_Thread *my_thread
137 : Z_Assoc(the_PDU_Observable)
139 m_my_thread = my_thread;
142 yaz_log(YLOG_LOG, "Construct Myserver=%p", this);
145 IPDU_Observer *MyServer::sessionNotify(IPDU_Observable
146 *the_PDU_Observable, int fd)
148 MyServer *my = new MyServer(the_PDU_Observable, m_my_thread);
149 yaz_log(YLOG_LOG, "New session %s", the_PDU_Observable->getpeername());
153 MyServer::~MyServer()
155 yaz_log(YLOG_LOG, "Destroy Myserver=%p", this);
158 void MyServer::recv_GDU(Z_GDU *apdu, int len)
160 GDU *gdu = new GDU(apdu);
161 Auth_Msg *m = new Auth_Msg(gdu, this);
166 void MyServer::failNotify()
169 if (m_no_requests == 0)
174 void MyServer::timeoutNotify()
177 if (m_no_requests == 0)
181 void MyServer::connectNotify()
186 void usage(const char *prog)
188 fprintf (stderr, "%s: [-a log] [-v level] [-T] @:port\n", prog);
192 int main(int argc, char **argv)
198 const char *addr = "tcp:@:9999";
202 while ((ret = options("n:a:v:T", argv, argc, &arg)) != -2)
210 no_threads = atoi(arg);
213 apdu_log = xstrdup(arg);
216 yaz_log_init_level (yaz_log_mask_str(arg));
227 SocketManager mySocketManager;
229 PDU_Assoc *my_PDU_Assoc = 0;
233 Msg_Thread *my_thread = new Msg_Thread(&mySocketManager, no_threads);
235 #if YAZ_POSIX_THREADS
237 my_PDU_Assoc = new PDU_AssocThread(&mySocketManager);
239 my_PDU_Assoc = new PDU_Assoc(&mySocketManager);
241 my_PDU_Assoc = new PDU_Assoc(&mySocketManager);
244 z = new MyServer(my_PDU_Assoc, my_thread);
248 yaz_log (YLOG_LOG, "set_APDU_log %s", apdu_log);
249 z->set_APDU_log(apdu_log);
252 while (mySocketManager.processEvent() > 0)
261 * indent-tabs-mode: nil
263 * vim: shiftwidth=4 tabstop=8 expandtab