1 /*
2    BAREOS® - Backup Archiving REcovery Open Sourced
3 
4    Copyright (C) 2016-2020 Bareos GmbH & Co. KG
5 
6    This program is Free Software; you can redistribute it and/or
7    modify it under the terms of version three of the GNU Affero General Public
8    License as published by the Free Software Foundation and included
9    in the file LICENSE.
10 
11    This program is distributed in the hope that it will be useful, but
12    WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14    Affero General Public License for more details.
15 
16    You should have received a copy of the GNU Affero General Public License
17    along with this program; if not, write to the Free Software
18    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19    02110-1301, USA.
20 */
21 /**
22  * @file
23  * Connection Pool
24  *
25  * handle/store multiple connections
26  */
27 
28 #ifndef BAREOS_LIB_CONNECTION_POOL_H_
29 #define BAREOS_LIB_CONNECTION_POOL_H_
30 
31 
32 class alist;
33 class BareosSocket;
34 
35 class Connection {
36  public:
37   Connection(const char* name,
38              int protocol_version,
39              BareosSocket* socket,
40              bool authenticated = true);
41   ~Connection();
42 
name()43   const char* name() { return name_; }
protocol_version()44   int protocol_version() { return protocol_version_; }
bsock()45   BareosSocket* bsock() { return socket_; }
authenticated()46   bool authenticated() { return authenticated_; }
in_use()47   bool in_use() { return in_use_; }
ConnectTime()48   time_t ConnectTime() { return connect_time_; }
49 
50   bool check(int timeout = 0);
51   bool take();
52 
53  private:
lock()54   void lock() { P(mutex_); }
unlock()55   void unlock() { V(mutex_); }
56   pthread_t tid_;
57   BareosSocket* socket_;
58   char name_[MAX_NAME_LENGTH];
59   int protocol_version_;
60   bool authenticated_;
61   volatile bool in_use_;
62   time_t connect_time_;
63   pthread_mutex_t mutex_;
64 };
65 
66 class ConnectionPool {
67  public:
68   ConnectionPool();
69   ~ConnectionPool();
70 
71   Connection* add_connection(const char* name,
72                              int protocol_version,
73                              BareosSocket* socket,
74                              bool authenticated = true);
75   Connection* remove(const char* name, int timeout_in_seconds = 0);
76   alist* get_as_alist();
77   void cleanup();
78 
79  private:
80   alist* connections_;
81   int WaitForNewConnection(timespec& timeout);
82   bool add(Connection* connection);
83   bool remove(Connection* connection);
84   Connection* get_connection(const char* name);
85   Connection* get_connection(const char* name, timespec& timeout);
86   pthread_mutex_t add_mutex_;
87   pthread_cond_t add_cond_var_;
88 };
89 #endif
90