~fnux/yggdrasil-go-coap

576fe73ef26446fa75698992025ca861265c7ebd — Jozef Kralik 2 years ago 12af0ac
publish message where user can set coap options
2 files changed, 52 insertions(+), 4 deletions(-)

M examples/mcast/client/main.go
M multicastClient.go
M examples/mcast/client/main.go => examples/mcast/client/main.go +38 -2
@@ 1,11 1,42 @@
package main

import (
	"bytes"
	"fmt"
	"log"

	coap "github.com/go-ocf/go-coap"
	"github.com/ugorji/go/codec"
)

func decodeMsgToDebug(resp coap.Message, tag string) {
	var m interface{}
	err := codec.NewDecoderBytes(resp.Payload(), new(codec.CborHandle)).Decode(&m)
	out := fmt.Sprintf("RAW:\n%v\n", resp.Payload())
	if err == nil {
		bw := new(bytes.Buffer)
		h := new(codec.JsonHandle)
		h.BasicHandle.Canonical = true
		enc := codec.NewEncoder(bw, h)
		err = enc.Encode(m)
		if err != nil {
			log.Printf("Cannot encode %v to JSON: %v", m, err)
		} else {
			out = fmt.Sprintf("JSON:\n%v\n", bw.String())
		}
	}

	log.Print(
		"\n-------------------", tag, "------------------\n",
		"Path: ", resp.PathString(), "\n",
		"Code: ", resp.Code(), "\n",
		"Type: ", resp.Type(), "\n",
		"Query: ", resp.Options(coap.URIQuery), "\n",
		"ContentFormat: ", resp.Options(coap.ContentFormat), "\n",
		out,
	)
}

func main() {
	client := &coap.MulticastClient{}



@@ 15,8 46,13 @@ func main() {
	}

	sync := make(chan bool)
	_, err = conn.Publish("/oic/res", func(req *coap.Request) {
		log.Printf("Got message: %#v from %v", req.Msg, req.Client.RemoteAddr())
	req, err := conn.NewGetRequest("/oic/res")
	if err != nil {
		log.Fatalf("Error sending request: %v", err)
	}
	req.SetOption(coap.URIQuery, "rt=oic.wk.d")
	_, err = conn.PublishMsg(req, func(req *coap.Request) {
		decodeMsgToDebug(req.Msg, "MCAST")
		sync <- true
	})
	if err != nil {

M multicastClient.go => multicastClient.go +14 -2
@@ 134,16 134,28 @@ func (r *ResponseWaiter) Cancel() error {
// Publish subscribe to sever on path. After subscription and every change on path,
// server sends immediately response
func (mconn *MulticastClientConn) Publish(path string, responseHandler func(req *Request)) (*ResponseWaiter, error) {
	req, err := mconn.conn.NewGetRequest(path)
	req, err := mconn.NewGetRequest(path)
	if err != nil {
		return nil, err
	}
	return mconn.PublishMsg(req, responseHandler)
}

// PublishMsg subscribe to sever with GET message. After subscription and every change on path,
// server sends immediately response
func (mconn *MulticastClientConn) PublishMsg(req Message, responseHandler func(req *Request)) (*ResponseWaiter, error) {
	if req.Code() != GET || req.PathString() == "" {
		return nil, ErrInvalidRequest
	}

	path := req.PathString()

	r := &ResponseWaiter{
		token: req.Token(),
		path:  path,
		conn:  mconn,
	}
	err = mconn.client.multicastHandler.Add(req.Token(), func(w ResponseWriter, r *Request) {
	err := mconn.client.multicastHandler.Add(req.Token(), func(w ResponseWriter, r *Request) {
		var err error
		switch r.Msg.Code() {
		case GET, POST, PUT, DELETE: