2 * Copyright (c) 1998-2005, Index Data.
3 * See the file LICENSE for details.
5 * $Id: t-server.cpp,v 1.2 2005-06-25 15:58:33 adam Exp $
11 #include <yaz/diagbib1.h>
12 #include <yaz/options.h>
13 #include "msg-thread.h"
14 #include <yaz++/z-assoc.h>
15 #include <yaz++/pdu-assoc.h>
16 #include <yaz++/gdu.h>
17 #include <yaz++/socket-manager.h>
19 using namespace yazpp_1;
28 pthread_mutex_t m_mutex;
34 pthread_mutex_init(&m_mutex, 0);
39 pthread_mutex_destroy(&m_mutex);
44 pthread_mutex_lock(&m_mutex);
49 pthread_mutex_unlock(&m_mutex);
55 class Auth_Msg : public IMsg_Thread {
61 IMsg_Thread *handle();
63 Auth_Msg(GDU *gdu, MyServer *front);
67 Auth_Msg::Auth_Msg(GDU *gdu, MyServer *front)
81 IMsg_Thread *Auth_Msg::handle()
83 ODR odr = odr_createmem(ODR_ENCODE);
84 yaz_log(YLOG_LOG, "Auth_Msg:handle begin");
85 Z_GDU *z_gdu = m_gdu->get();
86 if (z_gdu->which == Z_GDU_Z3950)
89 switch(z_gdu->u.z3950->which)
91 case Z_APDU_initRequest:
92 apdu = zget_APDU(odr, Z_APDU_initResponse);
94 case Z_APDU_searchRequest:
96 apdu = zget_APDU(odr, Z_APDU_searchResponse);
99 apdu = zget_APDU(odr, Z_APDU_close);
104 m_output = new GDU(apdu);
106 yaz_log(YLOG_LOG, "Auth_Msg:handle end");
111 class MyServer : public Z_Assoc {
114 MyServer(IPDU_Observable *the_PDU_Observable,
115 Msg_Thread *m_my_thread
117 IPDU_Observer* sessionNotify(IPDU_Observable *the_PDU_Observable,
120 void recv_GDU(Z_GDU *apdu, int len);
123 void timeoutNotify();
124 void connectNotify();
129 yazpp_1::GDUQueue m_in_queue;
130 Msg_Thread *m_my_thread;
133 void Auth_Msg::result()
135 m_front->m_no_requests--;
136 if (!m_front->m_delete_flag)
141 m_front->send_GDU(m_output->get(), &len);
146 m_front->m_delete_flag = 1;
149 if (m_front->m_delete_flag && m_front->m_no_requests == 0)
153 MyServer::MyServer(IPDU_Observable *the_PDU_Observable,
154 Msg_Thread *my_thread
156 : Z_Assoc(the_PDU_Observable)
158 m_my_thread = my_thread;
161 yaz_log(YLOG_LOG, "Construct Myserver=%p", this);
164 IPDU_Observer *MyServer::sessionNotify(IPDU_Observable
165 *the_PDU_Observable, int fd)
167 MyServer *my = new MyServer(the_PDU_Observable, m_my_thread);
168 yaz_log(YLOG_LOG, "New session %s", the_PDU_Observable->getpeername());
172 MyServer::~MyServer()
174 yaz_log(YLOG_LOG, "Destroy Myserver=%p", this);
177 void MyServer::recv_GDU(Z_GDU *apdu, int len)
179 GDU *gdu = new GDU(apdu);
180 Auth_Msg *m = new Auth_Msg(gdu, this);
185 void MyServer::failNotify()
188 if (m_no_requests == 0)
193 void MyServer::timeoutNotify()
196 if (m_no_requests == 0)
200 void MyServer::connectNotify()
205 void usage(const char *prog)
207 fprintf (stderr, "%s: [-a log] [-v level] [-T] @:port\n", prog);
211 int main(int argc, char **argv)
217 const char *addr = "tcp:@:9999";
220 while ((ret = options("a:v:T", argv, argc, &arg)) != -2)
228 apdu_log = xstrdup(arg);
231 yaz_log_init_level (yaz_log_mask_str(arg));
242 SocketManager mySocketManager;
244 PDU_Assoc *my_PDU_Assoc = 0;
248 Msg_Thread *my_thread = new Msg_Thread(&mySocketManager);
250 #if YAZ_POSIX_THREADS
252 my_PDU_Assoc = new PDU_AssocThread(&mySocketManager);
254 my_PDU_Assoc = new PDU_Assoc(&mySocketManager);
256 my_PDU_Assoc = new PDU_Assoc(&mySocketManager);
259 z = new MyServer(my_PDU_Assoc, my_thread);
263 yaz_log (YLOG_LOG, "set_APDU_log %s", apdu_log);
264 z->set_APDU_log(apdu_log);
267 while (mySocketManager.processEvent() > 0)
276 * indent-tabs-mode: nil
278 * vim: shiftwidth=4 tabstop=8 expandtab