"git.fd.io/govpp.git/adapter"
"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
const (
- _ replyMode = 0
- useRepliesQueue = 1 // use replies in the queue
- useReplyHandlers = 2 // use reply handler
+ _ replyMode = iota
+ useRepliesQueue // use replies in the queue
+ useReplyHandlers // use reply handler
)
// VppAdapter represents a mock VPP adapter that can be used for unit/integration testing instead of the vppapiclient adapter.
type VppAdapter struct {
- callback func(context uint32, msgId uint16, data []byte)
+ callback adapter.MsgCallback
+ msgIDSeq uint16
+ access sync.RWMutex
msgNameToIds map[string]uint16
msgIDsToName map[uint16]string
- msgIDSeq uint16
binAPITypes map[string]reflect.Type
- access sync.RWMutex
+ repliesLock sync.Mutex // mutex for the queue
replies []reply // FIFO queue of messages
replyHandlers []ReplyHandler // callbacks that are able to calculate mock responses
- repliesLock sync.Mutex // mutex for the queue
mode replyMode // mode in which the mock operates
}
a.msgNameToIds[msgName] = msgID
a.msgIDsToName[msgID] = msgName
- log.Println("VPP GetMessageId ", msgID, " name:", msgName, " crc:", msgCrc)
-
return msgID, nil
}
Data: data,
})
if finished {
- a.callback(clientID, msgID, reply)
+ a.callback(msgID, clientID, reply)
return nil
}
}
fallthrough
+
case useRepliesQueue:
a.repliesLock.Lock()
defer a.repliesLock.Unlock()
}
if msg.Msg.GetMessageType() == api.ReplyMessage {
struc.Pack(buf, &codec.VppReplyHeader{VlMsgID: msgID, Context: context})
- } else if msg.Msg.GetMessageType() == api.EventMessage {
- struc.Pack(buf, &codec.VppEventHeader{VlMsgID: msgID, Context: 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})
}
struc.Pack(buf, msg.Msg)
- a.callback(context, msgID, buf.Bytes())
+ a.callback(msgID, context, buf.Bytes())
}
a.replies = a.replies[1:]
msgID := uint16(defaultReplyMsgID)
struc.Pack(buf, &codec.VppReplyHeader{VlMsgID: msgID, Context: clientID})
struc.Pack(buf, &defaultReply{})
- a.callback(clientID, msgID, buf.Bytes())
+ a.callback(msgID, clientID, buf.Bytes())
}
return nil
}
// SetMsgCallback sets a callback function that will be called by the adapter whenever a message comes from the mock.
-func (a *VppAdapter) SetMsgCallback(cb func(context uint32, msgID uint16, data []byte)) {
+func (a *VppAdapter) SetMsgCallback(cb adapter.MsgCallback) {
a.callback = cb
}