mirror of
https://github.com/mainflux/mainflux.git
synced 2025-05-04 22:17:59 +08:00

* Switch CoAP lib Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> * Revert removed adapter code Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> * WIP CoAP refactor Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> * Add auth key Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> * Fix observers map Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> * Fix reading message body Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> * Fix subtopic parsing Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> * Fix error handling Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> * Fix multi-protocol communication Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> * Separate client from observer Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> * Remove unused config Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> * Remove TCP option Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> * Inline error check Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> * Add logging client errors Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> * Replace RWMutex since we're not using RLock Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> * Inline error handling Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> * Inline error handling Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com>
121 lines
2.9 KiB
Go
121 lines
2.9 KiB
Go
package tcp
|
|
|
|
import (
|
|
"context"
|
|
"io"
|
|
"net"
|
|
|
|
"github.com/plgd-dev/go-coap/v2/message"
|
|
"github.com/plgd-dev/go-coap/v2/mux"
|
|
"github.com/plgd-dev/go-coap/v2/tcp/message/pool"
|
|
)
|
|
|
|
type ClientTCP struct {
|
|
cc *ClientConn
|
|
}
|
|
|
|
func NewClientTCP(cc *ClientConn) *ClientTCP {
|
|
return &ClientTCP{
|
|
cc: cc,
|
|
}
|
|
}
|
|
|
|
func (c *ClientTCP) Ping(ctx context.Context) error {
|
|
return c.cc.Ping(ctx)
|
|
}
|
|
|
|
func (c *ClientTCP) Delete(ctx context.Context, path string, opts ...message.Option) (*message.Message, error) {
|
|
resp, err := c.cc.Delete(ctx, path, opts...)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer pool.ReleaseMessage(resp)
|
|
return pool.ConvertTo(resp)
|
|
}
|
|
|
|
func (c *ClientTCP) Put(ctx context.Context, path string, contentFormat message.MediaType, payload io.ReadSeeker, opts ...message.Option) (*message.Message, error) {
|
|
resp, err := c.cc.Put(ctx, path, contentFormat, payload, opts...)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer pool.ReleaseMessage(resp)
|
|
return pool.ConvertTo(resp)
|
|
}
|
|
|
|
func (c *ClientTCP) Post(ctx context.Context, path string, contentFormat message.MediaType, payload io.ReadSeeker, opts ...message.Option) (*message.Message, error) {
|
|
resp, err := c.cc.Post(ctx, path, contentFormat, payload, opts...)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer pool.ReleaseMessage(resp)
|
|
return pool.ConvertTo(resp)
|
|
}
|
|
|
|
func (c *ClientTCP) Get(ctx context.Context, path string, opts ...message.Option) (*message.Message, error) {
|
|
resp, err := c.cc.Get(ctx, path, opts...)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer pool.ReleaseMessage(resp)
|
|
return pool.ConvertTo(resp)
|
|
}
|
|
|
|
func (c *ClientTCP) Close() error {
|
|
return c.cc.Close()
|
|
}
|
|
|
|
func (c *ClientTCP) RemoteAddr() net.Addr {
|
|
return c.cc.RemoteAddr()
|
|
}
|
|
|
|
func (c *ClientTCP) Context() context.Context {
|
|
return c.cc.Context()
|
|
}
|
|
|
|
func (c *ClientTCP) WriteMessage(req *message.Message) error {
|
|
r, err := pool.ConvertFrom(req)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer pool.ReleaseMessage(r)
|
|
return c.cc.WriteMessage(r)
|
|
}
|
|
|
|
func (c *ClientTCP) Do(req *message.Message) (*message.Message, error) {
|
|
r, err := pool.ConvertFrom(req)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer pool.ReleaseMessage(r)
|
|
resp, err := c.cc.Do(r)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer pool.ReleaseMessage(resp)
|
|
return pool.ConvertTo(resp)
|
|
}
|
|
|
|
func createClientConnObserveHandler(observeFunc func(notification *message.Message)) func(n *pool.Message) {
|
|
return func(n *pool.Message) {
|
|
muxn, err := pool.ConvertTo(n)
|
|
if err != nil {
|
|
return
|
|
}
|
|
observeFunc(muxn)
|
|
}
|
|
}
|
|
|
|
func (c *ClientTCP) Observe(ctx context.Context, path string, observeFunc func(notification *message.Message), opts ...message.Option) (mux.Observation, error) {
|
|
return c.cc.Observe(ctx, path, createClientConnObserveHandler(observeFunc), opts...)
|
|
}
|
|
|
|
// Sequence acquires sequence number.
|
|
func (c *ClientTCP) Sequence() uint64 {
|
|
return c.cc.Sequence()
|
|
}
|
|
|
|
// ClientConn get's underlaying client connection.
|
|
func (c *ClientTCP) ClientConn() interface{} {
|
|
return c.cc
|
|
}
|