//
//  Beta release only.
//
//  Copyright(c) Ivyware Pty Ltd 2013  (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.
//
//
//  SQLmapiHub definitions and prototypes
//  NOTES: Extracts emails using ODBC API from SQL database and sends
//         using MAPI.  Specialisation etc isolated in this P2PmsgHub
//       : Refer P2PmsgODBC for SQL implementation.
//       : Refer P2PmsgMAPI for MAPI implementation
//                

#pragma once
#include "P2PeerHub.h"

//
//  ODBC and MAPI Operational states
//  NOTES: Used to summarise P2PeerCon connection behaviour and
//         states
const DWORD State_ODBConnected   = (1<<0);
const DWORD State_MAPInitialized = (1<<1);
const DWORD State_MAPIConnected  = (1<<2);

const DWORD State_Operational    = (State_ODBConnected|State_MAPInitialized|State_MAPIConnected);

//
//   Single SQLmapiHub implementation class
//   NOTES: Expectation would be for container of implementation logic
class SQLmapiHub : public P2PeerHub
{
      void
        RenderThisSafe();

    // Constructors and destructor
    public:
        SQLmapiHub ( P2PaddrSTR strP2PaddrThis );
      virtual
       ~SQLmapiHub ( );
       
    // P2PmsgHub management
    // NOTES: Specialise default implementation
    public:
      virtual void
        PauseHub ( );
      virtual void
        WakeupHub ( );

    // Operations
    protected:
      void
        SelectNextEmail ( );
      void
        DisconnectMAPI ( );
      void
        DisconnectODBC ( );

    // Properties
    protected:
      LPCTSTR
        GetP2PaddrMAPI ( );
      LPCTSTR
        GetP2PaddrODBC ( );

    // P2PeerTarget virtual overrides
    protected:
      void
        On_PITimer ( bool bCancel, PITimerID nPITimerID, DWORD dwUserKey );

    // P2PeerCon_MAP handlers
    // NOTES: Placemarker for map.  Should Hub be integrated into P2Pmsg
    //        network for control and monitoring purposes place handlers
    //        here.
    protected:
    DECLARE_P2PeerCon_MAP()

    // P2PeerMsg_MAP handlers
    // NOTES: Placemarker for map.  Default set of handlers reside
    //        in P2PeerTarget.  Usually this default set is customised
    protected:
	  DECLARE_P2PeerMsg_MAP()
      // External control
      msgRESULT
        On_StartSQL2MAPI ( P2PeerMsg *pMsg );
      msgRESULT
        On_StopSQL2MAPI ( P2PeerMsg *pMsg );

      // ODBC Handlers
      // NOTES: ODBC interface responses
      msgRESULT
        On_SQLconnected ( P2PeerMsg *pMsg );
      msgRESULT
        On_SQLemail2send ( P2PeerMsg *pMsg );
      msgRESULT
        On_SQLemailCompleted ( P2PeerMsg *pMsg );
      msgRESULT
        On_SQLdisconnected ( P2PeerMsg *pMsg );
      msgRESULT                        // ODBC interface issues
        On_SQLexception ( P2PeerMsg *pMsg );

      // MAPI Handlers
      // NOTES: MAPI interface responses
      msgRESULT
        On_MAPIInitialised ( P2PeerMsg *pMsg );
      msgRESULT
        On_MAPILoggedOn ( P2PeerMsg *pMsg );
      msgRESULT
        On_MAPIemailsent ( P2PeerMsg *pMsg );
      msgRESULT                        // MAPI interface issues
        On_MAPIexception ( P2PeerMsg *pMsg );

    // Helpers
    private:

    // Attributes
    private:
      P2PumpID         m_nPumpODBC;
      P2PeerTarget    *m_pTargetODBC;
      P2PumpID         m_nPumpMAPI;
      P2PeerTarget    *m_pTargetMAPI;
      P2PconID         m_nWebServerConID;
      long             m_nEmailID;
      PITimerID        m_nPITimerSQL;
      int              m_nPITimerSecs;
      int              m_nPITimerSecsMax;
      short            m_nIPort;
      CString          m_strFrom_Email;
      CString          m_strUsernameODBC;
      CString          m_strPasswordODBC;
      CString          m_strServernameODBC;
      CString          m_strEmailTablename;
};

