File: targetlist-thread.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 (67 lines) | stat: -rw-r--r-- 2,177 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
/*
	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 <iostream>
#include <functional>
#include <unordered_set>
#include "../../common/utils/utils.h"
#include "../../common/zeroconf/zeroconf-thread.h"

namespace Aseba
{
	using namespace std;

	class TargetLister : public ThreadZeroconf
	{
		virtual void targetFound(const Aseba::Zeroconf::TargetInformation & target) override
		{
			// output could be JSON but for now is Dashel target [Target name (DNS domain)]
			cout << target.host << ";port=" << target.port;
			cout << " [" << target.name << " (" << target.regtype + "." + target.domain << ")]" << endl;
			// also output properties, typically the DNS-encoded full host name and fields from TXT record
			for (auto const& field: target.properties)
			{
				cout << "\t" << field.first << ":";
				// ids and pids are a special case because they contain vectors of 16-bit integers
				if (field.first.rfind("ids") == field.first.size()-3)
					for (size_t i = 0; i < field.second.length(); i += 2)
						cout << " " << (int(field.second[i])<<8) + int(field.second[i+1]);
				else
					cout << " " << field.second;
				cout << endl;
			}
		}

	public:
		void run()
		{
			// run 10 seconds
			UnifiedTime(10, 0).sleep();
		}
	};
}

int main(int argc, char* argv[])
{
	// Browse for _aseba._tcp services on all interfaces
	Aseba::TargetLister lister;
	lister.browse();
	lister.run();
}