////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// This is a part of the COM Serialization SDK VC++ Library.
//  
// Copyright 2009 BPM Microsystems
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
//                                                                                                                    //
// $Header: //sesca/src/Winprog/COMSerializationSDK/VC++/HeaderLib/ComSerSdk/rcs/SerialServer.h 1.2 2010/01/04 09:03:43 arturoc Exp $
//                                                                                                                    //
///////// 120 columns, Tab = Insert 3 Spaces ///////////////////////////////////////////////////////////////////////////

/**
   This header file defines the CSerialServerBase class.
*/

#pragma once

#include "Detail/Server.h"

namespace ComSerSdk
{

/**
   Base of the library-user-defined concrete "serial server class".
   Consult the COM Serialization SDK documentation for details.
*/
class CSocketSerialServerBase : public Detail::CServerBase
{
   /**
      At the very least, user must define this member function in their class to configure the dictionary
      object with how they want to modify the data pattern based on the Appendix of the COM Serialization SDK
      documentation.
   */
   virtual HRESULT GetSocketSerialData(BSTR sCurrentHandle,
                                       long lSite,
                                       long lSocket,
                                       BSTR sSiteId,
                                       BSTR sAdapter,
                                       BSTR sSocketId,
                                       Scripting::IDictionaryPtr & spoDictionary) = 0;

   virtual HRESULT GetSerialData(BSTR,IDispatch **)
   {
      return ERROR_CALL_NOT_IMPLEMENTED;
   }
   
   /**
      So that the library user can implement the OnBeginJob method if they so desire.
      Consult the Appendix of the COM Serialization SDK documentation for details.
   */
   virtual HRESULT OnBeginJob(BSTR sLastHandle, LONG lNumDevices)
   {
      return S_OK;
   }

   /**
      So that the library user can implement the OnResult method if they so desire.
      Consult the Appendix of the COM Serialization SDK documentation for details.
   */
   virtual HRESULT OnResult(BSTR sHandle, VARIANT_BOOL bSuccess, LONG lSite, LONG lSocket)
   {
      return S_OK;
   }

   virtual HRESULT OnSocketResult(BSTR,VARIANT_BOOL,LONG,LONG,BSTR,BSTR,BSTR)
   {
      return S_OK;
   }

   /**
      So that the library user can implement the SetInitializationData method if they so desire.
      Consult the Appendix of the COM Serialization SDK documentation for details.
   */
   virtual HRESULT SetInitializationData(BSTR sInitializationData)
   {
      return S_OK;
   }

   /**
      /override
      Overrides the ComSerSdk::Detail::CServerBase class member function to retrieve its dictionary instance
      and provide that to the library-user's override of our GetSerialData() pure-virtual.
   */
   HRESULT GetSocketSerialData(BSTR sCurrentNumber, 
                               long lSite,
                               long lSocket,
                               BSTR sSiteId,
                               BSTR sAdapter,
                               BSTR sSocketId,
                               IDispatch ** ppoDictionaryAsIDispatch)
   {
      const HRESULT hReturn = GetSocketSerialData(sCurrentNumber,
                                                  lSite,
                                                  lSocket, 
                                                  sSiteId,
                                                  sAdapter,
                                                  sSocketId,
                                                  Dictionary());
      *ppoDictionaryAsIDispatch = Dictionary();
      return hReturn;
   }

   virtual HRESULT OnRetry(BSTR,LONG,LONG,BSTR,BSTR,BSTR)
   {
      return S_OK;
   }

   HRESULT GetSerialAddresses(IDispatch **)
   {
      return S_OK;
   }
};

/**
   Base of the library-user-defined concrete "serial server class".
   Consult the COM Serialization SDK documentation for details.
*/
class CSerialServerBase : public Detail::CServerBase
{
   /**
      At the very least, user must define this member function in their class to configure the dictionary
      object with how they want to modify the data pattern based on the Appendix of the COM Serialization SDK
      documentation.
   */
   virtual HRESULT GetSerialData(BSTR sCurrentHandle, Scripting::IDictionaryPtr & spoDictionary) = 0;
   
   /**
      So that the library user can implement the OnBeginJob method if they so desire.
      Consult the Appendix of the COM Serialization SDK documentation for details.
   */
   virtual HRESULT OnBeginJob(BSTR sLastHandle, LONG lNumDevices)
   {
      return S_OK;
   }

   /**
      So that the library user can implement the OnResult method if they so desire.
      Consult the Appendix of the COM Serialization SDK documentation for details.
   */
   virtual HRESULT OnResult(BSTR sHandle, VARIANT_BOOL bSuccess, LONG lSite, LONG lSocket)
   {
      return S_OK;
   }

   HRESULT OnSocketResult(BSTR sHandle,VARIANT_BOOL bSuccess,LONG lSite,LONG lSocket,BSTR,BSTR,BSTR)
   {
      return OnResult(sHandle, bSuccess, lSite, lSocket);
   }

   /**
      So that the library user can implement the SetInitializationData method if they so desire.
      Consult the Appendix of the COM Serialization SDK documentation for details.
   */
   virtual HRESULT SetInitializationData(BSTR sInitializationData)
   {
      return S_OK;
   }

   /**
      /override
      Overrides the ComSerSdk::Detail::CServerBase class member function to retrieve its dictionary instance
      and provide that to the library-user's override of our GetSerialData() pure-virtual.
   */
   HRESULT GetSerialData(BSTR sCurrentNumber, IDispatch ** ppoDictionaryAsIDispatch)
   {
      const HRESULT hReturn = GetSerialData(sCurrentNumber, Dictionary());
      *ppoDictionaryAsIDispatch = Dictionary();
      return hReturn;
   }

   HRESULT GetSocketSerialData(BSTR sCurrentNumber,long,long,BSTR,BSTR,BSTR,IDispatch **ppoDictionaryAsIDispatch)
   {
      return GetSerialData(sCurrentNumber, ppoDictionaryAsIDispatch);
   }

   HRESULT OnRetry(BSTR,LONG,LONG,BSTR,BSTR,BSTR)
   {
      return S_OK;
   }

   HRESULT GetSerialAddresses(IDispatch **ppoDictionaryAsIDispatch)
   {
      Scripting::IDictionaryPtr spoDictionary = CServerBase::CreateDictionary();
      *ppoDictionaryAsIDispatch = spoDictionary.Detach();
      return S_OK;
   }
};

}