-
Notifications
You must be signed in to change notification settings - Fork 41
/
conn.go
206 lines (168 loc) · 4.41 KB
/
conn.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
package canopus
import (
"log"
"net"
"sync"
)
func MessageSizeAllowed(req Request) bool {
msg := req.GetMessage()
b, _ := MessageToBytes(msg)
if len(b) > 65536 {
return false
}
return true
}
type UDPConnection struct {
conn net.Conn
}
func (c *UDPConnection) ObserveResource(resource string) (tok string, err error) {
req := NewRequest(MessageConfirmable, Get)
req.SetRequestURI(resource)
req.GetMessage().AddOption(OptionObserve, 0)
resp, err := c.Send(req)
tok = string(resp.GetMessage().GetToken())
return
}
func (c *UDPConnection) CancelObserveResource(resource string, token string) (err error) {
req := NewRequest(MessageConfirmable, Get)
req.SetRequestURI(resource)
req.GetMessage().AddOption(OptionObserve, 1)
_, err = c.Send(req)
return
}
func (c *UDPConnection) StopObserve(ch chan ObserveMessage) {
close(ch)
}
func (c *UDPConnection) Close() error {
return c.conn.Close()
}
func (c *UDPConnection) Observe(ch chan ObserveMessage) {
readBuf := make([]byte, MaxPacketSize)
for {
len, err := c.Read(readBuf)
if err == nil {
msgBuf := make([]byte, len)
copy(msgBuf, readBuf)
msg, err := BytesToMessage(msgBuf)
if msg.GetOption(OptionObserve) != nil {
ch <- NewObserveMessage(msg.GetURIPath(), msg.GetPayload(), msg)
}
if err != nil {
log.Println("Error occured reading UDP", err)
close(ch)
}
} else {
log.Println("Error occured reading UDP", err)
close(ch)
}
}
}
func (c *UDPConnection) Send(req Request) (resp Response, err error) {
msg := req.GetMessage()
opt := msg.GetOption(OptionBlock1)
if opt == nil { // Block1 was not set
if MessageSizeAllowed(req) != true {
return nil, ErrMessageSizeTooLongBlockOptionValNotSet
}
} else { // Block1 was set
// log.Println("Block 1 was set")
}
if opt != nil {
blockOpt := Block1OptionFromOption(opt)
if blockOpt.Value == nil {
if MessageSizeAllowed(req) != true {
err = ErrMessageSizeTooLongBlockOptionValNotSet
return
} else {
// - Block # = one and only block (sz = unspecified), whereas 0 = 16bits
// - MOre bit = 0
}
} else { // BLock transfer request
payload := msg.GetPayload().GetBytes()
payloadLen := uint32(len(payload))
blockSize := blockOpt.BlockSizeLength()
currSeq := uint32(0)
totalBlocks := uint32(payloadLen / blockSize)
completed := false
var wg sync.WaitGroup
wg.Add(1)
for completed == false {
if currSeq <= totalBlocks {
var blockPayloadStart uint32
var blockPayloadEnd uint32
var blockPayload []byte
blockPayloadStart = currSeq * uint32(blockSize)
more := true
if currSeq == totalBlocks {
more = false
blockPayloadEnd = payloadLen
} else {
blockPayloadEnd = blockPayloadStart + uint32(blockSize)
}
blockPayload = payload[blockPayloadStart : blockPayloadEnd+1]
blockOpt = NewBlock1Option(blockOpt.Size(), more, currSeq)
msg.ReplaceOptions(blockOpt.Code, []Option{blockOpt})
modifiedMsg := msg.(*CoapMessage)
modifiedMsg.SetMessageId(GenerateMessageID())
modifiedMsg.SetPayload(NewBytesPayload(blockPayload))
// send message
_, err2 := c.SendMessage(msg)
if err2 != nil {
wg.Done()
return
}
currSeq = currSeq + 1
} else {
completed = true
wg.Done()
}
}
}
}
resp, err = c.SendMessage(msg)
return
}
func (c *UDPConnection) SendMessage(msg Message) (resp Response, err error) {
if msg == nil {
return nil, ErrNilMessage
}
b, err := MessageToBytes(msg)
if err != nil {
return
}
if msg.GetMessageType() == MessageNonConfirmable {
go c.Write(b)
resp = NewResponse(NewEmptyMessage(msg.GetMessageId()), nil)
return
}
_, err = c.Write(b)
if err != nil {
return
}
msgBuf := make([]byte, 1500)
if msg.GetMessageType() == MessageAcknowledgment {
resp = NewResponse(NewEmptyMessage(msg.GetMessageId()), nil)
return
}
n, err := c.Read(msgBuf)
if err != nil {
return
}
respMsg, err := BytesToMessage(msgBuf[:n])
if err != nil {
return
}
resp = NewResponse(respMsg, nil)
if msg.GetMessageType() == MessageConfirmable {
// TODO: Send out message and wait for a confirm. If confirmation not retrieved,
// resend (taking into account timeouts and back-off transmissions
// c.Send(NewRequestFromMessage(msg))
}
return
}
func (c *UDPConnection) Write(b []byte) (int, error) {
return c.conn.Write(b)
}
func (c *UDPConnection) Read(b []byte) (int, error) {
return c.conn.Read(b)
}