From d1f24d37bd447b64e402298bb8eb2479681facf9 Mon Sep 17 00:00:00 2001 From: Ondrej Fabry Date: Fri, 17 Jul 2020 10:36:28 +0200 Subject: Improve binapi generator - simplified Size/Marshal/Unmarshal methods - replace struc in unions with custom marshal/unmarshal - fix imports in generated files - fix mock adapter - generate rpc service using low-level stream API (dumps generate control ping or stream msg..) - move examples/binapi to binapi and generate all API for latest release - add binapigen.Plugin for developing custom generator plugins - optionally generate HTTP handlers (REST API) for RPC services - add govpp program for browsing VPP API Change-Id: I092e9ed2b0c17972b3476463c3d4b14dd76ed42b Signed-off-by: Ondrej Fabry --- adapter/mock/mock_vpp_adapter.go | 86 ++++++++++++++++++++++++++-------------- 1 file changed, 56 insertions(+), 30 deletions(-) (limited to 'adapter/mock/mock_vpp_adapter.go') diff --git a/adapter/mock/mock_vpp_adapter.go b/adapter/mock/mock_vpp_adapter.go index c05148d..b7fa002 100644 --- a/adapter/mock/mock_vpp_adapter.go +++ b/adapter/mock/mock_vpp_adapter.go @@ -17,7 +17,7 @@ package mock import ( - "bytes" + "encoding/binary" "log" "reflect" "sync" @@ -26,7 +26,6 @@ import ( "git.fd.io/govpp.git/adapter/mock/binapi" "git.fd.io/govpp.git/api" "git.fd.io/govpp.git/codec" - "github.com/lunixbochs/struc" ) type replyMode int @@ -58,6 +57,38 @@ type defaultReply struct { Retval int32 } +func (*defaultReply) GetMessageName() string { return "mock_default_reply" } +func (*defaultReply) GetCrcString() string { return "xxxxxxxx" } +func (*defaultReply) GetMessageType() api.MessageType { + return api.ReplyMessage +} +func (m *defaultReply) Size() int { + if m == nil { + return 0 + } + var size int + // field[1] m.Retval + size += 4 + return size +} +func (m *defaultReply) Marshal(b []byte) ([]byte, error) { + var buf *codec.Buffer + if b == nil { + buf = codec.NewBuffer(make([]byte, m.Size())) + } else { + buf = codec.NewBuffer(b) + } + // field[1] m.Retval + buf.EncodeUint32(uint32(m.Retval)) + return buf.Bytes(), nil +} +func (m *defaultReply) Unmarshal(b []byte) error { + buf := codec.NewBuffer(b) + // field[1] m.Retval + m.Retval = int32(buf.DecodeUint32()) + return nil +} + // MessageDTO is a structure used for propagating information to ReplyHandlers. type MessageDTO struct { MsgID uint16 @@ -178,19 +209,16 @@ func (a *VppAdapter) ReplyBytes(request MessageDTO, reply api.Message) ([]byte, } log.Println("ReplyBytes ", replyMsgID, " ", reply.GetMessageName(), " clientId: ", request.ClientID) - buf := new(bytes.Buffer) - err = struc.Pack(buf, &codec.VppReplyHeader{ - VlMsgID: replyMsgID, - Context: request.ClientID, - }) + data, err := codec.DefaultCodec.EncodeMsg(reply, replyMsgID) if err != nil { return nil, err } - if err = struc.Pack(buf, reply); err != nil { - return nil, err + if reply.GetMessageType() == api.ReplyMessage { + binary.BigEndian.PutUint32(data[2:6], request.ClientID) + } else if reply.GetMessageType() == api.RequestMessage { + binary.BigEndian.PutUint32(data[6:10], request.ClientID) } - - return buf.Bytes(), nil + return data, nil } // GetMsgID returns mocked message ID for the given message name and CRC. @@ -229,16 +257,14 @@ func (a *VppAdapter) SendMsg(clientID uint32, data []byte) error { for i := len(a.replyHandlers) - 1; i >= 0; i-- { replyHandler := a.replyHandlers[i] - buf := bytes.NewReader(data) - reqHeader := codec.VppRequestHeader{} - struc.Unpack(buf, &reqHeader) + msgID := binary.BigEndian.Uint16(data[0:2]) a.access.Lock() - reqMsgName := a.msgIDsToName[reqHeader.VlMsgID] + reqMsgName := a.msgIDsToName[msgID] a.access.Unlock() reply, msgID, finished := replyHandler(MessageDTO{ - MsgID: reqHeader.VlMsgID, + MsgID: msgID, MsgName: reqMsgName, ClientID: clientID, Data: data, @@ -259,23 +285,21 @@ func (a *VppAdapter) SendMsg(clientID uint32, data []byte) error { reply := a.replies[0] for _, msg := range reply.msgs { msgID, _ := a.GetMsgID(msg.Msg.GetMessageName(), msg.Msg.GetCrcString()) - buf := new(bytes.Buffer) context := clientID if msg.hasCtx { context = setMultipart(context, msg.Multipart) context = setSeqNum(context, msg.SeqNum) } + data, err := codec.DefaultCodec.EncodeMsg(msg.Msg, msgID) + if err != nil { + panic(err) + } if msg.Msg.GetMessageType() == api.ReplyMessage { - struc.Pack(buf, &codec.VppReplyHeader{VlMsgID: msgID, Context: context}) + binary.BigEndian.PutUint32(data[2:6], context) } else if msg.Msg.GetMessageType() == api.RequestMessage { - struc.Pack(buf, &codec.VppRequestHeader{VlMsgID: msgID, Context: context}) - } else if msg.Msg.GetMessageType() == api.EventMessage { - struc.Pack(buf, &codec.VppEventHeader{VlMsgID: msgID}) - } else { - struc.Pack(buf, &codec.VppOtherHeader{VlMsgID: msgID}) + binary.BigEndian.PutUint32(data[6:10], context) } - struc.Pack(buf, msg.Msg) - a.callback(msgID, buf.Bytes()) + a.callback(msgID, data) } a.replies = a.replies[1:] @@ -290,11 +314,13 @@ func (a *VppAdapter) SendMsg(clientID uint32, data []byte) error { //fallthrough default: // return default reply - buf := new(bytes.Buffer) msgID := uint16(defaultReplyMsgID) - struc.Pack(buf, &codec.VppReplyHeader{VlMsgID: msgID, Context: clientID}) - struc.Pack(buf, &defaultReply{}) - a.callback(msgID, buf.Bytes()) + data, err := codec.DefaultCodec.EncodeMsg(&defaultReply{}, msgID) + if err != nil { + panic(err) + } + binary.BigEndian.PutUint32(data[2:6], clientID) + a.callback(msgID, data) } return nil } @@ -374,7 +400,7 @@ func (a *VppAdapter) MockReplyHandler(replyHandler ReplyHandler) { // MockClearReplyHanders clears all reply handlers that were registered // Will also set the mode to useReplyHandlers -func (a *VppAdapter) MockClearReplyHandlers () { +func (a *VppAdapter) MockClearReplyHandlers() { a.repliesLock.Lock() defer a.repliesLock.Unlock() -- cgit 1.2.3-korg