//  Beta release only.
//
//  Copyright(c) Ivyware Pty Ltd 2007-10  (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.
//
//
//  P2Peer message definitions and prototypes
//
#pragma once

#include "wtypes.h"
#include "P2Peer.h"
#include "P2PTL.h"
#include "P2Pmsg.h"

#define  UINT08 UCHAR

///////////////////////////////////////////////////////////////////////
//  P2PmsgMgr trigger containers and definitions
//  NOTES:
//

const DWORD P2PmsgTrig_INSERT = (1<<0);
const DWORD P2PmsgTrig_UPDATE = (1<<1);
const DWORD P2PmsgTrig_DELETE = (1<<2);
const DWORD P2PmsgTrig_ALL    = (P2PmsgTrig_INSERT|P2PmsgTrig_UPDATE|P2PmsgTrig_DELETE);

//
//  P2PmsgTrigger notification messages
//  NOTES: Definition sequence is designed to trap use of the
//         message number elsewhere
//       : Default set, re-define within P2PmsgMgr to resolve conflicts
const DWORD WM_APP_0x00a1        = WM_APP + 0x00a1;
const DWORD WM_P2Pmsg_TrigUPDATE = WM_APP_0x00a1;
const DWORD WM_APP_0x00a2        = WM_APP + 0x00a2;
const DWORD WM_P2Pmsg_TrigINSERT = WM_APP_0x00a2;
const DWORD WM_APP_0x00a3        = WM_APP + 0x00a3;
const DWORD WM_P2Pmsg_TrigDELETE = WM_APP_0x00a3;
const DWORD WM_APP_0x00a4        = WM_APP + 0x00a4;
const DWORD WM_P2Pmsg_Properties = WM_APP_0x00a4;

///////////////////////////////////////////////////////////////////////
//  P2PmsgMgr class
//  NOTES: Manages chunk of memory holding a self contained contiguous
//         sequence of P3Pmsg<Field, Node, List, ...> objects held
//         beneath a transparent P3PmsgNode container
class P2PmsgKERNEL_EXT P2PmsgMgr : public P3PmsgNode
{
      void
        RenderThisSafe ( );

    // Constructors and destructor
    public:
        P2PmsgMgr ( );

        P2PmsgMgr ( UCHAR uAddrNN, UINT nSizeInitial, UINT nSizeMax );

        P2PmsgMgr ( const P2PmsgMgr& rhs );

        P2PmsgMgr ( LPCTSTR lpszFilename );

      virtual
       ~P2PmsgMgr ( );

    // Initialisation and serialisation
    public:
      P2PmsgMgr&
        Init ( LPCTSTR lpszName, const P3PmsgData& oNodeData );
      BOOL
        Load ( LPCTSTR lpszFilename );
      BOOL
        Save ( LPCTSTR lpszFilename = 0 );
      void
        Nullify ( );

    // Factories
    public:
      static P2PmsgMgr*
        Factory ( LPCTSTR lpszFilename
                , UCHAR uAddrNN, UINT nSizeInitial, UINT nSizeMax );
      static P2PmsgMgr*
        Factory ( LPCTSTR lpszFilename );

    // Operators
    public:
      P2PmsgMgr&
        operator  = ( const P2PmsgMgr& rhs );

    // Addressing
    public:
      P2PmsgNodeHdl
        P2Pos2NodeHdl ( P2Pos pos );
      P3PmsgField
        P2Pos2Field ( P2Pos pos );
      P3PmsgNode
        P2Pos2Node ( P2Pos pos );
      P3PmsgAttr
        P2Pos2Attr ( P2Pos pos );

    // Triggers
    // NOTES: External manual activation
    public:
      UINT
        CreateTrigger ( UINT dwTrigMask, P2Pos posP2Pobject
                      , HWND hWnd, WPARAM wParam );
      UINT
        CreateTrigger ( UINT dwTrigMask, P3PmsgNode& oNode
                      , HWND hWnd, WPARAM wParam );
      UINT
        CreateTrigger ( UINT dwTrigMask, P3PmsgField& oField
                      , HWND hWnd, WPARAM wParam );
      UINT
        DropTrigger ( P2Pos posP2Pobject, HWND hWnd
                    , DWORD dwTrigMask = ~0, bool bRecurse = true );
      DWORD
        SelectTrigger ( P2Pos posP2Pobject, HWND hWnd );
      UINT
        TriggerINSERT ( P3PmsgNode& oNodeParent
                      , P3PmsgNode& oNodeChild );
      UINT
        TriggerUPDATE ( P3PmsgNode& oChild );
      UINT
        TriggerDELETE ( P3PmsgNode& oNode );
      P2PmsgListHdl
        GetTriggerList ( P3PmsgNode& oNode );
      P2PmsgListHdl
        GetTriggerList ( P3PmsgField& oField );
      P2PmsgListHdl
        MakeTriggerList ( P2Pos posP2Pobject );
      P2PmsgListHdl
        MakeTriggerList ( P3PmsgNode& oNode );
      P2PmsgListHdl
        MakeTriggerList ( P3PmsgField& oField );
      bool
        TriggerListExists ( P3PmsgNode& oNode );
      bool
        TriggerListExists ( P3PmsgField& oField );
      P2PmsgListHdl
        GetP2PmsgListHdlTrig ( P2Pos posP2Pobject );

    // Troubleshooting
    public:
      virtual void
        AssertValid ( ) const;
      virtual void
        Print ( FILE *fd, int nDepthOS, int nDepthOSinc = 1  );

    // Properties
    public:
      LPCTSTR
        GetFilename ( );
      virtual bool
        IsDirty ( );
      bool
        IsNode ( P2Pos posP2Pobject );
      bool
        IsField ( P2Pos posP2Pobject );

    // Attributes
    protected:
        P2PmsgHANDLE     m_hMgr;
        HANDLE           m_hFile;
        CString          m_strFilename;
        GUID             m_oGUID;
    public:
        CRITICAL_SECTION m_oCSectionMgr;
        UINT             m_uiWM_APP_TrigINSERT;
        UINT             m_uiWM_APP_TrigUPDATE;
        UINT             m_uiWM_APP_TrigDELETE;
};
typedef P2PSafePtr<P2PmsgMgr> P2PmsgMgrSP;

///////////////////////////////////////////////////////////////////////
//  P2PmsgMgr helpers
//  NOTES: Standard extensions and activities
P2PmsgKERNEL_EXT BOOL
P2PmsgMgr_IsValid ( LPCTSTR lpszFilename );
