Monitor Class

Monitor class is designed to check the Channel status (i.e. message availablity for receiving) of a given set of Channels within a user specified time bound. It is defined in a system header file Channel.h

Private Data:

         number of channels that are ready to receive data.           the array of index of the channels that are ready to receive data.

Public Functions:

Example Usage
 

int SelectReadReadyChannels(AsynOneToOneChannels *channels[], int num_channels, int milliseconds)
   DESCRIPTIONS:
       (1) This function keeps checking the an array of asynchronous one-to-one channels (given by an array of
             AsynOneToOneChannels pointers) within the time bound milliseconds. Once it finds that a channel has data available
             to receive, it will increase num_ready by 1 and put the index of this channel into readychannels.
       (2) Upon return, the user can use the function ReadyChannelsIndex() to see which channels are ready to receive
             message, and the function NumReadyChannels() to get the number of channels that are ready.
   PARAMETERS:
        channels:            an array of asynchronous one-to-one channel pointers, some of them can be null
        num_channels:   the size of this array
        milliseconds:       the time bound that the user specified for polling, in milliseconds.
                                       MTU_INFINITE    --- polls for unlimited amount of time, until it finds one channel in the array is                                                                                     ready to receive message.
                                       MTU_ZERO  --- it will return immediately.
  RETURN VALUE:
            If it reaches the time bound or at least one channel is ready for message receiving for the case of MTU_INFINITE, it
            will return the number of channels that are ready to receive message, and the array of readychannels is set up
            corresponingly. If a error occurs, MTU_SOCKERROR (-1) will be returned, and readychannels will be set to null.
  NOTE:
       (1) use the function ReadyChannelsIndex() to get the array of ready channel index.
       (2) a second call of this function will automatically clear the data (structures) set up by the first call.
 

int SelectReadReadyChannels(AsynOneToOneChannels channels[], int num_channels, int milliseconds)
   DESCRIPTIONS:
       (1) This function keeps checking the an array of asynchronous one-to-one channels (given by an array of
             AsynOneToOneChannels channels) within the time bound milliseconds. Once it finds that a channel has data
              available to receive, it will increase num_ready by 1 and put the index of this channel into readychannels.
       (2) Upon return, the user can use the function ReadyChannelsIndex() to see which channels are ready to receive
             message,and the function NumReadyChannels() to get the number of channels that are ready.
   PARAMETERS:
        channels:            an array of asynchronous one-to-one channels.
        num_channels:   the size of this array
        milliseconds:       the time bound that the user specified for polling, in milliseconds.
                                       MTU_INFINITE    --- polls for unlimited amount of time, until it finds one channel in the array is                                                                                     ready to receive message.
                                       MTU_ZERO  --- it will return immediately.
  RETURN VALUE:
        If it reaches the time bound or at least one channel is ready for message receiving for the case of MTU_INFINITE, it will
        return the number of channels that are ready to receive message, and the array of readychannels is set up corresponingly.
        If a error occurs, MTU_SOCKERROR (-1) will be returned, and readychannels will be set to null.
  NOTE:
       (1) use the function ReadyChannelsIndex() to get the array of ready channel index.
       (2) a second call of this function will automatically clear the data (structures) set up by the first call.
 
 

int SelectReadReadyChannels(SynOneToOneChannels *channels[], int num_channels, int milliseconds)
   DESCRIPTIONS:
       (1) This function keeps checking the an array of synchronous one-to-one channels (given by an array of
             SynOneToOneChannels pointers) within the time bound milliseconds. Once it finds that a channel has data available
             to receive, it will increase num_ready by 1 and put the index of this channel into readychannels.
       (2) Upon return, the user can use the function ReadyChannelsIndex() to see which channels are ready to receive
             message,and the function NumReadyChannels() to get the number of channels that are ready.
   PARAMETERS:
        channels:            an array of synchronous one-to-one channel pointers, some of them can be null
        num_channels:   the size of this array
        milliseconds:       the time bound that the user specified for polling, in milliseconds.
                                       MTU_INFINITE    --- polls for unlimited amount of time, until it finds one channel in the array is                                                                                     ready to receive message.
                                       MTU_ZERO  --- it will return immediately.
  RETURN VALUE:
        If it reaches the time bound or at least one channel is ready for message receiving for the case of MTU_INFINITE, it will
        return the number of channels that are ready to receive message, and the array of readychannels is set up corresponingly.         If a error occurs, MTU_SOCKERROR (-1) will be returned, and readychannels will be set to null.
NOTE:
       (1) use the function ReadyChannelsIndex() to get the array of ready channel index.
       (2) a second call of this function will automatically clear the data (structures) set up by the first call.
 
 

int SelectReadReadyChannels(SynOneToOneChannels channels[], int num_channels, int milliseconds)
   DESCRIPTIONS:
       (1) This function keeps checking the an array of synchronous one-to-one channels (given by an array of
             AsynOneToOneChannels) within the time bound milliseconds. Once it finds that a channel has data available to
             receive, it will increase num_ready by 1 and put the index of this channel into readychannels.
       (2) Upon return, the user can use the function ReadyChannelsIndex() to see which channels are ready to receive
             message, and the function NumReadyChannels() to get the number of channels that are ready.
   PARAMETERS:
        channels:            an array of synchronous one-to-one channels.
        num_channels:   the size of this array
        milliseconds:       the time bound that the user specified for polling, in milliseconds.
                                       MTU_INFINITE    --- polls for unlimited amount of time, until it finds one channel in the array is                                                                                     ready to receive message.
                                       MTU_ZERO  --- it will return immediately.
  RETURN VALUE:
        If it reaches the time bound or at least one channel is ready for message receiving for the case of MTU_INFINITE, it will        return the number of channels that are ready to receive message, and the array of readychannels is set up corresponingly.
        If a error occurs, MTU_SOCKERROR (-1) will be returned, and readychannels will be set to null.
   NOTE:
       (1) use the function ReadyChannelsIndex() to get the array of ready channel index.
       (2) a second call of this function will automatically clear the data (structures) set up by the first call.
 

bool ChannelReadReady(AsynOneToOneChannel channel, int milliseconds)
  DESCRIPTIONS:
       (1) This function keeps checking the asynchronous one-to-one channel channel within the time bound milliseconds to
             see if this channel has data available for receiving.

   PARAMETERS:
        channel:            an asynchronous one-to-one channel.
        milliseconds:       the time bound that the user specified for polling, in milliseconds.
                                       MTU_INFINITE    --- polls for unlimited amount of time, until it finds one channel in the array is                                                                                     ready to receive message.
                                       MTU_ZERO  --- it will return immediately.
  RETURN VALUE:
        If, within the time bound, the channel is ready for message receiving,  it returns true; otherwise false is returned.

bool ChannelReadReady(SynOneToOneChannel channel, int milliseconds)
  DESCRIPTIONS:
       (1) This function keeps checking the synchronous one-to-one channel channel within the time bound milliseconds to
             see if this channel has data available for receiving.

   PARAMETERS:
        channel:              a synchronous one-to-one channel.
        milliseconds:       the time bound that the user specified for polling, in milliseconds.
                                       MTU_INFINITE    --- polls for unlimited amount of time, until it finds one channel in the array is                                                                                     ready to receive message.
                                       MTU_ZERO  --- it will return immediately.
  RETURN VALUE:
        If, within the time bound, the channel is ready for message receiving,  it returns true; otherwise false is returned.
 

int NumReadyChannels()
DESCRIPTIONS:
       (1) This function is used to return the number of  channels ready to receive message.
       (2)  Must be used after calling one the following functions:
             int SelectReadReadyChannels(AsynOneToOneChannels *channels[], int num_channels, int milliseconds)
             int SelectReadReadyChannels(AsynOneToOneChannels channels[], int num_channels, int milliseconds)
             int SelectReadReadyChannels(SynOneToOneChannels *channels[], int num_channels, int milliseconds)
             int SelectReadReadyChannels(SynOneToOneChannels channels[], int num_channels, int milliseconds)

   PARAMETERS:
            NONE
 
  RETURN VALUE:
        Upon successful, the number of ready channels is returned. Otherwise, MTU_SOCKERROR (-1) is returned.
 

int *ReadyChannelsIndex()
DESCRIPTIONS:
       (1) This function is used to return a integer pointer which points to an array of the index of the channels that are currently
             ready to receive message.
       (2)  Must be used after calling one the following functions:
             int SelectReadReadyChannels(AsynOneToOneChannels *channels[], int num_channels, int milliseconds)
             int SelectReadReadyChannels(AsynOneToOneChannels channels[], int num_channels, int milliseconds)
             int SelectReadReadyChannels(SynOneToOneChannels *channels[], int num_channels, int milliseconds)
             int SelectReadReadyChannels(SynOneToOneChannels channels[], int num_channels, int milliseconds)

   PARAMETERS:
            NONE
 
  RETURN VALUE:
        Upon successful, it returns a integer pointer which points to an array of the index of the channels that are currently
          ready to receive message.
 
 

Example

#define N 20
        .
int myId, numProcs;
AsynOneToOneChannel *channels[N];
Monitor channelMonitor;
        .
        .

// building channels
for(int i=0; i<numProcs; i++)
  if( i != myId)
         channels[i] = new AsynOneToOneChannel(i, myId);   //build an asynchronous with each of the other process
   else
         channels[i] = NULL;
        .
        .
        .
//polling for ready channels
int how_many = channelMonitor.SelectReadReadyChannels(channels, numProcs, MTU_INFINITE);

// get ready channels' index array
int *who = channelMonitor.ReadChannelsIndex();

// receive message from each ready channel
for(int j = 0; j<how_many; j++)
    channels[ who[j]]->Receive(recvbuf, 1024);
         .
         .
         .