-
Notifications
You must be signed in to change notification settings - Fork 0
/
device.go
69 lines (57 loc) · 2.02 KB
/
device.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
package stagelinq
import "net"
// DeviceState represents a device's state in the network.
// Possible values are DevicePresent and DeviceLeaving.
type DeviceState byte
const (
// DevicePresent indicates that a device is actively announcing itself to the network.
DevicePresent DeviceState = iota
// DeviceLeaving indicates that a device has announced that it is leaving the network. It will no longer send announcements after this.
DeviceLeaving
)
// Device presents information about a discovered StagelinQ device on the network.
type Device struct {
port uint16
token Token
IP net.IP
Name string
SoftwareName string
SoftwareVersion string
}
// Dial starts a TCP connection with the device on the given port.
func (device *Device) Dial(port uint16) (conn net.Conn, err error) {
ip := device.IP
conn, err = net.DialTCP("tcp", nil, &net.TCPAddr{
IP: ip,
Port: int(port),
})
return
}
// Connect starts a new main connection with the device.
// You need to pass the StagelinQ token announced for your own device.
// You also need to pass services you want to provide; if you don't have any, pass an empty array.
func (device *Device) Connect(token Token, offeredServices []*Service) (conn *MainConnection, err error) {
tcpConn, err := device.Dial(device.port)
if err != nil {
return
}
conn, err = newMainConnection(tcpConn, token, device.token, offeredServices)
return
}
// IsEqual checks if this device has the same address and values as the other given device.
func (device *Device) IsEqual(anotherDevice *Device) bool {
return device.token == anotherDevice.token &&
device.Name == anotherDevice.Name &&
device.SoftwareName == anotherDevice.SoftwareName &&
device.SoftwareVersion == anotherDevice.SoftwareVersion
}
func newDeviceFromDiscovery(addr *net.UDPAddr, msg *discoveryMessage) *Device {
return &Device{
port: msg.Port,
token: msg.Token,
IP: addr.IP,
Name: msg.Source,
SoftwareName: msg.SoftwareName,
SoftwareVersion: msg.SoftwareVersion,
}
}