Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions cmd/di.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ func (di *Dependencies) bootstrapNodeComponents(nodeOptions node.Options) {
di.StatsKeeper = stats.NewSessionStatsKeeper(time.Now)

di.ConnectionRegistry = connection.NewRegistry()
di.ConnectionManager = connection.NewManager(di.MysteriumClient, dialogFactory, promiseIssuerFactory, di.ConnectionRegistry, di.StatsKeeper)
di.ConnectionManager = connection.NewManager(di.MysteriumClient, dialogFactory, promiseIssuerFactory, di.ConnectionRegistry.CreateConnection, di.StatsKeeper)

router := tequilapi.NewAPIRouter()
tequilapi_endpoints.AddRouteForStop(router, utils.SoftKiller(di.Shutdown))
Expand All @@ -197,19 +197,20 @@ func (di *Dependencies) bootstrapNodeComponents(nodeOptions node.Options) {
}

func (di *Dependencies) bootstrapServiceOpenvpn(nodeOptions node.Options) {
di.ConnectionRegistry.Register("openvpn", openvpn.NewProcessBasedConnectionFactory(
connectionFactory := openvpn.NewProcessBasedConnectionFactory(
di.MysteriumClient,
nodeOptions.Openvpn.BinaryPath,
nodeOptions.Directories.Config,
nodeOptions.Directories.Runtime,
di.StatsKeeper,
di.LocationOriginal,
di.SignerFactory,
))
)
di.ConnectionRegistry.Register("openvpn", connectionFactory.CreateConnection)
}

func (di *Dependencies) bootstrapServiceNoop(nodeOptions node.Options) {
di.ConnectionRegistry.Register("dummy", &service_noop.ConnectionFactory{})
di.ConnectionRegistry.Register("dummy", service_noop.NewConnectionCreator())
}

// BootstrapServiceComponents initiates ServiceManager dependency
Expand Down
6 changes: 0 additions & 6 deletions core/connection/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,6 @@ type Connection interface {
// StateChannel is the channel we receive state change events on
type StateChannel chan State

// ConnectionCreator creates new vpn client by given session,
// consumer identity, provider identity and uses state channel to report state changes
type ConnectionCreator interface {
CreateConnection(ConnectOptions, StateChannel) (Connection, error)
}

// PromiseIssuer issues promises from consumer to provider.
// Consumer signs those promises.
type PromiseIssuer interface {
Expand Down
30 changes: 17 additions & 13 deletions core/connection/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,17 @@ var (
ErrUnsupportedServiceType = errors.New("unsupported service type in proposal")
)

// ConnectionCreator creates new vpn client by given session,
// consumer identity, provider identity and uses state channel to report state changes
type ConnectionCreator func(ConnectOptions, StateChannel) (Connection, error)

type connectionManager struct {
//these are passed on creation
mysteriumClient server.Client
newDialog DialogCreator
newPromiseIssuer PromiseIssuerCreator
connectionCreator ConnectionCreator
statsKeeper stats.SessionStatsKeeper
mysteriumClient server.Client
newDialog DialogCreator
newPromiseIssuer PromiseIssuerCreator
newConnection ConnectionCreator
statsKeeper stats.SessionStatsKeeper
//these are populated by Connect at runtime
ctx context.Context
mutex sync.RWMutex
Expand All @@ -70,13 +74,13 @@ func NewManager(
statsKeeper stats.SessionStatsKeeper,
) *connectionManager {
return &connectionManager{
statsKeeper: statsKeeper,
mysteriumClient: mysteriumClient,
newDialog: dialogCreator,
newPromiseIssuer: promiseIssuerCreator,
connectionCreator: connectionCreator,
status: statusNotConnected(),
cleanConnection: warnOnClean,
statsKeeper: statsKeeper,
mysteriumClient: mysteriumClient,
newDialog: dialogCreator,
newPromiseIssuer: promiseIssuerCreator,
newConnection: connectionCreator,
status: statusNotConnected(),
cleanConnection: warnOnClean,
}
}

Expand Down Expand Up @@ -149,7 +153,7 @@ func (manager *connectionManager) startConnection(consumerID, providerID identit

stateChannel := make(chan State, 10)

connection, err := manager.connectionCreator.CreateConnection(
connection, err := manager.newConnection(
ConnectOptions{
SessionID: sessionID,
SessionConfig: sessionConfig,
Expand Down
2 changes: 1 addition & 1 deletion core/connection/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ func (tc *testContext) SetupTest() {

tc.fakeStatsKeeper = &fakeSessionStatsKeeper{}

tc.connManager = NewManager(tc.fakeDiscoveryClient, dialogCreator, promiseIssuerFactory, tc.fakeConnectionFactory, tc.fakeStatsKeeper)
tc.connManager = NewManager(tc.fakeDiscoveryClient, dialogCreator, promiseIssuerFactory, tc.fakeConnectionFactory.CreateConnection, tc.fakeStatsKeeper)
}

func (tc *testContext) TestWhenNoConnectionIsMadeStatusIsNotConnected() {
Expand Down
4 changes: 2 additions & 2 deletions core/connection/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ func (registry *Registry) Register(serviceType string, creator ConnectionCreator

// CreateConnection create plugable connection
func (registry *Registry) CreateConnection(options ConnectOptions, stateChannel StateChannel) (Connection, error) {
creator, exists := registry.creators[options.Proposal.ServiceType]
createConnection, exists := registry.creators[options.Proposal.ServiceType]
if !exists {
return nil, ErrUnsupportedServiceType
}

return creator.CreateConnection(options, stateChannel)
return createConnection(options, stateChannel)
}
6 changes: 3 additions & 3 deletions core/connection/registry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ import (
"github.com/stretchr/testify/assert"
)

var _ ConnectionCreator = &Registry{}
var _ ConnectionCreator = (&Registry{}).CreateConnection

var (
connectionMock = &vpnClientFake{}
connectionFactory = &connectionFactoryFake{
fakeVpnClient: connectionMock,
connectionFactory = func(connectionParams ConnectOptions, stateChannel StateChannel) (Connection, error) {
return connectionMock, nil
}
)

Expand Down
15 changes: 7 additions & 8 deletions services/noop/connection_factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,11 @@ import (
"github.com/mysteriumnetwork/node/core/connection"
)

// ConnectionFactory creates noop connections
type ConnectionFactory struct{}

// CreateConnection implements the connection.ConnectionCreator interface
func (establisher *ConnectionFactory) CreateConnection(options connection.ConnectOptions, stateChannel connection.StateChannel) (connection.Connection, error) {
return &Connection{
stateChannel: stateChannel,
}, nil
// NewConnectionCreator creates noop connections
func NewConnectionCreator() connection.ConnectionCreator {
return func(options connection.ConnectOptions, stateChannel connection.StateChannel) (connection.Connection, error) {
return &Connection{
stateChannel: stateChannel,
}, nil
}
}
4 changes: 2 additions & 2 deletions services/openvpn/connection_factory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ func (cf *cacheFake) RefreshAndGet() (location.Location, error) {
return cf.location, cf.err
}

var _ connection.ConnectionCreator = &ProcessBasedConnectionFactory{}
var _ connection.ConnectionCreator = (&ProcessBasedConnectionFactory{}).CreateConnection

func fakeSignerFactory(id identity.Identity) identity.Signer {
func fakeSignerFactory(_ identity.Identity) identity.Signer {
return &identity.SignerFake{}
}

Expand Down