Skip to content
Snippets Groups Projects
Commit 13d8b4f0 authored by Richard T. Carback III's avatar Richard T. Carback III
Browse files

Fix name of authenticatedConnection and merge the (internal) tracker code into it.

parent c2e72b83
No related branches found
No related tags found
2 merge requests!510Release,!262Add params options to bindings
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)
}
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)
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment