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

Commit

Permalink
Added methods for getting lists of peers and a current hostname
Browse files Browse the repository at this point in the history
  • Loading branch information
pidpawel committed Sep 30, 2020
1 parent 6f2abf9 commit 6239a48
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 4 deletions.
23 changes: 23 additions & 0 deletions libraries/Husarnet/src/Husarnet.cpp
Original file line number Diff line number Diff line change
@@ -1,14 +1,22 @@
#include "Husarnet.h"
#include <cassert>


_Husarnet Husarnet;

extern "C" {
void husarnet_start();
void husarnet_join(const char* joinCode, const char* hostname);
void husarnet_retrieve_license(const char* hostname);
const char* husarnet_get_hostname();
}

struct InternalIP6Address {
std::array<uint8_t, 16> data;
};

extern std::vector<std::pair<InternalIP6Address, std::string>> husarnet_hosts;

static bool alreadyStarted = false;

void _Husarnet::selfHostedSetup(const char* hostname) {
Expand All @@ -26,3 +34,18 @@ void _Husarnet::join(const char* joinCode, const char* hostname) {
assert(!alreadyStarted);
husarnet_join(joinCode, hostname);
}

typedef std::pair<IPv6Address, String> hostPair;
std::vector<hostPair> _Husarnet::listPeers() {
std::vector<hostPair> peers;

for(auto const& host: husarnet_hosts) {
peers.push_back(hostPair(IPv6Address(host.first.data.data()), String(host.second.c_str())));
}

return peers;
}

String _Husarnet::getHostname() {
return String(husarnet_get_hostname());
}
19 changes: 15 additions & 4 deletions libraries/Husarnet/src/Husarnet.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,28 @@

#include "Arduino.h"
#include "HusarnetServer.h"
//#include <freertos/task.h>
#include "IPv6Address.h"
#include "WString.h"
#include <vector>
#include <utility>
#include <string>

struct _Husarnet {
struct _Husarnet
{
// Sets up Husarnet to use self-hosted base server
void selfHostedSetup(const char* hostname);
void selfHostedSetup(const char *hostname);

// Starts the Husarnet
void start();

// Provides join code. Use before Husarnet.start().
void join(const char* joinCode, const char* hostname="");
void join(const char *joinCode, const char *hostname = "");

// Get list of peers' hostnames and addresses
std::vector<std::pair<IPv6Address, String>> listPeers();

// Get hostname you're currently known at
String getHostname();
};

extern _Husarnet Husarnet;
Expand Down

0 comments on commit 6239a48

Please sign in to comment.