Refactored binapi generator with message encoding
[govpp.git] / api / binapi.go
index ac9f8a4..20ddd28 100644 (file)
@@ -15,6 +15,8 @@
 package api
 
 import (
+       "fmt"
+       "reflect"
        "time"
 )
 
@@ -52,9 +54,6 @@ type Message interface {
 type DataType interface {
        // GetTypeName returns the original VPP name of the data type, as defined in the VPP API.
        GetTypeName() string
-
-       // GetCrcString returns the string with CRC checksum of the data type definition (the string represents a hexadecimal number).
-       GetCrcString() string
 }
 
 // ChannelProvider provides the communication channel with govpp core.
@@ -92,41 +91,63 @@ type Channel interface {
        // It will return an error if any of the given messages are not compatible.
        CheckCompatiblity(msgs ...Message) error
 
-       // Close closes the API channel and releases all API channel-related resources in the ChannelProvider.
+       // Close closes the API channel and releases all API channel-related resources
+       // in the ChannelProvider.
        Close()
 }
 
 // RequestCtx is helper interface which allows to receive reply on request.
 type RequestCtx interface {
-       // ReceiveReply receives a reply from VPP (blocks until a reply is delivered from VPP, or until an error occurs).
-       // The reply will be decoded into the msg argument. Error will be returned if the response cannot be received or decoded.
+       // ReceiveReply receives a reply from VPP (blocks until a reply is delivered
+       // from VPP, or until an error occurs). The reply will be decoded into the msg
+       // argument. Error will be returned if the response cannot be received or decoded.
        ReceiveReply(msg Message) error
 }
 
 // MultiRequestCtx is helper interface which allows to receive reply on multi-request.
 type MultiRequestCtx interface {
-       // ReceiveReply receives a reply from VPP (blocks until a reply is delivered from VPP, or until an error occurs).
-       // The reply will be decoded into the msg argument. If the last reply has been already consumed, lastReplyReceived is
-       // set to true. Do not use the message itself if lastReplyReceived is true - it won't be filled with actual data.
-       // Error will be returned if the response cannot be received or decoded.
+       // ReceiveReply receives a reply from VPP (blocks until a reply is delivered
+       // from VPP, or until an error occurs).The reply will be decoded into the msg
+       // argument. If the last reply has been already consumed, lastReplyReceived is
+       // set to true. Do not use the message itself if lastReplyReceived is
+       // true - it won't be filled with actual data.Error will be returned if the
+       // response cannot be received or decoded.
        ReceiveReply(msg Message) (lastReplyReceived bool, err error)
 }
 
-// SubscriptionCtx is helper interface which allows to control subscription for notification events.
+// SubscriptionCtx is helper interface which allows to control subscription for
+// notification events.
 type SubscriptionCtx interface {
-       // Unsubscribe unsubscribes from receiving the notifications tied to the subscription context.
+       // Unsubscribe unsubscribes from receiving the notifications tied to the
+       // subscription context.
        Unsubscribe() error
 }
 
-var registeredMessages = make(map[string]Message)
+// CompatibilityError is the error type usually returned by CheckCompatibility
+// method of Channel. It contains list of all the compatible/incompatible messages.
+type CompatibilityError struct {
+       CompatibleMessages   []string
+       IncompatibleMessages []string
+}
+
+func (c *CompatibilityError) Error() string {
+       return fmt.Sprintf("%d/%d messages incompatible", len(c.IncompatibleMessages), len(c.CompatibleMessages)+len(c.IncompatibleMessages))
+}
+
+var (
+       registeredMessageTypes = make(map[reflect.Type]string)
+       registeredMessages     = make(map[string]Message)
+)
 
 // RegisterMessage is called from generated code to register message.
 func RegisterMessage(x Message, name string) {
-       name = x.GetMessageName() + "_" + x.GetCrcString()
-       /*if _, ok := registeredMessages[name]; ok {
-               panic(fmt.Errorf("govpp: duplicate message registered: %s (%s)", name, x.GetCrcString()))
-       }*/
-       registeredMessages[name] = x
+       typ := reflect.TypeOf(x)
+       namecrc := x.GetMessageName() + "_" + x.GetCrcString()
+       if _, ok := registeredMessageTypes[typ]; ok {
+               panic(fmt.Errorf("govpp: message type %v already registered as %s (%s)", typ, name, namecrc))
+       }
+       registeredMessages[namecrc] = x
+       registeredMessageTypes[typ] = name
 }
 
 // GetRegisteredMessages returns list of all registered messages.
@@ -134,6 +155,12 @@ func GetRegisteredMessages() map[string]Message {
        return registeredMessages
 }
 
-// GoVppAPIPackageIsVersion1 is referenced from generated binapi files
+// GetRegisteredMessageTypes returns list of all registered message types.
+func GetRegisteredMessageTypes() map[reflect.Type]string {
+       return registeredMessageTypes
+}
+
+// GoVppAPIPackageIsVersionX is referenced from generated binapi files
 // to assert that that code is compatible with this version of the GoVPP api package.
 const GoVppAPIPackageIsVersion1 = true
+const GoVppAPIPackageIsVersion2 = true