提交 1eedf992 authored 作者: blu's avatar blu

feature: report edge box ip addresses

上级 e64ec7c5
...@@ -481,10 +481,13 @@ private: ...@@ -481,10 +481,13 @@ private:
// message to evcloudsvc // message to evcloudsvc
// spdlog::info("evcloudsvc {} subsystem report msg received: {}; {}; {}", devSn, zmqhelper::body2str(body[0]), zmqhelper::body2str(body[1]), zmqhelper::body2str(body[2])); // spdlog::info("evcloudsvc {} subsystem report msg received: {}; {}; {}", devSn, zmqhelper::body2str(body[0]), zmqhelper::body2str(body[1]), zmqhelper::body2str(body[2]));
if(meta == "pong"||meta == "ping") { if(meta == "pong"||meta == "ping") {
// update status
spdlog::info("evcloudsvc {}, ping msg from {}", devSn, selfId);
// handleConnection(selfId); // handleConnection(selfId);
if(meta=="ping") { if(meta=="ping") {
auto ips = body2str(body[3]);
spdlog::info("evcloudsvc {}, ping msg from {}: {}", devSn, selfId, ips);
this->peerData["info"]["ips"][selfId] = ips;
if(cachedMsg.find(selfId) != cachedMsg.end()) { if(cachedMsg.find(selfId) != cachedMsg.end()) {
while(!cachedMsg[selfId].empty()) { while(!cachedMsg[selfId].empty()) {
lock_guard<mutex> lock(cacheLock); lock_guard<mutex> lock(cacheLock);
...@@ -1109,6 +1112,11 @@ public: ...@@ -1109,6 +1112,11 @@ public:
EvCloudSvc() EvCloudSvc()
{ {
int ret = 0; int ret = 0;
this->peerData["info"] = json();
this->peerData["info"]["ips"] = json();
this->peerData["config"] = json();
this->peerData["status"] = json();
spdlog::info("evcloudsvc boot"); spdlog::info("evcloudsvc boot");
loadConfigMap(); loadConfigMap();
char *strEnv = getenv("HTTP_PORT"); char *strEnv = getenv("HTTP_PORT");
......
...@@ -18,6 +18,7 @@ update: 2019/09/10 ...@@ -18,6 +18,7 @@ update: 2019/09/10
#include "inc/database.h" #include "inc/database.h"
#include "inc/json.hpp" #include "inc/json.hpp"
#include "inc/utils.hpp" #include "inc/utils.hpp"
#include "networks.hpp"
#include <unistd.h> #include <unistd.h>
#include <sys/wait.h> #include <sys/wait.h>
...@@ -43,7 +44,6 @@ private: ...@@ -43,7 +44,6 @@ private:
thread::id thIdMain; thread::id thIdMain;
thread thRouter; thread thRouter;
thread thCloud; thread thCloud;
bool bReload = false;
bool bBootstrap = true; bool bBootstrap = true;
bool bColdStart = true; bool bColdStart = true;
...@@ -74,11 +74,12 @@ private: ...@@ -74,11 +74,12 @@ private:
/// tracking sub-systems: evmgr, evpuller, evpusher, evml*, evslicer etc. /// tracking sub-systems: evmgr, evpuller, evpusher, evml*, evslicer etc.
json mapSubSystems; json mapSubSystems;
json jsonIPs;
int ping(void *s) int ping(void *s)
{ {
int ret = 0; int ret = 0;
vector<vector<uint8_t> >body = {str2body("evcloudsvc:0:0"), str2body(EV_MSG_META_PING), str2body(MSG_HELLO)}; vector<vector<uint8_t> >body = {str2body("evcloudsvc:0:0"), str2body(EV_MSG_META_PING), str2body(this->jsonIPs.dump())};
ret = z_send_multiple(s, body); ret = z_send_multiple(s, body);
if(ret < 0) { if(ret < 0) {
...@@ -733,14 +734,6 @@ public: ...@@ -733,14 +734,6 @@ public:
res.set_content(ret.dump(), "text/json"); res.set_content(ret.dump(), "text/json");
}); });
svr.Get("/sync-cloud", [this](const Request& req, Response& res) {
json ret;
ret["code"] = 0;
ret["msg"] = "syncing ...";
res.set_content(ret.dump(), "text/json");
this->bReload = true;
});
svr.Get("/reset", [](const Request& req, Response& res) { svr.Get("/reset", [](const Request& req, Response& res) {
json ret; json ret;
ret["code"] = 0; ret["code"] = 0;
...@@ -771,12 +764,22 @@ public: ...@@ -771,12 +764,22 @@ public:
LVDB::getSn(info); LVDB::getSn(info);
} }
catch(exception &e) { catch(exception &e) {
spdlog::error("evdaemon failed to get sn: {}", e.what()); spdlog::error("evdaemon failed to get local info: {}", e.what());
exit(1); exit(1);
} }
spdlog::info("evdaemon boot \n{}",info.dump()); spdlog::info("evdaemon boot \n{}",info.dump());
auto ipAddrs = netutils::getIps();
if(ipAddrs.size() == 0) {
spdlog::error("evdaemon failed to start: no network available!");
exit(2);
}
for(const auto &[k, v]: ipAddrs) {
jsonIPs.push_back(k);
}
devSn = info["sn"]; devSn = info["sn"];
char* strEnv = getenv("BOOTSTRAP"); char* strEnv = getenv("BOOTSTRAP");
......
#ifndef __EV_NETWORKS_H__
#define __EV_NETWORKS_H__
#include <sys/types.h>
#include <ifaddrs.h>
#include <netinet/in.h>
#include <string>
#include <arpa/inet.h>
#include <map>
#include <spdlog/spdlog.h>
namespace netutils {
using namespace std;
map<string, string> getIps()
{
map<string, string> ret;
struct ifaddrs * ifAddrStruct=nullptr;
struct ifaddrs * ifa=nullptr;
void * tmpAddrPtr=nullptr;
char addressBuffer[INET_ADDRSTRLEN];
getifaddrs(&ifAddrStruct);
for (ifa = ifAddrStruct; ifa != nullptr; ifa = ifa->ifa_next) {
if (!ifa->ifa_addr) {
continue;
}
if (ifa->ifa_addr->sa_family == AF_INET) { // check it is IP4
// is a valid IP4 Address
tmpAddrPtr=&((struct sockaddr_in *)ifa->ifa_addr)->sin_addr;
int ip = htonl(*(int*)tmpAddrPtr);
// printf("%d %d %d %d", ( ip >> 24) & 0xFF, (ip >> 16) & 0xFF, (ip>> 8) & 0xFF, (ip >> 0) & 0xFF);
if(((ip >> 24) & 0xFF) == 127) {
continue;
}
if(inet_ntop(AF_INET, tmpAddrPtr, addressBuffer, INET_ADDRSTRLEN) != nullptr) {
string addrStr(addressBuffer);
string ifStr(ifa->ifa_name);
ret[addrStr] = ifStr;
}
}
}
if (ifAddrStruct!=nullptr) freeifaddrs(ifAddrStruct);
return ret;
}
}
#endif
\ No newline at end of file
#include <sys/types.h>
#include <ifaddrs.h>
#include <netinet/in.h>
#include <string>
#include <arpa/inet.h>
#include <map>
#include <spdlog/spdlog.h>
using namespace std;
map<string, string> getIps()
{
map<string, string> ret;
struct ifaddrs * ifAddrStruct=nullptr;
struct ifaddrs * ifa=nullptr;
void * tmpAddrPtr=nullptr;
char addressBuffer[INET_ADDRSTRLEN];
getifaddrs(&ifAddrStruct);
for (ifa = ifAddrStruct; ifa != nullptr; ifa = ifa->ifa_next) {
if (!ifa->ifa_addr) {
continue;
}
if (ifa->ifa_addr->sa_family == AF_INET) { // check it is IP4
// is a valid IP4 Address
tmpAddrPtr=&((struct sockaddr_in *)ifa->ifa_addr)->sin_addr;
int ip = htonl(*(int*)tmpAddrPtr);
// printf("%d %d %d %d", ( ip >> 24) & 0xFF, (ip >> 16) & 0xFF, (ip>> 8) & 0xFF, (ip >> 0) & 0xFF);
if(((ip >> 24) & 0xFF) == 127) {
continue;
}
if(inet_ntop(AF_INET, tmpAddrPtr, addressBuffer, INET_ADDRSTRLEN) != nullptr) {
string addrStr(addressBuffer);
string ifStr(ifa->ifa_name);
ret[addrStr] = ifStr;
}
}
}
if (ifAddrStruct!=nullptr) freeifaddrs(ifAddrStruct);
return ret;
}
int main (int argc, const char * argv[])
{
auto r = getIps();
for(const auto &[k,v]: r) {
spdlog::info("{} {}", k, v);
}
return 0;
}
\ No newline at end of file
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论