upnpsub

package module
v0.5.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 14, 2022 License: MIT Imports: 15 Imported by: 3

README

go-upnpsub

GitHub GitHub tag (latest SemVer) GitHub last commit GitHub go.mod Go version Go Reference

Go library that handles subscribing to UPnP events.

CLI

Use the command line version to test UPnP events.

Install
go install github.com/ItsNotGoodName/go-upnpsub/cmd/go-upnpsub@latest
Run
go-upnpsub -url http://192.168.1.23:8050/421fec64-9d4a-40e7-9ce9-058c474fc209/Radio/event

Library

package main

import (
	"context"
	"fmt"
	"net/url"
	"time"

	"github.com/ItsNotGoodName/go-upnpsub"
)

func main() {
	// Create control point
	cp := upnpsub.NewControlPoint()
	go upnpsub.ListenAndServe("", cp)

	// Parse event url
	url, err := url.Parse("http://192.168.1.23:8050/421fec64-9d4a-40e7-9ce9-058c474fc209/Radio/event")
	if err != nil {
		panic(err)
	}

	// Create context that ends in 5 seconds
	ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
	defer cancel()

	// Create subscription
	sub, err := cp.Subscribe(ctx, url)
	if err != nil {
		panic(err)
	}

	// Print events until the context is done
	for {
		select {
		case <-sub.Done():
			// Subscription's context was canceled and it has finished cleaning up
			return
		case event := <-sub.Events():
			fmt.Printf("%+v\n", event)
		}
	}
}

Documentation

Overview

Package upnpsub handles subscribing to UPnP events. It tries to follow section 4 of "UPnP Device Architecture 1.0".

Index

Constants

View Source
const (
	DefaultPort = 8058        // DefaultPort is the port that the HTTP server listens on.
	DefaultURI  = "/eventSub" // DefaultURI is the default URI where the UPnP publisher sends notify requests.

)

Variables

This section is empty.

Functions

func ListenAndServe added in v0.5.0

func ListenAndServe(host string, cp ControlPoint) error

ListenAndServe is a wrapper for http.ListenAndServe.

func WithPort added in v0.5.0

func WithPort(port int) func(cp *controlPoint)

WithPort sets the port for controlPoint.

func WithURI added in v0.5.0

func WithURI(uri string) func(cp *controlPoint)

WithURI sets the uri for controlPoint.

Types

type ControlPoint

type ControlPoint interface {
	// ServeHTTP handles HTTP notify requests from UPnP event publishers.
	ServeHTTP(http.ResponseWriter, *http.Request)
	// URI returns the URI that the ControlPoint has to be mounted on.
	URI() string
	// Port returns the port that the ControlPoint has to listens on.
	Port() int
	// Subscribe to event publisher and return a Subscription.
	// Subscription is unsubscribed when the provided context is done.
	// ControlPoint must be listening before calling this function.
	Subscribe(ctx context.Context, eventURL *url.URL) (Subscription, error)
}

ControlPoint handles the HTTP notify requests and keeps track of subscriptions.

func NewControlPoint

func NewControlPoint(opts ...func(cp *controlPoint)) ControlPoint

NewControlPoint creates a new ControlPoint.

type Event

type Event struct {
	Properties []Property
	SEQ        int
	// contains filtered or unexported fields
}

Event represents a parsed notify request.

type Property

type Property struct {
	Name  string // Name of inner field from UPnP property.
	Value string // Value of inner field from UPnP property.
}

Property is the notify request's property.

type Subscription

type Subscription interface {
	// Events returns channel that receives events from the UPnP event publisher. Should only be consumed by one goroutine.
	Events() <-chan *Event
	// Renew queues an early subscription renewal.
	Renew()
	// IsActive returns true if the subscription is active.
	IsActive() bool
	// LastActive returns the time the subscription was last active.
	LastActive() time.Time
	// Done returns channel that signals when the subscription is done cleaning up after the context was canceled.
	Done() <-chan struct{}
}

Subscription represents a subscription to UPnP event publisher.

Directories

Path Synopsis
cmd
internal

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL