2 * Copyright (c) 2005, Index Data.
3 * See the file LICENSE for details.
5 * $Id: t-server.cpp,v 1.8 2006-03-29 13:33:47 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>
20 using namespace yazpp_1;
24 class Auth_Msg : public IMsg_Thread {
30 IMsg_Thread *handle();
32 Auth_Msg(GDU *gdu, MyServer *front);
36 Auth_Msg::Auth_Msg(GDU *gdu, MyServer *front)
50 IMsg_Thread *Auth_Msg::handle()
52 ODR odr = odr_createmem(ODR_ENCODE);
53 yaz_log(YLOG_LOG, "Auth_Msg:handle begin");
54 Z_GDU *z_gdu = m_gdu->get();
55 if (z_gdu->which == Z_GDU_Z3950)
58 switch(z_gdu->u.z3950->which)
60 case Z_APDU_initRequest:
61 apdu = zget_APDU(odr, Z_APDU_initResponse);
62 ODR_MASK_SET(apdu->u.initResponse->options, Z_Options_triggerResourceCtrl);
63 ODR_MASK_SET(apdu->u.initResponse->options, Z_Options_search);
64 ODR_MASK_SET(apdu->u.initResponse->options, Z_Options_present);
66 case Z_APDU_searchRequest:
68 apdu = zget_APDU(odr, Z_APDU_searchResponse);
70 case Z_APDU_triggerResourceControlRequest:
73 apdu = zget_APDU(odr, Z_APDU_close);
78 m_output = new GDU(apdu);
80 yaz_log(YLOG_LOG, "Auth_Msg:handle end");
85 class MyServer : public Z_Assoc {
88 MyServer(IPDU_Observable *the_PDU_Observable,
89 Msg_Thread *m_my_thread
91 IPDU_Observer* sessionNotify(IPDU_Observable *the_PDU_Observable,
94 void recv_GDU(Z_GDU *apdu, int len);
103 yazpp_1::GDUQueue m_in_queue;
104 Msg_Thread *m_my_thread;
107 void Auth_Msg::result()
109 m_front->m_no_requests--;
110 if (!m_front->m_delete_flag)
115 m_front->send_GDU(m_output->get(), &len);
120 m_front->m_delete_flag = 1;
123 if (m_front->m_delete_flag && m_front->m_no_requests == 0)
128 MyServer::MyServer(IPDU_Observable *the_PDU_Observable,
129 Msg_Thread *my_thread
131 : Z_Assoc(the_PDU_Observable)
133 m_my_thread = my_thread;
136 yaz_log(YLOG_LOG, "Construct Myserver=%p", this);
139 IPDU_Observer *MyServer::sessionNotify(IPDU_Observable
140 *the_PDU_Observable, int fd)
142 MyServer *my = new MyServer(the_PDU_Observable, m_my_thread);
143 yaz_log(YLOG_LOG, "New session %s", the_PDU_Observable->getpeername());
147 MyServer::~MyServer()
149 yaz_log(YLOG_LOG, "Destroy Myserver=%p", this);
152 void MyServer::recv_GDU(Z_GDU *apdu, int len)
154 GDU *gdu = new GDU(apdu);
155 Auth_Msg *m = new Auth_Msg(gdu, this);
160 void MyServer::failNotify()
163 if (m_no_requests == 0)
168 void MyServer::timeoutNotify()
171 if (m_no_requests == 0)
175 void MyServer::connectNotify()
180 void usage(const char *prog)
182 fprintf (stderr, "%s: [-a log] [-v level] [-T] @:port\n", prog);
186 int main(int argc, char **argv)
192 const char *addr = "tcp:@:9999";
196 while ((ret = options("n:a:v:T", argv, argc, &arg)) != -2)
204 no_threads = atoi(arg);
207 apdu_log = xstrdup(arg);
210 yaz_log_init_level (yaz_log_mask_str(arg));
221 SocketManager mySocketManager;
223 PDU_Assoc *my_PDU_Assoc = 0;
227 Msg_Thread *my_thread = new Msg_Thread(&mySocketManager, no_threads);
229 #if YAZ_POSIX_THREADS
231 my_PDU_Assoc = new PDU_AssocThread(&mySocketManager);
233 my_PDU_Assoc = new PDU_Assoc(&mySocketManager);
235 my_PDU_Assoc = new PDU_Assoc(&mySocketManager);
238 z = new MyServer(my_PDU_Assoc, my_thread);
242 yaz_log (YLOG_LOG, "set_APDU_log %s", apdu_log);
243 z->set_APDU_log(apdu_log);
246 while (mySocketManager.processEvent() > 0)
255 * indent-tabs-mode: nil
257 * vim: shiftwidth=4 tabstop=8 expandtab