Skip to content
This repository was archived by the owner on Jul 9, 2024. It is now read-only.

Commit 6239a48

Browse files
committed
Added methods for getting lists of peers and a current hostname
1 parent 6f2abf9 commit 6239a48

File tree

2 files changed

+38
-4
lines changed

2 files changed

+38
-4
lines changed

libraries/Husarnet/src/Husarnet.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,22 @@
11
#include "Husarnet.h"
22
#include <cassert>
33

4+
45
_Husarnet Husarnet;
56

67
extern "C" {
78
void husarnet_start();
89
void husarnet_join(const char* joinCode, const char* hostname);
910
void husarnet_retrieve_license(const char* hostname);
11+
const char* husarnet_get_hostname();
1012
}
1113

14+
struct InternalIP6Address {
15+
std::array<uint8_t, 16> data;
16+
};
17+
18+
extern std::vector<std::pair<InternalIP6Address, std::string>> husarnet_hosts;
19+
1220
static bool alreadyStarted = false;
1321

1422
void _Husarnet::selfHostedSetup(const char* hostname) {
@@ -26,3 +34,18 @@ void _Husarnet::join(const char* joinCode, const char* hostname) {
2634
assert(!alreadyStarted);
2735
husarnet_join(joinCode, hostname);
2836
}
37+
38+
typedef std::pair<IPv6Address, String> hostPair;
39+
std::vector<hostPair> _Husarnet::listPeers() {
40+
std::vector<hostPair> peers;
41+
42+
for(auto const& host: husarnet_hosts) {
43+
peers.push_back(hostPair(IPv6Address(host.first.data.data()), String(host.second.c_str())));
44+
}
45+
46+
return peers;
47+
}
48+
49+
String _Husarnet::getHostname() {
50+
return String(husarnet_get_hostname());
51+
}

libraries/Husarnet/src/Husarnet.h

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,28 @@
33

44
#include "Arduino.h"
55
#include "HusarnetServer.h"
6-
//#include <freertos/task.h>
6+
#include "IPv6Address.h"
7+
#include "WString.h"
8+
#include <vector>
9+
#include <utility>
10+
#include <string>
711

8-
struct _Husarnet {
12+
struct _Husarnet
13+
{
914
// Sets up Husarnet to use self-hosted base server
10-
void selfHostedSetup(const char* hostname);
15+
void selfHostedSetup(const char *hostname);
1116

1217
// Starts the Husarnet
1318
void start();
1419

1520
// Provides join code. Use before Husarnet.start().
16-
void join(const char* joinCode, const char* hostname="");
21+
void join(const char *joinCode, const char *hostname = "");
22+
23+
// Get list of peers' hostnames and addresses
24+
std::vector<std::pair<IPv6Address, String>> listPeers();
25+
26+
// Get hostname you're currently known at
27+
String getHostname();
1728
};
1829

1930
extern _Husarnet Husarnet;

0 commit comments

Comments
 (0)