/* 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 Reload_h_DEFINED
#define Reload_h_DEFINED

using namespace std;

#include <OPENR/OObject.h>
#include <OPENR/OSubject.h>
#include <OPENR/OObserver.h>
#include <OPENR/ODataFormats.h>
#include "def.h"

#include "../headers/EasyNet.h"

enum ReloadServerState {
    RSS_DISCONNECTED,
    RSS_WAITING_FOR_PASSWORD,
    RSS_WAITING_FOR_COMMAND,
    RSS_LIST__WAITING_FOR_PARAMS,
    RSS_GET__WAITING_FOR_PARAMS,
    RSS_GET__SENDING_FILE,
    RSS_PUT__WAITING_FOR_PARAMS,
    RSS_PUT__RECEIVING_FILE,
    RSS_TOUCH__WAITING_FOR_PARAMS,
    RSS_MKDIR__WAITING_FOR_PARAMS,
    RSS_RMDIR__WAITING_FOR_PARAMS,
    RSS_REMOVE__WAITING_FOR_PARAMS,
    RSS_SETTIME__WAITING_FOR_PARAMS,
};


class Reload : public OObject, public NetObject<Reload> {
public:
  Reload();
  virtual ~Reload() {}

  OSubject*  subject[numOfSubject];
  OObserver* observer[numOfObserver];

  virtual OStatus DoInit   (const OSystemEvent& event);
  virtual OStatus DoStart  (const OSystemEvent& event);
  virtual OStatus DoStop   (const OSystemEvent& event);
  virtual OStatus DoDestroy(const OSystemEvent& event);
    
private:

  NetTCPConnection         Listen;
  ReloadServerState        State;
  char                     Command;

  FILE*                    file;
  unsigned char            list_depth;
  unsigned char            name_length;
  unsigned long            timestamp;
  unsigned long            filesize;
  unsigned long            count;
  char                     filename[256];
    
    void Connected(NetTCPConnection Conn, int Dummy);
    void Sent(NetTCPConnection Conn, int Bytes);
    void Received(NetTCPConnection Conn, int Bytes);
    void Disconnected(NetTCPConnection Conn, int Dummy);

    #ifdef DEBUG
    void TelnetConnected(NetTCPConnection Conn, int Dummy);
    #endif

    int List(NetTCPConnection Conn, char* name, unsigned char depth);
    int Start_Get(NetTCPConnection Conn, char* name);
    int Start_Put(NetTCPConnection Conn, char* name,
                           unsigned long filesize);
    void Touch(NetTCPConnection Conn, char* filename);

};

#endif // Reload_h_DEFINED



