
#include "other.h"
#include <stdio.h>
#include <String.h>

class History;
class TtyForkExec;

// See .cc file for documentation.

class Telnet
{
  int xx[200];
  enum { _Closed,
	 _Connecting,
	 _Connected }      state;
  enum { _none,
         _UnknownHost,
	 _Unable,
	 _TimedOut,
	 _Refused,
	 _ClosedByHost,
	 _ResponseTimeOut,
	 _Bug }            result;

  TtyForkExec *tfex =NULL;
  void  open_tfex();
  void  close_tfex();
  int   pid =0;

  // These maintain telnet state & result information.
  History& tno;
  virtual void update();
  virtual void red   (char* buf, int nchars, int cnt);
  virtual void wrote (char* buf, int nchars, int cnt) {};

  // These are for response checking.
  void               time_check();
  long               current_time();

 public:

  long               time_of_last_responseless_write =0;
  long               response_timeout = 60;

  Telnet(char* host, int rsptimeout =60);
  ~Telnet();
  virtual void Open (char* host, int rsptimeout =60);
  virtual void Close();

  int   theReadFileDescriptor();
  bool  read_possible();
  virtual int  read  (char* buf, int nchars);

  int IncrementalWrite(char* buf, int nchars);
  int IncrementalRead(char* buf, int nchars);

  virtual int write (char* buf) { return write(buf,strlen(buf)); };
  virtual int write (String& s) { return write(s,s.length()); };
  virtual int write (char* buf, int nchars);

  void response_expected();

  bool isClosed();
  bool isConnecting();
  bool isConnected();
  bool wasUnknown();
  bool wasUnable();
  bool wasTimedOut();
  bool wasRefused();
  bool wasClosed();
  bool wasNotResponding();
  bool hadBug();

  char* BugMessage();
};
