From 45b5ea30ac9a5048c8e8c2532e272861c329a730 Mon Sep 17 00:00:00 2001 From: Thulinma Date: Fri, 19 Jun 2020 13:12:31 +0200 Subject: [PATCH] Added location storage and handling in Controller and Load Balancer --- src/controller/controller_api.cpp | 5 ++++ src/controller/controller_statistics.cpp | 7 ++++++ src/utils/util_load.cpp | 30 ++++-------------------- 3 files changed, 17 insertions(+), 25 deletions(-) diff --git a/src/controller/controller_api.cpp b/src/controller/controller_api.cpp index d0121412..c8508db9 100644 --- a/src/controller/controller_api.cpp +++ b/src/controller/controller_api.cpp @@ -551,6 +551,11 @@ void Controller::handleAPICommands(JSON::Value &Request, JSON::Value &Response){ Controller::prometheus = out["prometheus"].asStringRef(); } if (in.isMember("defaultStream")){out["defaultStream"] = in["defaultStream"];} + if (in.isMember("location") && in["location"].isObject()){ + out["location"]["lat"] = in["location"]["lat"].asDouble(); + out["location"]["lon"] = in["location"]["lon"].asDouble(); + out["location"]["name"] = in["location"]["name"].asStringRef(); + } } if (Request.isMember("bandwidth")){ if (Request["bandwidth"].isObject()){ diff --git a/src/controller/controller_statistics.cpp b/src/controller/controller_statistics.cpp index 78938487..6b7488a8 100644 --- a/src/controller/controller_statistics.cpp +++ b/src/controller/controller_statistics.cpp @@ -1729,6 +1729,13 @@ void Controller::handlePrometheus(HTTP::Parser &H, Socket::Connection &conn, int resp["bw"].append(servUpBytes); resp["bw"].append(servDownBytes); resp["bwlimit"] = bwLimit; + if (Storage["config"].isMember("location") && Storage["config"]["location"].isMember("lat") && Storage["config"]["location"].isMember("lon")){ + resp["loc"]["lat"] = Storage["config"]["location"]["lat"].asDouble(); + resp["loc"]["lon"] = Storage["config"]["location"]["lon"].asDouble(); + if (Storage["config"]["location"].isMember("name")){ + resp["loc"]["name"] = Storage["config"]["location"]["name"].asStringRef(); + } + } resp["obw"].append(servUpOtherBytes); resp["obw"].append(servDownOtherBytes); diff --git a/src/utils/util_load.cpp b/src/utils/util_load.cpp index 5c6580c1..9e9bb2a5 100644 --- a/src/utils/util_load.cpp +++ b/src/utils/util_load.cpp @@ -289,6 +289,11 @@ public: tthread::lock_guard guard(*hostMutex); cpu = d["cpu"].asInt(); if (d.isMember("bwlimit") && d["bwlimit"].asInt()){availBandwidth = d["bwlimit"].asInt();} + if (d.isMember("loc")){ + if (d["loc"]["lat"].asDouble() != servLati){servLati = d["loc"]["lat"].asDouble();} + if (d["loc"]["lon"].asDouble() != servLongi){servLongi = d["loc"]["lon"].asDouble();} + if (d["loc"]["name"].asStringRef() != servLoc){servLoc = d["loc"]["name"].asStringRef();} + } int64_t nRamMax = d["mem_total"].asInt(); int64_t nRamCur = d["mem_used"].asInt(); int64_t nShmMax = d["shm_total"].asInt(); @@ -665,31 +670,6 @@ void handleServer(void *hostEntryPointer){ HTTP::Downloader DL; - if (DL.get(HTTP::URL("http://api.ipstack.com/" + url.host + "?access_key=05eb21db3983dec4cd6d22131ec0a40d&format=1")) && - DL.isOk()){ - JSON::Value &gDet = entry->details->geoDetails; - INFO_MSG("Location: %s", DL.data().c_str()); - gDet = JSON::fromString(DL.data()); - INFO_MSG("Location: %s", gDet.toString().c_str()); - if (gDet && gDet.isMember("latitude") && gDet.isMember("longitude")){ - entry->details->servLati = gDet["latitude"].asDouble(); - entry->details->servLongi = gDet["longitude"].asDouble(); - std::stringstream loc; - if (gDet.isMember("country_name")){ - if (gDet.isMember("city") && gDet["city"].asString().size()){ - entry->details->servLoc = gDet["city"].asString() + ", " + gDet["country_name"].asString(); - }else{ - entry->details->servLoc = gDet["country_name"].asString(); - } - INFO_MSG("%s is in %s", url.host.c_str(), entry->details->servLoc.c_str()); - }else{ - INFO_MSG("%s is at %f, %f", url.host.c_str(), entry->details->servLati, entry->details->servLongi); - } - } - }else{ - WARN_MSG("Could not reach location server for %s", url.host.c_str()); - } - while (cfg->is_active && (entry->state != STATE_GODOWN)){ if (DL.get(url) && DL.isOk()){ JSON::Value servData = JSON::fromString(DL.data());