PracticeDev/study_clang/Mimic/OperateNos/FOperateNos.cpp

93 lines
2.9 KiB
C++
Raw Normal View History

2022-12-20 17:31:11 +08:00
/*************************************************************************
> File Name : OperateNos.cpp
> Author : TL Song
> EMail : songtianlun@frytea.com
> Created Time : Thu 03 Dec 2020 09:13:38 AM CST
************************************************************************/
#include "FOperateNos.h"
int FOperateNos::StartNos(int iNosIdx)
{
// char szmsg[1024] = {0};
// strcpy(szmsg,"Start Nos");
Web_Msg_T webMsg;
memset(&webMsg, 0x00, sizeof(webMsg));
webMsg.nos_id = iNosIdx;
webMsg.nos_op = NOS_POWER_ON;
SendCmdToShd(&webMsg, sizeof(webMsg));
std::cout << "Nos" << webMsg.nos_id << " will be power on, result " << webMsg.result << std::endl;
}
int FOperateNos::StopNos(int iNosIdx)
{
Web_Msg_T webMsg;
memset(&webMsg, 0x00, sizeof(webMsg));
webMsg.nos_id = iNosIdx;
webMsg.nos_op = NOS_POWER_OFF;
SendCmdToShd(&webMsg, sizeof(webMsg));
std::cout << "Nos" << webMsg.nos_id << " will be power off, result " << webMsg.result << std::endl;
}
int FOperateNos::RestartNos(int iNosIdx)
{
Web_Msg_T webMsg;
memset(&webMsg, 0x00, sizeof(webMsg));
webMsg.nos_id = iNosIdx;
webMsg.nos_op = NOS_RESTART;
SendCmdToShd(&webMsg, sizeof(webMsg));
std::cout << "Nos" << webMsg.nos_id << " will be restart, result " << webMsg.result << std::endl;
}
void FOperateNos::Init()
{
reader = INIReader("operate_nos_conf.ini");
if (reader.ParseError() < 0)
std::cout << "Can't load 'test.ini'\n";
std::cout << "Init F Operate Nos" << std::endl;
InitSocket();
}
void FOperateNos::InitSocket()
{
char szPubKey[64] = {0};
char szPriKey[64] = {0};
char szSerKey[64] = {0};
char szaddr[128] = {0};
ctx = zmq_ctx_new();
sock = zmq_socket(ctx, ZMQ_REQ);
strcpy(szSerKey, ZMQ_PUBLIC_KEY_1);
snprintf(szaddr, sizeof(szaddr), "tcp://%s:%d", \
reader.Get("general", "shd_ip", "UNKNOWN").c_str(), \
reader.GetInteger("general", "shd_port", -1)
);
std::cout << "Connect to zmq server : " << szaddr << std::endl;
// 加密
// zmq_setsockopt(sock, ZMQ_CURVE_SERVERKEY, szSerKey, strlen(szSerKey));
// zmq_curve_keypair(szPubKey, szPriKey);
// zmq_setsockopt(sock, ZMQ_CURVE_PUBLICKEY, szPubKey, strlen(szPubKey));
// zmq_setsockopt(sock, ZMQ_CURVE_SECRETKEY, szPriKey, strlen(szPriKey));
zmq_connect(sock, szaddr);
// 超时
unsigned int uiTimeout = 30 * 1000;
zmq_setsockopt(sock, ZMQ_RCVTIMEO, &uiTimeout, sizeof(uiTimeout));
}
void FOperateNos::DestroySocket()
{
zmq_close(sock);
zmq_ctx_term(ctx);
zmq_ctx_destroy(ctx);
}
int FOperateNos::SendCmdToShd(void * sMsg, int iMsgLen)
{
int iResult;
iResult = zmq_send(sock, sMsg, iMsgLen, 0);
if(0 > iResult)
std::cout << "Send Msg To Shd Error, Code " << iResult << std::endl;
iResult = zmq_recv(sock, sMsg, iMsgLen, 0);
if(0 > iResult)
std::cout << "Recv Msg From Shd Error, Code " << iResult << std::endl;
}