//  Beta release only.
//
//  Copyright(c) Ivyware Pty Ltd 2002-9  (all rights reserved)
//               MELBOURNE, VICTORIA, AUSTRALIA, 3130
//
//  Provided no fee is charged (either by the distributer or user)
//  permission to use, copy, modify, and distribute this software and
//  its documentation for any purpose and without fee is hereby granted,
//  provided that the above copyright notice appear in all copies and
//  that both that copyright notice and this permission notice appear
//  in supporting documentation.
//
//  No waranty or suitablity for purpose is implied.
//
//
//  Global P2Peer datatypes, definitions and prototypes
//

#pragma once
#include "P2PmsgKERNEL.h"

//
//  P2Peer data types
typedef LPCTSTR        P2PaddrSTR;     // P2PmsgHub address string
typedef LPCTSTR        P2PaddrTemplateSTR;
typedef LPCTSTR        P2PadomSTR;     // P2Peer domain string
typedef DWORD          P2PmsgHubID;    // P2PmsgHub identification
typedef DWORD          P2PumpID;       // Pump  identification
typedef DWORD          P2PconID;       // P2PeerCon identification
typedef DWORD          P2PexpumpID;    // P2Pexplorer pump identification
typedef unsigned int   P2PsigID;       // OVERLAPPED IO signal
typedef unsigned int   P2PeventID;     // Event identification
typedef __time64_t     P2Pmsecs_t;     // Time in milliseconds
typedef UINT           PITimerID;      // Programmable interval timer ID
typedef BOOL           mapRESULT;      // Map result
typedef const TCHAR *  P2PmsgID;       // P2PeerMsg identification
typedef unsigned int   P2PsysID;       // P2PeerSys identification
typedef unsigned short P2Pmsg_t;       // P2PeerMsg type
typedef unsigned short P2Psize_t;      // P2PeerMsg size
typedef unsigned int   P2PmsgCN;       // Control notification
typedef short          P2Pri_t;        // P2PeerMsg priority
typedef void *         P2PmsgHANDLE;   // P2Pmsg handle
typedef unsigned char  P2Pmsgnn_t;     // P2Pmsg 08, 16, 32, 64 addressing

//
//  Limits
const UINT MAX_P2PmsgHub = 256;        // Maximum P2PmsgHub's within application
                                       // NOTES: P2Pmsg networks have no limit
const UINT MAX_P2PmsgPump= 256;        // Maximum P2PmsgPump's within P2PmsgHub
                                       // NOTES: P2Pmsg networks have no limit

#define P2Pmsg_NULL_DBLE -1.7976931348623158e+308
#define P2PmsgIsNULL_DBLE(dValue) (dValue<=P2Pmsg_NULL_DBLE)
                                       // Psuedo double NULL value

//
//  P2Pmsg address object
//  NOTES: Supports Virtual P2Peer Network addresses of the form
//         [VNetname:]RootHubname.Hubname[i]. . .Hubname etc
class P2PmsgKERNEL_EXT P2Paddr
{
    // Constructors and destructors
    public:
        P2Paddr ( );
        P2Paddr ( const P2Paddr& rhs );
        P2Paddr ( P2PaddrSTR strP2Paddr );
        P2Paddr ( P2PaddrSTR strP2PaddrHub
                , const char *pszFormatCon, ... );
        P2Paddr ( GUID *pGUID );
       ~P2Paddr ( );

    // Overloaded operators
    public:
      P2Paddr&
        operator  = ( const P2Paddr& rhs );
      P2Paddr&
        operator  = ( P2PaddrSTR rhs );
      bool
        operator != ( const P2Paddr& rhs ) const;
      bool
        operator != ( P2PaddrSTR rhs ) const;
      bool
        operator == ( P2PaddrSTR rhs ) const;
      bool
        operator == ( const P2Paddr& rhs ) const;

        operator P2PaddrSTR ( ) const;

    // Operations
    public:
      void
        Empty ( );

    // Routing
    public:
      bool
        IsChild ( P2PaddrSTR strP2Paddr ) const;
      bool
        IsRable ( P2PaddrSTR strP2Paddr ) const;
      bool
        IsMapped( P2PaddrSTR strP2Paddr ) const;

    // Troubleshooting
    public:
      //virtual void
      //  AssertValid ( ) const;
      //virtual bool
      //  IsValid ( ) const;

    // Properties
    public:
      P2Psize_t
        Sizeof ( ) const;
      bool
        IsNull ( ) const;
      LPCTSTR
        GetVNet ( );
      //LPCTSTR
      //  GetPath ( );
      LPCTSTR
        c_hopname ( int nHop ) const;
      LPCTSTR
        c_name ( ) const;
      LPCTSTR
        c_str ( ) const;
      BOOL
        IsEmpty ( );

    // Attributes
    private:
      CString  m_sP2Paddr;
      CString  m_sVNetname;
      CString  m_sVNetaddr;
      CString  m_sHubname;
};
typedef CList<P2Paddr*, P2Paddr*> CListP2Paddr;
typedef const P2Paddr CP2Paddress;
#define       strP2PaddrNULL _T("\0")

//
//  P2Padomain
//  NOTES: Supports Virtual P2Peer Network addresses of the form
//         [VNetname:]RootHubname.Hubname[i]. . .Hubname etc
#define P2Padom P2Padomain
class P2PmsgKERNEL_EXT P2Padomain
{
    // Constructors
    public:
        P2Padomain ( );
        P2Padomain ( P2PadomSTR strP2Padomain );
       ~P2Padomain ( );

      P2PaddrSTR
        Expand  ( P2PaddrSTR strP2Paddr, bool bSkip );

    // Overloaded operators
    public:
	  P2Padomain&
	    operator = ( const P2Padomain& rhs );
      P2Padomain&
        operator = ( P2PadomSTR rhs );

        operator P2PadomSTR ( ) const;

    // Activities
    public:
      bool
        GetNextP2Paddr ( P2PadomSTR strDomain
                       , P2Paddr& oP2PaddrNext, long& nState ) const;

    // Routing
    public:
      bool
        IsMapped ( P2PaddrSTR strP2Paddr ) const;

    // Troubleshooting
    public:
      //virtual void
      //  AssertValid ( ) const;
      //virtual bool
      //  IsValid ( ) const;

    // Properties
    public:
      P2Psize_t
        Sizeof ( ) const;
      bool
        IsNull ( ) const;
      LPCTSTR
        c_str ( ) const;

    // Attributes
    private:
      CString      m_csDomain;
      mutable
      P2Paddr      m_oP2PaddrWork;
      POSITION     m_oPos;
      CList<CString*, CString*>
                   m_oCListDomains;
      CListP2Paddr m_oCListP2Paddr;
      bool         m_bWildcard;
};
typedef const P2Padomain CP2Padomain;

//
//  P2PaddrTemplate
//  NOTES: Supports Virtual P2Peer Network addresses of the form
//         [VNetname:]RootHubname.Hubname[i]. . .Hubname etc
//         that may or may not contain wildcards
/*class P2PaddrTemplate
{
    // Constructors
    public:
        P2PaddrTemplate ( P2PaddrTemplateSTR strP2PaddrTemplate );
       ~P2PaddrTemplate ( );

    // Routing
    public:
      bool
        IsMapped ( P2PaddrSTR strP2Paddr ) const;

    // Troubleshooting
    public:
      //virtual void
      //  AssertValid ( ) const;
      //virtual bool
      //  IsValid ( ) const;

    // Properties
    public:
      LPCTSTR
        c_str ( ) const;

    // Attributes
    private:
      CString  m_sP2PaddrTemplate;
};
typedef const P2PaddrTemplate CP2PaddrTemplate ;*/

//
//  Prototypes
//
P2PmsgKERNEL_EXT DWORD
GetHostID ( );

//
//  P2Pevent definitions
//  NOTES: Assigned to P2Pmsg_Exceptions thrown by the framework
const P2PeventID P2Pevent_OFFSET = 0x01000000;

const P2PeventID                       // Undeliverable P2PeerMsg
P2Pevent_UNDELIVERABLE = P2Pevent_OFFSET+0x01;
const P2PeventID                       // Unknown to destination
P2Pevent_UNKNOWN       = P2Pevent_OFFSET+0x02;
const P2PeventID                       // Invalid P2PeerMsg version
P2Pevent_VERSION       = P2Pevent_OFFSET+0x03;
const P2PeventID                       // P2Pump queue full
P2Pevent_QUEFULL       = P2Pevent_OFFSET+0x04;

