
/*
 *  TIME.C
 *
 *  Handle IPC alert functions & timekeeping for connect and idle time.
 */

#include "dnet.h"
#include <stdio.h>
#include <local/ipc.h>

#define TICKSIZE    10	    /*	10 second intervals */

static long TickIdle;
static long TickConnect;

static long AlertIdle;
static long AlertConnect;

static char AIWho[64];	    /*	app-name to alert   */
static char ACWho[64];	    /*	app-name to alert   */

/*
 *  CTO:    Idle timeout (check carrier)
 *
 */

void
do_cto(ior)
IOT *ior;
{
    if (Cto_act) {
	AbortIO((IOR *)ior);
	WaitIO((IOR *)ior);
    }
    ior->tr_time.tv_secs = TICKSIZE;
    ior->tr_time.tv_micro = 0;
    SendIO((IOR *)ior);
    Cto_act = 1;
    if (Cd) {
	++TickConnect;
	++TickIdle;
    }
    if (DDebug)
	printf("I/C: %ld %ld (%ld %ld)\n", TickIdle, TickConnect, AlertIdle, AlertConnect);
#ifdef NOTDEF
    if (AlertConnect && TickConnect > AlertConnect)      /*  alert ipc msg */
	doipcmsg(ACWho, "\0connalert", 11);
    if (AlertIdle && TickIdle > AlertIdle)               /*  alert ipc msg */
	doipcmsg(AIWho, "\0idlealert", 11);
#endif
}

/*
 *  Idle/Connect time & alerter vars.  IPC:
 *			resetidle
 *			alertidle <N>	(N in seconds)
 *			alertconn <N>	(N in seconds)
 */

void
ResetIdle()
{
    TickIdle = 0;
}

void
ResetConnect()
{
    TickConnect = 0;
}

void
LessConnect(n)
{
    TickConnect -= n;
    if (TickConnect < 0)
	TickConnect = 0;
}

void
SetConnectAlert(n, who)
char *who;
{
    AlertConnect = (n+(TICKSIZE-1))/TICKSIZE;
    ACWho[0] = 0;
    if (who)
	strcpy(ACWho, who);
}

void
SetIdleAlert(n, who)
char *who;
{
    AlertIdle = (n+(TICKSIZE-1))/TICKSIZE;
    AIWho[0] = 0;
    if (who)
	strcpy(AIWho, who);
}

GetIdle()
{
    return(TickIdle * TICKSIZE);
}

GetConnect()
{
    return(TickConnect * TICKSIZE);
}

#ifdef NOTDEF

doipcmsg(app, cmd, len)
char *app;
char *cmd;
short len;
{
    IPCMSG *msg;
    long res = 1;
    IPCMSG *SendIPC();

    if (DDebug)
	printf("IPC: <%s><%s> = ", app, cmd+1);
    fflush(stdout);
    if (msg = SendIPC(app, cmd, len, 0)) {
	WaitMsg(msg);
	res = msg->Error;
	FreeIPC(msg);
    }
    if (DDebug)
	printf("%ld\n", res);
}

#endif
