aqhome-tool: recorganized code for improved reusability.

This commit is contained in:
Martin Preuss
2023-04-19 15:39:15 +02:00
parent cf3a90dccc
commit 20efe7d473
4 changed files with 148 additions and 97 deletions

View File

@@ -11,6 +11,7 @@
#endif
#include "./ping.h"
#include "./utils.h"
#include "aqhome/ipc/msg_ipc_setaccmsggrps.h"
#include "aqhome/ipc/msg_ipc_ping.h"
@@ -34,13 +35,7 @@
static int _doPing(GWEN_DB_NODE *dbArgs);
static int _sendAcceptedMsgGroups(GWEN_MSG_ENDPOINT *epTcp);
static int _sendPing(GWEN_MSG_ENDPOINT *epTcp, int nodeAddr);
static GWEN_MSG *_waitForSpecificNodeMessage(GWEN_MSG_ENDPOINT_MGR *emgr, GWEN_MSG_ENDPOINT *epTcp,
int msgCode,
int nodeAddr,
int timeoutInSeconds);
static GWEN_MSG_ENDPOINT *_setupIpcEndpoint(GWEN_MSG_ENDPOINT_MGR *emgr, GWEN_DB_NODE *dbArgs);
@@ -144,7 +139,7 @@ int _doPing(GWEN_DB_NODE *dbArgs)
emgr=GWEN_MsgEndpointMgr_new();
epTcp=_setupIpcEndpoint(emgr, dbArgs);
epTcp=Utils_SetupIpcEndpoint(emgr, dbArgs);
if (epTcp==NULL) {
DBG_ERROR(NULL, "ERROR creating TCP connection");
return 2;
@@ -154,7 +149,7 @@ int _doPing(GWEN_DB_NODE *dbArgs)
nodeAddr=GWEN_DB_GetIntValue(dbArgs, "nodeAddr", 0, 0);
timeoutInSeconds=GWEN_DB_GetIntValue(dbArgs, "timeout", 0, 5);
rv=_sendAcceptedMsgGroups(epTcp);
rv=Utils_SendAcceptedMsgGroups(epTcp, AQH_MSG_TYPEGROUP_ALL);
if (rv<0) {
DBG_INFO(NULL, "here (%d)", rv);
return 2;
@@ -166,7 +161,7 @@ int _doPing(GWEN_DB_NODE *dbArgs)
return 2;
}
msg=_waitForSpecificNodeMessage(emgr, epTcp, AQH_MSG_TYPE_PONG, nodeAddr, timeoutInSeconds);
msg=Utils_WaitForSpecificNodeMessage(emgr, epTcp, AQH_MSG_TYPE_PONG, nodeAddr, timeoutInSeconds);
if (msg==NULL) {
DBG_INFO(NULL, "No PONG response received.");
GWEN_MsgEndpointMgr_free(emgr);
@@ -180,69 +175,6 @@ int _doPing(GWEN_DB_NODE *dbArgs)
GWEN_MSG *_waitForSpecificNodeMessage(GWEN_MSG_ENDPOINT_MGR *emgr, GWEN_MSG_ENDPOINT *epTcp,
int msgCode,
int nodeAddr,
int timeoutInSeconds)
{
time_t startTime;
startTime=time(NULL);
for (;;) {
GWEN_MSG *msg;
time_t now;
GWEN_MsgEndpointMgr_RunAllEndpoints(emgr);
GWEN_MsgEndpointMgr_IoLoopOnce(emgr);
msg=GWEN_MsgEndpoint_TakeFirstReceivedMessage(epTcp);
if (msg) {
if (GWEN_IpcMsg_GetCode(msg)==AQH_MSGTYPE_IPC_FORWARD) {
GWEN_MSG *nodeMsg;
DBG_INFO(NULL, "Received IPC FORWARD message");
nodeMsg=AQH_ForwardIpcMsg_GetCopyOfNodeMsg(msg);
if (nodeMsg) {
DBG_ERROR(AQH_LOGDOMAIN,
"Received node msg from %d (%d)",
AQH_NodeMsg_GetSourceAddress(nodeMsg),
AQH_NodeMsg_GetMsgType(nodeMsg));
if (AQH_NodeMsg_GetMsgType(nodeMsg)==msgCode &&
AQH_NodeMsg_GetSourceAddress(nodeMsg)==nodeAddr) {
GWEN_Msg_free(msg);
return nodeMsg;
}
}
}
GWEN_Msg_free(msg);
}
now=time(NULL);
if (now-startTime>timeoutInSeconds) {
DBG_INFO(NULL, "Timeout");
break;
}
}
return NULL;
}
int _sendAcceptedMsgGroups(GWEN_MSG_ENDPOINT *epTcp)
{
GWEN_MSG *msgOut;
msgOut=AQH_SetAcceptedMsgGroupsIpcMsg_new(AQH_MSGTYPE_IPC_SETACCMSGGRPS, AQH_MSG_TYPEGROUP_ALL);
if (msgOut==NULL) {
DBG_ERROR(NULL, "Error creating message");
return GWEN_ERROR_GENERIC;
}
GWEN_MsgEndpoint_AddSendMessage(epTcp, msgOut);
return 0;
}
int _sendPing(GWEN_MSG_ENDPOINT *epTcp, int nodeAddr)
{
GWEN_MSG *msgOut;
@@ -261,28 +193,3 @@ int _sendPing(GWEN_MSG_ENDPOINT *epTcp, int nodeAddr)
GWEN_MSG_ENDPOINT *_setupIpcEndpoint(GWEN_MSG_ENDPOINT_MGR *emgr, GWEN_DB_NODE *dbArgs)
{
GWEN_MSG_ENDPOINT *epTcp;
const char *tcpAddress;
int tcpPort;
tcpAddress=GWEN_DB_GetCharValue(dbArgs, "tcpAddress", 0, "127.0.0.1");
tcpPort=GWEN_DB_GetIntValue(dbArgs, "tcpPort", 0, 45454);
DBG_INFO(NULL, "Setup tcp client endpoint to %s:%d", tcpAddress, tcpPort);
epTcp=AQH_IpcTcpClientEndpoint_new(tcpAddress, tcpPort, "aqhome-tool-IPC", 0);
if (epTcp==NULL) {
DBG_ERROR(NULL, "Error creating endpoint TCPc");
return NULL;
}
return epTcp;
}