My Project 2.4.4
C++ Distributed Hash Table
Loading...
Searching...
No Matches
peer_discovery.h
1/*
2 * Copyright (C) 2014-2022 Savoir-faire Linux Inc.
3 * Author(s) : Mingrui Zhang <mingrui.zhang@savoirfairelinux.com>
4 * Vsevolod Ivanov <vsevolod.ivanov@savoirfairelinux.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18 */
19
20#pragma once
21
22#include "def.h"
23#include "sockaddr.h"
24#include "infohash.h"
25#include "log_enable.h"
26
27#include <thread>
28
29namespace asio {
30class io_context;
31}
32
33namespace dht {
34
35class OPENDHT_PUBLIC PeerDiscovery
36{
37public:
38 static constexpr in_port_t DEFAULT_PORT = 8888;
39 using ServiceDiscoveredCallback = std::function<void(msgpack::object&&, SockAddr&&)>;
40
41 PeerDiscovery(in_port_t port = DEFAULT_PORT, std::shared_ptr<asio::io_context> ioContext = {}, std::shared_ptr<Logger> logger = {});
43
47 void startDiscovery(const std::string &type, ServiceDiscoveredCallback callback);
48
49 template<typename T>
50 void startDiscovery(const std::string &type, std::function<void(T&&, SockAddr&&)> cb) {
51 startDiscovery(type, [cb](msgpack::object&& ob, SockAddr&& addr) {
52 cb(ob.as<T>(), std::move(addr));
53 });
54 }
55
59 void startPublish(const std::string &type, const msgpack::sbuffer &pack_buf);
60 void startPublish(sa_family_t domain, const std::string &type, const msgpack::sbuffer &pack_buf);
61
62 template<typename T>
63 void startPublish(const std::string &type, const T& object) {
64 msgpack::sbuffer buf;
65 msgpack::pack(buf, object);
66 startPublish(type, buf);
67 }
68
72 void stop();
73
77 bool stopDiscovery(const std::string &type);
78
82 bool stopPublish(const std::string &type);
83 bool stopPublish(sa_family_t domain, const std::string &type);
84
85 void connectivityChanged();
86
87private:
88 class DomainPeerDiscovery;
89 std::unique_ptr<DomainPeerDiscovery> peerDiscovery4_;
90 std::unique_ptr<DomainPeerDiscovery> peerDiscovery6_;
91 std::shared_ptr<asio::io_context> ioContext_;
92 std::thread ioRunnner_;
93};
94
95}
bool stopDiscovery(const std::string &type)
bool stopPublish(const std::string &type)
void startDiscovery(const std::string &type, ServiceDiscoveredCallback callback)
void startPublish(const std::string &type, const msgpack::sbuffer &pack_buf)
Definition: callbacks.h:35