From 3a5f81426395f1468e6d88e98da984a3fc2c2e78 Mon Sep 17 00:00:00 2001 From: dec05eba Date: Sun, 30 Dec 2018 15:23:59 +0100 Subject: Use config file for boostrap nodes, currently set to localhost for debug --- src/Window.cpp | 51 +++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 47 insertions(+), 4 deletions(-) (limited to 'src/Window.cpp') diff --git a/src/Window.cpp b/src/Window.cpp index 4c4aab6..5e4930f 100644 --- a/src/Window.cpp +++ b/src/Window.cpp @@ -10,6 +10,37 @@ namespace dchat const int nodesPerColumn = 10; const int nodesPerRow = 10; + static std::vector getBootstrapNodesFromFile() + { + std::vector result; + auto boostrapNodesConfigFile = Gio::File::create_for_path("bootstrap_nodes"); + auto bootstrapNodesFileContent = boostrapNodesConfigFile->read()->read_bytes(1024 * 10, Glib::RefPtr()); + if(!bootstrapNodesFileContent) + throw std::runtime_error("Failed to read bootstrap_nodes file"); + + gsize start = 0; + + gsize size = bootstrapNodesFileContent->get_size(); + const unsigned char *data = (const unsigned char*)bootstrapNodesFileContent->get_data(size); + for(gsize i = 0; i < size; ++i) + { + unsigned char c = data[i]; + if(c == ' ' || c == '\n' || c == '\r' || c == '\t') + { + gsize length = i - start; + if(length > 0) + result.push_back(std::string(&data[start], &data[start + length])); + start = i + 1; + } + } + + gsize length = size - start; + if(length > 0) + result.push_back(std::string(&data[start], &data[start + length])); + + return result; + } + Window::Window() : chatWindow(this) { @@ -91,15 +122,28 @@ namespace dchat windowNotification->show("Passwords do not match"); }); + auto bootstrapNodes = getBootstrapNodesFromFile(); + if(bootstrapNodes.empty()) + throw std::runtime_error("No boostrap nodes in boostrap_nodes file"); + + std::string msg = "Connecting to first boostrap node: "; + msg += bootstrapNodes[0]; + msg += ":27130"; + windowNotification->show(msg); + std::string *bootstrapNode = new std::string(bootstrapNodes[0]); + RoomCallbackFuncs roomCallbackFuncs; - roomCallbackFuncs.connectCallbackFunc = [this](std::shared_ptr rooms, const char *errMsg) + roomCallbackFuncs.connectCallbackFunc = [this, bootstrapNode](std::shared_ptr rooms, const char *errMsg) { this->rooms = rooms; if(rooms) { loginWindow.show(); stack.set_visible_child(loginWindow); - windowNotification->show("Connected to 83.252.53.188:27130"); + std::string msg = "Connected to "; + msg += *bootstrapNode; + msg += ":27130"; + windowNotification->show(msg); loginWindow.loginUsernameInput.grab_focus(); } else @@ -138,8 +182,7 @@ namespace dchat chatWindow.addInviteRequest(request); }; - windowNotification->show("Connecting to 83.252.53.188:27130"); - Rooms::connect("83.252.53.188", 27130, roomCallbackFuncs); + Rooms::connect(bootstrapNode->c_str(), 27130, roomCallbackFuncs); backgroundRng.seed(std::random_device()()); std::uniform_int_distribution sizeDeviationRand(0, 5); -- cgit v1.2.3