wirelessgateway/udpqt/SH_UdpQt.cpp

194 lines
6.8 KiB
C++

#include "SH_UdpQt.hpp"
#include "../common/SH_CommonFunc.hpp"
#include "../common/SH_global.h"
#include "../API_log/SH_log.h"
UdpSys::UdpSys():
mIoSev(), udpSock(mIoSev, boost::asio::ip::udp::endpoint(boost::asio::ip::udp::v4(), 7303))
{
print_light_green("UdpQt Init\n");
}
void UdpSys::ClearConnect()
{
remoteIp.clear();
status = false;
}
void UdpSys::StartConnectSysUdp()
{
print_light_green("start connect QT\n");
if (udpSock.is_open()) {
boost::asio::socket_base::linger option1(true, 0);
udpSock.set_option(option1);
boost::asio::socket_base::send_buffer_size option4(125000);
udpSock.set_option(option4);
remoteEndPoint = boost::asio::ip::udp::endpoint(boost::asio::ip::udp::v4(), 9998);
remoteEndPoint.address(boost::asio::ip::address::from_string("127.0.0.1"));
recvUdpData();
// boost::thread sendHeartTh(boost::bind(&UdpQt::ConfirmStatus, this));
// sendHeartTh.detach();
mIoSev.run();
}
}
void UdpSys::handle_send_to(const boost::system::error_code& ec,
size_t trans)
{
if (ec) {
print_error("send the udp to sys error! \n");
} else {
print_info("send the udp to sys ok! \n");
}
}
void UdpSys::SendUdpToSingle(std::string pData)
{
boost::mutex::scoped_lock lock(udpMutex);
int len = pData.length();
if (len <= 0)
return ;
char *mi = new char[len + 3];
memset(mi, 0, len + 3);
strcpy(mi, pData.c_str());
strcat(mi, "\r\n");
// std::cout << "sys send single:" << pData << remoteEndPoint.address() << remoteEndPoint.port() <<std::endl;
print_brown("sys send single : %s \n", pData.c_str());
udpSock.async_send_to(boost::asio::buffer(mi,len + 3), remoteEndPoint,
boost::bind(&UdpSys::handle_send_to, this,
boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred));
delete [] mi;
}
void UdpSys::recvUdpData()
{
udpSock.async_receive_from(boost::asio::buffer(m_buffer), senderEndPoint,
boost::bind(&UdpSys::HandleRead, this,
boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred));
}
void UdpSys::HandleRead(const boost::system::error_code& pEc,
std::size_t pBytesTransferred)
{
std::string ip = senderEndPoint.address().to_string();
if (pEc) {
std::cerr << pEc.value() <<std::endl;
std::cerr << pEc.category().name() << std::endl;
std::cerr << pEc.message() << std::endl;
} else if (pBytesTransferred == 0) {
std::cout << "rev has rec!" << std::endl;
if (pBytesTransferred == 0) {
std::cerr << "[ Read 0 Bytes ][ " << ip << " Quit ! ]" << std::endl;
}
} else {
std::string read_cmd(m_buffer.data(), pBytesTransferred);
print_light_purple("%s", read_cmd.c_str());
if(read_cmd.length()>0){
AnalysisDataSys(read_cmd);
}
}
recvUdpData();
}
void UdpSys::AnalysisDataSys(std::string cmd)
{
printf("%s", cmd.c_str());
Json::Reader recvReader;
Json::Value JsonVal;
if(recvReader.parse(cmd,JsonVal)) {
int cmdType = atoi(JsonVal["cmd"].asString().c_str());
switch (cmdType) {
case 3:{ //配置服务器
std::string strServerIp = JsonVal["localServerIpAddress"].asString();
int localServerPort = JsonVal["localServerPort"].asInt();
std::string strDataWatchAddedBy = "system";
if (!JsonVal["dataNodeGatewayAddedBy"].isNull()) {
strDataWatchAddedBy = JsonVal["dataNodeGatewayAddedBy"].asString();
WriteStr2Config(SYSTEMINFOFILE, "SystemInfo", "dataNodeGatewayAddedBy", strDataWatchAddedBy);
}
print_info("strServerIp : %s \n", strServerIp.c_str());
if (strServerIp.compare(GlobalConfig::ServerIP) != 0 || localServerPort != GlobalConfig::ServerPort)
{
GlobalConfig::ServerIP = strServerIp;
GlobalConfig::ServerPort = localServerPort;
WriteStr2Config(SERVERCONFIG, "Server", "localServerIpAddress", GlobalConfig::ServerIP);
WriteStr2Config(SERVERCONFIG, "Server", "localServerPort", boost::lexical_cast<std::string>(GlobalConfig::ServerPort));
std::string fileserver = ReadStrByOpt(SERVERCONFIG, "FileServer", "FileServerIpAddress");
if (0 == fileserver.compare("0.0.0.0") || 0 == fileserver.length()) {
WriteStr2Config(SERVERCONFIG, "FileServer", "FileServerIpAddress", GlobalConfig::ServerIP);
}
JsonVal["status"] = "ACK";
Json::FastWriter fw;
std::string str = fw.write(JsonVal);
print_info("send info : %s \n", str.c_str());
boost::asio::ip::udp::endpoint remoteEP(boost::asio::ip::address::from_string(senderEndPoint.address().to_string()),
MULTICAST_PORT_SEND);
udpSock.async_send_to(boost::asio::buffer(str),remoteEP,
boost::bind(&UdpSys::handle_send_to,this,boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred));
// Json::Value jsValue;
// Json::FastWriter fw1;
// jsValue["type"] = "set";
// std::string data = fw1.write(jsValue);
// data_publish_local(data.c_str(), GlobalConfig::Topic_G.mPubLocalConfig.c_str());
// boost::this_thread::sleep(boost::posix_time::seconds(2));
// exit(0);
} else {
// char reply_string[256] = {0};
// sprintf(reply_string, "{\"dataNodeGatewayNo\":\"%s\",\"softVersion\":\"%s\",\"status\":\"0\"}",
// GlobalConfig::MacAddr_G.c_str(), GlobalConfig::Version.c_str());
// std::string instr = std::string(reply_string);
// std::string topic = "equipment/state/" + GlobalConfig::MacAddr_G;
// int ret = data_publish(instr.c_str(), topic.c_str());
// std::string cmd20 = JsonCmd_Cgi_20();
// data_publish(cmd20.c_str(), GlobalConfig::Topic_G.mPubCmd.c_str());
}
}
break;
case 4:{
Json::FastWriter fw;
std::string status = JsonVal["status"].asString();
if(status.compare("REQ") == 0)
{
JsonVal["dataNodeGatewayNo"] = GlobalConfig::MacAddr_G.c_str();
JsonVal["localServerIpAddress"] = GlobalConfig::ServerIP;
JsonVal["status"] = "ACK";
std::string data = fw.write(JsonVal);
boost::asio::ip::udp::endpoint remoteEP(boost::asio::ip::address::from_string(senderEndPoint.address().to_string()),
MULTICAST_PORT_SEND);
udpSock.async_send_to(boost::asio::buffer(data),remoteEP,
boost::bind(&UdpSys::handle_send_to,this,boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred));
}
}
break;
default:
break;
}
}else
{
print_error("json parse failed");
}
}
UdpSys::~UdpSys()
{
if (udpSock.is_open())
udpSock.close();
}