/* ===========================================================================

  irisNetIPC.h    common funcs functions

=============================================================================*/
#ifndef IRIS_NET_IPC_H
#define IRIS_NET_IPC_H

#ifdef IRIS_NET_IPC_ALLOCATE
#define EXTERN
#else
#define EXTERN extern
#endif

EXTERN UINT2 lIrisnetIsAlreadyRunning;

/* globals */
/*************************
** C++ prototypes
***************************/ 

struct PollInfo 
{
  SINT2  lBusy;
  SINT2  lAvailHostnameSlot;  // 0 = Available
  UINT4  iSiteStatus;
  UINT4  iSiteType;
  char   hostname[50];
  struct ymds_time Time;      /* Poll time in UTC */
};

struct pSharedData
{
   UINT2  lExiting;
   pid_t  iParentPID;
   SINT4  numPollProcess;
   SINT4  timeBetweenUpdate;
   UINT2  lAvailEventFlagProcessSlot[MAX_NUM_PROCESS];  // 0 = Available
   struct PollInfo pollInfo[TOTAL_SITES]; 
};

class SharedMem 
{
   private :
      SINT4  iID;
      SINT4  iDataSize;
      SINT4  iEventFlagProcessNum;
      struct pSharedData *pData;
      void   createFind(SINT2 lcreate_a);
   public  :
      void   create(void);
      void   find(void);
      void   destroy(void);
      void   addHostToPollInfo(UINT4 isite_selected_a, char *hostname_a,
               UINT4 isite_type_a);
      void   deleteHostFromPollInfo(UINT4 isite_selected_a, char *hostname_a);
      void   setHostTimesForUpdate(void);
      void   setNumPollProcess(SINT4 inum_process_a);
      SINT4  getNumPollProcess(void);
      void   setTimeBetweenUpdate(SINT4 itime_a);
      UINT2  lGetNextHostToCheck(char *hostname_a, 
               UINT4 *isite_type_a, UINT4 *isite_num_a);
      char   *getHostname(UINT4 isite_num_a);
      void   setExiting(void);
      UINT2  getExiting(void);
      void   setParentPID(pid_t iparent_pid_a);
      pid_t  getParentPID(void);
      void   setHostStatusAndFreeBusy(UINT4 isite_num_a,
              UINT4 inetwork_status_a);
      UINT4  getHostStatus(UINT4 isite_num_a);
      void   freeBusy(UINT4 isite_num_a);
      SINT4  getEventFlagProcessNum(void);
      void   getAllEventFlagProcessNums(SINT4 ievent_flag_process_num[MAX_NUM_PROCESS]);


      // This is only called once in Semaphore::create routine to get
      // the correct event flag number. 
      UINT4  getEventFlagProcessNumAllocSlot(void); 
      void   freeEventFlagSlot(void); 
      //  For testing and debuging
      void  printAllHostnames(void); 
      SINT4  getID(void); 
};
EXTERN SharedMem sharedMem;

 
class Semaphore 
{
   private :
      void   createWithInit(SINT2 lsem_init_a);
   public  :
      void   createInit(void);  // Only do init by main creater
      void   create(void);
      void   destroy(void);
      void   lock(void);
      void   unlock(void);
};
EXTERN Semaphore sem;

class  EventFlag 
{
   private :
   public  :
      UINT2  set(void);
      UINT2  setAll(void);
      UINT2  clr(void);
      void   wait(void);
      void   schedule(SINT4 imilli_a);
      void   cancelSchedule(void);
};
EXTERN EventFlag ef;
 

class IrisNetStatProcess 
{
   private : 
      pid_t  iPID[MAX_NUM_PROCESS];
   public :
      void  execProcesses(void);
      void  killProcesses(void);
      pid_t getPID(UINT4 process_num_a);
      void  zeroPID(UINT4 process_num_a);

};
EXTERN IrisNetStatProcess  irisNetStatProcess;


#undef EXTERN
#endif

