/* Agent Tcl
   Bob Gray
   4 January 1998

   genAgentIO.h

   This file defines the AgentIO class which handles the background connection.
*/

#ifndef _AGENT_IO_H
#define _AGENT_IO_H

#ifndef _PLAT_TIMERS_H
#include "platTimers.h"			// TimerTrap
#endif
#ifndef _MESG_CONNECTION_H
#include "mesgConnection.h"		// ConnectionTrap
#endif
#ifndef _TRUEFALSE_H
#include "truefalse.h"			// BOOLEAN
#endif

// forward declarations

class AGENT;
class AgentConnection;
class INCOMING;
class MEETING;
class AgentSelectParameters;

class AgentIO: public TimerTrap, public ConnectionTrap
{
public:

	/*
	 * return the background connection 
	 */

    AgentConnection *getBackgroundConnection (void) {
	return (m_connection);
    }

	/* 
	 * turn on and off the background connection
	 */

    void turnBackgroundOn (AgentConnection *connection);
    void turnBackgroundOff (void);

	/* 
	 * unconditional sleep
	 */

    int sleep (struct timeval delay);

	/* 
	 * wait for an incoming message, event, or meeting request
	 */

    int receiveIncomingItem
	(int which, INCOMING *&item, struct timeval delay);

	/*
	 * wait for a meeting status change (including an incoming 
	 * message or file on the meeting)
	 */

    int simpleSelect_noLock
	(MEETING &meeting, UINT_8 &meetingStatus, struct timeval delay);

	/*
	 * wait for a meeting status change (including an incoming message 
	 * or file on the meeting) --OR-- an incoming message, event or 
	 * meeting request
	 */
	
    int select
	(struct timeval delay, AgentSelectParameters &selectParams);

	/*
	 * called when AgentMasks queues an incoming item
	 */

    void setQueuedItem (void) {
	m_queuedItem = e_TRUE;
    }

	/*
	 * constructor and destructor
	 */

    AgentIO (AGENT *agent);
   ~AgentIO ();

private:

	/*
	 * backpointer to the associated AGENT instance
	 */

    AGENT *m_agent;

	/* 
         * connection over which incoming items arrive from the server 
	 */

    AgentConnection *m_connection;

	/*
	 * e_TRUE on potential violation of the cpu-time or wall-time limit
	 */

    BOOLEAN m_potentialLimitViolation;

	/*
	 * number of AgentIO method invocations that are currently
	 * waiting for an I/O event (we use a counter rather than a BOOLEAN
	 * flag since we are starting to think ahead to a day when one agent
	 * might have multiple receiver threads)
	 */

    int m_waiters;

	/*
	 * e_TRUE if a new message, meeting or event has been queued up
	 */

    BOOLEAN m_queuedItem;

	/* 
	 * called on potential violation of the cpu-time or wall-time limit
	 */

    void cpuAlarm (void);		// inherited from TimerTrap
    void wallAlarm (void);		// inherited from TimerTrap

	/* 
	 * called when there is an incoming item on the background connection
	 */

    void connectionAlarm (Connection &connection, Connection::ConnectionStatus status);
};

#endif
