Initial checkin of proxy 2 code
[yazproxy-moved-to-github.git] / src / p2.cpp
1 /* $Id: p2.cpp,v 1.1 2005-10-05 12:07:14 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 <yaz++/socket-manager.h>
29 #include "p2_config.h"
30 #include "p2_frontend.h"
31 #include "p2_xmlerror.h"
32 #include "p2_modules.h"
33
34 using namespace yazpp_1;
35
36 extern P2_ModuleEntry *p2_backend_dummy;
37
38 /*
39   frontend result set map
40     resultset -> db,query
41
42   backend result set map
43     db,query -> resultset, target
44                 resultset, target
45 */
46 class P2_Frontend;
47
48 P2_Config *P2_Server::lockConfig()
49 {
50     pthread_mutex_lock(&m_mutex_config);
51     return m_config;
52 }
53
54 void P2_Server::unlockConfig()
55 {
56     pthread_mutex_unlock(&m_mutex_config);
57 }
58
59 P2_Server::P2_Server(IPDU_Observable *the_PDU_Observable,
60                      Msg_Thread *my_thread,
61                      P2_Config *config,
62                      P2_ModuleFactory *modules)
63     :  Z_Assoc(the_PDU_Observable)
64 {
65     m_my_thread = my_thread;
66     m_modules = modules;
67     m_config = config;
68
69     pthread_mutex_init(&m_mutex_config, 0);
70     
71     yaz_log(YLOG_LOG, "Construct P2_Server=%p", this);
72 }
73
74 IPDU_Observer *P2_Server::sessionNotify(IPDU_Observable
75                                        *the_PDU_Observable, int fd)
76 {
77     P2_Frontend *my = new P2_Frontend(the_PDU_Observable, m_my_thread, this);
78     yaz_log(YLOG_LOG, "New session %s", the_PDU_Observable->getpeername());
79     return my;
80 }
81
82 P2_Server::~P2_Server()
83 {
84     yaz_log(YLOG_LOG, "Destroy P2_server=%p", this);
85     pthread_mutex_destroy(&m_mutex_config);
86 }
87
88 void P2_Server::recv_GDU(Z_GDU *apdu, int len)
89 {
90 }
91
92 void P2_Server::failNotify()
93 {
94 }
95
96 void P2_Server::timeoutNotify()
97 {
98 }
99
100 void P2_Server::connectNotify()
101 {
102 }
103
104 int main(int argc, char **argv)
105 {
106     p2_xmlerror_setup();
107
108     P2_Config config;
109
110     if (!config.parse_options(argc, argv))
111     {
112         yaz_log(YLOG_FATAL, "Configuration incorrect. Exiting");
113         exit(1);
114     }
115
116     SocketManager mySocketManager;
117
118     PDU_Assoc *my_PDU_Assoc = 0;
119     
120     Msg_Thread my_thread(&mySocketManager, config.m_no_threads);
121
122     my_PDU_Assoc = new PDU_Assoc(&mySocketManager);
123
124     P2_ModuleFactory modules;
125
126     modules.add(p2_backend_dummy);
127
128     std::list<P2_ConfigModule *>::const_iterator it;
129     for (it = config.m_modules.begin(); it != config.m_modules.end(); it++)
130         modules.add((*it)->m_fname.c_str());
131     
132     P2_Server z(my_PDU_Assoc, &my_thread, &config, &modules);
133     z.server(config.m_listen_address.c_str());
134
135     while (mySocketManager.processEvent() > 0)
136         ;
137     return 0;
138 }
139 /*
140  * Local variables:
141  * c-basic-offset: 4
142  * indent-tabs-mode: nil
143  * End:
144  * vim: shiftwidth=4 tabstop=8 expandtab
145  */
146