////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Copyright 2012 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++/Examples/SingleSourceFiles/rcs/LastNumberExceeded.cpp 1.2 2012/06/05 13:18:51 arturoc Exp $
//                                                                                                                    //
///////// 120 columns, Tab = Insert 3 Spaces ///////////////////////////////////////////////////////////////////////////

#include <ComSerSdk/Application.h>
#include <ComSerSdk/StaticVectorServer.h>

typedef ComSerSdk::CStaticVector<0, 1> CVector;

class CStaticVectorServer : public   
   ComSerSdk::CStaticVectorServerBase<CVector>
{
public:
   CStaticVectorServer():
      m_llLast(-1)
   {}
private:
   virtual void OnBeginJob(const std::string & sLast, unsigned uNumDevices)
   {
      std::istringstream(sLast.c_str()) >> m_llLast;
      
   }

   virtual void OnResult(const std::string & sHandle, bool bSuccess, unsigned uSite, unsigned uSocket)
   {
   }

   void SetVector(const std::string & sCurrent, std::string & sNext, CVector & oVec)
   {
      long long llCurrent = -1;
      std::istringstream(sCurrent.c_str()) >> llCurrent;
      if(llCurrent > m_llLast)
      {
         std::ostringstream oOutStrStrm;
         oOutStrStrm << "llCurrent = " << llCurrent << ", m_llLast = " << m_llLast;
         throw ComSerSdk::CLastNumberExceeded(oOutStrStrm.str(), COMSERSDK_CODELOCATION);
      }

      std::ostringstream oOutStrStrm;
      oOutStrStrm << (llCurrent+1);
      sNext = oOutStrStrm.str();

      oVec.SetAtRelativeIndex(0, static_cast<BYTE>(llCurrent));
   }

   long long m_llLast;
};

class CEssApp : public ComSerSdk::CApplicationBase
{
public:
   virtual ComSerSdk::CServerPtr CreateServer()
   {
      return ComSerSdk::CServerPtr(new CStaticVectorServer());
   }
};

CEssApp g_oEssApp;
