aqhome: more work on transformation to event2/ipc2.
This commit is contained in:
222
apps/aqhome-tool/data/client.c
Normal file
222
apps/aqhome-tool/data/client.c
Normal file
@@ -0,0 +1,222 @@
|
||||
/****************************************************************************
|
||||
* This file is part of the project AqHome.
|
||||
* AqHome (c) by 2025 Martin Preuss, all rights reserved.
|
||||
*
|
||||
* The license for this file can be found in the file COPYING which you
|
||||
* should have received along with this file.
|
||||
****************************************************************************/
|
||||
|
||||
#ifdef HAVE_CONFIG_H
|
||||
# include <config.h>
|
||||
#endif
|
||||
|
||||
#include "./client_p.h"
|
||||
#include "../utils.h"
|
||||
|
||||
#include "aqhome/aqhome.h"
|
||||
#include "aqhome/msg/ipc/m_ipc.h"
|
||||
#include "aqhome/msg/ipc/m_ipc_tag16.h"
|
||||
#include "aqhome/msg/ipc/m_ipc_result.h"
|
||||
#include "aqhome/ipc2/endpoint.h"
|
||||
|
||||
#include <gwenhywfar/args.h>
|
||||
#include <gwenhywfar/i18n.h>
|
||||
#include <gwenhywfar/debug.h>
|
||||
#include <gwenhywfar/text.h>
|
||||
|
||||
|
||||
|
||||
GWEN_INHERIT(AQH_OBJECT, AQH_TOOL_CLIENT)
|
||||
|
||||
|
||||
|
||||
|
||||
static void GWENHYWFAR_CB _freeData(void *bp, void *p);
|
||||
|
||||
static int _sendWaitHandle(AQH_OBJECT *o, AQH_TOOL_CLIENT *xo);
|
||||
static AQH_MESSAGE *_createRequestMessage(AQH_OBJECT *o, uint32_t msgId);
|
||||
static int _handleResponseMessage(AQH_OBJECT *o, const AQH_MESSAGE *msg, const GWEN_TAG16_LIST *tagList);
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
AQH_OBJECT *AQH_ToolClient_new(AQH_EVENT_LOOP *eventLoop, GWEN_DB_NODE *dbGlobalArgs, const GWEN_ARGS *argDescrs)
|
||||
{
|
||||
AQH_OBJECT *o;
|
||||
AQH_TOOL_CLIENT *xo;
|
||||
|
||||
o=AQH_Object_new(eventLoop);
|
||||
GWEN_NEW_OBJECT(AQH_TOOL_CLIENT, xo);
|
||||
GWEN_INHERIT_SETDATA(AQH_OBJECT, AQH_TOOL_CLIENT, o, xo, _freeData);
|
||||
xo->dbGlobalArgs=dbGlobalArgs;
|
||||
xo->args=argDescrs;
|
||||
|
||||
return o;
|
||||
}
|
||||
|
||||
|
||||
|
||||
void GWENHYWFAR_CB _freeData(GWEN_UNUSED void *bp, void *p)
|
||||
{
|
||||
AQH_TOOL_CLIENT *xo;
|
||||
|
||||
xo=(AQH_TOOL_CLIENT*)p;
|
||||
GWEN_DB_Group_free(xo->dbLocalArgs);
|
||||
GWEN_DB_Group_free(xo->dbGlobalArgs);
|
||||
AQH_Object_free(xo->ipcEndpoint);
|
||||
GWEN_FREE_OBJECT(xo);
|
||||
}
|
||||
|
||||
|
||||
|
||||
int AQH_ToolClient_ReadLocalArgs(AQH_OBJECT *o, int argc, char **argv)
|
||||
{
|
||||
if (o) {
|
||||
AQH_TOOL_CLIENT *xo;
|
||||
|
||||
xo=GWEN_INHERIT_GETDATA(AQH_OBJECT, AQH_TOOL_CLIENT, o);
|
||||
if (xo) {
|
||||
int rv;
|
||||
|
||||
GWEN_DB_Group_free(xo->dbLocalArgs);
|
||||
xo->dbLocalArgs=GWEN_DB_GetGroup(xo->dbGlobalArgs, GWEN_DB_FLAGS_DEFAULT, "local");
|
||||
rv=GWEN_Args_Check(argc, argv, 1, GWEN_ARGS_MODE_ALLOW_FREEPARAM, xo->args, xo->dbLocalArgs);
|
||||
if (rv==GWEN_ARGS_RESULT_ERROR) {
|
||||
fprintf(stderr, "ERROR: Could not parse arguments\n");
|
||||
return 1;
|
||||
}
|
||||
else if (rv==GWEN_ARGS_RESULT_HELP) {
|
||||
GWEN_BUFFER *ubuf;
|
||||
|
||||
ubuf=GWEN_Buffer_new(0, 1024, 0, 1);
|
||||
if (GWEN_Args_Usage(xo->args, ubuf, GWEN_ArgsOutType_Txt)) {
|
||||
fprintf(stderr, "ERROR: Could not create help string\n");
|
||||
return 1;
|
||||
}
|
||||
fprintf(stderr, "%s\n", GWEN_Buffer_GetStart(ubuf));
|
||||
GWEN_Buffer_free(ubuf);
|
||||
return 1;
|
||||
}
|
||||
xo->timeoutInSeconds=GWEN_DB_GetIntValue(xo->dbLocalArgs, "timeout", 0, 5);
|
||||
|
||||
AQH_MergeConfigFileIntoConfig(xo->dbLocalArgs, "ConfigFile");
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
|
||||
void AQH_ToolClient_SetCreateRequestMessageFn(AQH_OBJECT *o, AQH_TOOLCLIENT_CREATEREQUESTMESSAGE_FN f)
|
||||
{
|
||||
AQH_TOOL_CLIENT *xo;
|
||||
|
||||
xo=GWEN_INHERIT_GETDATA(AQH_OBJECT, AQH_TOOL_CLIENT, o);
|
||||
if (xo)
|
||||
xo->createRequestMessageFn=f;
|
||||
}
|
||||
|
||||
|
||||
|
||||
void AQH_ToolClient_SetHandleResponseMessageFn(AQH_OBJECT *o, AQH_TOOLCLIENT_HANDLERESPONSEMESSAGE_FN f)
|
||||
{
|
||||
AQH_TOOL_CLIENT *xo;
|
||||
|
||||
xo=GWEN_INHERIT_GETDATA(AQH_OBJECT, AQH_TOOL_CLIENT, o);
|
||||
if (xo)
|
||||
xo->handleResponseMessageFn=f;
|
||||
}
|
||||
|
||||
|
||||
|
||||
int AQH_ToolClient_Run(AQH_OBJECT *o)
|
||||
{
|
||||
AQH_TOOL_CLIENT *xo;
|
||||
|
||||
xo=GWEN_INHERIT_GETDATA(AQH_OBJECT, AQH_TOOL_CLIENT, o);
|
||||
if (xo) {
|
||||
xo->ipcEndpoint=Utils2_SetupBrokerClientEndpoint(AQH_Object_GetEventLoop(o), xo->dbLocalArgs, 0);
|
||||
if (xo->ipcEndpoint==NULL) {
|
||||
DBG_ERROR(NULL, "ERROR creating TCP connection");
|
||||
return 2;
|
||||
}
|
||||
return _sendWaitHandle(o, xo);
|
||||
}
|
||||
return GWEN_ERROR_INVALID;
|
||||
}
|
||||
|
||||
|
||||
|
||||
int _sendWaitHandle(AQH_OBJECT *o, AQH_TOOL_CLIENT *xo)
|
||||
{
|
||||
AQH_EVENT_LOOP *eventLoop;
|
||||
AQH_MESSAGE *msgOut;
|
||||
uint32_t msgId;
|
||||
|
||||
eventLoop=AQH_Object_GetEventLoop(o);
|
||||
msgId=AQH_Endpoint_GetNextMessageId(xo->ipcEndpoint);
|
||||
msgOut=_createRequestMessage(o, msgId);
|
||||
if (msgOut==NULL) {
|
||||
DBG_ERROR(NULL, "Error creating outbound message");
|
||||
return 2;
|
||||
}
|
||||
AQH_Endpoint_AddMsgOut(xo->ipcEndpoint, msgOut);
|
||||
|
||||
for (;;) {
|
||||
AQH_MESSAGE *msgIn;
|
||||
|
||||
msgIn=Utils2_WaitForResponseMsg(eventLoop, xo->ipcEndpoint, msgId, xo->timeoutInSeconds);
|
||||
if (msgIn) {
|
||||
GWEN_TAG16_LIST *tagList;
|
||||
|
||||
tagList=AQH_IpcMessageTag16_ParsePayload(msgIn, 0);
|
||||
if (tagList) {
|
||||
int rv;
|
||||
|
||||
rv=_handleResponseMessage(o, msgIn, tagList);
|
||||
AQH_Message_free(msgIn);
|
||||
if (rv<0) {
|
||||
DBG_ERROR(NULL, "here (%d)", rv);
|
||||
return 3;
|
||||
}
|
||||
else if (rv==1) {
|
||||
DBG_ERROR(NULL, "Done.");
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
} /* for */
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
|
||||
AQH_MESSAGE *_createRequestMessage(AQH_OBJECT *o, uint32_t msgId)
|
||||
{
|
||||
AQH_TOOL_CLIENT *xo;
|
||||
|
||||
xo=GWEN_INHERIT_GETDATA(AQH_OBJECT, AQH_TOOL_CLIENT, o);
|
||||
if (xo) {
|
||||
if (xo->createRequestMessageFn)
|
||||
return xo->createRequestMessageFn(o, msgId);
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
|
||||
int _handleResponseMessage(AQH_OBJECT *o, const AQH_MESSAGE *msg, const GWEN_TAG16_LIST *tagList)
|
||||
{
|
||||
AQH_TOOL_CLIENT *xo;
|
||||
|
||||
xo=GWEN_INHERIT_GETDATA(AQH_OBJECT, AQH_TOOL_CLIENT, o);
|
||||
if (xo) {
|
||||
if (xo->handleResponseMessageFn)
|
||||
return xo->handleResponseMessageFn(o, msg, tagList);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user