Exemplo n.º 1
0
void justine::sampleclient::ShmClient::gangsters ( boost::asio::ip::tcp::socket & socket, int id )
{

  boost::system::error_code err;

  size_t length = std::sprintf ( data, "<gangsters " );
  length += std::sprintf ( data+length, "%d>", id );

  socket.send ( boost::asio::buffer ( data, length ) );

  length = socket.read_some ( boost::asio::buffer ( data ), err );

  if ( err == boost::asio::error::eof )
    {

      // TODO

    }
  else if ( err )
    {

      throw boost::system::system_error ( err );
    }

  std::cout.write ( data, length );
  std::cout << "Command GANGSTER sent." << std::endl;

}
Exemplo n.º 2
0
int justine::sampleclient::ShmClient::init ( boost::asio::ip::tcp::socket & socket )
{

  boost::system::error_code err;

  size_t length = std::sprintf ( data, "<init guided Binfavágók 1 c>" );

  socket.send ( boost::asio::buffer ( data, length ) );

  length = socket.read_some ( boost::asio::buffer ( data ), err );

  if ( err == boost::asio::error::eof )
    {

      // TODO

    }
  else if ( err )
    {

      throw boost::system::system_error ( err );

    }

  int id {0};
  std::sscanf ( data, "<OK %d", &id );

  std::cout.write ( data, length );
  std::cout << "Command INIT sent." << std::endl;

  return id;

}
Exemplo n.º 3
0
void justine::sampleclient::ShmClient::car ( boost::asio::ip::tcp::socket & socket, int id, unsigned *f, unsigned *t, unsigned* s )
{

  boost::system::error_code err;

  size_t length = std::sprintf ( data, "<car " );
  length += std::sprintf ( data+length, "%d>", id );

  socket.send ( boost::asio::buffer ( data, length ) );

  length = socket.read_some ( boost::asio::buffer ( data ), err );

  if ( err == boost::asio::error::eof )
    {

      // TODO

    }
  else if ( err )
    {

      throw boost::system::system_error ( err );
    }

  int idd {0};
  std::sscanf ( data, "<OK %d %u %u %u", &idd, f, t, s );

  std::cout.write ( data, length );
  std::cout << "Command CAR sent." << std::endl;

}
Exemplo n.º 4
0
void justine::sampleclient::ShmClient::route2 ( boost::asio::ip::tcp::socket & socket, int id )
{

  boost::system::error_code err;

  size_t length = std::sprintf ( data,
                                 "<route 6 %d 1348670117 320900594 1348670109 320900801 651365957 2969934868>", id );

  socket.send ( boost::asio::buffer ( data, length ) );

  length = socket.read_some ( boost::asio::buffer ( data ), err );

  if ( err == boost::asio::error::eof )
    {

      // TODO

    }
  else if ( err )
    {

      throw boost::system::system_error ( err );

    }

  std::cout.write ( data, length );
  std::cout << "Command ROUTE2 sent." << std::endl;

}
std::vector<justine::sampleclient::MyShmClient::Gangster> justine::sampleclient::MyShmClient::gangsters ( boost::asio::ip::tcp::socket & socket, int id,
    osmium::unsigned_object_id_type cop )
{

  boost::system::error_code err;

  size_t length = std::sprintf ( data, "<gangsters " );
  length += std::sprintf ( data+length, "%d>", id );

  socket.send ( boost::asio::buffer ( data, length ) );

  length = socket.read_some ( boost::asio::buffer ( data ), err );

  if ( err == boost::asio::error::eof )
    {

      // TODO

    }
  else if ( err )
    {

      throw boost::system::system_error ( err );
    }

  /* reading all gangsters into a vector */
  int idd {0};
  unsigned f, t, s;
  int n {0};
  int nn {0};
  std::vector<Gangster> gangsters;

  while ( std::sscanf ( data+nn, "<OK %d %u %u %u>%n", &idd, &f, &t, &s, &n ) == 4 )
    {
      nn += n;
      gangsters.push_back ( Gangster {idd, f, t, s} );
    }

  std::sort ( gangsters.begin(), gangsters.end(), [this, cop] ( Gangster x, Gangster y )
  {
    return dst ( cop, x.to ) < dst ( cop, y.to );
  } );

  std::cout.write ( data, length );
  std::cout << "Command GANGSTER sent." << std::endl;

  return gangsters;
}
Exemplo n.º 6
0
  size_t receive(Connection::BufferType & data)
  {
	boost::system::error_code error;
	size_t length = socket.read_some
	(
	  boost::asio::buffer
	  (
	data.data()
	,data.size()
	  )
	  ,error
	);
	if(!error) return length;
	if(error == boost::asio::error::eof)
	  return length;
	else if(error)
	  throw boost::system::system_error(error);
  }
std::vector<justine::sampleclient::MyShmClient::Cop> justine::sampleclient::MyShmClient::initcops ( boost::asio::ip::tcp::socket & socket )
{

  boost::system::error_code err;

  size_t length = std::sprintf ( data, "<init guided %s 10 c>", m_teamname.c_str() );

  socket.send ( boost::asio::buffer ( data, length ) );

  length = socket.read_some ( boost::asio::buffer ( data ), err );


  if ( err == boost::asio::error::eof )
    {

      // TODO

    }
  else if ( err )
    {

      throw boost::system::system_error ( err );
    }

  /* reading all gangsters into a vector */
  int idd {0};
  int f, t;
  char c;
  int n {0};
  int nn {0};
  std::vector<Cop> cops;

  while ( std::sscanf ( data+nn, "<OK %d %d/%d %c>%n", &idd, &f, &t, &c, &n ) == 4 )
    {
      nn += n;
      cops.push_back ( idd );
    }

  std::cout.write ( data, length );
  std::cout << "Command INIT sent." << std::endl;

  return cops;
}
void justine::sampleclient::MyShmClient::route (
  boost::asio::ip::tcp::socket & socket,
  int id,
  std::vector<osmium::unsigned_object_id_type> & path
)
{

  boost::system::error_code err;

  size_t length = std::sprintf ( data,
                                 "<route %d %d", path.size(), id );

  for ( auto ui: path )
    length += std::sprintf ( data+length, " %u", ui );

  length += std::sprintf ( data+length, ">" );

  socket.send ( boost::asio::buffer ( data, length ) );

  length = socket.read_some ( boost::asio::buffer ( data ), err );

  if ( err == boost::asio::error::eof )
    {

      // TODO

    }
  else if ( err )
    {

      throw boost::system::system_error ( err );

    }

  std::cout.write ( data, length );
  std::cout << "Command ROUTE sent." << std::endl;

}
Exemplo n.º 9
0
void justine::robocar::Traffic::cmd_session ( boost::asio::ip::tcp::socket client_socket )
{
  const int network_buffer_size = 524288;
  char data[network_buffer_size]; // TODO buffered write...

  try
    {
      for ( ;; )
        {

          CarLexer cl;

          boost::system::error_code err;
          size_t length = client_socket.read_some ( boost::asio::buffer ( data ), err );

          if ( err == boost::asio::error::eof )
            {
              break;
            }
          else if ( err )
            {
              throw boost::system::system_error ( err );
            }

          std::istringstream  pdata ( data );
          cl.switch_streams ( &pdata );
          cl.yylex();

          length = 0;
          int resp_code = cl.get_errnumber();
          int num = cl.get_num();
          int id {0};

          if ( cl.get_cmd() == 0 )
            {

              for ( ;; )
                {
                  std::vector<std::shared_ptr<Car>> cars_copy;
                  {
                    std::lock_guard<std::mutex> lock ( cars_mutex );
                    cars_copy = cars;
                  }

                  std::stringstream ss;
                  ss <<
                     m_time <<
                     " " <<
                     m_minutes <<
                     " " <<
                     cars_copy.size()
                     << std::endl;
                 /*for ( auto car:cars_copy )
                    {
                      car->step();

                      ss << *car
                         <<  " " << std::endl;

                    }*/
                    //atirva
                  for ( std::vector<std::shared_ptr<Car>>::iterator it=cars_copy.begin(); it != cars_copy.end(); ++it )
                    {
                      auto car = *it;

                      car->step();

                      ss << *car
                         <<  " " << std::endl;

                    }

                  boost::asio::write ( client_socket, boost::asio::buffer ( data, length ) );
                  length = std::sprintf ( data,
                                          "%s", ss.str().c_str() );

                  boost::asio::write ( client_socket, boost::asio::buffer ( data, length ) );

                  std::this_thread::sleep_for ( std::chrono::milliseconds ( 200 ) );

                }

            }
          else if ( cl.get_cmd() <100 )
            {
              std::lock_guard<std::mutex> lock ( cars_mutex );

              for ( int i {0}; i<cl.get_num(); ++i )
                {

                  if ( cl.get_role() =='c' )
                    id = addCop ( cl );
                  else
                    id = addGangster ( cl );

                  if ( !resp_code )
                    length += std::sprintf ( data+length,
                                             "<OK %d %d/%d %c>", id, num,
                                             i+1, cl.get_role() );
                  else
                    length += std::sprintf ( data+length,
                                             "<WARN %d %d/%d %c>", id, num,
                                             i+1, cl.get_role() );

                }
            } // cmd 100
          else if ( cl.get_cmd() == 101 )
            {

              if ( m_smart_cars_map.find ( cl.get_id() ) != m_smart_cars_map.end() )
                {

                  std::shared_ptr<SmartCar> c = m_smart_cars_map[cl.get_id()];

                  if ( c->set_route ( cl.get_route() ) )
                    length += std::sprintf ( data+length, "<OK %d>", cl.get_id() );
                  else
                    length += std::sprintf ( data+length, "<ERR bad routing vector>" );

                }
              else
                length += std::sprintf ( data+length, "<ERR unknown car id>" );

            }
          else if ( cl.get_cmd() == 1001 )
            {

              if ( m_smart_cars_map.find ( cl.get_id() ) != m_smart_cars_map.end() )
                {

                  std::shared_ptr<SmartCar> c = m_smart_cars_map[cl.get_id()];

                  length += std::sprintf ( data+length,
                                           "<OK %d %u %u %u>", cl.get_id(), c->from(),
                                           c->to_node(), c->get_step() );
                }
              else
                length += std::sprintf ( data+length, "<ERR unknown car id>" );


            }
          else if ( cl.get_cmd() == 1002 )
            {

              std::lock_guard<std::mutex> lock ( cars_mutex );

              if ( m_smart_cars_map.find ( cl.get_id() ) != m_smart_cars_map.end() )
                {

                  bool hasGangsters {false};
                  /*for ( auto c:m_smart_cars )
                    {
                      if ( c->get_type() == CarType::GANGSTER )
                        {
                          length += std::sprintf ( data+length,
                                                   "<OK %d %u %u %u>", cl.get_id(), c->from(),
                                                   c->to_node(), c->get_step() );

                          if ( length > network_buffer_size - 512 )
                            {
                              length += std::sprintf ( data+length,
                                                       "<WARN too many gangsters to send through this implementation...>" );
                              break;
                            }

                          hasGangsters = true;
                        }
                    } */
                  //atirva
                  for ( std::vector<std::shared_ptr<SmartCar>>::iterator it = m_smart_cars.begin(); it != m_smart_cars.end(); ++it )
                    {
                      auto c = *it;

                      if ( c->get_type() == CarType::GANGSTER )
                        {
                          length += std::sprintf ( data+length,
                                                   "<OK %d %u %u %u>", cl.get_id(), c->from(),
                                                   c->to_node(), c->get_step() );

                          if ( length > network_buffer_size - 512 )
                            {
                              length += std::sprintf ( data+length,
                                                       "<WARN too many gangsters to send through this implementation...>" );
                              break;
                            }

                          hasGangsters = true;
                        }
                    }
                  if ( !hasGangsters )
                    length += std::sprintf ( data+length, "<WARN there is no gangsters>" );

                }
              else
                length += std::sprintf ( data+length, "<ERR unknown car id>" );

            }
          else if ( cl.get_cmd() == 1003 )
            {

              std::lock_guard<std::mutex> lock ( cars_mutex );

              if ( m_smart_cars_map.find ( cl.get_id() ) != m_smart_cars_map.end() )
                {

                  bool hasCops {false};
                  /* for ( auto c:m_cop_cars )
                    {
                      length += std::sprintf ( data+length,
                                               "<OK %d %u %u %u %d>", cl.get_id(), c->from(),
                                               c->to_node(), c->get_step(), c->get_num_captured_gangsters() );

                      if ( length > network_buffer_size - 512 )
                        {
                          length += std::sprintf ( data+length,
                                                   "<WARN too many cops to send through this implementation...>" );
                          break;
                        }

                      hasCops = true;

                    }*/
                    //atirva
                  for ( std::vector<std::shared_ptr<CopCar>>::iterator it = m_cop_cars.begin(); it != m_cop_cars.end(); ++it )
                    {
                      auto c = *it;

                      length += std::sprintf ( data+length,
                                               "<OK %d %u %u %u %d>", cl.get_id(), c->from(),
                                               c->to_node(), c->get_step(), c->get_num_captured_gangsters() );

                      if ( length > network_buffer_size - 512 )
                        {
                          length += std::sprintf ( data+length,
                                                   "<WARN too many cops to send through this implementation...>" );
                          break;
                        }

                      hasCops = true;

                    }
                  if ( !hasCops )
                    length += std::sprintf ( data+length, "<WARN there is no cops>" );

                }
              else
                length += std::sprintf ( data+length, "<ERR unknown car id>" );


            }
          else if ( cl.get_cmd() == 10001 )
            {

              if ( m_smart_cars_map.find ( cl.get_id() ) != m_smart_cars_map.end() )
                {

                  std::shared_ptr<SmartCar> c = m_smart_cars_map[cl.get_id()];

                  if ( c->set_fromto ( cl.get_from(), cl.get_to() ) )
                    length += std::sprintf ( data+length, "<OK %d>", cl.get_id() );
                  else
                    length += std::sprintf ( data+length, "<ERR cannot set>" );

                }
              else
                length += std::sprintf ( data+length, "<ERR unknown car id>" );

            }
          else
            {

              length += std::sprintf ( data+length, "<ERR unknown proto command>" );

            }

          boost::asio::write ( client_socket, boost::asio::buffer ( data, length ) );

        }

    }
  catch ( std::exception& e )
    {
      std::cerr << "Ooops: " << e.what() << std::endl;
    }
}
Exemplo n.º 10
-1
void justine::sampleclient::ShmClient::pos ( boost::asio::ip::tcp::socket & socket, int id )
{

  boost::system::error_code err;

  size_t length = std::sprintf ( data, "<pos " );
  length += std::sprintf ( data+length, "%d %u %u>", id, 2969934868u, 651365957u );

  socket.send ( boost::asio::buffer ( data, length ) );

  length = socket.read_some ( boost::asio::buffer ( data ), err );

  if ( err == boost::asio::error::eof )
    {

      // TODO

    }
  else if ( err )
    {

      throw boost::system::system_error ( err );

    }

  std::cout.write ( data, length );
  std::cout << "Command POS sent." << std::endl;
}