Example #1
0
bool Frontend::RPC( const HTTPRequest &request, const std::string &cat, const std::string &action )
{
  if( cat == "port" )
  {
    int port_id;
    if( !request.GetParam( "port_id", port_id ))
      return false;

    LockPorts( );
    std::map<int, Port *>::iterator it = ports.find( port_id );
    if( it == ports.end( ))
    {
      LogError( "Port %d not found", port_id );
      return false;
    }
    bool ret = it->second->RPC( request, cat, action );
    UnlockPorts( );
    return ret;
  }

  if( action == "create_port" )
  {
    int port_num;
    if( !request.GetParam( "port_num", port_num ))
      return false;
    std::string name;
    if( !request.GetParam( "name", name ))
      return false;
    int source_id;
    if( !request.GetParam( "source_id", source_id ))
      return false;

    Port *port = AddPort( name, port_num );
    if( port == NULL )
    {
      request.NotFound( "Port with number %d already exists", port_num );
      return false;
    }

    Source *source = TVDaemon::Instance( )->GetSource( source_id );
    if( !source )
      LogError( "Source not found: %d", source_id );
    else
    {
      source->AddPort( port );
      port->SetSource( source );
    }
    request.Reply( HTTP_OK, port->GetKey( ));
    return true;
  }

  if( action == "delete_port" )
  {
    int port_id;
    if( !request.GetParam( "port_id", port_id ))
      return false;
    LockPorts( );
    std::map<int, Port *>::iterator it = ports.find( port_id );
    if( it == ports.end( ))
    {
      LogError( "Port not found: %d", port_id );
      UnlockPorts( );
      return false;
    }
    it->second->Delete( );
    delete it->second;
    ports.erase( it );
    UnlockPorts( );

    return true;
  }

  request.NotFound( "RPC unknown action: '%s'", action.c_str( ));
  return false;
}