Generator improvements
[govpp.git] / core / connection.go
index c4048f0..08f08f5 100644 (file)
@@ -65,11 +65,6 @@ type ConnectionEvent struct {
        Error error
 }
 
-var (
-       connLock sync.RWMutex // lock for the global connection
-       conn     *Connection  // global handle to the Connection (used in the message receive callback)
-)
-
 // Connection represents a shared memory connection to VPP via vppAdapter.
 type Connection struct {
        vppClient adapter.VppAPI // VPP binary API client adapter
@@ -111,10 +106,7 @@ func newConnection(binapi adapter.VppAPI) *Connection {
 // This call blocks until VPP is connected, or an error occurs. Only one connection attempt will be performed.
 func Connect(binapi adapter.VppAPI) (*Connection, error) {
        // create new connection handle
-       c, err := createConnection(binapi)
-       if err != nil {
-               return nil, err
-       }
+       c := newConnection(binapi)
 
        // blocking attempt to connect to VPP
        if err := c.connectVPP(); err != nil {
@@ -130,10 +122,7 @@ func Connect(binapi adapter.VppAPI) (*Connection, error) {
 // Connected/Disconnected events. In case of disconnect, the library will asynchronously try to reconnect.
 func AsyncConnect(binapi adapter.VppAPI) (*Connection, chan ConnectionEvent, error) {
        // create new connection handle
-       c, err := createConnection(binapi)
-       if err != nil {
-               return nil, nil, err
-       }
+       c := newConnection(binapi)
 
        // asynchronously attempt to connect to VPP
        connChan := make(chan ConnectionEvent, NotificationChanBufSize)
@@ -142,35 +131,6 @@ func AsyncConnect(binapi adapter.VppAPI) (*Connection, chan ConnectionEvent, err
        return c, connChan, nil
 }
 
-// Disconnect disconnects from VPP and releases all connection-related resources.
-func (c *Connection) Disconnect() {
-       if c == nil {
-               return
-       }
-
-       connLock.Lock()
-       defer connLock.Unlock()
-
-       if c.vppClient != nil {
-               c.disconnectVPP()
-       }
-       conn = nil
-}
-
-// newConnection returns new connection handle.
-func createConnection(binapi adapter.VppAPI) (*Connection, error) {
-       connLock.Lock()
-       defer connLock.Unlock()
-
-       if conn != nil {
-               return nil, errors.New("only one connection per process is supported")
-       }
-
-       conn = newConnection(binapi)
-
-       return conn, nil
-}
-
 // connectVPP performs blocking attempt to connect to VPP.
 func (c *Connection) connectVPP() error {
        log.Debug("Connecting to VPP..")
@@ -193,6 +153,24 @@ func (c *Connection) connectVPP() error {
        return nil
 }
 
+// Disconnect disconnects from VPP and releases all connection-related resources.
+func (c *Connection) Disconnect() {
+       if c == nil {
+               return
+       }
+
+       if c.vppClient != nil {
+               c.disconnectVPP()
+       }
+}
+
+// disconnectVPP disconnects from VPP in case it is connected.
+func (c *Connection) disconnectVPP() {
+       if atomic.CompareAndSwapUint32(&c.vppConnected, 1, 0) {
+               c.vppClient.Disconnect()
+       }
+}
+
 func (c *Connection) NewAPIChannel() (api.Channel, error) {
        return c.newAPIChannel(RequestChanBufSize, ReplyChanBufSize)
 }
@@ -270,7 +248,7 @@ func (c *Connection) retrieveMessageIDs() (err error) {
                c.msgMap[msgID] = msg
        }
 
-       msgs := api.GetAllMessages()
+       msgs := api.GetRegisteredMessages()
 
        for name, msg := range msgs {
                msgID, err := c.vppClient.GetMsgID(msg.GetMessageName(), msg.GetCrcString())
@@ -314,13 +292,6 @@ func (c *Connection) retrieveMessageIDs() (err error) {
        return nil
 }
 
-// disconnectVPP disconnects from VPP in case it is connected.
-func (c *Connection) disconnectVPP() {
-       if atomic.CompareAndSwapUint32(&c.vppConnected, 1, 0) {
-               c.vppClient.Disconnect()
-       }
-}
-
 // connectLoop attempts to connect to VPP until it succeeds.
 // Then it continues with healthCheckLoop.
 func (c *Connection) connectLoop(connChan chan ConnectionEvent) {