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 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325
|
/*
Aseba - an event-based framework for distributed robot control
Copyright (C) 2007--2016:
Stephane Magnenat <stephane at magnenat dot net>
(http://stephane.magnenat.net)
and other contributors, see authors.txt for details
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published
by the Free Software Foundation, version 3 of the License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "NodesManager.h"
#include "msg.h"
#include <iostream>
using namespace std;
namespace Aseba
{
NodesManager::Node::Node(const TargetDescription& targetDescription) :
TargetDescription(targetDescription),
namedVariablesReceptionCounter(0),
localEventsReceptionCounter(0),
nativeFunctionReceptionCounter(0)
{
}
bool NodesManager::Node::isComplete() const
{
return (namedVariablesReceptionCounter == namedVariables.size()) &&
(localEventsReceptionCounter == localEvents.size()) &&
(nativeFunctionReceptionCounter == nativeFunctions.size());
}
void NodesManager::pingNetwork()
{
// check whether there are new nodes in the network, for new targets (protocol >= 5)
ListNodes listNodes;
sendMessage(listNodes);
// check nodes that have not been seen for long, mark them as disconnected
const UnifiedTime now;
const UnifiedTime delayToDisconnect(3000);
bool isAnyConnected(false);
for (auto & node : nodes)
{
// if node supports listing,
if (node.second.protocolVersion >= 5 && (now - node.second.lastSeen) > delayToDisconnect && node.second.connected)
{
node.second.connected = false;
nodeDisconnected(node.first);
}
// is this node connected?
isAnyConnected = isAnyConnected || node.second.connected;
}
// if no node is connected, broadcast get description as well, for old targets (protocol 4)
if (!isAnyConnected)
{
GetDescription getDescription;
sendMessage(getDescription);
}
}
void NodesManager::processMessage(const Message* message)
{
// check whether the node is known
auto nodeIt(nodes.find(message->source));
if (nodeIt == nodes.end())
{
// node is not known, so ignore excepted if the message type
// is node present and it is not a known mismatch protocol,
// in that case, request description...
if ((message->type == ASEBA_MESSAGE_NODE_PRESENT) &&
mismatchingNodes.find(message->source) == mismatchingNodes.end())
{
GetNodeDescription getNodeDescription(message->source);
sendMessage(getNodeDescription);
}
// or if message type is description, in that case, proceed further
if (message->type != ASEBA_MESSAGE_DESCRIPTION)
return;
}
else
{
// node is known, check if connected...
if (!nodeIt->second.connected)
{
// if not, build complete, set as connected and notify client
nodeIt->second.connected = true;
if (nodeIt->second.isComplete())
{
// only notify connections of completed known nodes
nodeConnected(nodeIt->first);
}
}
// update last seen time
nodeIt->second.lastSeen = UnifiedTime();
}
// if we have a disconnection message
{
// FIXME: handle disconnected state
const auto *disconnected = dynamic_cast<const Disconnected *>(message);
if (disconnected)
{
auto nodeIt = nodes.find(disconnected->source);
assert (nodeIt != nodes.end());
nodes.erase(nodeIt);
}
}
// if we have an initial description
{
const auto *description = dynamic_cast<const Description *>(message);
if (description)
{
auto nodeIt = nodes.find(description->source);
// We can receive a description twice, for instance if there is another IDE connected
if (nodeIt != nodes.end() || (mismatchingNodes.find(description->source) != mismatchingNodes.end()))
return;
// Call a user function when a node protocol version mismatches
if ((description->protocolVersion < ASEBA_MIN_TARGET_PROTOCOL_VERSION) ||
(description->protocolVersion > ASEBA_PROTOCOL_VERSION))
{
nodeProtocolVersionMismatch(description->source, description->name, description->protocolVersion);
mismatchingNodes.insert(description->source);
return;
}
// create node and copy description into it
nodes[description->source] = Node(*description);
checkIfNodeDescriptionComplete(description->source, nodes[description->source]);
}
}
// if we have a named variable description
{
const auto *description = dynamic_cast<const NamedVariableDescription *>(message);
if (description)
{
auto nodeIt = nodes.find(description->source);
assert (nodeIt != nodes.end());
// copy description into array if array is empty
if (nodeIt->second.namedVariablesReceptionCounter < nodeIt->second.namedVariables.size())
{
nodeIt->second.namedVariables[nodeIt->second.namedVariablesReceptionCounter++] = *description;
checkIfNodeDescriptionComplete(nodeIt->first, nodeIt->second);
}
}
}
// if we have a local event description
{
const auto *description = dynamic_cast<const LocalEventDescription *>(message);
if (description)
{
auto nodeIt = nodes.find(description->source);
assert (nodeIt != nodes.end());
// copy description into array if array is empty
if (nodeIt->second.localEventsReceptionCounter < nodeIt->second.localEvents.size())
{
nodeIt->second.localEvents[nodeIt->second.localEventsReceptionCounter++] = *description;
checkIfNodeDescriptionComplete(nodeIt->first, nodeIt->second);
}
}
}
// if we have a native function description
{
const auto *description = dynamic_cast<const NativeFunctionDescription *>(message);
if (description)
{
auto nodeIt = nodes.find(description->source);
assert (nodeIt != nodes.end());
// copy description into array
if (nodeIt->second.nativeFunctionReceptionCounter < nodeIt->second.nativeFunctions.size())
{
nodeIt->second.nativeFunctions[nodeIt->second.nativeFunctionReceptionCounter++] = *description;
checkIfNodeDescriptionComplete(nodeIt->first, nodeIt->second);
}
}
}
}
void NodesManager::checkIfNodeDescriptionComplete(unsigned id, const Node& description)
{
// we will call the virtual function only when we have received all local events and native functions
if (description.isComplete() && description.connected)
{
nodeDescriptionReceived(id);
nodeConnected(id);
}
}
std::wstring NodesManager::getNodeName(unsigned nodeId) const
{
auto nodeIt = nodes.find(nodeId);
if (nodeIt != nodes.end())
{
return nodeIt->second.name;
}
else
{
return L"";
}
}
unsigned NodesManager::getNodeId(const std::wstring& name, unsigned preferedId, bool *ok) const
{
// search for the first node with a given name
bool found(false);
unsigned foundId(0);
for (const auto & node : nodes)
{
if (node.second.name == name)
{
if (ok)
*ok = true;
if (node.first == preferedId)
return node.first;
else if (!found)
foundId = node.first;
found = true;
}
}
// node found, but with another id than prefered
if (found)
return foundId;
// node not found
if (ok)
*ok = false;
return 0xFFFFFFFF;
}
const TargetDescription * NodesManager::getDescription(unsigned nodeId, bool *ok) const
{
auto nodeIt = nodes.find(nodeId);
// node not found
if (nodeIt == nodes.end())
{
if (ok)
*ok = false;
return nullptr;
}
if (ok)
*ok = true;
return &(nodeIt->second);
}
unsigned NodesManager::getVariablePos(unsigned nodeId, const std::wstring& name, bool *ok) const
{
auto nodeIt = nodes.find(nodeId);
// node not found
if (nodeIt != nodes.end())
{
size_t pos = 0;
for (const auto & namedVariable : nodeIt->second.namedVariables)
{
if (namedVariable.name == name)
{
if (ok)
*ok = true;
return pos;
}
pos += namedVariable.size;
}
}
// node not found or variable not found
if (ok)
*ok = false;
return 0xFFFFFFFF;
}
unsigned NodesManager::getVariableSize(unsigned nodeId, const std::wstring& name, bool *ok) const
{
auto nodeIt = nodes.find(nodeId);
// node not found
if (nodeIt != nodes.end())
{
for (const auto & namedVariable : nodeIt->second.namedVariables)
{
if (namedVariable.name == name)
{
if (ok)
*ok = true;
return namedVariable.size;
}
}
}
// node not found or variable not found
if (ok)
*ok = false;
return 0xFFFFFFFF;
}
void NodesManager::reset()
{
nodes.clear();
}
} // namespace Aseba
|