1 /* This file is part of YAZ proxy
2 Copyright (C) 1998-2009 Index Data
4 YAZ proxy is free software; you can redistribute it and/or modify it under
5 the terms of the GNU General Public License as published by the Free
6 Software Foundation; either version 2, or (at your option) any later
9 YAZ proxy is distributed in the hope that it will be useful, but WITHOUT ANY
10 WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 #include <yaz/diagbib1.h>
23 #include <yaz/options.h>
24 #include "msg-thread.h"
25 #include <yazpp/z-assoc.h>
26 #include <yazpp/pdu-assoc.h>
27 #include <yazpp/gdu.h>
28 #include <yazpp/gduqueue.h>
29 #include <yazpp/socket-manager.h>
35 using namespace yazpp_1;
39 class Auth_Msg : public IMsg_Thread {
45 IMsg_Thread *handle();
47 Auth_Msg(GDU *gdu, MyServer *front);
51 Auth_Msg::Auth_Msg(GDU *gdu, MyServer *front)
65 IMsg_Thread *Auth_Msg::handle()
67 ODR odr = odr_createmem(ODR_ENCODE);
68 yaz_log(YLOG_LOG, "Auth_Msg:handle begin");
69 Z_GDU *z_gdu = m_gdu->get();
70 if (z_gdu->which == Z_GDU_Z3950)
73 switch(z_gdu->u.z3950->which)
75 case Z_APDU_initRequest:
76 apdu = zget_APDU(odr, Z_APDU_initResponse);
77 ODR_MASK_SET(apdu->u.initResponse->options, Z_Options_triggerResourceCtrl);
78 ODR_MASK_SET(apdu->u.initResponse->options, Z_Options_search);
79 ODR_MASK_SET(apdu->u.initResponse->options, Z_Options_present);
81 case Z_APDU_searchRequest:
85 apdu = zget_APDU(odr, Z_APDU_searchResponse);
87 case Z_APDU_triggerResourceControlRequest:
90 apdu = zget_APDU(odr, Z_APDU_close);
95 m_output = new GDU(apdu);
97 yaz_log(YLOG_LOG, "Auth_Msg:handle end");
102 class MyServer : public Z_Assoc {
105 MyServer(IPDU_Observable *the_PDU_Observable,
106 Msg_Thread *m_my_thread
108 IPDU_Observer* sessionNotify(IPDU_Observable *the_PDU_Observable,
111 void recv_GDU(Z_GDU *apdu, int len);
114 void timeoutNotify();
115 void connectNotify();
120 yazpp_1::GDUQueue m_in_queue;
121 Msg_Thread *m_my_thread;
124 void Auth_Msg::result()
126 m_front->m_no_requests--;
127 if (!m_front->m_delete_flag)
132 m_front->send_GDU(m_output->get(), &len);
137 m_front->m_delete_flag = 1;
140 if (m_front->m_delete_flag && m_front->m_no_requests == 0)
145 MyServer::MyServer(IPDU_Observable *the_PDU_Observable,
146 Msg_Thread *my_thread
148 : Z_Assoc(the_PDU_Observable)
150 m_my_thread = my_thread;
153 yaz_log(YLOG_LOG, "Construct Myserver=%p", this);
156 IPDU_Observer *MyServer::sessionNotify(IPDU_Observable
157 *the_PDU_Observable, int fd)
159 MyServer *my = new MyServer(the_PDU_Observable, m_my_thread);
160 yaz_log(YLOG_LOG, "New session %s", the_PDU_Observable->getpeername());
164 MyServer::~MyServer()
166 yaz_log(YLOG_LOG, "Destroy Myserver=%p", this);
169 void MyServer::recv_GDU(Z_GDU *apdu, int len)
171 GDU *gdu = new GDU(apdu);
172 Auth_Msg *m = new Auth_Msg(gdu, this);
177 void MyServer::failNotify()
180 if (m_no_requests == 0)
185 void MyServer::timeoutNotify()
188 if (m_no_requests == 0)
192 void MyServer::connectNotify()
197 void usage(const char *prog)
199 fprintf (stderr, "%s: [-a log] [-v level] [-T] @:port\n", prog);
203 int main(int argc, char **argv)
209 const char *addr = "tcp:@:9999";
213 while ((ret = options("n:a:v:T", argv, argc, &arg)) != -2)
221 no_threads = atoi(arg);
224 apdu_log = xstrdup(arg);
227 yaz_log_init_level (yaz_log_mask_str(arg));
238 SocketManager mySocketManager;
240 PDU_Assoc *my_PDU_Assoc = 0;
244 Msg_Thread *my_thread = new Msg_Thread(&mySocketManager, no_threads);
246 #if YAZ_POSIX_THREADS
248 my_PDU_Assoc = new PDU_AssocThread(&mySocketManager);
250 my_PDU_Assoc = new PDU_Assoc(&mySocketManager);
252 my_PDU_Assoc = new PDU_Assoc(&mySocketManager);
255 z = new MyServer(my_PDU_Assoc, my_thread);
259 yaz_log (YLOG_LOG, "set_APDU_log %s", apdu_log);
260 z->set_APDU_log(apdu_log);
263 while (mySocketManager.processEvent() > 0)
272 * indent-tabs-mode: nil
274 * vim: shiftwidth=4 tabstop=8 expandtab