File: targetadvertise.cpp

package info (click to toggle)
aseba 1.6.0-5
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 18,300 kB
  • sloc: cpp: 44,647; ansic: 5,686; python: 1,455; java: 1,136; sh: 393; xml: 202; makefile: 10
file content (208 lines) | stat: -rw-r--r-- 5,425 bytes parent folder | download
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
/*
	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 <cstring>
#include <dashel/dashel.h>
#include "../../common/productids.h"
#include "../../common/consts.h"
#include "../../common/msg/msg.h"
#include "../../common/msg/NodesManager.h"
#include "../../common/utils/utils.h"
#include "../../common/zeroconf/zeroconf-dashelhub.h"

namespace Aseba
{
	using namespace Dashel;
	using namespace std;

	//! StreamNodesManager encapsulates a Dashel::Stream with its own NodesManager, to avoid node id conflicts.
	class StreamNodesManager : NodesManager
	{
	private:
		Stream* stream; //!< encapsulated stream
		map<unsigned, pair<unsigned,unsigned>> pidMap; //!< remember each node's _productId pos and value

	public:
		StreamNodesManager(Stream* stream) :
			stream(stream)
		{}

		void advertiseNodes(Aseba::Zeroconf & zeroconf)
		{
			vector<unsigned int> ids;
			vector<unsigned int> pids;
			vector<string> names;
			unsigned protocolVersion{99};
			for (auto const & node: nodes)
			{
				bool ok;
				auto description = getDescription(node.first, &ok);
				if (ok)
				{
					ids.push_back(node.first);
					names.push_back(WStringToUTF8(description->name));
					protocolVersion = min(protocolVersion, description->protocolVersion);
					if (pidMap.find(node.first) != pidMap.end())
						pids.push_back(pidMap.at(node.first).second);
				}
			}

			Aseba::Zeroconf::TxtRecord txt{protocolVersion, join(names," "), false, ids, pids};
			try
			{
				zeroconf.advertise("Aseba Local " + stream->getTargetParameter("port"), stream, txt);
			}
			catch (const runtime_error& e)
			{
				cerr << "Can't advertise stream " << stream->getTargetName() << " (" << join(names," ") << "): " << e.what() << endl;
			}
		}

		void requestProductIds()
		{
			for (auto const & node: nodes)
			{
				bool ok;
				auto pos = getVariablePos(node.first, L"_productId", &ok);
				if (ok)
				{
					pidMap[node.first] = make_pair(pos,0);
					GetVariables getVariables(node.first,pos,1);
					getVariables.serialize(stream);
					stream->flush();
				}
			}
		}

		void incomingVariable(const Variables * message)
		{
			auto nodeId = message->source;
			if (pidMap.find(nodeId) != pidMap.end() && pidMap.at(nodeId).first == message->start)
				pidMap.at(nodeId).second = message->variables[0];
		}

		void sendMessage(const Message& message)
		{
			message.serialize(stream);
			stream->flush();
		}

		void processMessage(const Message* message)
		{
			NodesManager::processMessage(message);
		}
	};

	//! Dashel::Hub::Advertiser collects node descriptions from a set of tcp Dashel streams
	//! and advertises them on the local network using Zeroconf.
	class Advertiser: public Hub
	{
	protected:
		map<Stream*,StreamNodesManager> streamMap;
		Aseba::DashelhubZeroconf zeroconf;

	protected:
		virtual void incomingData(Stream *stream) override
		{
			Message *message(Message::receive(stream));
			streamMap.at(stream).processMessage(message);
			const Variables *variables(dynamic_cast<Variables *>(message));
			if (variables)
				streamMap.at(stream).incomingVariable(variables);
		}

		virtual void connectionClosed(Stream *stream, bool abnormal) override
		{
			zeroconf.forget("Aseba Local " + stream->getTargetParameter("port"), stream);
		}

	public:
		Advertiser(const vector<string> & dashel_targets):
			zeroconf(*this)
		{
			for (auto const & dashel: dashel_targets)
			{
				try
				{
					if (Dashel::Stream* cxn = connect(dashel))
						streamMap.emplace(cxn,StreamNodesManager(cxn));
				}
				catch(Dashel::DashelException e)
				{
					cerr << "Can't connect target " << dashel << ": " << e.what() << endl;
				}
			}

			ListNodes listNodes;
			for (auto const & stream: streamMap)
			{
				listNodes.serialize(stream.first);
				stream.first->flush();
			}
			run5s(); // wait for descriptions
		}

		void advertiseNodes()
		{
			for (auto & stream: streamMap)
				stream.second.advertiseNodes(zeroconf);
		}

		void requestProductIds()
		{
			for (auto & stream: streamMap)
				stream.second.requestProductIds();
			run5s(); // wait for product id values
		}

		void run5s()
		{
			int timeout{5000};
			UnifiedTime startTime;
			while (timeout > 0)
			{
				if (!step(100))
					break;
				const UnifiedTime now;
				timeout -= (now - startTime).value;
				startTime = now;
			}
		}
	};
}

int main(int argc, char* argv[])
{
	std::vector<std::string> dashel_targets;

	int argCounter = 1;
	while (argCounter < argc)
	{
		auto arg = argv[argCounter++];
		if (strncmp(arg, "-", 1) != 0)
			dashel_targets.push_back(arg);
	}

	Aseba::Advertiser hub(dashel_targets);
	hub.requestProductIds();
	hub.advertiseNodes();

	hub.run();
}