bool Adapter::RPC( const HTTPRequest &request, const std::string &cat, const std::string &action ) { if( cat == "port" or cat == "frontend" ) { std::string t; if( !request.GetParam( "frontend_id", t )) return false; int i = atoi( t.c_str( )); if( i >= 0 && i < frontends.size( )) { return frontends[i]->RPC( request, cat, action ); } } request.NotFound( "RPC: unknown action '%s'", action.c_str( )); return false; }
bool Port::RPC( const HTTPRequest &request, const std::string &cat, const std::string &action ) { if( action == "set" ) { int port_num; std::string name; int source_id; if( !request.GetParam( "port_num", port_num )) return false; this->port_num = port_num; if( !request.GetParam( "name", name )) return false; this->name = name; if( !request.GetParam( "source_id", source_id )) return false; if( source && ( source_id == -1 || source_id != source->GetKey( ))) { source->RemovePort( this ); source = NULL; } if( source_id != -1 ) { if( !source || ( source && source_id != source->GetKey( ))) { source = TVDaemon::Instance( )->GetSource( source_id ); source->AddPort( this ); SetSource( source ); } } request.Reply( HTTP_OK ); return true; } request.NotFound( "Port::RPC unknown action '%s'", action.c_str( )); return false; }
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; }
bool Service::RPC( const HTTPRequest &request, const std::string &cat, const std::string &action ) { if( cat == "service" ) { if( action == "show" ) { HTTPServer::Response response; response.AddStatus( HTTP_OK ); response.AddTimeStamp( ); response.AddMime( "html" ); std::string data; std::string filename = request.GetDocRoot( ) + "/service.html"; int fd = open( filename.c_str( ), O_RDONLY ); if( fd < 0 ) { request.NotFound( "RPC template not found" ); return false; } char tmp[256]; int len; while(( len = read( fd, tmp, 255 )) > 0 ) { tmp[len] = '\0'; data += tmp; } size_t pos = 0; snprintf( tmp, sizeof( tmp ), "%d", transponder.GetParent( ).GetKey( )); if(( pos = data.find( "@source_id@" )) != std::string::npos ) data.replace( pos, strlen( "@source_id@" ), tmp ); snprintf( tmp, sizeof( tmp ), "%d", transponder.GetKey( )); if(( pos = data.find( "@transponder_id@" )) != std::string::npos ) data.replace( pos, strlen( "@transponder_id@" ), tmp ); snprintf( tmp, sizeof( tmp ), "%d", GetKey( )); if(( pos = data.find( "@service_id@" )) != std::string::npos ) data.replace( pos, strlen( "@service_id@" ), tmp ); response.AddContent( data ); request.Reply( response ); return true; } else if( action == "list_streams" ) { json_object *h = json_object_new_object(); json_object_object_add( h, "iTotalRecords", json_object_new_int( streams.size( ))); json_object *a = json_object_new_array(); for( std::map<uint16_t, Stream *>::iterator it = streams.begin( ); it != streams.end( ); it++ ) { json_object *entry = json_object_new_object( ); it->second->json( entry ); json_object_array_add( a, entry ); } json_object_object_add( h, "data", a ); request.Reply( h ); return true; } else if( action == "add_channel" ) { if( channel ) { LogError( "Channel already assigned" ); request.NotFound( "Channel already assigned" ); return false; } channel = TVDaemon::Instance( )->CreateChannel( this ); if( !channel ) { request.NotFound( "Channel already exists" ); return false; } request.Reply( HTTP_OK ); transponder.HasChannels( true ); transponder.SaveConfig( ); return true; } } request.NotFound( "RPC transponder: unknown action" ); return false; }