1 /*****
2 *
3 * Copyright (C) 2008-2015 CS-SI. All Rights Reserved.
4 * Author: Yoann Vandoorselaere <yoann@prelude-ids.com>
5 *
6 * This file is part of the Prelude library.
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2, or (at your option)
11 * any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 *
22 *****/
23 
24 #include <vector>
25 
26 #include "prelude.h"
27 #include "prelude-connection-pool.h"
28 
29 #include "prelude-connection-pool.hxx"
30 #include "prelude-client.hxx"
31 #include "prelude-error.hxx"
32 
33 using namespace Prelude;
34 
35 
~ConnectionPool()36 ConnectionPool::~ConnectionPool()
37 {
38         if ( _pool )
39                 prelude_connection_pool_destroy(_pool);
40 }
41 
42 
ConnectionPool()43 ConnectionPool::ConnectionPool()
44 {
45         _pool = NULL;
46 }
47 
48 
ConnectionPool(prelude_connection_pool_t * pool)49 ConnectionPool::ConnectionPool(prelude_connection_pool_t *pool)
50 {
51         _pool = pool;
52 }
53 
54 
ConnectionPool(const ConnectionPool & con)55 ConnectionPool::ConnectionPool(const ConnectionPool &con)
56 {
57         _pool = (con._pool) ? prelude_connection_pool_ref(con._pool) : NULL;
58 }
59 
60 
ConnectionPool(ClientProfile & cp,int permission)61 ConnectionPool::ConnectionPool(ClientProfile &cp, int permission)
62 {
63         int ret;
64 
65         ret = prelude_connection_pool_new(&_pool, cp, (prelude_connection_permission_t) permission);
66         if ( ret < 0 )
67                 throw PreludeError(ret);
68 }
69 
70 
71 
init()72 void ConnectionPool::init()
73 {
74         int ret;
75 
76         ret = prelude_connection_pool_init(_pool);
77         if ( ret < 0 )
78                 throw PreludeError(ret);
79 }
80 
81 
getConnectionList() const82 std::vector<Prelude::Connection> ConnectionPool::getConnectionList() const
83 {
84         std::vector<Prelude::Connection> clist;
85         prelude_connection_t *con;
86         prelude_list_t *head, *tmp;
87 
88         head = prelude_connection_pool_get_connection_list(_pool);
89 
90         prelude_list_for_each(head, tmp) {
91                 con = (prelude_connection_t *) prelude_linked_object_get_object(tmp);
92                 clist.push_back(Connection(prelude_connection_ref(con)));
93         }
94 
95         return clist;
96 }
97 
98 
addConnection(Connection con)99 void ConnectionPool::addConnection(Connection con)
100 {
101         prelude_connection_pool_add_connection(_pool, prelude_connection_ref(con));
102 }
103 
104 
delConnection(Connection con)105 void ConnectionPool::delConnection(Connection con)
106 {
107         prelude_connection_pool_del_connection(_pool, con);
108 }
109 
110 
setConnectionDead(Connection & con)111 void ConnectionPool::setConnectionDead(Connection &con)
112 {
113         prelude_connection_pool_set_connection_dead(_pool, con);
114 }
115 
116 
setConnectionAlive(Connection & con)117 void ConnectionPool::setConnectionAlive(Connection &con)
118 {
119         prelude_connection_pool_set_connection_alive(_pool, con);
120 }
121 
122 
setConnectionString(const char * str)123 void ConnectionPool::setConnectionString(const char *str)
124 {
125         int ret;
126 
127         ret = prelude_connection_pool_set_connection_string(_pool, str);
128         if ( ret < 0 )
129                 throw PreludeError(ret);
130 }
131 
132 
getConnectionString() const133 const char *ConnectionPool::getConnectionString() const
134 {
135         return prelude_connection_pool_get_connection_string(_pool);
136 }
137 
138 
getFlags() const139 int ConnectionPool::getFlags() const
140 {
141         return prelude_connection_pool_get_flags(_pool);
142 }
143 
144 
setFlags(int flags)145 void ConnectionPool::setFlags(int flags)
146 {
147         prelude_connection_pool_set_flags(_pool, (prelude_connection_pool_flags_t) flags);
148 }
149 
150 
setRequiredPermission(int permission)151 void ConnectionPool::setRequiredPermission(int permission)
152 {
153         prelude_connection_pool_set_required_permission(_pool, (prelude_connection_permission_t) permission);
154 }
155 
156 
setData(void * data)157 void ConnectionPool::setData(void *data)
158 {
159         prelude_connection_pool_set_data(_pool, data);
160 }
161 
162 
getData() const163 void *ConnectionPool::getData() const
164 {
165         return prelude_connection_pool_get_data(_pool);
166 }
167 
168 
operator =(const ConnectionPool & pool)169 ConnectionPool &ConnectionPool::operator=(const ConnectionPool &pool)
170 {
171         if ( this != &pool && _pool != pool._pool ) {
172                 if ( _pool )
173                         prelude_connection_pool_destroy(_pool);
174 
175                 _pool = (pool._pool) ? prelude_connection_pool_ref(pool._pool) : NULL;
176         }
177 
178         return *this;
179 }
180 
181 
operator prelude_connection_pool_t*()182 ConnectionPool::operator prelude_connection_pool_t *()
183 {
184         return _pool;
185 }
186