-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAlmMulticastPublisher.cpp
More file actions
165 lines (128 loc) · 5.34 KB
/
Copy pathAlmMulticastPublisher.cpp
File metadata and controls
165 lines (128 loc) · 5.34 KB
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
#include "MicroMiddleware/Net/AlmMulticastPublisher.h"
#include "MicroMiddleware/Net/AlmPublisher.h"
#include "MicroMiddleware/RmiObjectBase.h"
#include "MicroMiddleware/NetObjects/HostInformation.h"
namespace MicroMiddleware
{
map<ClientTriplet, AlmPublisher*>& AlmMulticastPublisher::GetRpcGroup(const string &groupName)
{
MutexLocker lock(&clientMutex_);
ASSERT(mapGroupNameAlmPublisher_.count(groupName) > 0);
MapClientTripletAlmPublisher &mapAlmPublisher = mapGroupNameAlmPublisher_[groupName];
return mapAlmPublisher;
}
void AlmMulticastPublisher::ExecuteRpc(RpcIdentifier *rpc, RmiObjectBase *rpcObject, const string &groupName)
{
MutexLocker lock(&clientMutex_);
ASSERT(rpcObject != NULL);
ASSERT(!groupName.empty());
ASSERT(mapGroupNameAlmPublisher_.count(groupName) > 0);
//char* buf = rpcObject->toRaw();
//int length = rpcObject->GetLength();
MapClientTripletAlmPublisher &mapAlmPublisher = mapGroupNameAlmPublisher_[groupName];
for(MapClientTripletAlmPublisher::iterator it = mapAlmPublisher.begin(), it_end = mapAlmPublisher.end(); it != it_end; ++it)
{
AlmPublisher *almPublisher = it->second;
ASSERT(almPublisher);
if(!almPublisher->IsConnected())
{
IWARNING() << *rpc << " client on " << almPublisher->GetInterfaceHandle() << " is disconnected " ;
continue;
}
IDEBUG() << *rpc << " sending to almPublisher " << almPublisher->GetInterfaceHandle() ;
almPublisher->ExecuteRpc(rpc, rpcObject);
}
}
void AlmMulticastPublisher::AddOrRemoveMulticastClients(GroupInformation &groupInfo)
{
MutexLocker lock(&clientMutex_);
IWARNING() << " Adding clients from group name: " << groupInfo.GetGroupName() ;
if(mapGroupNameAlmPublisher_.count(groupInfo.GetGroupName()) <= 0)
mapGroupNameAlmPublisher_.insert(pair<string, MapClientTripletAlmPublisher>(groupInfo.GetGroupName(), MapClientTripletAlmPublisher()));
MapClientTripletAlmPublisher &mapAlmPublisher = mapGroupNameAlmPublisher_[groupInfo.GetGroupName()];
for(GroupInformation::MapHostInformation::iterator it = groupInfo.GetGroupMembers().begin(), it_end = groupInfo.GetGroupMembers().end(); it != it_end; ++it)
{
const HostInformation &hostInfo = it->second;
IWARNING() << hostInfo ;
// socket, port, host
ClientTriplet clientTriplet(0, hostInfo.GetPortNumber(), hostInfo.GetHostName());
if(hostInfo.GetOnlineStatus() == HostInformation::OFFLINE) // -> remove
{
if(mapAlmPublisher.count(clientTriplet) > 0)
{
AlmPublisher *almPublisher = mapAlmPublisher[clientTriplet];
ASSERT(almPublisher != NULL);
almPublisher->StopClient();
delete almPublisher;
mapAlmPublisher.erase(clientTriplet);
}
else
{
IWARNING() << "Already removed " << hostInfo ;
}
}
else // ONLINE -> add
{
if(mapAlmPublisher.count(clientTriplet) <= 0)
{
InterfaceHandle handle(hostInfo.GetHostName(), hostInfo.GetPortNumber());
AlmPublisher* almPublisher = new AlmPublisher(handle);
almPublisher->StartClient();
almPublisher->WaitForUdpSocket();
IWARNING() << " Adding as target " << multicastServerInterface_ ;
GroupHandle groupServerHandle(groupInfo.GetGroupName(), multicastServerInterface_);
almPublisher->AddTarget(groupServerHandle);
mapAlmPublisher.insert(pair<ClientTriplet, AlmPublisher*>(clientTriplet, almPublisher));
}
else
{
IWARNING() << " " << groupInfo.GetGroupName() << " Already a target " << multicastServerInterface_;
}
}
}
}
void AlmMulticastPublisher::AddMulticastClient(const GroupHandle &groupHandle)
{
MutexLocker lock(&clientMutex_);
ASSERT(!groupHandle.GetGroupName().empty());
if(mapGroupNameAlmPublisher_.count(groupHandle.GetGroupName()) <= 0)
mapGroupNameAlmPublisher_.insert(pair<string, MapClientTripletAlmPublisher>(groupHandle.GetGroupName(), MapClientTripletAlmPublisher()));
MapClientTripletAlmPublisher &mapAlmPublisher = mapGroupNameAlmPublisher_[groupHandle.GetGroupName()];
ClientTriplet clientTriplet(0, groupHandle.GetPortNumber(),groupHandle.GetHostName());
if(mapAlmPublisher.count(clientTriplet) <= 0)
{
IWARNING() << " Adding client " << groupHandle ;
AlmPublisher* almPublisher = new AlmPublisher(
InterfaceHandle(groupHandle.GetHostName(), groupHandle.GetPortNumber()));
almPublisher->StartClient();
almPublisher->WaitConnected();
ASSERT(almPublisher->IsConnected());
mapAlmPublisher.insert(pair<ClientTriplet, AlmPublisher*>(clientTriplet, almPublisher));
}
else
{
IWARNING() << "WARNING! Already a target " << groupHandle ;
}
}
void AlmMulticastPublisher::RemoveMulticastClient(const GroupHandle &groupHandle)
{
MutexLocker lock(&clientMutex_);
ASSERT(!groupHandle.GetGroupName().empty());
if(mapGroupNameAlmPublisher_.count(groupHandle.GetGroupName()) <= 0)
mapGroupNameAlmPublisher_.insert(pair<string, MapClientTripletAlmPublisher>(groupHandle.GetGroupName(), MapClientTripletAlmPublisher()));
MapClientTripletAlmPublisher &mapAlmPublisher = mapGroupNameAlmPublisher_[groupHandle.GetGroupName()];
ClientTriplet clientTriplet(0, groupHandle.GetPortNumber(), groupHandle.GetHostName());
if(mapAlmPublisher.count(clientTriplet) > 0)
{
AlmPublisher *almPublisher = mapAlmPublisher[clientTriplet];
ASSERT(almPublisher != NULL);
almPublisher->StopClient();
delete almPublisher;
mapAlmPublisher.erase(clientTriplet);
}
else
{
IWARNING() << "WARNING! Already removed " << groupHandle ;
}
}
}; // namespace end