/* Agent Tcl
   Bob Gray
   25 January 1995

   tclAgent.h

   This file defines the agent library routines.

   Copyright (c) 1995, Robert S. Gray Dartmouth College

   See the file "agent.terms" for information on usage and redistribution
   of this file and for a DISCLAIMER OF ALL WARRANTIES.
*/

#ifndef _AGENT_TCL_H
#define _AGENT_TCL_H

#ifndef NO_PRAGMAS
#pragma interface
#endif

    /* forward declarations */

#include <sys/types.h>
#include <time.h>
#include "tcl.h"
#include "tclAgent.h"
#include "tclConversion.h"
#include "tcpipTcl.h"
#include "truefalse.h"

#ifdef __cplusplus

class AgentId;
class Version;
class AgentSelectParameters;

#define EXTERN extern "C"
#define CONST const

  /* default number of seconds to wait when connecting */

const int DEFAULT_CONNECT_SECONDS = 20;

#else

#define EXTERN extern
#define CONST

  /* default number of seconds to wait when connecting */

#define DEFAULT_CONNECT_SECONDS	   20

#endif

    /* start, name, end and forcibly terminate an agent */

EXTERN struct AgentId *Agent_Begin 
	(Tcl_Interp *interp, double seconds, struct MachineId *id);

EXTERN struct AgentId *Agent_Name
	(Tcl_Interp *interp, double seconds, char *name);
 
EXTERN int Agent_End 
	(Tcl_Interp *interp, double seconds);
 
EXTERN int Agent_Force 
	(Tcl_Interp *interp, double seconds, 
	struct AgentId *id, struct AgentId **forcedId);
 
    /* turn the agent into a root agent */

EXTERN int Agent_Root
	(Tcl_Interp *interp);

    /* submit and fork the agent */

EXTERN char *Agent_Script
	(Tcl_Interp *interp, 
	 int numProcedures, char **procedures, 
         int numVariables, char **variables,
         char *script);

EXTERN struct AgentId *Agent_Submit 
	(Tcl_Interp *interp, double seconds, 
	 struct MachineId *id, Version *language,
	 int numProcedures, char **procedures, 
         int numVariables, char **variables,
         char *script);

EXTERN struct AgentId *Agent_Fork 
	(Tcl_Interp *interp, double seconds, struct MachineId *machine_id);

EXTERN int Agent_Jump
	(Tcl_Interp *interp, double seconds, struct MachineId *machine_id);

    /* save a jump image to disk and transfer a saved jump image to a server */

EXTERN int Agent_Disk
	(Tcl_Interp *interp, Tcl_Channel channel);

EXTERN struct AgentId * Agent_Transfer
	(Tcl_Interp *interp, double seconds, struct MachineId *machine_id,
	 Tcl_Channel channel);

    /* wait for meetings, events, messages and activity on arbitrary sockets */

EXTERN int Agent_Select
	(Tcl_Interp *interp, double seconds, 
	 struct AgentSelectParameters *selectParams);
 
    /* send and receive results */ 

EXTERN int Agent_Send
	(Tcl_Interp *interp, double seconds, struct AgentId *id, 
	 UINT_32 code, char *string);

EXTERN int Agent_Receive
	(Tcl_Interp *interp, double seconds, struct INCOMING_MESSAGE **message);

    /* send and receive events */

EXTERN int Agent_Event
	(Tcl_Interp *interp, double seconds, struct AgentId *id, 
	 char *tag, char *string);

EXTERN int Agent_GetEvent
	(Tcl_Interp *interp, double seconds, struct INCOMING_EVENT **event);

    /* request and accept meetings */

EXTERN int Agent_Meet
	(Tcl_Interp *interp, double seconds, AgentId *id, UINT_32 *handle);

EXTERN int Agent_AcceptAndWait
	(Tcl_Interp *interp, double seconds, UINT_32 handle);

EXTERN int Agent_Request
	(Tcl_Interp *interp, double seconds, AgentId *id, UINT_32 *handle);

EXTERN int Agent_Accept
	(Tcl_Interp *interp, double seconds, UINT_32 handle);

EXTERN int Agent_Reject
	(Tcl_Interp *interp, double seconds, UINT_32 handle);

EXTERN int Agent_GetReq
	(Tcl_Interp *interp, double seconds, struct INCOMING_MEETING **meeting);

EXTERN int Meeting_Receive
	(Tcl_Interp *interp, double seconds, UINT_32 handle, UINT_32 *code, 
	 char **string);

EXTERN int Meeting_Getfile
	(Tcl_Interp *interp, double seconds, UINT_32 handle,
	 Tcl_Channel channel, UINT_32 &bytes); 

EXTERN int Meeting_Send
	(Tcl_Interp *interp, UINT_32 handle, UINT_32 code, char *string);

EXTERN int Meeting_Sendfile
	(Tcl_Interp *interp, UINT_32 handle, Tcl_Channel channel, 
	 UINT_32 bytes);

EXTERN int Meeting_Close
	(Tcl_Interp *interp, UINT_32 handle);

    /* sleep for a certain number of seconds */

EXTERN int Agent_Sleep
	(Tcl_Interp *interp, double seconds);

EXTERN double Agent_StartTime 
	(Tcl_Interp *interp);

EXTERN double Agent_StartCpuTime 
	(Tcl_Interp *interp);

EXTERN double Agent_CurrentTime
	(Tcl_Interp *interp);

EXTERN double Agent_CurrentCpuTime
	(Tcl_Interp *interp);

EXTERN double Agent_ElapsedTime
	(Tcl_Interp *interp);

EXTERN double Agent_ElapsedCpuTime
	(Tcl_Interp *interp);

    /* send the result of script execution to the root agent */

EXTERN int Agent_SendResult
	(Tcl_Interp *interp, int retries, int maxSleep, UINT_32 code);

    /* get information about agents */

EXTERN struct ID_LIST *Agent_InfoIds
	(Tcl_Interp *interp, double seconds, struct MachineId *id);

EXTERN struct NAME_LIST *Agent_InfoNames
	(Tcl_Interp *interp, double seconds, struct MachineId *id);

EXTERN struct AGENT_INFO *Agent_Info
        (Tcl_Interp *interp, double seconds, struct AgentId *id);

EXTERN int Agent_Status
	(Tcl_Interp *interp, double seconds, int type, int provideEvent,
 	 AgentId *id);

    /* message, meeting and event masks */

EXTERN int Agent_MaskNew 
	(Tcl_Interp *interp, UINT_32 *handle);

EXTERN int Agent_MaskDelete 
	(Tcl_Interp *interp, UINT_32 handle);

EXTERN int Agent_MaskFill 
	(Tcl_Interp *interp, UINT_32 handle);

EXTERN int Agent_MaskEmpty
	(Tcl_Interp *interp, UINT_32 handle);

EXTERN int Agent_MaskAdd
	(Tcl_Interp *interp, UINT_32 handle, int count, struct AgentMaskEntry **masks);

EXTERN int Agent_MaskRemove
	(Tcl_Interp *interp, UINT_32 handle, int count, struct AgentMaskEntry **masks);

EXTERN int Agent_MaskSpecial
	(Tcl_Interp *interp, int which, UINT_32 handle);
	
EXTERN struct AgentMask *Agent_MaskContents
	(Tcl_Interp *interp, UINT_32 handle);

    /* security */

EXTERN int Agent_SetEncryption 
	(Tcl_Interp *interp, BOOLEAN state);

EXTERN int Agent_SetSignatures
	(Tcl_Interp *interp, BOOLEAN state);

    /* initialization and main routine */

EXTERN int Agent_Init 
	(Tcl_Interp *interp, const Version& version);

EXTERN void Agent_Main 
	(int argc, char **argv, Tcl_AppInitProc *appInitProc);

EXTERN void Agent_ServerMain
	(Tcl_AppInitProc *appInitProc);

    /* cryptographically secure random number generator */

EXTERN int Agent_Seed
	(Tcl_Interp *interp, UINT_32 seed);

EXTERN int Agent_Trueseed
	(Tcl_Interp *interp, int bits);

EXTERN int Agent_Trueevent
	(Tcl_Interp *interp, char c);

EXTERN int Agent_Random
	(Tcl_Interp *interp, UINT_32 *number);

    /* safe initialization of the Tcl interpreter */

EXTERN int Tcl_SafeInit
	(Tcl_Interp *interp, Tcl_Interp *masterInterp);

#endif
