/
control_point.go
207 lines (160 loc) · 4.83 KB
/
control_point.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
// Copyright 2015 Satoshi Konno. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package upnp
import (
"fmt"
"math/rand"
"sync"
"github.com/cybergarage/go-logger/log"
"github.com/cybergarage/go-net-upnp/net/upnp/ssdp"
"github.com/cybergarage/go-net-upnp/net/upnp/util"
)
// A ControlPointListener represents a listener for ControlPoint.
type ControlPointListener interface {
ssdp.MulticastListener
ssdp.UnicastListener
}
// A ControlPoint represents a ControlPoint.
type ControlPoint struct {
*sync.Mutex
Port int
SearchMX int
rootDeviceMap *DeviceMap
ssdpMcastServerList *ssdp.MulticastServerList
ssdpUcastServerList *ssdp.UnicastServerList
Listener ControlPointListener
}
// NewControlPoint returns a new ControlPoint.
func NewControlPoint() *ControlPoint {
cp := &ControlPoint{}
cp.Mutex = &sync.Mutex{}
cp.rootDeviceMap = NewDeviceMap()
cp.ssdpMcastServerList = ssdp.NewMulticastServerList()
cp.ssdpUcastServerList = ssdp.NewUnicastServerList()
cp.SearchMX = ControlPointDefaultSearchMX
return cp
}
// Start starts this control point.
func (ctrl *ControlPoint) StartWithPort(port int) error {
ctrl.ssdpMcastServerList.Listener = ctrl
err := ctrl.ssdpMcastServerList.Start()
if err != nil {
ctrl.Stop()
return err
}
ctrl.ssdpUcastServerList.Listener = ctrl
err = ctrl.ssdpUcastServerList.Start(port)
if err != nil {
ctrl.Stop()
return err
}
ctrl.Port = port
return nil
}
// Start starts this control point.
func (ctrl *ControlPoint) Start() error {
port := rand.Intn(ControlPointDefaultPortRange) + ControlPointDefaultPortBase
return ctrl.StartWithPort(port)
}
// Stop stops this control point.
func (ctrl *ControlPoint) Stop() error {
err := ctrl.ssdpMcastServerList.Stop()
if err != nil {
return err
}
return nil
}
// Search sends a M-SEARCH request of the specified ST.
func (ctrl *ControlPoint) Search(st string) error {
return ctrl.ssdpUcastServerList.Search(st, ctrl.SearchMX)
}
// SearchRootDevice sends a M-SEARCH request for root devices.
func (ctrl *ControlPoint) SearchRootDevice() error {
return ctrl.Search(ssdp.RootDevice)
}
// GetRootDevices returns found root devices.
func (ctrl *ControlPoint) GetRootDevices() []*Device {
ctrl.Lock()
devs := ctrl.rootDeviceMap.GetAllDevices()
ctrl.Unlock()
return devs
}
// GetRootDevicesByType returns found root devices of the specified device type.
func (ctrl *ControlPoint) GetRootDevicesByType(deviceType string) []*Device {
ctrl.Lock()
devs := ctrl.rootDeviceMap.GetDevicesByType(deviceType)
ctrl.Unlock()
return devs
}
// FindDeviceByTypeAndUDN returns a devices of the specified deviceType and UDN.
func (ctrl *ControlPoint) FindDeviceByTypeAndUDN(deviceType string, udn string) (*Device, bool) {
ctrl.Lock()
dev, ok := ctrl.rootDeviceMap.FindDeviceByTypeAndUDN(deviceType, udn)
ctrl.Unlock()
return dev, ok
}
// AddDevice adds a specified device.
func (ctrl *ControlPoint) addDevice(dev *Device) (bool, error) {
ctrl.Lock()
defer ctrl.Unlock()
if ctrl.rootDeviceMap.HasDevice(dev) {
log.Tracef(fmt.Sprintf("device (%s, %s) is already added", dev.DeviceType, dev.UDN))
return false, nil
}
err := dev.LoadServiceDescriptions()
if err != nil {
return false, err
}
ok := ctrl.rootDeviceMap.AddDevice(dev)
if ok {
log.Tracef(fmt.Sprintf("device (%s, %s) is added", dev.DeviceType, dev.UDN))
}
return ok, nil
}
func getFromToMessageFromSSDPPacket(req *ssdp.Packet) string {
fromAddr := req.From.String()
toAddr := ""
ifAddr, err := util.GetInterfaceAddress(req.Interface)
if err == nil {
toAddr = ifAddr
}
return fmt.Sprintf("(%s -> %s)", fromAddr, toAddr)
}
func (ctrl *ControlPoint) DeviceNotifyReceived(ssdpReq *ssdp.Request) {
usn, _ := ssdpReq.GetUSN()
log.Tracef(fmt.Sprintf("notiry req : %s %s", usn, getFromToMessageFromSSDPPacket(ssdpReq.Packet)))
if ssdpReq.IsRootDevice() {
newDev, err := NewDeviceFromSSDPRequest(ssdpReq)
if err == nil {
_, err = ctrl.addDevice(newDev)
}
if err != nil {
log.Warnf("%s", err.Error())
}
}
if ctrl.Listener != nil {
ctrl.Listener.DeviceNotifyReceived(ssdpReq)
}
}
func (ctrl *ControlPoint) DeviceSearchReceived(ssdpReq *ssdp.Request) {
st, _ := ssdpReq.GetST()
log.Tracef(fmt.Sprintf("search req : %s %s", st, getFromToMessageFromSSDPPacket(ssdpReq.Packet)))
if ctrl.Listener != nil {
ctrl.Listener.DeviceSearchReceived(ssdpReq)
}
}
func (ctrl *ControlPoint) DeviceResponseReceived(ssdpRes *ssdp.Response) {
url, _ := ssdpRes.GetLocation()
log.Tracef(fmt.Sprintf("search res : %s %s", url, getFromToMessageFromSSDPPacket(ssdpRes.Packet)))
newDev, err := NewDeviceFromSSDPResponse(ssdpRes)
if err == nil {
_, err = ctrl.addDevice(newDev)
}
if err != nil {
log.Warnf("%s", err.Error())
}
if ctrl.Listener != nil {
ctrl.Listener.DeviceResponseReceived(ssdpRes)
}
}