/***************************** LICENSE START ***********************************

 Copyright 2012 ECMWF and INPE. This software is distributed under the terms
 of the Apache License version 2.0. In applying this license, ECMWF does not
 waive the privileges and immunities granted to it by virtue of its status as
 an Intergovernmental Organization or submit itself to any jurisdiction.

 ***************************** LICENSE END *************************************/

// FAMI20181012 uPlotBatch is now a MvService, instead of a MvApplication

#if 0
#include "MvTemplates.h"
#include "uPlotBatchApp.h"
#include "PmContext.h"
#include "Root.h"

uPlotBatchApp::uPlotBatchApp(int& ac, char** av, const char* name) : 
   MvApplication(ac,av,name)
{
   // Install Resources
   //InstallResources();

   // Register this service
   registerTMvServe(*this,0);
}
#else
#include "MagPlusBatchService.h"
#include "PlotModBatch.h"
#include "PmContext.h"
#include "Root.h"
#include "uPlotBatchApp.h"

uPlotBatchApp::uPlotBatchApp(const char* kw): MvService(kw)
{
   // Create Root instance
   Root::Create();

   MagPlusBatchService* batch = new MagPlusBatchService();
   MagPlusService::Instance(batch);

   PlotModBatch* pm = new PlotModBatch();
   PlotMod::Instance(pm);
}
#endif

uPlotBatchApp::~uPlotBatchApp()
{
//cout << "SHOULD FINISHED NOW -> uPlotBatchApp destructor" << endl;
}

#if 0
// Serve Method
// called by MvTransaction to process the request
void
uPlotBatchApp::serve ( MvProtocol& proto, MvRequest& inRequest )
{
   // Create a new context, based on the input Request
   PmContext * context = new PmContext ( proto, inRequest );

   // Increment the reference counting
   context->Attach();

   // It is a new call, so it first needs to clean the previous structure
   Root::Instance().Clean();

   // Execute the context
   context->Execute();

   // Decrement the reference counting
   //FAMI092013: it was causing function TMvServe::callback to crash because
   // this function is called afterwards and it still needs the context object.
   //context->Detach();
}
#else
void
uPlotBatchApp::serve ( MvRequest& in, MvRequest& out )
{
cout << "request IN" << endl;
in.print();

   // Create a new context, based on the input Request
   MvProtocol* proto = this;
   PmContext* context = new PmContext ( *proto, in );

   // Increment the reference counting
   context->Attach();

   // It is a new call, so it first needs to clean the previous structure
   Root::Instance().Clean();

   // Execute the context
   context->Execute();

   // Handle error message
   if ( getError() )
   {
      out.setVerb("ERROR");
      const char* msg;
      int n = 0;
      while( (msg = getMessage(n++)) )
      {
         if ( n == 1 )
            out.setValue("MESSAGE",msg);
         else
            out.addValue("MESSAGE",msg);
      }
   }

cout << "request OUT" << endl;
out.print();
   // Decrement the reference counting
   //FAMI092013: it was causing function TMvServe::callback to crash because
   // this function is called afterwards and it still needs the context object.
   //context->Detach();
}
#endif
