//-------------------------------------------------------------------
// ECLCommNotify class
//
// This sample demonstrates the use of:
//
// ECLCommNotify::NotifyEvent
// ECLCommNotify::NotifyError
// ECLCommNotify::NotifyStop
// ECLConnection::RegisterCommEvent
// ECLConnection::UnregisterCommEvent
//-------------------------------------------------------------------
 
 
 //...................................................................
// Define a class derived from ECLCommNotify
//...................................................................
class MyCommNotify: public ECLCommNotify
{
public:
  // Define my own constructor to store instance data
  MyCommNotify(HANDLE DataHandle);
 
  // We have to implement this function
  void	NotifyEvent(ECLConnection *ConnObj, BOOL Connected);
 
  // We choose to implement this function
  void NotifyStop (ECLConnection *ConnObj, int Reason);
 
  // We will take the default behaviour for this so we
  // don't implement it in our class:
  // void NotifyError (ECLConnection *ConnObj, ECLErr ErrObject);
 
private:
  // We will store our application data handle here
  HANDLE MyDataH;
};
 
 
//...................................................................
void MyCommNotify::NotifyEvent(ECLConnection *ConnObj,
                           BOOL Connected)
//
// This function is called whenever the communications link
// with the host connects or disconnects.
//
// For this example, we will just write a message.  Note that we
// have access the the MyDataH handle which could have application
// instance data if we needed it here.
//
// The ConnObj pointer is to the ECLConnection object upon which
// this event was registered.
//...................................................................
{
  if (Connected)
    printf("Connection %c is now connected.\n", ConnObj->GetName());
  else
    printf("Connection %c is now disconnected.\n", ConnObj->GetName());
 
  return;
}
 
 //...................................................................
MyCommNotify::MyCommNotify(HANDLE DataHandle)   // Constructor
//...................................................................
{
  MyDataH = DataHandle;  // Save data handle for later use
}
 
//...................................................................
void MyCommNotify::NotifyStop(ECLConnection *ConnObj,
                         int Reason)
//...................................................................
{
  // When notification ends, display message
  printf("Comm link monitoring for %c stopped.\n", ConnObj->GetName());
}
 
 //...................................................................
// Create the class and start notification on connection 'A'.
//...................................................................
void Sample30() {
 
ECLConnection *Conn;    // Ptr to connection object
MyCommNotify *Event;    // Ptr to my event handling object
HANDLE InstData;	    	// Handle to application data block (for example)
 
try {
  Conn = new ECLConnection('A');         // Create connection obj
  Event = new MyCommNotify(InstData);    // Create event handler
 
  Conn->RegisterCommEvent(Event);        // Register for comm events
 
  // At this point, any comm link event will cause the
  // MyCommEvent::NotifyEvent() function to execute.  For
  // this sample, we put this thread to sleep during this
  // time.
 
  printf("Monitoring comm link on 'A' for 60 seconds...\n");
  Sleep(60000);
 
 
  // Now stop event generation.  This will cause the NotifyStop
  // member to be called.
  Conn->UnregisterCommEvent(Event);
 
  delete Event;  // Don't delete until after unregister!
  delete Conn;
}
catch (ECLErr Err) {
  printf("ECL Error: %s\n", Err.GetMsgText());
}
 
} // end sample