UPnP/SSDP Server: allow advertising slice of URLs

This makes it possible for the Manager to expose multiple URLs. This way
the Worker can try them out and see which ones work.
This commit is contained in:
Sybren A. Stüvel 2022-03-08 13:54:41 +01:00
parent 13a74e61d4
commit d6a60c73d0
2 changed files with 33 additions and 6 deletions

View File

@ -31,6 +31,7 @@ import (
"git.blender.org/flamenco/internal/manager/swagger_ui" "git.blender.org/flamenco/internal/manager/swagger_ui"
"git.blender.org/flamenco/internal/manager/task_logs" "git.blender.org/flamenco/internal/manager/task_logs"
"git.blender.org/flamenco/internal/manager/task_state_machine" "git.blender.org/flamenco/internal/manager/task_state_machine"
"git.blender.org/flamenco/internal/own_url"
"git.blender.org/flamenco/internal/upnp_ssdp" "git.blender.org/flamenco/internal/upnp_ssdp"
"git.blender.org/flamenco/pkg/api" "git.blender.org/flamenco/pkg/api"
) )
@ -67,12 +68,7 @@ func main() {
_, port, _ := net.SplitHostPort(listen) _, port, _ := net.SplitHostPort(listen)
log.Info().Str("port", port).Msg("listening") log.Info().Str("port", port).Msg("listening")
ssdp, err := upnp_ssdp.NewServer(log.Logger) ssdp := makeAutoDiscoverable("http", listen)
if err != nil {
log.Error().Err(err).Msg("error creating UPnP/SSDP server")
} else {
ssdp.AddAdvertisement(listen) // TODO: convert this to an entire URL.
}
// Construct the services. // Construct the services.
persist := openDB(*configService) persist := openDB(*configService)
@ -271,3 +267,20 @@ func installSignalHandler(cancelFunc context.CancelFunc) {
} }
}() }()
} }
func makeAutoDiscoverable(scheme, listen string) *upnp_ssdp.Server {
urls, err := own_url.AvailableURLs("http", listen)
if err != nil {
log.Error().Err(err).Msg("unable to figure out my own URL")
return nil
}
ssdp, err := upnp_ssdp.NewServer(log.Logger)
if err != nil {
log.Error().Err(err).Msg("error creating UPnP/SSDP server")
return nil
}
ssdp.AddAdvertisementURLs(urls)
return ssdp
}

View File

@ -22,6 +22,8 @@ package upnp_ssdp
import ( import (
"context" "context"
"net/url"
"strings"
"github.com/fromkeith/gossdp" "github.com/fromkeith/gossdp"
"github.com/rs/zerolog" "github.com/rs/zerolog"
@ -54,6 +56,18 @@ func (s *Server) AddAdvertisement(serviceLocation string) {
MaxAge: 3600, // Number of seconds this advertisement is valid for. MaxAge: 3600, // Number of seconds this advertisement is valid for.
} }
s.ssdp.AdvertiseServer(serverDef) s.ssdp.AdvertiseServer(serverDef)
s.log.Info().Str("location", serviceLocation).Msg("UPnP/SSDP location registered")
}
// AddAdvertisementURLs constructs a service location from the given URLs, and
// adds the advertisement for it.
func (s *Server) AddAdvertisementURLs(urls []url.URL) {
urlStrings := make([]string, len(urls))
for idx := range urls {
urlStrings[idx] = urls[idx].String()
}
location := strings.Join(urlStrings, LocationSeparator)
s.AddAdvertisement(location)
} }
// Run starts the advertisement, and blocks until the context is closed. // Run starts the advertisement, and blocks until the context is closed.