/* LICENSE:
  =========================================================================
    CMPack'04 Source Code Release for OPEN-R SDK 1.1.5-r2 for ERS7
    Copyright (C) 2004 Multirobot Lab [Project Head: Manuela Veloso]
    School of Computer Science, Carnegie Mellon University
    All rights reserved.
  ========================================================================= */

#ifndef NetBehavior_h
#define NetBehavior_h

#include "state_machine.h"
#include "Behavior.h"
#include "FeatureSet.h"
#include "../Main/Events.h"
#include "../Motion/MotionInterface.h"

// ----------------------------------
// **** Include EasyNet.h header ****
// ----------------------------------

#include "../headers/EasyNet.h"


// ---------------------------------------
// **** Add inheritance from NetClass ****
// ---------------------------------------

class NetBehavior : public IndependentBehavior,
                    public NetClass<NetBehavior> {

 public:

  static const int beh_id;
  static char const * const beh_name;

  // This example has three states: a start state, and two
  // states which alternate at sending the letters "A" or "B"

  static const int NumStates = 3;
  enum State { NET_START, NET_SEND_A, NET_SEND_B };
  static char const * const state_names[NumStates];

  typedef FiniteStateMachine<State,ulong> FSM;

private:

  // These functions will be our callbacks:

  void Connected(NetTCPConnection Conn, int Result);
  void Closed   (NetTCPConnection Conn, int Result);

  // This will be our connection

  NetTCPConnection Connection;



  // The rest is behavior stuff...

  FSM fsm;
  MotionCommand out_command;
  uint fs_id;
  FeatureSet *fs;

public:

  static EventProcessor *create() { return new NetBehavior; }
  NetBehavior();
  virtual ~NetBehavior();
  void reset(ulong time);
  double operator()(FeatureSet *FS, Motion::MotionCommand *Command);
  virtual bool initConnections();
  virtual bool setupEventMgr();
  virtual bool update(ulong time,const EventList *events);
  virtual const MotionCommand *get(ulong time);
};

#endif


