OSDN Git Service

ソースIP伝播UT追加(realaserver)
[ultramonkey-l7/ultramonkey-l7-v3.git] / l7vsd / include / realserver.h
1 /*
2  *    @file    l7vs_realserver.h
3  *    @brief    realserver data prototype
4  *
5  * L7VSD: Linux Virtual Server for Layer7 Load Balancing
6  * Copyright (C) 2009  NTT COMWARE Corporation.
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) 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 GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
21  * 02110-1301 USA
22  *
23  **********************************************************************/
24 #ifndef    REALSERVER_H
25 #define    REALSERVER_H
26
27 #include    <boost/thread.hpp>
28 #include    <boost/shared_ptr.hpp>
29 #include    "realserver_element.h"
30
31 namespace l7vs
32 {
33
34 class    realserver : public realserver_element
35 {
36 public:
37         typedef    boost::shared_ptr<boost::mutex>        mutex_ptr;
38 protected:
39         mutex_ptr            active_mutex_ptr;
40         mutex_ptr            inact_mutex_ptr;
41 public:
42         unsigned long long    send_byte;
43
44         realserver() : send_byte(0LL) {
45                 active_mutex_ptr = mutex_ptr(new boost::mutex);
46                 inact_mutex_ptr = mutex_ptr(new boost::mutex);
47         }
48         realserver(const realserver &in) : realserver_element(in),
49                 send_byte(in.send_byte) {
50                 active_mutex_ptr = mutex_ptr(new boost::mutex);
51                 inact_mutex_ptr = mutex_ptr(new boost::mutex);
52         }
53
54         realserver &operator=(const realserver &rs) {
55                 realserver_element::operator= (rs);
56                 send_byte = rs.send_byte;
57                 return *this;
58         }
59
60         ~realserver() {}
61
62         friend    bool    operator==(const realserver &rs1, const realserver &rs2) {
63                 realserver &rselem1 = const_cast<realserver &>(rs1);
64                 realserver &rselem2 = const_cast<realserver &>(rs2);
65                 return    rselem1.tcp_endpoint == rselem2.tcp_endpoint &&
66                           rselem1.udp_endpoint == rselem2.udp_endpoint &&
67                           rselem1.weight == rselem2.weight &&
68                           rselem1.fwdmode == rselem2.fwdmode;
69         }
70
71         friend    bool    operator!=(const realserver &rs1, const realserver &rs2) {
72                 realserver &rselem1 = const_cast<realserver &>(rs1);
73                 realserver &rselem2 = const_cast<realserver &>(rs2);
74                 return  rselem1.tcp_endpoint != rselem2.tcp_endpoint ||
75                         rselem1.udp_endpoint != rselem2.udp_endpoint ||
76                         rselem1.weight != rselem2.weight ||
77                         rselem1.fwdmode != rselem2.fwdmode;
78         }
79
80         friend    bool    operator<(const realserver &rs1, const realserver &rs2) {
81                 realserver &rselem1 = const_cast<realserver &>(rs1);
82                 realserver &rselem2 = const_cast<realserver &>(rs2);
83                 if (rselem1.tcp_endpoint < rselem2.tcp_endpoint) return true;
84                 if (rselem1.tcp_endpoint != rselem2.tcp_endpoint) return false;
85                 if (rselem1.weight < rselem2.weight) return true;
86                 if (rselem1.weight != rselem2.weight) return false;
87                 return rselem1.fwdmode < rselem2.fwdmode;
88         }
89
90         void    increment_active() {
91                 boost::mutex::scoped_lock lock(*active_mutex_ptr);
92
93                 nactive++;
94                 if (nactive == INT_MAX) {
95                         nactive = 0;
96                 }
97         }
98
99         void    decrement_active() {
100                 boost::mutex::scoped_lock lock(*active_mutex_ptr);
101
102                 if (nactive > 0) {
103                         nactive--;
104                 }
105         }
106
107         void    increment_inact() {
108                 boost::mutex::scoped_lock lock(*inact_mutex_ptr);
109
110                 ninact++;
111                 if (ninact == INT_MAX) {
112                         ninact = 0;
113                 }
114         }
115
116 protected:
117         // nullify function, only effective on realserver_element
118         void    set_active(const int in_active) {}
119         void    set_inact(const int in_inact) {}
120 };
121
122
123 }    //namespace l7vs
124 #endif    //REALSERVER_H