/****************************************************************************/
/* Copyright (c) 2007 MBARI                                                 */
/* MBARI Proprietary Information. All rights reserved.                      */
/****************************************************************************/
/* Summary  :                                                               */
/* Filename : VcsServer.h                                                    */
/* Author   :                                                               */
/* Project  : Onboard Deliberative Autonomy                                 */
/* Version  : 1.0                                                           */
/* Created  : 02/14/2007                                                    */
/* Modified :                                                               */
/* Archived :                                                               */
/****************************************************************************/
/* Modification History:                                                    */
/****************************************************************************/
#ifndef _VcsServer_H
#define _VcsServer_H

#include <sys/select.h>
#include "Attributes.h"
#include "Task.h"

#define DEFAULT_PORT 8004
#define DEFAULT_HUP  20    // AMC timeout (seconds) after which assume AMC died

#define DEFAULT_CONFIG "vcsServer.cfg"

/*
CLASS 
VcsServer

DESCRIPTION
Gather data representing the state of the vehicle, and broadcast data
over socket. We'll implement the publisher as a device driver in order
to use common idioms and framework components we're all familiar with.

*/

class LayeredControlIF;

class VcsServer {
public:
  VcsServer(Boolean sim, Boolean verbose = False, char *configFile = DEFAULT_CONFIG);
  ~VcsServer();

  int init();
  void run();

private:
  // Socket management
  int acceptClient();
  char *recvString();

  long _port;
  struct sockaddr_in _servaddr, _cliaddr;
  int _socket, _client;
  long _hupTime;

  // log management 
  void log(char const *format, ...);

  FILE *_log;
  Boolean _verbose;
  
  // Configuration 
  void createCfgAttributes();

  Attributes _attributes;
  char *     _config;

  // supervisor execution management
  int  initMission(char const *initialStack);
  char *     _sim;

  // LayeredControl interface
  int openLC();
  int appendBehaviors(char const *behaviors);
  int insertBehaviors(char const *behaviors);
  int deleteBehavior(char const *id);

  void handleAmcRequest(double tick, char const *msg);
  int writeAndVerify(char const *behaviors, char const *file);
  LayeredControlIF *_layeredControlIF;
  long _lcTOTime;
  unsigned long _insertNum;
};


// class VcsServer {

// public:
  
//   /////////////////////////////////////////////////////////
//   // overloaded Constructor
//   VcsServer(Boolean sim = False, 
//            Boolean verbose = False, char* configfile = DEFAULT_CONFIG);

//   ///////////////////////////////////////////////////////
//   //destructor
//   virtual  ~VcsServer();
//   int init();
//   void run();
//   LayeredControlIF* getLC() { return _layeredControlIF; }

// protected:

//   FILE *_log;
//   Boolean _verbose;
//   Boolean _missionStarted;
//   int     _socket;
//   int     _client;
//   int     _insertNum, _appendNum;
//   long    _port;
//   long    _hupTime;
//   long    _lcTOTime;
//   long    _msgTime;
//   long    _period;
//   long    _sdelta;
//   fd_set  _fdset;
//   LayeredControlIF* _layeredControlIF;

//   char*  _config;
//   char*  _sim;
//   struct sockaddr_in _servaddr, _cliaddr;
//   Attributes _attributes;
//   VcsMessage *_msgs;

//   /// Socket utility functions
//   int sendString(char const *msg);
//   char *recvString();

//   //////////////////////////////////////////////////////////////// 
//   // initialization function - create interfaces to the servers
//   // we're going to need, create the socket, etc.
//   //
//   int openLC();  // Open interface to LayeredControl
//   int  setupSocket();
//   int  acceptClient();
//   void createCfgAttributes();
//   void loadConfigFile(const char* file);
//   void reportCfgAttributes();
//   void handleAmcRequest(const char *msg);
//   Boolean handleLCMessages();
//   void ackAmc (long id, const char *msg);
//   void nackAmc(long id, const char *msg);
//   int writeAndVerify(const char *behaviors, const char *file);
  
//   void abortMission();
//   int initMission(const char* initialStack);
//   int appendBehaviors(const char *behaviors);
//   int insertBehaviors(const char *behaviors);
//   int deleteBehavior(const char *id);
  
//   void log(const char *format, ...);
// };

#endif
