Unexport adapter implementations to make it clear that API interfaces should be used
[govpp.git] / adapter / vppapiclient / vppapiclient.go
index 34ad199..cf2665a 100644 (file)
@@ -91,25 +91,26 @@ const (
        vppShmFile = "vpe-api"
 )
 
-// global VPP binary API client adapter context
-var client *VppClient
+// global VPP binary API client, library vppapiclient only supports
+// single connection at a time
+var globalVppClient *vppClient
 
-// VppClient is the default implementation of the VppAPI.
-type VppClient struct {
+// stubVppClient is the default implementation of the VppAPI.
+type vppClient struct {
        shmPrefix   string
        msgCallback adapter.MsgCallback
 }
 
 // NewVppClient returns a new VPP binary API client.
-func NewVppClient(shmPrefix string) *VppClient {
-       return &VppClient{
+func NewVppClient(shmPrefix string) adapter.VppAPI {
+       return &vppClient{
                shmPrefix: shmPrefix,
        }
 }
 
 // Connect connects the process to VPP.
-func (a *VppClient) Connect() error {
-       if client != nil {
+func (a *vppClient) Connect() error {
+       if globalVppClient != nil {
                return fmt.Errorf("already connected to binary API, disconnect first")
        }
 
@@ -124,13 +125,13 @@ func (a *VppClient) Connect() error {
                return fmt.Errorf("connecting to VPP binary API failed (rc=%v)", rc)
        }
 
-       client = a
+       globalVppClient = a
        return nil
 }
 
 // Disconnect disconnects the process from VPP.
-func (a *VppClient) Disconnect() error {
-       client = nil
+func (a *vppClient) Disconnect() error {
+       globalVppClient = nil
 
        rc := C.govpp_disconnect()
        if rc != 0 {
@@ -141,7 +142,7 @@ func (a *VppClient) Disconnect() error {
 }
 
 // GetMsgID returns a runtime message ID for the given message name and CRC.
-func (a *VppClient) GetMsgID(msgName string, msgCrc string) (uint16, error) {
+func (a *vppClient) GetMsgID(msgName string, msgCrc string) (uint16, error) {
        nameAndCrc := C.CString(msgName + "_" + msgCrc)
        defer C.free(unsafe.Pointer(nameAndCrc))
 
@@ -155,7 +156,7 @@ func (a *VppClient) GetMsgID(msgName string, msgCrc string) (uint16, error) {
 }
 
 // SendMsg sends a binary-encoded message to VPP.
-func (a *VppClient) SendMsg(context uint32, data []byte) error {
+func (a *vppClient) SendMsg(context uint32, data []byte) error {
        rc := C.govpp_send(C.uint32_t(context), unsafe.Pointer(&data[0]), C.size_t(len(data)))
        if rc != 0 {
                return fmt.Errorf("unable to send the message (rc=%v)", rc)
@@ -165,13 +166,13 @@ func (a *VppClient) SendMsg(context uint32, data []byte) error {
 
 // SetMsgCallback sets a callback function that will be called by the adapter
 // whenever a message comes from VPP.
-func (a *VppClient) SetMsgCallback(cb adapter.MsgCallback) {
+func (a *vppClient) SetMsgCallback(cb adapter.MsgCallback) {
        a.msgCallback = cb
 }
 
 // WaitReady blocks until shared memory for sending
 // binary api calls is present on the file system.
-func (a *VppClient) WaitReady() error {
+func (a *vppClient) WaitReady() error {
        var path string
 
        // join the path to the shared memory segment
@@ -219,5 +220,5 @@ func go_msg_callback(msgID C.uint16_t, data unsafe.Pointer, size C.size_t) {
        sliceHeader := &reflect.SliceHeader{Data: uintptr(data), Len: int(size), Cap: int(size)}
        byteSlice := *(*[]byte)(unsafe.Pointer(sliceHeader))
 
-       client.msgCallback(uint16(msgID), byteSlice)
+       globalVppClient.msgCallback(uint16(msgID), byteSlice)
 }