//  Beta release only.
//
//  Copyright(c) Ivyware Pty Ltd 2001-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.
//
//
//  P2PeerHub definitions and prototypes
//  NOTES: Within a virtual P2Peer network P2PeerMsg's are only exchanged
//         between objects derived from P2PeerHub's.  However, such
//         P2PeerMsg's may be routed through a heirarchy of P2PeerTarget
//         derived objects registered with a particular P2PeerHub
//         
#pragma once

#include "P2PeerTarget.h"
#include "P2PeerExplorer.h"

typedef void (P2PeerHub::*RUN_HUB)(void);
#define RUN_HUB_cast(method) ((RUN_HUB)(RUN_HUB) \
		(static_cast<void (P2P_MSG_CALL P2PeerHub::*)(void)>(method)))

//
//  P2PeerHub base class
//  NOTES: Manages the routing of P2PeerMsg's between hubs and implements
//         the P2PeerSys_MAP, P2PeerCon_MAP and P2PeerMsg_MAP's
//
class P2PmsgKERNEL_EXT P2PeerHub : public P2PeerTarget
{
      void
        RenderHubSafe();

    // Constructors and destructor
    public:
        P2PeerHub ( P2PaddrSTR strP2PaddrHub );
      virtual
       ~P2PeerHub ( );

    // P2PmsgHub management
    // NOTES: Hub's exist in the context of the thread under which 
    //        they are created
    public:
      virtual HANDLE
        SpawnHub ( LPTHREAD_START_ROUTINE pfnHubProc = 0
                 , RUN_HUB  pfnRunHub = 0 );
      virtual BOOL
        CreateHub( P2PaddrSTR strP2PaddrHub
                 , UINT nPumpsMax = 1 );
      virtual void
        PauseHub ( );
      virtual void
        WakeupHub ( );
      virtual void
        CloseHub ( );
      static DWORD WINAPI
        ProcHub  ( void *pvData );
      virtual void
        RunHub   ( );
      virtual void
        PostDestroyHub ( );
        
    // P2PeerSys management
    public:
      UINT
        PostP2PeerSys ( P2PsysID nP2PsysID, WPARAM wParam, LPARAM lParam );

    // P2PeerCon management
    public:
      BOOL
        PostP2PeerCon ( P2PeerCon *pCon, P2PumpID nPumpID = 0 );
      BOOL
        ConExists  ( P2PaddrSTR strP2Paddress );
      bool
        ConQuery   ( P2PaddrSTR strP2Paddress, SafeP2PeerCon& rSafeCon );
      BOOL
        ConSignal  ( P2PaddrSTR strP2Paddress
                   , P2PsigID nSigConID
                   , void *pvData = 0, int iDataSize = 0 );
      BOOL
        ConSignal  ( P2PconID nConID
                   , P2PsigID nSigConID
                   , void *pvData = 0, int iDataSize = 0 );

    // P2PeerMsg management
    public:
      msgRESULT
        RouteP2PeerMsg ( P2PeerMsg *pMsg );
      P2PeerMsg*
        PostP2PeerMsg  ( P2PeerMsg *pMsg );

    // P2PeerExpump management
    public:
      P2PeerTarget*
        PostP2PeerExpump ( P2PeerTarget *pP2PeerExpump );
      BOOL
        DropP2PeerExpump ( );

    // Troubleshooting
    public:
      virtual void
        AssertValid ( ) const;
      virtual P3PmsgNode
        Serialise ( LPCTSTR lpszVar, bool bDsc = false );

    // Properties
    public:
      virtual const P2Paddr&
        SetP2PaddrHub ( P2PaddrSTR strP2PaddrHub );
      const P2Paddr&
        GetP2PaddrHub ( );
      P2PeerHub*
        GetP2PeerHub  ( );
      P2PmsgHubID
        GetHubID ( ) const;
      P2PeerTarget*
        GetP2PeerExpump ( ) const;

    // Attributes
    public:
      P2PmsgHubID       m_nHubID;
      P2Paddr           m_oP2PaddrHub;
      CRITICAL_SECTION  m_oCSectionHub;
	    P2PeerTarget     *m_pP2PeerExpump;
    
    // P2PeerSys_MAP handlers
    // NOTES: Placemarker for map
    protected:
    DECLARE_P2PeerSys_MAP()

    // P2PeerCon_MAP handlers
    // NOTES: Placemarker for map.  Default set of handlers reside
    //        in P2PeerTarget.
    protected:
    DECLARE_P2PeerCon_MAP()

    // P2PeerMsg_MAP handlers
    // NOTES: Placemarker for map.  Default set of handlers reside
    //        in P2PeerTarget.
    protected:
	  DECLARE_P2PeerMsg_MAP()
	  //virtual msgRESULT
	  //  On_P2PexpumpCtrl( P2PeerMsg *pMsg );
      virtual msgRESULT
        On_P2PeerBCast  ( P2PeerMsg *pMsg );
      virtual msgRESULT
        On_P2PeerUCast  ( P2PeerMsg *pMsg );
      virtual msgRESULT
        On_P2PeerError  ( P2PeerMsg *pMsg );
};

//
//  P2Psys notification messages for P2PeerSys_MAP handlers
//  NOTES: Definition sequence is designed to trap use of the
//         message number elsewhere
//       : Use windows standard windows conventions etc.  Prudent
//         to be unique in Windows context
const DWORD    WM_APP_0x00b1           = WM_APP + 0x00b1;
const P2PsysID P2Psys_Win32ServiceCtrl = WM_APP_0x00b1;