/****************************************************************************
 * 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 "./ipc_server_p.h"

#include <aqhome/ipc2/tcpd_object.h>
#include <aqhome/ipc2/ipc_msgreader.h>
#include <aqhome/ipc2/msgwriter.h>
#include <aqhome/events2/fdobject.h>

#include <gwenhywfar/debug.h>

#include <unistd.h>



/* ------------------------------------------------------------------------------------------------
 * defs and enums
 * ------------------------------------------------------------------------------------------------
 */

enum {
  AQH_IPCD_OBJECT_SLOT_NEWCONN=1
};



/* ------------------------------------------------------------------------------------------------
 * global vars
 * ------------------------------------------------------------------------------------------------
 */

GWEN_INHERIT(AQH_OBJECT, AQH_IPC_SERVER)



/* ------------------------------------------------------------------------------------------------
 * forward declarations
 * ------------------------------------------------------------------------------------------------
 */

static void GWENHYWFAR_CB _freeData(void *bp, void *p);
static int _handleSignal(AQH_OBJECT *o, uint32_t slotId, AQH_OBJECT *senderObject, int param1, void *param2);
static int _handleNewConn(AQH_OBJECT *o, int newFd);



/* ------------------------------------------------------------------------------------------------
 * implementation
 * ------------------------------------------------------------------------------------------------
 */

AQH_OBJECT *AQH_IpcServerObject_new(AQH_EVENT_LOOP *eventLoop, int fd)
{
  AQH_OBJECT *o;
  AQH_IPC_SERVER *xo;

  o=AQH_Object_new(eventLoop);
  GWEN_NEW_OBJECT(AQH_IPC_SERVER, xo);
  GWEN_INHERIT_SETDATA(AQH_OBJECT, AQH_IPC_SERVER, o, xo, _freeData);

  AQH_Object_SetSignalHandlerFn(o, _handleSignal);

  xo->tcpdObject=AQH_TcpdObject_new(eventLoop, fd);
  AQH_Object_AddLink(xo->tcpdObject, AQH_TCPD_OBJECT_SIGNAL_NEWCONN, AQH_IPCD_OBJECT_SLOT_NEWCONN, o);
  AQH_Object_Enable(xo->tcpdObject);

  return o;
}



void GWENHYWFAR_CB _freeData(GWEN_UNUSED void *bp, void *p)
{
  AQH_IPC_SERVER *xo;

  xo=(AQH_IPC_SERVER*) p;
  if (xo->tcpdObject) {
    AQH_Object_Disable(xo->tcpdObject);
    AQH_Object_free(xo->tcpdObject);
    xo->tcpdObject=NULL;
  }
  GWEN_FREE_OBJECT(xo);
}



int _handleSignal(AQH_OBJECT *o,
                  uint32_t slotId,
                  GWEN_UNUSED AQH_OBJECT *senderObject,
                  int param1,
                  GWEN_UNUSED void *param2)
{
  switch(slotId) {
  case AQH_IPCD_OBJECT_SLOT_NEWCONN: return _handleNewConn(o, param1);
  default:
    break;
  }

  return 0; /* not handled */
}



int _handleNewConn(AQH_OBJECT *o, int newFd)
{
  AQH_EVENT_LOOP *eventLoop;
  int fdCopy;
  AQH_OBJECT *fdReader;
  AQH_OBJECT *fdWriter;
  AQH_OBJECT *msgReader;
  AQH_OBJECT *msgWriter;
  AQH_OBJECT *endpoint;

  DBG_INFO(AQH_LOGDOMAIN, "Incoming connection");
  eventLoop=AQH_Object_GetEventLoop(o);
  fdCopy=dup(newFd);

  fdReader=AQH_FdObject_new(eventLoop, newFd, AQH_FDOBJECT_FDMODE_READ);
  msgReader=AQH_IpcMsgReader_new(eventLoop, fdReader);
  AQH_Object_Enable(msgReader);

  fdWriter=AQH_FdObject_new(eventLoop, fdCopy, AQH_FDOBJECT_FDMODE_WRITE);
  msgWriter=AQH_MsgWriter_new(eventLoop, fdWriter);

  endpoint=AQH_Endpoint_new(eventLoop, msgReader, msgWriter);
  if (0==AQH_Object_EmitSignal(o, AQH_TCPD_OBJECT_SIGNAL_NEWCONN, 0, (void*) endpoint)) {
    DBG_ERROR(AQH_LOGDOMAIN, "New connection not handled");
    AQH_Object_free(endpoint);
  }

  return 1; /* msg handled */
}





