-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
76 lines (64 loc) · 1.39 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package main
import (
"errors"
"fmt"
"upnp-mediaserver/service"
"upnp-mediaserver/ssdp"
"log"
"net"
"os"
"os/signal"
"syscall"
"github.com/google/uuid"
)
func localIP() (net.IP, error) {
ifAddrs, err := net.InterfaceAddrs()
if err != nil {
log.Fatal(err)
}
for _, ifAddr := range ifAddrs {
netIP, ok := ifAddr.(*net.IPNet)
if ok && !netIP.IP.IsLoopback() && netIP.IP.To4() != nil {
return netIP.IP, nil
}
}
return nil, errors.New("could not get local IP address")
}
func main() {
deviceUUID := uuid.New()
localIP, err := localIP()
server := service.NewServer(deviceUUID, localIP)
if err != nil {
log.Fatal(err)
}
server.Listen()
log.Println("Listening: ", service.URLBase)
server.Setup()
errSrv := make(chan error)
go func() {
errSrv <- server.Serve()
}()
ssdpadv := ssdp.NewSSDPAdvertiser(deviceUUID, service.URLBase)
ssdpres := ssdp.NewSSDPDiscoveryResponder(deviceUUID, service.URLBase)
errSsdpRes := make(chan error)
errSsdpAdvRes := make(chan error)
go func() {
errSsdpRes <- ssdpres.ListenAndServe()
}()
go func() {
errSsdpAdvRes <- ssdpadv.Serve()
}()
c := make(chan os.Signal)
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
go func() {
<-c
ssdpadv.NotifyByebye()
os.Exit(1)
}()
msgSrv := <-errSrv
fmt.Println(msgSrv)
msgSsdpRes := <-errSsdpRes
fmt.Println(msgSsdpRes)
msgSsdpAdvRes := <-errSsdpAdvRes
fmt.Println(msgSsdpAdvRes)
}