namespace EpicsBld
{
  
namespace ConfigurationMulticast
{
  
extern "C"
{
  extern const unsigned int uDefaultAddr; /// multicast address
  extern const unsigned int uDefaultPort;
  extern const unsigned int uDefaultMaxDataSize; /// in bytes
  extern const unsigned char ucDefaultTTL; /// minimum: 1 + (# of routers in the middle)
}

};

/**
 * Abastract Interface of Bld Multicast Client 
 * 
 * The interface class for providing Bld Mutlicast Client functions.
 *
 * Design Issue:
 * 1. The value semantics are disabled. 
 */
class BldInterface
{
public:
  /**
   * Send raw data out to the Bld Server
   *
   * @param iSizeData  size of data (in bytes)
   * @param pData      pointer to the data buffer (char[] buffer)
   * @return  0 if successful,  otherwise the "errno" code (see <errno.h>)
   */
  virtual int sendRawData(int iSizeData, const char* pData) = 0;
  
  virtual ~BldInterface() {} /// polymorphism support
protected:  
  BldInterface() {} /// To be called from implementation class
private:
  ///  Disable value semantics. No definitions (function bodies).
  BldInterface(const BldInterface&);
  BldInterface& operator=(const BldInterface&);
};

/**
 * Factory class of Bld Multicast Client 
 *
 * Design Issue:
 * 1. Object semantics are disabled. Only static utility functions are provided.
 * 2. Later if more factorie classes are needed, this class can have public 
 *    constructor, virtual destructor and virtual member functions.
 */
class BldFactory
{
public:
  /**
   * Create a Bld Client object
   *
   * @param uAddr     mutlicast address. Usually with 239.0.0.1 ~ 239.255.255.255
   * @param uPort     UDP port
   * @param uMaxDataSize  Maximum Bld data size. Better to be less than MTU.
   * @param ucTTL     TTL value in UDP packet. Ideal value is 1 + (# of middle routers)
   * @param sInteraceIp Specify the NIC by IP address (in c string format)
   * @return        The created Bld Client object
   */
  static BldInterface* createBld(unsigned uAddr, 
    unsigned uPort, unsigned int uMaxDataSize, unsigned char ucTTL = 32, 
    char* sInteraceIp = 0);
    
  /**
   * Create a Bld Client object
   *
   * Overloaded version
   * @param uInteraceIp Specify the NIC by IP address (in unsigned int format)
   */
  static BldInterface* createBld(unsigned uAddr, 
    unsigned uPort, unsigned int uMaxDataSize, unsigned char ucTTL = 32, 
    unsigned int uInteraceIp = 0);
private:
  /// Disable object instantiation (No object semantics).
  BldFactory();
};

} // namespace EpicsBld

extern "C"
{

/**
 * Bld Client basic test function
 *
 * Will continuously send out the multicast packets to a default
 * address with default values. Need to be stopped manually from keyboard
 * by pressing Ctrl+C
 *
 * This fucntion is only used for quick testing of Bld Client, such as 
 * running from CExp Command Line.
 */
int BldTestSendBasic(int iDataSeed);

/**
 * Bld Client test function with IP interface selection
 *
 * Similar to BldTestSendBasic(), but with the argument (sInterfaceIp)
 * to specify the IP interface for sending multicast.
 *
 * Will continuously send out the multicast packets to a default
 * address with default values. Need to be stopped manually from keyboard
 * by pressing Ctrl+C
 *
 * This fucntion is only used for quick testing of Bld , such as 
 * running from CExp Command Line.
 */
int BldTestSendInterface(int iDataSeed, char* sInterfaceIp=0, int iNumPackets=0);


/* 
 * The following functions provide C wrappers for accesing EpicsBld::BldInterface
 * and EpicsBld::BldFactory
 */
 
/**
 * Init function: Use EpicsBld::BldFactory to construct the Bld
 * and save the pointer in (*ppVoidBld)
 */
int BldInitByInterfaceName(unsigned uAddr, unsigned uPort, 
  unsigned int uMaxDataSize, unsigned char ucTTL, char* sInterfaceIp, 
  void** ppVoidBld);
int BldInitByInterfaceAddress(unsigned uAddr, unsigned uPort, 
  unsigned int uMaxDataSize, unsigned char ucTTL, unsigned int uInterfaceIp, 
  void** ppVoidBld);  

/**
 * Release function: Call C++ delete operator to delete the Bld
 */
int BldRelease(void* pVoidBld); 

/**
 * Call the Send function defined in EpicsBld::BldInterface 
 */
int BldSendRawData(void* pVoidBld, int iSizeData, char* pData);

} // extern "C"


