Fixed incorrect message error in the stream API
[govpp.git] / api / binapi.go
1 // Copyright (c) 2017 Cisco and/or its affiliates.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at:
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 package api
16
17 import (
18         "path"
19         "reflect"
20 )
21
22 // GoVppAPIPackageIsVersionX is referenced from generated binapi files
23 // to assert that that code is compatible with this version of the GoVPP api package.
24 const (
25         GoVppAPIPackageIsVersion1 = true
26         GoVppAPIPackageIsVersion2 = true
27 )
28
29 // MessageType represents the type of a VPP message derived from message header fields.
30 type MessageType int
31
32 const (
33         // RequestMessage represents a VPP request message
34         RequestMessage MessageType = iota
35         // ReplyMessage represents a VPP reply message
36         ReplyMessage
37         // EventMessage represents a VPP event message
38         EventMessage
39         // OtherMessage represents other VPP message
40         OtherMessage
41 )
42
43 // Message is an interface that is implemented by all VPP Binary API messages generated by the binapigenerator.
44 type Message interface {
45         // GetMessageName returns the original VPP name of the message, as defined in the VPP API.
46         GetMessageName() string
47
48         // GetCrcString returns the string with CRC checksum of the message definition (the string represents a hexadecimal number).
49         GetCrcString() string
50
51         // GetMessageType returns the type of the VPP message.
52         GetMessageType() MessageType
53 }
54
55 // DataType is an interface that is implemented by all VPP Binary API data types by the binapi_generator.
56 type DataType interface {
57         // GetTypeName returns the original VPP name of the data type, as defined in the VPP API.
58         GetTypeName() string
59 }
60
61 var (
62         registeredMessages     = make(map[string]map[string]Message)
63         registeredMessageTypes = make(map[string]map[reflect.Type]string)
64 )
65
66 // RegisterMessage is called from generated code to register message.
67 func RegisterMessage(x Message, name string) {
68         binapiPath := path.Dir(reflect.TypeOf(x).Elem().PkgPath())
69         if _, ok := registeredMessages[binapiPath]; !ok {
70                 registeredMessages[binapiPath] = make(map[string]Message)
71                 registeredMessageTypes[binapiPath] = make(map[reflect.Type]string)
72         }
73         registeredMessages[binapiPath][x.GetMessageName()+"_"+x.GetCrcString()] = x
74         registeredMessageTypes[binapiPath][reflect.TypeOf(x)] = name
75 }
76
77 // GetRegisteredMessages returns list of all registered messages.
78 func GetRegisteredMessages() map[string]map[string]Message {
79         return registeredMessages
80 }
81
82 // GetRegisteredMessageTypes returns list of all registered message types.
83 func GetRegisteredMessageTypes() map[string]map[reflect.Type]string {
84         return registeredMessageTypes
85 }