diff options
author | Ondrej Fabry <ofabry@cisco.com> | 2018-10-23 13:20:31 +0200 |
---|---|---|
committer | Ondrej Fabry <ofabry@cisco.com> | 2018-10-23 13:20:31 +0200 |
commit | 6d6ad42ec0942a378c5550ad3472f388d7c85d49 (patch) | |
tree | c938eca12ca683335a518e6c0bcf7df3c00cea1f /adapter | |
parent | c00356ec332203f353fcd5f5992226940d90da92 (diff) |
Unexport adapter implementations to make it clear that API interfaces should be used
- this decision was made to follow more idiomatic Go, described in
https://golang.org/doc/effective_go.html#generality
Change-Id: I341556c792df77ca35a60a1e4afc541482f23734
Signed-off-by: Ondrej Fabry <ofabry@cisco.com>
Diffstat (limited to 'adapter')
-rw-r--r-- | adapter/vppapiclient/stat_client.go | 28 | ||||
-rw-r--r-- | adapter/vppapiclient/stat_client_stub.go | 16 | ||||
-rw-r--r-- | adapter/vppapiclient/vppapiclient.go | 33 | ||||
-rw-r--r-- | adapter/vppapiclient/vppapiclient_stub.go | 20 |
4 files changed, 54 insertions, 43 deletions
diff --git a/adapter/vppapiclient/stat_client.go b/adapter/vppapiclient/stat_client.go index 5f3b932..2e2d6db 100644 --- a/adapter/vppapiclient/stat_client.go +++ b/adapter/vppapiclient/stat_client.go @@ -161,21 +161,28 @@ var ( DefaultStatSocket = "/run/vpp/stats.sock" ) -// StatClient is the default implementation of StatsAPI. -type StatClient struct { +// global VPP stats API client, library vppapiclient only supports +// single connection at a time +var globalStatClient *statClient + +// stubStatClient is the default implementation of StatsAPI. +type statClient struct { socketName string } // NewStatClient returns new VPP stats API client. -func NewStatClient(socketName string) *StatClient { - return &StatClient{ +func NewStatClient(socketName string) adapter.StatsAPI { + return &statClient{ socketName: socketName, } } -func (c *StatClient) Connect() error { - var sockName string +func (c *statClient) Connect() error { + if globalStatClient != nil { + return fmt.Errorf("already connected to stats API, disconnect first") + } + var sockName string if c.socketName == "" { sockName = DefaultStatSocket } else { @@ -187,15 +194,18 @@ func (c *StatClient) Connect() error { return fmt.Errorf("connecting to VPP stats API failed (rc=%v)", rc) } + globalStatClient = c return nil } -func (c *StatClient) Disconnect() error { +func (c *statClient) Disconnect() error { + globalStatClient = nil + C.govpp_stat_disconnect() return nil } -func (c *StatClient) ListStats(patterns ...string) (stats []string, err error) { +func (c *statClient) ListStats(patterns ...string) (stats []string, err error) { dir := C.govpp_stat_segment_ls(convertStringSlice(patterns)) defer C.govpp_stat_segment_vec_free(unsafe.Pointer(dir)) @@ -209,7 +219,7 @@ func (c *StatClient) ListStats(patterns ...string) (stats []string, err error) { return stats, nil } -func (c *StatClient) DumpStats(patterns ...string) (stats []*adapter.StatEntry, err error) { +func (c *statClient) DumpStats(patterns ...string) (stats []*adapter.StatEntry, err error) { dir := C.govpp_stat_segment_ls(convertStringSlice(patterns)) defer C.govpp_stat_segment_vec_free(unsafe.Pointer(dir)) diff --git a/adapter/vppapiclient/stat_client_stub.go b/adapter/vppapiclient/stat_client_stub.go index 24f7e13..b42619c 100644 --- a/adapter/vppapiclient/stat_client_stub.go +++ b/adapter/vppapiclient/stat_client_stub.go @@ -20,26 +20,26 @@ import ( "git.fd.io/govpp.git/adapter" ) -// StatClient is just an stub adapter that does nothing. It builds only on Windows and OSX, where the real +// stubStatClient is just an stub adapter that does nothing. It builds only on Windows and OSX, where the real // VPP stats API client adapter does not build. Its sole purpose is to make the compiler happy on Windows and OSX. -type StatClient struct{} +type stubStatClient struct{} -func NewStatClient(socketName string) *StatClient { - return new(StatClient) +func NewStatClient(socketName string) adapter.StatsAPI { + return new(stubStatClient) } -func (*StatClient) Connect() error { +func (*stubStatClient) Connect() error { return adapter.ErrNotImplemented } -func (*StatClient) Disconnect() error { +func (*stubStatClient) Disconnect() error { return nil } -func (*StatClient) ListStats(patterns ...string) (statNames []string, err error) { +func (*stubStatClient) ListStats(patterns ...string) (statNames []string, err error) { return nil, adapter.ErrNotImplemented } -func (*StatClient) DumpStats(patterns ...string) ([]*adapter.StatEntry, error) { +func (*stubStatClient) DumpStats(patterns ...string) ([]*adapter.StatEntry, error) { return nil, adapter.ErrNotImplemented } diff --git a/adapter/vppapiclient/vppapiclient.go b/adapter/vppapiclient/vppapiclient.go index 34ad199..cf2665a 100644 --- a/adapter/vppapiclient/vppapiclient.go +++ b/adapter/vppapiclient/vppapiclient.go @@ -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) } diff --git a/adapter/vppapiclient/vppapiclient_stub.go b/adapter/vppapiclient/vppapiclient_stub.go index 6de89a7..c5566c6 100644 --- a/adapter/vppapiclient/vppapiclient_stub.go +++ b/adapter/vppapiclient/vppapiclient_stub.go @@ -20,34 +20,34 @@ import ( "git.fd.io/govpp.git/adapter" ) -// VppClient is just an stub adapter that does nothing. It builds only on Windows and OSX, where the real +// stubVppClient is just an stub adapter that does nothing. It builds only on Windows and OSX, where the real // VPP binary API client adapter does not build. Its sole purpose is to make the compiler happy on Windows and OSX. -type VppClient struct{} +type stubVppClient struct{} -func NewVppAdapter(string) *VppClient { - return &VppClient{} +func NewVppClient(string) adapter.VppAPI { + return &stubVppClient{} } -func (a *VppClient) Connect() error { +func (a *stubVppClient) Connect() error { return adapter.ErrNotImplemented } -func (a *VppClient) Disconnect() error { +func (a *stubVppClient) Disconnect() error { return nil } -func (a *VppClient) GetMsgID(msgName string, msgCrc string) (uint16, error) { +func (a *stubVppClient) GetMsgID(msgName string, msgCrc string) (uint16, error) { return 0, nil } -func (a *VppClient) SendMsg(clientID uint32, data []byte) error { +func (a *stubVppClient) SendMsg(clientID uint32, data []byte) error { return nil } -func (a *VppClient) SetMsgCallback(cb adapter.MsgCallback) { +func (a *stubVppClient) SetMsgCallback(cb adapter.MsgCallback) { // no op } -func (a *VppClient) WaitReady() error { +func (a *stubVppClient) WaitReady() error { return adapter.ErrNotImplemented } |