Esempio n. 1
0
void Samu::FamilyCaregiverShell ( void )
{
  std::string cmd_prefix = "cmd";

  fd_set rfds;
  struct timeval tmo;

  int sleep {0};

  if ( sleep_ )
    sleep = sleep_after_ + 1;

  int prev_sec {0};
  for ( ; run_ ; )
    {

      try
        {

          disp.cg_read();

          if ( ++sleep > sleep_after_ )
            {
              if ( !sleep_ )
                {
                  std::cerr << "Isaac went to sleep." << std::endl;
                  disp.log ( "I went to sleep." );
                }
              sleep_ = true;
            }
          else
            {
              std::cerr << sleep << " " << std::flush;

              int sec = ( sleep * read_usec_ ) / ( 1000*1000 );
              if ( sec != prev_sec )
                {
                  int after = ( sleep_after_ * read_usec_ ) / ( 1000*1000 );

                  std::stringstream sleep_after;

                  sleep_after << "I will go to sleep after ";
                  sleep_after << ( after-sec );
                  sleep_after <<  " seconds";

                  disp.log ( sleep_after.str() );
                  prev_sec = sec;
                }
            }
        }
      catch ( std::string line )
        {

          if ( sleep_ )
            {
              std::cerr << "Isaac is awake now." << std::endl;
              disp.log ( "I am awake now." );
            }
          sleep_ = false;
          sleep = 0;

          if ( !line.compare ( 0, cmd_prefix.length(), cmd_prefix ) )
            {
              std::string readCmd {"cmd read"};

              size_t f = line.find ( readCmd );
              if ( f != std::string::npos )
                {
                  f = f+readCmd.length() +1;
                  if ( f < line.length() )
                    {
                      std::string fname = line.substr ( f );
                      set_training_file ( fname );
                    }
                }
              else
                NextCaregiver();
            }
          else
            {
              try
                {
                  sentence ( -1, line );
                }
              catch ( const char* err )
                {
                  std::cerr << err << std::endl;
                  disp.log ( err );
                }
            }
        }

      usleep ( read_usec_ );

    }

  run_ = false;
}
Esempio n. 2
0
void Samu::NetworkCaregiverShell ( void )
{

  std::string cmd_prefix = "cmd";

  int sleep {0};
  
  

  if ( sleep_ )
    sleep = sleep_after_ + 1;

  int prev_sec {0};
  for ( ; run_ ; )
    {

      try
        {

          net.cg_read();

          if ( ++sleep > sleep_after_ )
            {
              if ( state == NETWORK )
                {
                  std::cerr << "Isaac went to sleep." << std::endl;
#ifdef DISP_CURSES
                  disp.log ( "I went to sleep." );
#endif
                  sleep_ = true;
                  state = SLEEP;
                }
              sleep = sleep_after_ + 1;

            }
          else if ( state == NETWORK )
            {
              std::cerr << sleep << " *** " << std::flush;

              int sec = ( sleep * read_usec_ ) / ( 1000*1000 );
              if ( sec != prev_sec )
                {
                  int after = ( sleep_after_ * read_usec_ ) / ( 1000*1000 );

                  std::stringstream sleep_after;

                  sleep_after << "I will go to sleep after ";
                  sleep_after << ( after-sec );
                  sleep_after <<  " seconds";

#ifdef DISP_CURSES
                  disp.log ( sleep_after.str() );
#endif
                  prev_sec = sec;
                }
            }
        }
      catch ( std::string line )
        {

          if ( state != TERMINAL )
            {
              if ( sleep_ )
                {
                  std::cerr << "Isaac is awake now." << std::endl;
#ifdef DISP_CURSES
                  disp.log ( "I am awake now." );
#endif
                }
              sleep_ = false;
              sleep = 0;
              state = NETWORK;

              if ( !line.compare ( 0, cmd_prefix.length(), cmd_prefix ) )
                {
                  std::string readCmd {"cmd read"};

                  size_t f = line.find ( readCmd );
                  if ( f != std::string::npos )
                    {
                      f = f+readCmd.length() +1;
                      if ( f < line.length() )
                        {
                          std::string fname = line.substr ( f );
                          set_training_file ( fname );
                        }
                    }
                  else
                    NextCaregiver();
                }
              else
                {
                  std::istringstream iss ( line );
                  SPOTriplet t;
                  iss >> t;

                  SPOTriplets tv;
                  tv.push_back ( t );

                  try
                    {
                      //sentence ( -1, line );
                      triplet ( -1000, tv );
                    }
                  catch ( const char* err )
                    {
                      std::cerr << err << std::endl;
#ifdef DISP_CURSES
                      disp.log ( err );
#endif
                    }
                }
            }
        }

      usleep ( read_usec_ );

    }

  run_ = false;
}