1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
|
/*
*
* Copyright (c) 2006 The Apache Software Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
#include "Cpg.h"
#include "qpid/sys/Mutex.h"
#include "qpid/log/Statement.h"
#include <vector>
#include <limits>
#include <iterator>
namespace qpid {
namespace cluster {
using namespace std;
// Global vector of Cpg pointers by handle.
// TODO aconway 2007-06-12: Replace this with cpg_get/set_context,
// coming in in RHEL 5.1.
class Cpg::Handles
{
public:
void put(cpg_handle_t handle, Cpg::Handler* handler) {
sys::Mutex::ScopedLock l(lock);
uint32_t index=uint32_t(handle); // Lower 32 bits is an array index.
if (index >= handles.size())
handles.resize(index+1, 0);
handles[index] = handler;
}
Cpg::Handler* get(cpg_handle_t handle) {
sys::Mutex::ScopedLock l(lock);
uint32_t index=uint32_t(handle); // Lower 32 bits is an array index.
assert(index < handles.size());
assert(handles[index]);
return handles[index];
}
private:
sys::Mutex lock;
vector<Cpg::Handler*> handles;
};
Cpg::Handles Cpg::handles;
// Global callback functions call per-object callbacks via handles vector.
void Cpg::globalDeliver (
cpg_handle_t handle,
struct cpg_name *group,
uint32_t nodeid,
uint32_t pid,
void* msg,
int msg_len)
{
Cpg::Handler* handler=handles.get(handle);
if (handler)
handler->deliver(handle, group, nodeid, pid, msg, msg_len);
}
void Cpg::globalConfigChange(
cpg_handle_t handle,
struct cpg_name *group,
struct cpg_address *members, int nMembers,
struct cpg_address *left, int nLeft,
struct cpg_address *joined, int nJoined
)
{
Cpg::Handler* handler=handles.get(handle);
if (handler)
handler->configChange(handle, group, members, nMembers, left, nLeft, joined, nJoined);
}
Cpg::Cpg(Handler& h) : handler(h) {
cpg_callbacks_t callbacks = { &globalDeliver, &globalConfigChange };
check(cpg_initialize(&handle, &callbacks), "Cannot initialize CPG");
handles.put(handle, &handler);
QPID_LOG(debug, "Initialize CPG handle " << handle);
}
Cpg::~Cpg() {
try {
shutdown();
} catch (const std::exception& e) {
QPID_LOG(error, "Exception in Cpg destructor: " << e.what());
}
}
void Cpg::shutdown() {
QPID_LOG(debug, "Shutdown CPG handle " << handle);
if (handles.get(handle)) {
QPID_LOG(debug, "Finalize CPG handle " << handle);
handles.put(handle, 0);
check(cpg_finalize(handle), "Error in shutdown of CPG");
}
}
string Cpg::errorStr(cpg_error_t err, const std::string& msg) {
switch (err) {
case CPG_OK: return msg+": ok";
case CPG_ERR_LIBRARY: return msg+": library";
case CPG_ERR_TIMEOUT: return msg+": timeout";
case CPG_ERR_TRY_AGAIN: return msg+": timeout. The aisexec daemon may not be running";
case CPG_ERR_INVALID_PARAM: return msg+": invalid param";
case CPG_ERR_NO_MEMORY: return msg+": no memory";
case CPG_ERR_BAD_HANDLE: return msg+": bad handle";
case CPG_ERR_ACCESS: return msg+": access denied. You may need to set your group ID to 'ais'";
case CPG_ERR_NOT_EXIST: return msg+": not exist";
case CPG_ERR_EXIST: return msg+": exist";
case CPG_ERR_NOT_SUPPORTED: return msg+": not supported";
case CPG_ERR_SECURITY: return msg+": security";
case CPG_ERR_TOO_MANY_GROUPS: return msg+": too many groups";
default:
assert(0);
return ": unknown";
};
}
std::string Cpg::cantJoinMsg(const Name& group) {
return "Cannot join CPG group "+group.str();
}
std::string Cpg::cantLeaveMsg(const Name& group) {
return "Cannot leave CPG group "+group.str();
}
std::string Cpg::cantMcastMsg(const Name& group) {
return "Cannot mcast to CPG group "+group.str();
}
uint32_t Cpg::getLocalNoideId() const {
unsigned int nodeid;
check(cpg_local_get(handle, &nodeid), "Cannot get local node ID");
assert(nodeid <= std::numeric_limits<uint32_t>::max());
return nodeid;
}
ostream& operator<<(ostream& o, std::pair<cpg_address*,int> a) {
ostream_iterator<Cpg::Id> i(o, " ");
std::copy(a.first, a.first+a.second, i);
return o;
}
static int popbyte(uint32_t& n) {
uint8_t b=n&0xff;
n>>=8;
return b;
}
ostream& operator <<(ostream& out, const Cpg::Id& id) {
uint32_t node=id.nodeId();
out << popbyte(node);
for (int i = 0; i < 3; i++)
out << "." << popbyte(node);
return out << ":" << id.pid();
}
ostream& operator <<(ostream& out, const cpg_name& name) {
return out << string(name.value, name.length);
}
}} // namespace qpid::cluster
|