diff options
Diffstat (limited to 'core')
-rw-r--r-- | core/core.go | 62 | ||||
-rw-r--r-- | core/core_test.go | 19 | ||||
-rw-r--r-- | core/msg_codec.go | 32 |
3 files changed, 35 insertions, 78 deletions
diff --git a/core/core.go b/core/core.go index a045e60..ab0c6bf 100644 --- a/core/core.go +++ b/core/core.go @@ -28,7 +28,6 @@ import ( "git.fd.io/govpp.git/adapter" "git.fd.io/govpp.git/api" "git.fd.io/govpp.git/core/bin_api/vpe" - "github.com/fsnotify/fsnotify" ) const ( @@ -54,15 +53,6 @@ const ( Disconnected = iota ) -const ( - // watchedFolder is a folder where vpp's shared memory is supposed to be created. - // File system events are monitored in this folder. - watchedFolder = "/dev/shm/" - // watchedFile is a name of the file in the watchedFolder. Once the file is present - // the vpp is ready to accept a new connection. - watchedFile = watchedFolder + "vpe-api" -) - // ConnectionEvent is a notification about change in the VPP connection state. type ConnectionEvent struct { // Timestamp holds the time when the event has been generated. @@ -197,10 +187,13 @@ func newConnection(vppAdapter adapter.VppAdapter) (*Connection, error) { return nil, errors.New("only one connection per process is supported") } - conn = &Connection{vpp: vppAdapter, codec: &MsgCodec{}} - conn.channels = make(map[uint32]*api.Channel) - conn.msgIDs = make(map[string]uint16) - conn.notifSubscriptions = make(map[uint16][]*api.NotifSubscription) + conn = &Connection{ + vpp: vppAdapter, + codec: &MsgCodec{}, + channels: make(map[uint32]*api.Channel), + msgIDs: make(map[string]uint16), + notifSubscriptions: make(map[uint16][]*api.NotifSubscription), + } conn.vpp.SetMsgCallback(msgCallback) return conn, nil @@ -235,50 +228,13 @@ func (c *Connection) disconnectVPP() { } } -func fileExists(name string) bool { - if _, err := os.Stat(name); err != nil { - if os.IsNotExist(err) { - return false - } - } - return true -} - -// waitForVpp blocks until shared memory for sending bin api calls -// is present on the file system. -func waitForVpp() error { - watcher, err := fsnotify.NewWatcher() - if err != nil { - return err - } - defer watcher.Close() - - err = watcher.Add(watchedFolder) - if err != nil { - return err - } - - if fileExists(watchedFile) { - return nil - } - - for { - ev := <-watcher.Events - if ev.Name == watchedFile && (ev.Op&fsnotify.Create) == fsnotify.Create { - break - } - } - return nil -} - // connectLoop attempts to connect to VPP until it succeeds. // Then it continues with healthCheckLoop. func (c *Connection) connectLoop(connChan chan ConnectionEvent) { // loop until connected for { - waitForVpp() - err := c.connectVPP() - if err == nil { + c.vpp.WaitReady() + if err := c.connectVPP(); err == nil { // signal connected event connChan <- ConnectionEvent{Timestamp: time.Now(), State: Connected} break diff --git a/core/core_test.go b/core/core_test.go index 3184ef5..37c0b9c 100644 --- a/core/core_test.go +++ b/core/core_test.go @@ -12,13 +12,14 @@ // See the License for the specific language governing permissions and // limitations under the License. -package core +package core_test import ( "testing" "git.fd.io/govpp.git/adapter/mock" "git.fd.io/govpp.git/api" + "git.fd.io/govpp.git/core" "git.fd.io/govpp.git/core/bin_api/vpe" "git.fd.io/govpp.git/examples/bin_api/interfaces" @@ -27,7 +28,7 @@ import ( type testCtx struct { mockVpp *mock.VppAdapter - conn *Connection + conn *core.Connection ch *api.Channel } @@ -38,7 +39,7 @@ func setupTest(t *testing.T) *testCtx { ctx.mockVpp = &mock.VppAdapter{} var err error - ctx.conn, err = Connect(ctx.mockVpp) + ctx.conn, err = core.Connect(ctx.mockVpp) Expect(err).ShouldNot(HaveOccurred()) ctx.ch, err = ctx.conn.NewAPIChannel() @@ -146,7 +147,7 @@ func TestNotifications(t *testing.T) { func TestNilConnection(t *testing.T) { RegisterTestingT(t) - var conn *Connection + var conn *core.Connection ch, err := conn.NewAPIChannel() Expect(ch).Should(BeNil()) @@ -163,7 +164,7 @@ func TestDoubleConnection(t *testing.T) { ctx := setupTest(t) defer ctx.teardownTest() - conn, err := Connect(ctx.mockVpp) + conn, err := core.Connect(ctx.mockVpp) Expect(err).Should(HaveOccurred()) Expect(err.Error()).To(ContainSubstring("only one connection per process")) Expect(conn).Should(BeNil()) @@ -174,14 +175,14 @@ func TestAsyncConnection(t *testing.T) { defer ctx.teardownTest() ctx.conn.Disconnect() - conn, ch, err := AsyncConnect(ctx.mockVpp) + conn, ch, err := core.AsyncConnect(ctx.mockVpp) ctx.conn = conn Expect(err).ShouldNot(HaveOccurred()) Expect(conn).ShouldNot(BeNil()) ev := <-ch - Expect(ev.State).Should(BeEquivalentTo(Connected)) + Expect(ev.State).Should(BeEquivalentTo(core.Connected)) } func TestFullBuffer(t *testing.T) { @@ -218,7 +219,7 @@ func TestFullBuffer(t *testing.T) { func TestCodec(t *testing.T) { RegisterTestingT(t) - codec := &MsgCodec{} + codec := &core.MsgCodec{} // request data, err := codec.EncodeMsg(&vpe.CreateLoopback{MacAddress: []byte{1, 2, 3, 4, 5, 6}}, 11) @@ -254,7 +255,7 @@ func TestCodec(t *testing.T) { func TestCodecNegative(t *testing.T) { RegisterTestingT(t) - codec := &MsgCodec{} + codec := &core.MsgCodec{} // nil message for encoding data, err := codec.EncodeMsg(nil, 15) diff --git a/core/msg_codec.go b/core/msg_codec.go index c72b7f3..3887e3f 100644 --- a/core/msg_codec.go +++ b/core/msg_codec.go @@ -30,27 +30,27 @@ import ( // binary format as accepted by VPP. type MsgCodec struct{} -// vppRequestHeader struct contains header fields implemented by all VPP requests. -type vppRequestHeader struct { +// VppRequestHeader struct contains header fields implemented by all VPP requests. +type VppRequestHeader struct { VlMsgID uint16 ClientIndex uint32 Context uint32 } -// vppReplyHeader struct contains header fields implemented by all VPP replies. -type vppReplyHeader struct { +// VppReplyHeader struct contains header fields implemented by all VPP replies. +type VppReplyHeader struct { VlMsgID uint16 Context uint32 } -// vppEventHeader struct contains header fields implemented by all VPP events. -type vppEventHeader struct { +// VppEventHeader struct contains header fields implemented by all VPP events. +type VppEventHeader struct { VlMsgID uint16 Context uint32 } -// vppOtherHeader struct contains header fields implemented by other VPP messages (not requests nor replies). -type vppOtherHeader struct { +// VppOtherHeader struct contains header fields implemented by other VPP messages (not requests nor replies). +type VppOtherHeader struct { VlMsgID uint16 } @@ -72,13 +72,13 @@ func (*MsgCodec) EncodeMsg(msg api.Message, msgID uint16) ([]byte, error) { // encode message header var header interface{} if msg.GetMessageType() == api.RequestMessage { - header = &vppRequestHeader{VlMsgID: msgID} + header = &VppRequestHeader{VlMsgID: msgID} } else if msg.GetMessageType() == api.ReplyMessage { - header = &vppReplyHeader{VlMsgID: msgID} + header = &VppReplyHeader{VlMsgID: msgID} } else if msg.GetMessageType() == api.EventMessage { - header = &vppEventHeader{VlMsgID: msgID} + header = &VppEventHeader{VlMsgID: msgID} } else { - header = &vppOtherHeader{VlMsgID: msgID} + header = &VppOtherHeader{VlMsgID: msgID} } err := struc.Pack(buf, header) if err != nil { @@ -115,13 +115,13 @@ func (*MsgCodec) DecodeMsg(data []byte, msg api.Message) error { // check which header is expected var header interface{} if msg.GetMessageType() == api.RequestMessage { - header = &vppRequestHeader{} + header = &VppRequestHeader{} } else if msg.GetMessageType() == api.ReplyMessage { - header = &vppReplyHeader{} + header = &VppReplyHeader{} } else if msg.GetMessageType() == api.EventMessage { - header = &vppEventHeader{} + header = &VppEventHeader{} } else { - header = &vppOtherHeader{} + header = &VppOtherHeader{} } // decode message header |