forked from zubairhamed/betwixt
-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.go
188 lines (143 loc) · 4.53 KB
/
server.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
package betwixt
import (
"log"
"strconv"
"strings"
"github.com/thingspin/canopus"
)
type ServerConfig map[string]string
func NewLwm2mServer(name string, store Store, cfg ServerConfig) *LWM2MServer {
coapServer := canopus.NewServer()
return &LWM2MServer{
Name: name,
Store: store,
Config: cfg,
Stats: &BetwixtServerStatistics{},
Events: make(map[EventType]FnEvent),
CoapServer: coapServer,
}
}
type LWM2MServer struct {
Name string
Store Store
CoapServer canopus.CoapServer
CoapConn canopus.Connection
Config ServerConfig
Stats ServerStatistics
Events map[EventType]FnEvent
Registry Registry
EvtOnRegistered FnOnRegistered
EvtOnDeregistered FnOnDeregistered
}
func (c *LWM2MServer) OnRegistered(fn FnOnRegistered) {
c.EvtOnRegistered = fn
}
func (c *LWM2MServer) OnDeregistered(fn FnOnDeregistered) {
c.EvtOnDeregistered = fn
}
func (b *LWM2MServer) Serve() error {
b.CoapServer.OnMessage(func(msg canopus.Message, inbound bool) {
b.Stats.IncrementCoapRequestsCount()
})
b.CoapServer.Post("/rd", FnCoapRegisterClient(b))
b.CoapServer.Put("/rd/:id", FnCoapUpdateClient(b))
b.CoapServer.Delete("/rd/:id", FnCoapDeleteClient(b))
addr := b.Config["addr"]
go b.CoapServer.ListenAndServe(addr)
return nil
}
func (b *LWM2MServer) Register(ep string, addr string, resources []*canopus.CoreResource) (string, error) {
clientId := canopus.GenerateToken(8)
cli := NewRegisteredClient(ep, clientId, addr, b.CoapServer, b.CoapConn)
objs := make(map[LWM2MObjectType]Object)
for _, o := range resources {
t := o.Target[1:len(o.Target)]
sp := strings.Split(t, "/")
objectId, _ := strconv.Atoi(sp[0])
lwId := LWM2MObjectType(objectId)
obj, ok := objs[lwId]
if !ok {
obj = NewObject(lwId, nil, b.Registry)
}
if len(sp) > 1 {
// Has Object Instance
instanceId, _ := strconv.Atoi(sp[1])
obj.AddInstance(instanceId)
}
objs[lwId] = obj
}
cli.SetObjects(objs)
b.Store.PutClient(ep, cli)
if b.EvtOnRegistered != nil {
b.EvtOnRegistered(cli)
}
return clientId, nil
}
func (b *LWM2MServer) Delete(id string) {
log.Println("server.Delete")
b.Store.DeleteClient(id)
}
func (b *LWM2MServer) Update(id string) {
log.Println("server.Update")
b.Store.UpdateTS(id)
}
func (b *LWM2MServer) UseRegistry(reg Registry) {
b.Registry = reg
}
func (b *LWM2MServer) GetClients() map[string]RegisteredClient {
return b.Store.GetClients()
}
func (b *LWM2MServer) GetClient(id string) RegisteredClient {
return b.Store.GetClient(id)
}
func (b *LWM2MServer) GetServerStats() ServerStatistics {
return b.Stats
}
func FnCoapRegisterClient(b *LWM2MServer) canopus.RouteHandler {
return func(req canopus.Request) canopus.Response {
ep := req.GetURIQuery("ep")
// lt := req.GetUriQuery("lt")
// sms := req.GetUriQuery("sms")
// binding := req.GetUriQuery("b")
resources := canopus.CoreResourcesFromString(req.GetMessage().GetPayload().String())
//sooskim clientId, err := b.Register(ep, req.GetAddress().String(), resources)
clientId, err := b.Register(ep, "", resources)
if err != nil {
log.Println("Error registering client ", ep)
}
msg := canopus.NewMessageOfType(canopus.MessageAcknowledgment, req.GetMessage().GetMessageId(), canopus.NewEmptyPayload()).(*canopus.CoapMessage)
msg.Token = req.GetMessage().GetToken()
msg.AddOption(canopus.OptionLocationPath, "rd/"+clientId)
msg.Code = canopus.CoapCodeCreated
return canopus.NewResponseWithMessage(msg)
}
}
func FnCoapUpdateClient(b *LWM2MServer) canopus.RouteHandler {
return func(req canopus.Request) canopus.Response {
id := req.GetAttribute("id")
b.Update(id)
msg := canopus.NewMessageOfType(canopus.MessageAcknowledgment, req.GetMessage().GetMessageId(), canopus.NewEmptyPayload()).(*canopus.CoapMessage)
msg.Token = req.GetMessage().GetToken()
msg.Code = canopus.CoapCodeChanged
return canopus.NewResponseWithMessage(msg)
}
}
func FnCoapDeleteClient(b *LWM2MServer) canopus.RouteHandler {
return func(req canopus.Request) canopus.Response {
id := req.GetAttribute("id")
b.Delete(id)
msg := canopus.NewMessageOfType(canopus.MessageAcknowledgment, req.GetMessage().GetMessageId(), canopus.NewEmptyPayload()).(*canopus.CoapMessage)
msg.Token = req.GetMessage().GetToken()
msg.Code = canopus.CoapCodeDeleted
return canopus.NewResponseWithMessage(msg)
}
}
type BetwixtServerStatistics struct {
requestCount int
}
func (s *BetwixtServerStatistics) IncrementCoapRequestsCount() {
s.requestCount++
}
func (s *BetwixtServerStatistics) GetRequestsCount() int {
return s.requestCount
}