-
Notifications
You must be signed in to change notification settings - Fork 0
/
uiot.go
86 lines (79 loc) · 2.41 KB
/
uiot.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
// Copyright (C) <year> <name of author>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
// Package uiot provides functionality for building your own smart home network.
package uiot
import (
"os"
"os/signal"
"syscall"
)
var (
// net stores all devices we know about
network *Network
re *rpcEndpoint
me *mcastEndpoint
)
// NewDevice creates a new device with the provided name, room, and type.
// This device is intended to be used locally, as a place to define functions
// that can be called remotely.
func NewDevice(name string, t Type, r Room) *Device {
return &Device{
Name: name,
Type: t,
Room: r,
Funcs: make(map[string]*Func),
remote: false,
}
}
// Bootstrap starts networking services and broadcasts device information dev to
// the rest of the network.
func Bootstrap(dev *Device, port int) (*Network, error) {
// initialize the network struct
network = &Network{
event: make(chan *Event),
}
// add RPC port info to device struct
dev.port = port
// create the channel used to send data between the multicast service and the RPC service
c := make(chan *remote)
// start the RPC service
re = &rpcEndpoint{local: dev, network: network, channel: c}
if err := re.startRPCService(port); err != nil {
return nil, err
}
// start the multicast service
me = &mcastEndpoint{channel: c}
if err := me.startMulticastService(port); err != nil {
return nil, err
}
return network, nil
}
// Close notifies all known remote devices that this device is disconnecting from
// the network.
func Close() {
for _, dev := range network.devs {
re.sendQuit(re.local, dev)
}
}
// CloseHandler adds a SIGTERM handler to automatically disconnect from the network.
func CloseHandler() {
c := make(chan os.Signal)
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
go func() {
<-c
Close()
os.Exit(0)
}()
}