Browse Source

v2.1 | add ext debug info

master
zhaohe 4 months ago
parent
commit
99f2728278
  1. 2
      src/configs/version.hpp
  2. 45
      src/service/extapi_service.cpp
  3. 72
      src/service/wbesocket_connect_mgr_service.cpp
  4. 50
      src/service/wbesocket_connect_mgr_service.hpp

2
src/configs/version.hpp

@ -1,2 +1,2 @@
#pragma once #pragma once
#define VERSION "2.1"
#define VERSION "2.2"

45
src/service/extapi_service.cpp

@ -4,6 +4,7 @@
#include "ixwebsocket/IXWebSocketServer.h" #include "ixwebsocket/IXWebSocketServer.h"
#include "utils/stringutils.hpp" #include "utils/stringutils.hpp"
#include "utils/urlparser.hpp" #include "utils/urlparser.hpp"
#include "wbesocket_connect_mgr_service.hpp"
// #include "iflytop/components/zcanreceiver/zcanreceiverhost.hpp" // #include "iflytop/components/zcanreceiver/zcanreceiverhost.hpp"
// #include "iflytop/core/components/stringutils.hpp" // #include "iflytop/core/components/stringutils.hpp"
@ -20,26 +21,6 @@ using namespace ix;
namespace iflytop {}; namespace iflytop {};
// static map<string,
static map<string, list<shared_ptr<WebSocket>>> wsClients;
static void insertNewClient(string chname, shared_ptr<WebSocket> client) {
if (client == nullptr) return;
if (chname.empty()) return;
if (wsClients.find(chname) == wsClients.end()) {
wsClients[chname] = list<shared_ptr<WebSocket>>();
}
wsClients[chname].push_back(client);
}
static void removeClient(string chname, shared_ptr<WebSocket> client) {
if (wsClients.find(chname) != wsClients.end()) {
wsClients[chname].remove(client);
}
}
static list<shared_ptr<WebSocket>> &getWsClients(string chname) { return wsClients[chname]; }
void ExtAPIService::initialize() { void ExtAPIService::initialize() {
toml::table config; toml::table config;
@ -78,7 +59,7 @@ void ExtAPIService::initialize() {
if (cmd == "restart") { if (cmd == "restart") {
exit(0); exit(0);
} else if (cmd == "getStatus" || cmd == "get-status") { } else if (cmd == "getStatus" || cmd == "get-status") {
receipt["status"] = 0;
receipt["status"] = 0;
for (auto it : DataChannelMgr::getChannels()) { for (auto it : DataChannelMgr::getChannels()) {
if (it->getAlias().empty()) { if (it->getAlias().empty()) {
receipt["data"][it->getChannelName()] = it->getChannelInfo(); receipt["data"][it->getChannelName()] = it->getChannelInfo();
@ -89,6 +70,9 @@ void ExtAPIService::initialize() {
} else if (cmd == "getVersion") { } else if (cmd == "getVersion") {
receipt["status"] = 0; receipt["status"] = 0;
receipt["data"]["version"] = VERSION; receipt["data"]["version"] = VERSION;
} else if (cmd == "getConnections") {
receipt["status"] = 0;
receipt["data"] = WbesocketConnectMgrService::getConnectionList();
} }
} }
@ -136,8 +120,7 @@ void ExtAPIService::initialize() {
} }
string chname = webSocket.getUrl().substr(1); string chname = webSocket.getUrl().substr(1);
insertNewClient(chname, findws(chname));
WbesocketConnectMgrService::insertNewClient(chname, findws(chname), make_shared<WebSocketConnectInfo>(connectionState->getRemoteIp()));
} else if (msg->type == ix::WebSocketMessageType::Message) { } else if (msg->type == ix::WebSocketMessageType::Message) {
string chname = webSocket.getUrl().substr(1); string chname = webSocket.getUrl().substr(1);
auto channel = DataChannelMgr::findByChannel(chname); auto channel = DataChannelMgr::findByChannel(chname);
@ -148,23 +131,17 @@ void ExtAPIService::initialize() {
} }
} else if (msg->type == ix::WebSocketMessageType::Close) { } else if (msg->type == ix::WebSocketMessageType::Close) {
string chname = webSocket.getUrl().substr(1); string chname = webSocket.getUrl().substr(1);
removeClient(chname, findws(chname));
logger->info("close connect ip: {},url: {}, channel:{}", connectionState->getRemoteIp(), webSocket.getUrl(), chname);
WbesocketConnectMgrService::removeClient(chname, findws(chname));
} }
}); });
// On Data From Channel // On Data From Channel
DataChannelMgr::regOnChannelData([this](IDataChannel *fromch, bool binary, const char *data, size_t len) { DataChannelMgr::regOnChannelData([this](IDataChannel *fromch, bool binary, const char *data, size_t len) {
logger->info("ondata from channel {}", fromch->getChannelName());
list<shared_ptr<WebSocket>> clients; list<shared_ptr<WebSocket>> clients;
auto chs = getWsClients(fromch->getChannelName());
for (auto it : chs) {
clients.push_back(it);
}
auto aliaschs = getWsClients(fromch->getAlias());
for (auto it : aliaschs) {
clients.push_back(it);
}
WbesocketConnectMgrService::findClientByName(fromch->getChannelName(), clients);
WbesocketConnectMgrService::findClientByName(fromch->getAlias(), clients);
for (auto ch : clients) { for (auto ch : clients) {
if (ch) { if (ch) {
if (binary) { if (binary) {

72
src/service/wbesocket_connect_mgr_service.cpp

@ -0,0 +1,72 @@
#include "wbesocket_connect_mgr_service.hpp"
using namespace iflytop;
using namespace core;
using namespace std;
using namespace nlohmann;
using namespace ix;
// static map<string,
static map<string, list<shared_ptr<WebSocket>>> wsClients;
static map<void *, shared_ptr<WebSocketConnectInfo>> wsClientsInfo;
static std::mutex wsClientsLock;
shared_ptr<WebSocketConnectInfo> findwsinfo(shared_ptr<WebSocket> client) {
if (client == nullptr) return nullptr;
auto it = wsClientsInfo.find((void *)client.get());
if (it == wsClientsInfo.end()) return nullptr;
return it->second;
}
void WbesocketConnectMgrService::insertNewClient(string chname, shared_ptr<WebSocket> client, shared_ptr<WebSocketConnectInfo> connectInfo) {
lock_guard<mutex> lock(wsClientsLock);
if (client == nullptr) return;
if (chname.empty()) return;
if (wsClients.find(chname) == wsClients.end()) {
wsClients[chname] = list<shared_ptr<WebSocket>>();
}
wsClients[chname].push_back(client);
wsClientsInfo[(void *)client.get()] = connectInfo;
GET_LOGGER(WSConnectMgr)->info("new client: {} {}, channel: {}", client->getUrl(), (void *)client.get(), chname);
}
void WbesocketConnectMgrService::removeClient(string chname, shared_ptr<WebSocket> client) {
lock_guard<mutex> lock(wsClientsLock);
if (client == nullptr) {
return;
}
if (wsClients.find(chname) != wsClients.end()) {
wsClients[chname].remove(client);
wsClientsInfo.erase((void *)client.get());
GET_LOGGER(WSConnectMgr)->info("remove client: {} {}, channel: {}", client->getUrl(), (void *)client.get(), chname);
}
}
void WbesocketConnectMgrService::findClientByName(string chname, list<shared_ptr<WebSocket>> &clients) {
lock_guard<mutex> lock(wsClientsLock);
auto it = wsClients.find(chname);
if (it == wsClients.end()) {
return;
}
for (auto ch : it->second) {
clients.push_back(ch);
}
}
json WbesocketConnectMgrService::getConnectionList() {
lock_guard<mutex> lock(wsClientsLock);
json j;
for (auto it : wsClients) {
json jlist;
jlist["channelName"] = it.first;
for (auto ch : it.second) {
json chj;
chj["uri"] = ch->getUrl();
chj["ptr"] = (int64_t)(void *)ch.get();
chj["remoteIp"] = findwsinfo(ch) ? findwsinfo(ch)->getRemoteIp() : "null";
jlist["connections"].push_back(chj);
}
j.push_back(jlist);
}
return j;
}

50
src/service/wbesocket_connect_mgr_service.hpp

@ -0,0 +1,50 @@
//
// Created by zwsd
//
#pragma once
#include <fstream>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <set>
#include <sstream>
#include <string>
#include <vector>
//
#include "components/zcanreceiver/zcanreceiverhost.hpp"
#include "components/zservice_container/zservice_container.hpp"
//
#include "ixwebsocket/IXHttp.h"
#include "ixwebsocket/IXHttpServer.h"
#include "ixwebsocket/IXWebSocketServer.h"
#include "spdlogfactory/logger.hpp"
//
//
#include <mutex>
namespace iflytop {
using namespace std;
using namespace nlohmann;
using namespace ix;
class WebSocketConnectInfo {
string remoteIp;
public:
WebSocketConnectInfo() {}
WebSocketConnectInfo(string ip) : remoteIp(ip) {}
string getRemoteIp() { return remoteIp; }
};
class WbesocketConnectMgrService {
public:
static void insertNewClient(string chname, shared_ptr<WebSocket> client, shared_ptr<WebSocketConnectInfo> connectInfo);
static void removeClient(string chname, shared_ptr<WebSocket> client);
static void findClientByName(string chname, list<shared_ptr<WebSocket>> &clients);
static json getConnectionList();
};
} // namespace iflytop
Loading…
Cancel
Save