Moving ThreadPoolSocketObserver and IThreadPoolMsg to yp2 namespace
[metaproxy-moved-to-github.git] / src / p2.cpp
1 /* $Id: p2.cpp,v 1.4 2005-10-14 10:27:18 adam Exp $
2    Copyright (c) 1998-2005, Index Data.
3
4 This file is part of the yaz-proxy.
5
6 YAZ proxy is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 2, or (at your option) any later
9 version.
10
11 YAZ proxy is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with YAZ proxy; see the file LICENSE.  If not, write to the
18 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
19 02111-1307, USA.
20  */
21
22 #include <pthread.h>
23 #include <stdlib.h>
24 #include <yaz/log.h>
25 #include <yaz/diagbib1.h>
26 #include <yaz/options.h>
27
28 #include "config.hpp"
29 #include <yaz++/socket-manager.h>
30 #include "p2_config.h"
31 #include "p2_frontend.h"
32 #include "p2_xmlerror.h"
33 #include "p2_modules.h"
34
35 using namespace yazpp_1;
36
37 extern P2_ModuleEntry *p2_backend_dummy;
38
39 /*
40   frontend result set map
41     resultset -> db,query
42
43   backend result set map
44     db,query -> resultset, target
45                 resultset, target
46 */
47 class P2_Frontend;
48
49 P2_Config *P2_Server::lockConfig()
50 {
51     pthread_mutex_lock(&m_mutex_config);
52     return m_config;
53 }
54
55 void P2_Server::unlockConfig()
56 {
57     pthread_mutex_unlock(&m_mutex_config);
58 }
59
60 P2_Server::P2_Server(IPDU_Observable *the_PDU_Observable,
61                      yp2::ThreadPoolSocketObserver *my_thread,
62                      P2_Config *config,
63                      P2_ModuleFactory *modules)
64     :  Z_Assoc(the_PDU_Observable)
65 {
66     m_my_thread = my_thread;
67     m_modules = modules;
68     m_config = config;
69
70     pthread_mutex_init(&m_mutex_config, 0);
71     
72     yaz_log(YLOG_LOG, "Construct P2_Server=%p", this);
73 }
74
75 IPDU_Observer *P2_Server::sessionNotify(IPDU_Observable
76                                        *the_PDU_Observable, int fd)
77 {
78     P2_Frontend *my = new P2_Frontend(the_PDU_Observable, m_my_thread, this);
79     yaz_log(YLOG_LOG, "New session %s", the_PDU_Observable->getpeername());
80     return my;
81 }
82
83 P2_Server::~P2_Server()
84 {
85     yaz_log(YLOG_LOG, "Destroy P2_server=%p", this);
86     pthread_mutex_destroy(&m_mutex_config);
87 }
88
89 void P2_Server::recv_GDU(Z_GDU *apdu, int len)
90 {
91 }
92
93 void P2_Server::failNotify()
94 {
95 }
96
97 void P2_Server::timeoutNotify()
98 {
99 }
100
101 void P2_Server::connectNotify()
102 {
103 }
104
105 int main(int argc, char **argv)
106 {
107     p2_xmlerror_setup();
108
109     P2_Config config;
110
111     if (!config.parse_options(argc, argv))
112     {
113         yaz_log(YLOG_FATAL, "Configuration incorrect. Exiting");
114         exit(1);
115     }
116
117     SocketManager mySocketManager;
118
119     PDU_Assoc *my_PDU_Assoc = 0;
120     
121     yp2::ThreadPoolSocketObserver my_thread(&mySocketManager,
122                                             config.m_no_threads);
123
124     my_PDU_Assoc = new PDU_Assoc(&mySocketManager);
125
126     P2_ModuleFactory modules;
127
128     modules.add(p2_backend_dummy);
129
130     std::list<P2_ConfigModule *>::const_iterator it;
131     for (it = config.m_modules.begin(); it != config.m_modules.end(); it++)
132         modules.add((*it)->m_fname.c_str());
133     
134     P2_Server z(my_PDU_Assoc, &my_thread, &config, &modules);
135     z.server(config.m_listen_address.c_str());
136
137     while (mySocketManager.processEvent() > 0)
138         ;
139     return 0;
140 }
141 /*
142  * Local variables:
143  * c-basic-offset: 4
144  * indent-tabs-mode: nil
145  * End:
146  * vim: shiftwidth=4 tabstop=8 expandtab
147  */
148