mirror of
https://github.com/mainflux/mainflux.git
synced 2025-04-27 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>
67 lines
1.2 KiB
Go
67 lines
1.2 KiB
Go
package coap
|
|
|
|
import (
|
|
"net"
|
|
"time"
|
|
)
|
|
|
|
const (
|
|
// ResponseTimeout is the amount of time to wait for a
|
|
// response.
|
|
ResponseTimeout = time.Second * 2
|
|
// ResponseRandomFactor is a multiplier for response backoff.
|
|
ResponseRandomFactor = 1.5
|
|
// MaxRetransmit is the maximum number of times a message will
|
|
// be retransmitted.
|
|
MaxRetransmit = 4
|
|
)
|
|
|
|
// Conn is a CoAP client connection.
|
|
type Conn struct {
|
|
conn *net.UDPConn
|
|
buf []byte
|
|
}
|
|
|
|
// Dial connects a CoAP client.
|
|
func Dial(n, addr string) (*Conn, error) {
|
|
uaddr, err := net.ResolveUDPAddr(n, addr)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
s, err := net.DialUDP("udp", nil, uaddr)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return &Conn{s, make([]byte, maxPktLen)}, nil
|
|
}
|
|
|
|
// Send a message. Get a response if there is one.
|
|
func (c *Conn) Send(req Message) (*Message, error) {
|
|
err := Transmit(c.conn, nil, req)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
if !req.IsConfirmable() {
|
|
return nil, nil
|
|
}
|
|
|
|
rv, err := Receive(c.conn, c.buf)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return &rv, nil
|
|
}
|
|
|
|
// Receive a message.
|
|
func (c *Conn) Receive() (*Message, error) {
|
|
rv, err := Receive(c.conn, c.buf)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &rv, nil
|
|
}
|