// $Header: https://oberon.oapd.inaf.it/shins/trunk/shins/test/tcs_iif/iifApp_wrong.cpp 317 2017-11-23 14:41:27Z depa $

// #pragma once // not neede because this is main file

#include <config.h>
#include <Nice/App/Application.h>
#include <Nice/App/ConfigClient.h>
//#include <Nice/Map/NPoint.h>
#include <Basda/Mocca/Constant.h>
#include <Ltcs/Iif/Side.h>
#include <Ltcs/Iif/PropertyName.h>
#include <Ltcs/Iif/ServiceWorkerInterface.h>

class iifApp : public Nice::Application
{
public:
  /**
   * @brief That method introduce the user how to start the application. It will be shown every time when the user called the application on a wrong way.
   */
  virtual int usage ( int );

  /**
   * @brief The run method will be started through the main method. It keeps the motor control implementation.
   */
  virtual int run ( int argc, char* argv[] );

  /**
   * @brief A call back method in order to terminate the application.
   */
  virtual void interruptCallback ( int _sig );

};

int
iifApp::usage ( int _ret )
{
  std::cerr << "Usage: " << appName() << " --CONNECT:Endpoint=[..]\n";
  std::cerr << "Options:\n"
    "-h, --help                                       Show this message.\n"
    "-v, --version                                    Display version.\n"
    "\n"
    "\n"
    "--CONNECT:Endpoint=[NAME=NODENAME,...]          NAME=NODENAME  - name of the service\n"
    "                                                PORT=intval    - tcp port of the service\n"
    "                                                HOST=string    - tcp host address of the service\n"
    "                                                MULTICAST=true - resolve service by multicast\n"
    "\n"
    "Examples:\n"
    "\n";
  std::cerr  << appName() << " --CONNECT:Endpoint=[NAME=svc,HOST=host,PORT=4711]\n";
  std::cerr  << appName() << " --CONNECT:Endpoint=[NAME=svc,PORT=4711]\n";
  std::cerr  << appName() << " --CONNECT:Endpoint=[NAME=svc,MULTICAST=1]\n"
    "\n"
    "Hints:\n"
    " Don't put spaces in commandline arguments or use quotes\n"
    " NO: --CONNECT:Endpoint=[NAME=svc, PORT=4711]\n"
    " OK: --CONNECT:Endpoint=\"[NAME=svc, PORT=4711]\"\n"
    "\n"
    "\n"
    ;
  return _ret;
}

int
iifApp::run ( int /*argc*/, char* /*argv*/[] )
{
  Ltcs::IifServiceWorkerInterfacePtr iif;
  std::string ra = "-99999.0";
  U1_LOG(config()->leaf("CONNECT"));
  if(config()->getAtNode<Nice::Endpoint>("IIF") != Nice::Endpoint())
   {
      try
        {
          // NOTE: IIF
          Nice::Endpoint p = config()->getAtNode<Nice::Endpoint>("IIF");
          Nice::AppConfigClient::create(p, config()->node("IIF.SVC"));
          iif = Ltcs::IifServiceWorkerInterface::create(config()->node("IIF.SVC"));
          ra = iif->getRA();
        }
      catch (const Nice::Exception & ex)
        {
          E_LOG(ex);
        }
   }
}


int main(int argc, char* argv[])
{
  iifApp app;
  app.start(argc, argv);
}
