/* 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 PupPilot_h
#define PupPilot_h

#include "state_machine.h"
#include "Behavior.h"
#include "../Main/Events.h"
#include "../Motion/MotionInterface.h"

#include "../headers/EasyNet.h"

class PupPilot : public IndependentBehavior,
                 public NetClass<PupPilot> {

 public:

  static const int beh_id;
  static char const * const beh_name;

  static const int NumStates = 2;
  enum State { LISTENING, CONNECTED };

  static char const * const state_names[NumStates];
  
  // This is just for convenience.
  typedef FiniteStateMachine<State,ulong> FSM;

private:
  static const int LISTEN_PORT = 4000;

  // listen for connections
  void listen();

  // callbacks
  void connected(NetTCPConnection conn, int result);
  void closed   (NetTCPConnection conn, int result);
  void receive  (NetTCPConnection conn, int result);
  NetTCPConnection connection;


  FSM fsm;

  MotionCommand out_command;

public:

  static EventProcessor *create() { return new PupPilot; }

  PupPilot();
  virtual ~PupPilot();

  void reset(ulong time);
  double operator()(Motion::MotionCommand *Command);

  virtual bool initConnections();
  virtual bool setupEventMgr();
  virtual bool update(ulong time,const EventList *events);
  virtual const MotionCommand *get(ulong time);
};

#endif

