diff --git a/bindings/autheticatedConnection.go b/bindings/authenticatedConnection.go similarity index 53% rename from bindings/autheticatedConnection.go rename to bindings/authenticatedConnection.go index 837580897b1bb71f8faf9a6d916075731bad527f..ce33daf0e645dbda42badddcb6829712aa533413 100644 --- a/bindings/autheticatedConnection.go +++ b/bindings/authenticatedConnection.go @@ -1,6 +1,9 @@ package bindings import ( + "sync" + + "github.com/pkg/errors" "gitlab.com/elixxir/client/connect" "gitlab.com/elixxir/crypto/contact" ) @@ -46,3 +49,52 @@ func (c *Cmix) ConnectWithAuthentication(e2eId int, recipientContact []byte) (*A e2eClient.api, params) return authenticatedConnectionTrackerSingleton.make(connection), err } + +// connectionTracker is a singleton used to keep track of extant clients, allowing +// for race condition free passing over the bindings + +type authenticatedConnectionTracker struct { + connections map[int]*AuthenticatedConnection + count int + mux sync.RWMutex +} + +// make makes a client from an API client, assigning it a unique ID +func (act *authenticatedConnectionTracker) make(c connect.AuthenticatedConnection) *AuthenticatedConnection { + act.mux.Lock() + defer act.mux.Unlock() + + id := act.count + act.count++ + + act.connections[id] = &AuthenticatedConnection{ + Connection: Connection{ + connection: c, + id: id, + }, + } + + return act.connections[id] +} + +//get returns a client given its ID +func (act *authenticatedConnectionTracker) get(id int) (*AuthenticatedConnection, error) { + act.mux.RLock() + defer act.mux.RUnlock() + + c, exist := act.connections[id] + if !exist { + return nil, errors.Errorf("Cannot get client for id %d, client "+ + "does not exist", id) + } + + return c, nil +} + +//deletes a client if it exists +func (act *authenticatedConnectionTracker) delete(id int) { + act.mux.Lock() + defer act.mux.Unlock() + + delete(act.connections, id) +} diff --git a/bindings/authenticatedConnectionTracker.go b/bindings/authenticatedConnectionTracker.go deleted file mode 100644 index 57e260dc7c037b5cd82d5aebbb404d9b8b4ca751..0000000000000000000000000000000000000000 --- a/bindings/authenticatedConnectionTracker.go +++ /dev/null @@ -1,56 +0,0 @@ -package bindings - -import ( - "github.com/pkg/errors" - "gitlab.com/elixxir/client/connect" - "sync" -) - -// connectionTracker is a singleton used to keep track of extant clients, allowing -// for race condition free passing over the bindings - -type authenticatedConnectionTracker struct { - connections map[int]*AuthenticatedConnection - count int - mux sync.RWMutex -} - -// make makes a client from an API client, assigning it a unique ID -func (act *authenticatedConnectionTracker) make(c connect.AuthenticatedConnection) *AuthenticatedConnection { - act.mux.Lock() - defer act.mux.Unlock() - - id := act.count - act.count++ - - act.connections[id] = &AuthenticatedConnection{ - Connection: Connection{ - connection: c, - id: id, - }, - } - - return act.connections[id] -} - -//get returns a client given its ID -func (act *authenticatedConnectionTracker) get(id int) (*AuthenticatedConnection, error) { - act.mux.RLock() - defer act.mux.RUnlock() - - c, exist := act.connections[id] - if !exist { - return nil, errors.Errorf("Cannot get client for id %d, client "+ - "does not exist", id) - } - - return c, nil -} - -//deletes a client if it exists -func (act *authenticatedConnectionTracker) delete(id int) { - act.mux.Lock() - defer act.mux.Unlock() - - delete(act.connections, id) -}