00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include "descriptions-manager.h"
00025 #include "msg.h"
00026
00027 using namespace std;
00028
00029 namespace Aseba
00030 {
00031 DescriptionsManager::NodeDescription::NodeDescription() :
00032 namedVariablesReceptionCounter(0),
00033 localEventsReceptionCounter(0),
00034 nativeFunctionReceptionCounter(0)
00035 {
00036 }
00037
00038 DescriptionsManager::NodeDescription::NodeDescription(const TargetDescription& targetDescription) :
00039 TargetDescription(targetDescription),
00040 namedVariablesReceptionCounter(0),
00041 localEventsReceptionCounter(0),
00042 nativeFunctionReceptionCounter(0)
00043 {
00044 }
00045
00046 void DescriptionsManager::processMessage(const Message* message)
00047 {
00048
00049 {
00050 const Description *description = dynamic_cast<const Description *>(message);
00051 if (description)
00052 {
00053 NodesDescriptionsMap::iterator it = nodesDescriptions.find(description->source);
00054
00055
00056 if (it != nodesDescriptions.end())
00057 return;
00058
00059
00060 if (description->protocolVersion != ASEBA_PROTOCOL_VERSION)
00061 {
00062 nodeProtocolVersionMismatch(description->name, description->protocolVersion);
00063 return;
00064 }
00065
00066
00067 nodesDescriptions[description->source] = NodeDescription(*description);
00068 checkIfNodeDescriptionComplete(description->source, nodesDescriptions[description->source]);
00069 }
00070 }
00071
00072
00073 {
00074 const NamedVariableDescription *description = dynamic_cast<const NamedVariableDescription *>(message);
00075 if (description)
00076 {
00077 NodesDescriptionsMap::iterator it = nodesDescriptions.find(description->source);
00078
00079
00080 if (it == nodesDescriptions.end())
00081 return;
00082
00083
00084 if (it->second.namedVariablesReceptionCounter < it->second.namedVariables.size())
00085 {
00086 it->second.namedVariables[it->second.namedVariablesReceptionCounter++] = *description;
00087 checkIfNodeDescriptionComplete(it->first, it->second);
00088 }
00089 }
00090 }
00091
00092
00093 {
00094 const LocalEventDescription *description = dynamic_cast<const LocalEventDescription *>(message);
00095 if (description)
00096 {
00097 NodesDescriptionsMap::iterator it = nodesDescriptions.find(description->source);
00098
00099
00100 if (it == nodesDescriptions.end())
00101 return;
00102
00103
00104 if (it->second.localEventsReceptionCounter < it->second.localEvents.size())
00105 {
00106 it->second.localEvents[it->second.localEventsReceptionCounter++] = *description;
00107 checkIfNodeDescriptionComplete(it->first, it->second);
00108 }
00109 }
00110 }
00111
00112
00113 {
00114 const NativeFunctionDescription *description = dynamic_cast<const NativeFunctionDescription *>(message);
00115 if (description)
00116 {
00117 NodesDescriptionsMap::iterator it = nodesDescriptions.find(description->source);
00118
00119
00120 if (it == nodesDescriptions.end())
00121 return;
00122
00123
00124 if (it->second.nativeFunctionReceptionCounter < it->second.nativeFunctions.size())
00125 {
00126 it->second.nativeFunctions[it->second.nativeFunctionReceptionCounter++] = *description;
00127 checkIfNodeDescriptionComplete(it->first, it->second);
00128 }
00129 }
00130 }
00131 }
00132
00133 void DescriptionsManager::checkIfNodeDescriptionComplete(unsigned id, const NodeDescription& description)
00134 {
00135
00136 if ((description.namedVariablesReceptionCounter == description.namedVariables.size()) &&
00137 (description.localEventsReceptionCounter == description.localEvents.size()) &&
00138 (description.nativeFunctionReceptionCounter == description.nativeFunctions.size())
00139 )
00140 nodeDescriptionReceived(id);
00141 }
00142
00143 std::string DescriptionsManager::getNodeName(unsigned nodeId) const
00144 {
00145 NodesDescriptionsMap::const_iterator it = nodesDescriptions.find(nodeId);
00146 if (it != nodesDescriptions.end())
00147 {
00148 return it->second.name;
00149 }
00150 else
00151 {
00152 return "";
00153 }
00154 }
00155
00156 unsigned DescriptionsManager::getNodeId(const std::string& name, bool *ok ) const
00157 {
00158
00159 for (NodesDescriptionsMap::const_iterator it = nodesDescriptions.begin(); it != nodesDescriptions.end(); ++it)
00160 {
00161 if (it->second.name == name)
00162 {
00163 if (ok)
00164 *ok = true;
00165 return it->first;
00166 }
00167 }
00168
00169
00170 if (ok)
00171 *ok = false;
00172 return 0xFFFFFFFF;
00173 }
00174
00175 const TargetDescription * const DescriptionsManager::getDescription(unsigned nodeId, bool *ok) const
00176 {
00177 NodesDescriptionsMap::const_iterator it = nodesDescriptions.find(nodeId);
00178
00179
00180 if (it == nodesDescriptions.end())
00181 {
00182 if (ok)
00183 *ok = false;
00184 return 0;
00185 }
00186
00187 if (ok)
00188 *ok = true;
00189 return &(it->second);
00190 }
00191
00192 unsigned DescriptionsManager::getVariablePos(unsigned nodeId, const std::string& name, bool *ok) const
00193 {
00194 NodesDescriptionsMap::const_iterator it = nodesDescriptions.find(nodeId);
00195
00196
00197 if (it != nodesDescriptions.end())
00198 {
00199 size_t pos = 0;
00200 for (size_t i = 0; i < it->second.namedVariables.size(); ++i)
00201 {
00202 if (it->second.namedVariables[i].name == name)
00203 {
00204 if (ok)
00205 *ok = true;
00206 return pos;
00207 }
00208 pos += it->second.namedVariables[i].size;
00209 }
00210 }
00211
00212
00213 if (ok)
00214 *ok = false;
00215 return 0xFFFFFFFF;
00216 }
00217
00218 unsigned DescriptionsManager::getVariableSize(unsigned nodeId, const std::string& name, bool *ok) const
00219 {
00220 NodesDescriptionsMap::const_iterator it = nodesDescriptions.find(nodeId);
00221
00222
00223 if (it != nodesDescriptions.end())
00224 {
00225 for (size_t i = 0; i < it->second.namedVariables.size(); ++i)
00226 {
00227 if (it->second.namedVariables[i].name == name)
00228 {
00229 if (ok)
00230 *ok = true;
00231 return it->second.namedVariables[i].size;
00232 }
00233 }
00234 }
00235
00236
00237 if (ok)
00238 *ok = false;
00239 return 0xFFFFFFFF;
00240 }
00241 }