~fnux/yggdrasil-go-coap

71303ed87d0112b56235b74ef11562283bdc6f99 — Michal Cizmazia 2 years ago 3638c5d
Change Request.Client to ClientConn

- Changed Request.Client from ClientCommander to ClientConn
8 files changed, 33 insertions(+), 23 deletions(-)

M blockwise.go
M client.go
M networksession.go
M request.go
M responsewriter.go
M server.go
M server_test.go
M tokenhandler.go
M blockwise.go => blockwise.go +7 -7
@@ 674,7 674,7 @@ func handleBlockWiseMsg(w ResponseWriter, r *Request, next func(w ResponseWriter
	if r.Msg.Token() != nil {
		switch r.Msg.Code() {
		case PUT, POST:
			if b, ok := r.Client.networkSession.(*blockWiseSession); ok {
			if b, ok := r.Client.networkSession().(*blockWiseSession); ok {
				msg, err := b.receivePayload(r.Ctx, true, r.Msg, nil, Block1, Continue)

				if err != nil {


@@ 758,14 758,14 @@ func (w *blockWiseResponseWriter) WriteMsg(msg Message) error {
//Write send whole message with context if size of payload is less then block szx otherwise
//send message via blockwise.
func (w *blockWiseResponseWriter) WriteMsgWithContext(ctx context.Context, msg Message) error {
	suggestedSzx := w.responseWriter.getReq().Client.networkSession.blockWiseSzx()
	suggestedSzx := w.responseWriter.getReq().Client.networkSession().blockWiseSzx()
	if respBlock2, ok := w.responseWriter.getReq().Msg.Option(Block2).(uint32); ok {
		szx, _, _, err := UnmarshalBlockOption(respBlock2)
		if err != nil {
			return err
		}
		//BERT is supported only via TCP
		if szx == BlockWiseSzxBERT && !w.responseWriter.getReq().Client.networkSession.IsTCP() {
		if szx == BlockWiseSzxBERT && !w.responseWriter.getReq().Client.networkSession().IsTCP() {
			return ErrInvalidBlockWiseSzx
		}
		suggestedSzx = szx


@@ 776,7 776,7 @@ func (w *blockWiseResponseWriter) WriteMsgWithContext(ctx context.Context, msg M
		return w.responseWriter.WriteMsgWithContext(ctx, msg)
	}

	if b, ok := w.responseWriter.getReq().Client.networkSession.(*blockWiseSession); ok {
	if b, ok := w.responseWriter.getReq().Client.networkSession().(*blockWiseSession); ok {
		_, err := b.sendPayload(ctx, true, Block2, suggestedSzx, w.responseWriter.getReq().Msg.Code(), msg)
		return err
	}


@@ 832,14 832,14 @@ func (w *blockWiseNoticeWriter) WriteMsg(msg Message) error {
//send only first block. For Get whole msg client must call Get to
//resource.
func (w *blockWiseNoticeWriter) WriteMsgWithContext(ctx context.Context, msg Message) error {
	suggestedSzx := w.responseWriter.getReq().Client.networkSession.blockWiseSzx()
	suggestedSzx := w.responseWriter.getReq().Client.networkSession().blockWiseSzx()
	if respBlock2, ok := w.responseWriter.getReq().Msg.Option(Block2).(uint32); ok {
		szx, _, _, err := UnmarshalBlockOption(respBlock2)
		if err != nil {
			return err
		}
		//BERT is supported only via TCP
		if szx == BlockWiseSzxBERT && !w.responseWriter.getReq().Client.networkSession.IsTCP() {
		if szx == BlockWiseSzxBERT && !w.responseWriter.getReq().Client.networkSession().IsTCP() {
			return ErrInvalidBlockWiseSzx
		}
		suggestedSzx = szx


@@ 850,7 850,7 @@ func (w *blockWiseNoticeWriter) WriteMsgWithContext(ctx context.Context, msg Mes
		return w.responseWriter.WriteMsgWithContext(ctx, msg)
	}

	if b, ok := w.responseWriter.getReq().Client.networkSession.(*blockWiseSession); ok {
	if b, ok := w.responseWriter.getReq().Client.networkSession().(*blockWiseSession); ok {
		s := newSender(false, Block2, suggestedSzx, w.responseWriter.getReq().Msg.Code(), msg)
		req, err := s.newReq(b)
		if err != nil {

M client.go => client.go +10 -2
@@ 215,6 215,10 @@ func (c *Client) DialWithContext(ctx context.Context, address string) (clientCon
	return clientConn, nil
}

func (co *ClientConn) networkSession() networkSession {
	return co.commander.networkSession
}

// LocalAddr implements the networkSession.LocalAddr method.
func (co *ClientConn) LocalAddr() net.Addr {
	return co.commander.LocalAddr()


@@ 350,8 354,12 @@ func (co *ClientConn) ObserveWithContext(ctx context.Context, path string, obser

// Close close connection
func (co *ClientConn) Close() error {
	co.srv.Shutdown()
	<-co.shutdownSync
	if co.srv != nil {
		co.srv.Shutdown()
	}
	if co.shutdownSync != nil {
		<-co.shutdownSync
	}
	return nil
}


M networksession.go => networksession.go +2 -2
@@ 589,13 589,13 @@ func (s *sessionUDP) handleSignals(w ResponseWriter, r *Request) bool {
}

func handleSignalMsg(w ResponseWriter, r *Request, next HandlerFunc) {
	if !r.Client.networkSession.handleSignals(w, r) {
	if !r.Client.networkSession().handleSignals(w, r) {
		next(w, r)
	}
}

func handlePairMsg(w ResponseWriter, r *Request, next HandlerFunc) {
	if !r.Client.networkSession.handlePairMsg(w, r) {
	if !r.Client.networkSession().handlePairMsg(w, r) {
		next(w, r)
	}
}

M request.go => request.go +1 -1
@@ 4,6 4,6 @@ import "context"

type Request struct {
	Msg    Message
	Client *ClientCommander
	Client *ClientConn
	Ctx    context.Context
}

M responsewriter.go => responsewriter.go +3 -3
@@ 51,14 51,14 @@ func responseWriterFromRequest(r *Request) ResponseWriter {
	case r.Msg.Code() == GET:
		switch {
		// set blockwise notice writer for observe
		case r.Client.networkSession.blockWiseEnabled() && r.Msg.Option(Observe) != nil:
		case r.Client.networkSession().blockWiseEnabled() && r.Msg.Option(Observe) != nil:
			w = &blockWiseNoticeWriter{responseWriter: w}
		// set blockwise if it is enabled
		case r.Client.networkSession.blockWiseEnabled():
		case r.Client.networkSession().blockWiseEnabled():
			w = &blockWiseResponseWriter{responseWriter: w}
		}
		w = &getResponseWriter{w}
	case r.Client.networkSession.blockWiseEnabled():
	case r.Client.networkSession().blockWiseEnabled():
		w = &blockWiseResponseWriter{responseWriter: w}
	}


M server.go => server.go +4 -2
@@ 457,7 457,8 @@ func (srv *Server) serveTCPconnection(ctx *shutdownContext, netConn net.Conn) er

		// We will block poller wait loop when
		// all pool workers are busy.
		srv.spawnWorker(&Request{Client: &ClientCommander{session}, Msg: msg, Ctx: sessCtx})
		c := ClientConn{commander: &ClientCommander{session}}
		srv.spawnWorker(&Request{Client: &c, Msg: msg, Ctx: sessCtx})
	}
}



@@ 536,7 537,8 @@ func (srv *Server) serveUDP(ctx *shutdownContext, conn *net.UDPConn) error {
		if err != nil {
			continue
		}
		srv.spawnWorker(&Request{Msg: msg, Client: &ClientCommander{session}, Ctx: sessCtx})
		c := ClientConn{commander: &ClientCommander{session}}
		srv.spawnWorker(&Request{Msg: msg, Client: &c, Ctx: sessCtx})
	}
}


M server_test.go => server_test.go +5 -5
@@ 48,7 48,7 @@ func CreateRespMessageByReq(isTCP bool, code COAPCode, req Message) Message {

func EchoServer(w ResponseWriter, r *Request) {
	if r.Msg.IsConfirmable() {
		err := w.WriteMsg(CreateRespMessageByReq(r.Client.networkSession.IsTCP(), Valid, r.Msg))
		err := w.WriteMsg(CreateRespMessageByReq(r.Client.networkSession().IsTCP(), Valid, r.Msg))
		if err != nil {
			log.Printf("Cannot write echo %v", err)
		}


@@ 57,7 57,7 @@ func EchoServer(w ResponseWriter, r *Request) {

func EchoServerBadID(w ResponseWriter, r *Request) {
	if r.Msg.IsConfirmable() {
		w.WriteMsg(CreateRespMessageByReq(r.Client.networkSession.IsTCP(), BadRequest, r.Msg))
		w.WriteMsg(CreateRespMessageByReq(r.Client.networkSession().IsTCP(), BadRequest, r.Msg))
	}
}



@@ 302,7 302,7 @@ func ChallegingServer(w ResponseWriter, r *Request) {
		panic(err.Error())
	}

	w.WriteMsg(CreateRespMessageByReq(r.Client.networkSession.IsTCP(), Valid, r.Msg))
	w.WriteMsg(CreateRespMessageByReq(r.Client.networkSession().IsTCP(), Valid, r.Msg))
}

func ChallegingServerTimeout(w ResponseWriter, r *Request) {


@@ 316,12 316,12 @@ func ChallegingServerTimeout(w ResponseWriter, r *Request) {
	req.SetOption(ContentFormat, TextPlain)
	ctx, cancel := context.WithTimeout(context.Background(), time.Second)
	defer cancel()
	_, err := r.Client.networkSession.ExchangeWithContext(ctx, req)
	_, err := r.Client.networkSession().ExchangeWithContext(ctx, req)
	if err == nil {
		panic("Error: expected timeout")
	}

	w.WriteMsg(CreateRespMessageByReq(r.Client.networkSession.IsTCP(), Valid, r.Msg))
	w.WriteMsg(CreateRespMessageByReq(r.Client.networkSession().IsTCP(), Valid, r.Msg))
}

func simpleChallengingMsg(t *testing.T, payload []byte, co *ClientConn) {

M tokenhandler.go => tokenhandler.go +1 -1
@@ 3,7 3,7 @@ package coap
import "sync"

func handleBySessionTokenHandler(w ResponseWriter, r *Request, next HandlerFunc) {
	r.Client.networkSession.TokenHandler().Handle(w, r, next)
	r.Client.networkSession().TokenHandler().Handle(w, r, next)
}

//TokenHandler container for regirstration handlers by token