mirror of
https://github.com/mainflux/mainflux.git
synced 2025-04-28 13:48:49 +08:00

* Bring old CoAP code back Signed-off-by: Dušan Borovčanin <dusan.borovcanin@mainflux.com> * Fix channel ID formatting due to type change Uncomment error handling for authorization. Signed-off-by: Dušan Borovčanin <dusan.borovcanin@mainflux.com> * Update CoAP adapter docs Signed-off-by: Dušan Borovčanin <dusan.borovcanin@mainflux.com> * Add copyright headers Signed-off-by: Dusan Borovcanin <dusan.borovcanin@mainflux.com> * Remove redundant type declaration Signed-off-by: Dusan Borovcanin <dusan.borovcanin@mainflux.com> * Add CoAP adapter to the list of services Add CoAp adapter in Makefile services list and fix corresponding documentation. Signed-off-by: Dusan Borovcanin <dusan.borovcanin@mainflux.com> * Refactor CoAP code Merge multipe `const` block int single and declare consts before vars. Un-export notFound handler since there is no need to export it. Signed-off-by: Dusan Borovcanin <dusan.borovcanin@mainflux.com> * Update http version endpoint This separates CoAP and HTTP APIs. Signed-off-by: Dusan Borovcanin <dusan.borovcanin@mainflux.com> * Refactor CoAP POST method handling This PR is a part of CoAP adapter refactoring that will simplify adapter implementation. Signed-off-by: Dusan Borovcanin <dusan.borovcanin@mainflux.com> * Refactor CoAP adapter Change CoAP message handling to simplify adapter implementation. Signed-off-by: Dusan Borovcanin <dusan.borovcanin@mainflux.com> * Add backoff timeout for server ping to client Update CoAP adapter to provide subset of necessary features from protocol specification. Signed-off-by: Dusan Borovcanin <dusan.borovcanin@mainflux.com> * Fix leaking locked goroutine In case of the stopped ticker, its channel is NOT closed, so pinging might be left stuck waiting for the stopped ticker to send a notification. Signed-off-by: Dusan Borovcanin <dusan.borovcanin@mainflux.com> * Format code Use more meaningful name for Handlers map. Signed-off-by: Dusan Borovcanin <dusan.borovcanin@mainflux.com> * Use and stop ticker from the same goroutine Stop handler Ticker from ping goroutine rather than the cancel goroutine. Signed-off-by: Dusan Borovcanin <dusan.borovcanin@mainflux.com> * Check if subscription already exists in put method Fix potential leak of handlers providing check inside of put method. Signed-off-by: Dusan Borovcanin <dusan.borovcanin@mainflux.com> * Use MessageID as Observe option Since MessageID satisfies observe option behaviour, use Message ID instead of local timestamp. Remove Thicker from handler and use it on transport layer. Signed-off-by: Dusan Borovcanin <dusan.borovcanin@mainflux.com> * Use name Observer insted of Handler Name `Observer` is used in protocol specification, so this naming makes code more self-documenting. Signed-off-by: Dusan Borovcanin <dusan.borovcanin@mainflux.com> * Add CoAP adapter to docker-compose.yml Signed-off-by: Dusan Borovcanin <dusan.borovcanin@mainflux.com> * Add copyright headers Signed-off-by: Dusan Borovcanin <dusan.borovcanin@mainflux.com> * Remove unused constants Fix service name in startup log message. Signed-off-by: Dusan Borovcanin <dusan.borovcanin@mainflux.com> * Add metrics endpoint Signed-off-by: Dusan Borovcanin <dusan.borovcanin@mainflux.com> * Refactor code Config fields from main.go should not be exported; minor style changes. Signed-off-by: Dusan Borovcanin <dusan.borovcanin@mainflux.com> * Update authorization URI-Query option Use `authorization` value in URI-Query option instead of `key`. This mimics Authorization header in some other protocols (e.g. HTTP). Please note that this value can be replaced with simple `auth` to save space, due to constrained URI-Query option size. Signed-off-by: Dusan Borovcanin <dusan.borovcanin@mainflux.com>
103 lines
2.2 KiB
Go
103 lines
2.2 KiB
Go
// Package coap provides a CoAP client and server.
|
|
package coap
|
|
|
|
import (
|
|
"log"
|
|
"net"
|
|
"time"
|
|
)
|
|
|
|
const maxPktLen = 1500
|
|
|
|
// Handler is a type that handles CoAP messages.
|
|
type Handler interface {
|
|
// Handle the message and optionally return a response message.
|
|
ServeCOAP(l *net.UDPConn, a *net.UDPAddr, m *Message) *Message
|
|
}
|
|
|
|
type funcHandler func(l *net.UDPConn, a *net.UDPAddr, m *Message) *Message
|
|
|
|
func (f funcHandler) ServeCOAP(l *net.UDPConn, a *net.UDPAddr, m *Message) *Message {
|
|
return f(l, a, m)
|
|
}
|
|
|
|
// FuncHandler builds a handler from a function.
|
|
func FuncHandler(f func(l *net.UDPConn, a *net.UDPAddr, m *Message) *Message) Handler {
|
|
return funcHandler(f)
|
|
}
|
|
|
|
func handlePacket(l *net.UDPConn, data []byte, u *net.UDPAddr,
|
|
rh Handler) {
|
|
|
|
msg, err := ParseMessage(data)
|
|
if err != nil {
|
|
log.Printf("Error parsing %v", err)
|
|
return
|
|
}
|
|
|
|
rv := rh.ServeCOAP(l, u, &msg)
|
|
if rv != nil {
|
|
Transmit(l, u, *rv)
|
|
}
|
|
}
|
|
|
|
// Transmit a message.
|
|
func Transmit(l *net.UDPConn, a *net.UDPAddr, m Message) error {
|
|
d, err := m.MarshalBinary()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if a == nil {
|
|
_, err = l.Write(d)
|
|
} else {
|
|
_, err = l.WriteTo(d, a)
|
|
}
|
|
return err
|
|
}
|
|
|
|
// Receive a message.
|
|
func Receive(l *net.UDPConn, buf []byte) (Message, error) {
|
|
l.SetReadDeadline(time.Now().Add(ResponseTimeout))
|
|
|
|
nr, _, err := l.ReadFromUDP(buf)
|
|
if err != nil {
|
|
return Message{}, err
|
|
}
|
|
return ParseMessage(buf[:nr])
|
|
}
|
|
|
|
// ListenAndServe binds to the given address and serve requests forever.
|
|
func ListenAndServe(n, addr string, rh Handler) error {
|
|
uaddr, err := net.ResolveUDPAddr(n, addr)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
l, err := net.ListenUDP(n, uaddr)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return Serve(l, rh)
|
|
}
|
|
|
|
// Serve processes incoming UDP packets on the given listener, and processes
|
|
// these requests forever (or until the listener is closed).
|
|
func Serve(listener *net.UDPConn, rh Handler) error {
|
|
buf := make([]byte, maxPktLen)
|
|
for {
|
|
nr, addr, err := listener.ReadFromUDP(buf)
|
|
if err != nil {
|
|
if neterr, ok := err.(net.Error); ok && (neterr.Temporary() || neterr.Timeout()) {
|
|
time.Sleep(5 * time.Millisecond)
|
|
continue
|
|
}
|
|
return err
|
|
}
|
|
tmp := make([]byte, nr)
|
|
copy(tmp, buf)
|
|
go handlePacket(listener, tmp, addr, rh)
|
|
}
|
|
}
|