Skip to content

Commit

Permalink
multiple services now do start. Need to adjust nats next
Browse files Browse the repository at this point in the history
  • Loading branch information
vkuznecovas committed Nov 13, 2018
1 parent 5368c85 commit 003c239
Show file tree
Hide file tree
Showing 18 changed files with 184 additions and 71 deletions.
12 changes: 6 additions & 6 deletions cmd/commands/cli/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,17 +163,17 @@ func (c *cliApp) handleActions(line string) {
func (c *cliApp) connect(argsString string) {
options := strings.Fields(argsString)

if len(options) < 2 {
info("Please type in the provider identity. Connect <consumer-identity> <provider-identity> [disable-kill-switch]")
if len(options) < 3 {
info("Please type in the provider identity. Connect <consumer-identity> <provider-identity> <service-type> [disable-kill-switch]")
return
}

consumerID, providerID := options[0], options[1]
consumerID, providerID, serviceType := options[0], options[1], options[2]

var disableKill bool
var err error
if len(options) > 2 {
disableKillStr := options[2]
if len(options) > 3 {
disableKillStr := options[3]
disableKill, err = strconv.ParseBool(disableKillStr)
if err != nil {
info("Please use true / false for <disable-kill-switch>")
Expand All @@ -195,7 +195,7 @@ func (c *cliApp) connect(argsString string) {

status("CONNECTING", "from:", consumerID, "to:", providerID)

_, err = c.tequilapi.Connect(consumerID, providerID, connectOptions)
_, err = c.tequilapi.Connect(consumerID, providerID, serviceType, connectOptions)
if err != nil {
warn(err)
return
Expand Down
4 changes: 2 additions & 2 deletions cmd/di.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,8 +228,9 @@ func (di *Dependencies) bootstrapServiceOpenvpn(nodeOptions node.Options) {
}

func (di *Dependencies) bootstrapServiceNoop(nodeOptions node.Options) {
service_noop.Bootstrap()
di.ServiceRegistry.Register(service_noop.ServiceType, func(serviceOptions service.Options) (service.Service, error) {
return service_noop.NewManager(), nil
return service_noop.NewManager(di.LocationResolver), nil
})
di.ConnectionRegistry.Register(service_noop.ServiceType, service_noop.NewConnectionCreator())
}
Expand All @@ -244,7 +245,6 @@ func (di *Dependencies) bootstrapServiceComponents(nodeOptions node.Options) {
)

discoveryService := discovery.NewService(di.IdentityRegistry, di.IdentityRegistration, di.MysteriumClient, di.SignerFactory)

newDialogWaiter := func(providerID identity.Identity) communication.DialogWaiter {
return nats_dialog.NewDialogWaiter(
nats_discovery.NewAddressGenerate(di.NetworkDefinition.BrokerAddress, providerID),
Expand Down
2 changes: 1 addition & 1 deletion core/connection/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ type PromiseIssuerCreator func(issuerID identity.Identity, dialog communication.
// Manager interface provides methods to manage connection
type Manager interface {
// Connect creates new connection from given consumer to provider, reports error if connection already exists
Connect(consumerID identity.Identity, providerID identity.Identity, params ConnectParams) error
Connect(consumerID identity.Identity, providerID identity.Identity, serviceType string, params ConnectParams) error
// Status queries current status of connection
Status() ConnectionStatus
// Disconnect closes established connection, reports error if no connection
Expand Down
12 changes: 6 additions & 6 deletions core/connection/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ func NewManager(
}
}

func (manager *connectionManager) Connect(consumerID, providerID identity.Identity, params ConnectParams) (err error) {
func (manager *connectionManager) Connect(consumerID, providerID identity.Identity, serviceType string, params ConnectParams) (err error) {
if manager.status.State != NotConnected {
return ErrAlreadyExists
}
Expand All @@ -114,14 +114,14 @@ func (manager *connectionManager) Connect(consumerID, providerID identity.Identi
}
}()

err = manager.startConnection(consumerID, providerID, params)
err = manager.startConnection(consumerID, providerID, serviceType, params)
if err == context.Canceled {
return ErrConnectionCancelled
}
return err
}

func (manager *connectionManager) startConnection(consumerID, providerID identity.Identity, params ConnectParams) (err error) {
func (manager *connectionManager) startConnection(consumerID, providerID identity.Identity, serviceType string, params ConnectParams) (err error) {
manager.mutex.Lock()
cancelCtx := manager.cleanConnection
manager.mutex.Unlock()
Expand All @@ -141,7 +141,7 @@ func (manager *connectionManager) startConnection(consumerID, providerID identit
}
}()

proposal, err := manager.findProposalByProviderID(providerID)
proposal, err := manager.findProposalByProviderID(providerID, serviceType)
if err != nil {
return err
}
Expand Down Expand Up @@ -228,8 +228,8 @@ func warnOnClean() {
}

// TODO this can be extracted as dependency later when node selection criteria will be clear
func (manager *connectionManager) findProposalByProviderID(providerID identity.Identity) (proposal dto.ServiceProposal, err error) {
proposals, err := manager.mysteriumClient.FindProposals(providerID.Address)
func (manager *connectionManager) findProposalByProviderID(providerID identity.Identity, serviceType string) (proposal dto.ServiceProposal, err error) {
proposals, err := manager.mysteriumClient.FindProposals(providerID.Address, serviceType)
if err != nil {
return
}
Expand Down
36 changes: 18 additions & 18 deletions core/connection/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,20 +122,20 @@ func (tc *testContext) TestWhenNoConnectionIsMadeStatusIsNotConnected() {
func (tc *testContext) TestWithUnknownProviderConnectionIsNotMade() {
noProposalsError := errors.New("provider has no service proposals")

assert.Equal(tc.T(), noProposalsError, tc.connManager.Connect(myID, identity.FromAddress("unknown-node"), ConnectParams{}))
assert.Equal(tc.T(), noProposalsError, tc.connManager.Connect(myID, identity.FromAddress("unknown-node"), activeServiceType, ConnectParams{}))
assert.Equal(tc.T(), statusNotConnected(), tc.connManager.Status())
}

func (tc *testContext) TestOnConnectErrorStatusIsNotConnected() {
tc.fakeConnectionFactory.mockError = errors.New("fatal connection error")

assert.Error(tc.T(), tc.connManager.Connect(myID, activeProviderID, ConnectParams{}))
assert.Error(tc.T(), tc.connManager.Connect(myID, activeProviderID, activeServiceType, ConnectParams{}))
assert.Equal(tc.T(), statusNotConnected(), tc.connManager.Status())
assert.True(tc.T(), tc.fakeDialog.closed)
}

func (tc *testContext) TestWhenManagerMadeConnectionStatusReturnsConnectedStateAndSessionId() {
err := tc.connManager.Connect(myID, activeProviderID, ConnectParams{})
err := tc.connManager.Connect(myID, activeProviderID, activeServiceType, ConnectParams{})
assert.NoError(tc.T(), err)
assert.Equal(tc.T(), statusConnected(establishedSessionID), tc.connManager.Status())
assert.True(tc.T(), tc.fakeStatsKeeper.sessionStartMarked)
Expand All @@ -145,7 +145,7 @@ func (tc *testContext) TestStatusReportsConnectingWhenConnectionIsInProgress() {
tc.fakeConnectionFactory.mockConnection.onStartReportStates = []fakeState{}

go func() {
tc.connManager.Connect(myID, activeProviderID, ConnectParams{})
tc.connManager.Connect(myID, activeProviderID, activeServiceType, ConnectParams{})
}()

waitABit()
Expand All @@ -156,7 +156,7 @@ func (tc *testContext) TestStatusReportsConnectingWhenConnectionIsInProgress() {

func (tc *testContext) TestStatusReportsDisconnectingThenNotConnected() {
tc.fakeConnectionFactory.mockConnection.onStopReportStates = []fakeState{}
err := tc.connManager.Connect(myID, activeProviderID, ConnectParams{})
err := tc.connManager.Connect(myID, activeProviderID, activeServiceType, ConnectParams{})
assert.NoError(tc.T(), err)
assert.Equal(tc.T(), statusConnected(establishedSessionID), tc.connManager.Status())

Expand All @@ -170,23 +170,23 @@ func (tc *testContext) TestStatusReportsDisconnectingThenNotConnected() {
}

func (tc *testContext) TestConnectResultsInAlreadyConnectedErrorWhenConnectionExists() {
assert.NoError(tc.T(), tc.connManager.Connect(myID, activeProviderID, ConnectParams{}))
assert.Equal(tc.T(), ErrAlreadyExists, tc.connManager.Connect(myID, activeProviderID, ConnectParams{}))
assert.NoError(tc.T(), tc.connManager.Connect(myID, activeProviderID, activeServiceType, ConnectParams{}))
assert.Equal(tc.T(), ErrAlreadyExists, tc.connManager.Connect(myID, activeProviderID, activeServiceType, ConnectParams{}))
}

func (tc *testContext) TestDisconnectReturnsErrorWhenNoConnectionExists() {
assert.Equal(tc.T(), ErrNoConnection, tc.connManager.Disconnect())
}

func (tc *testContext) TestReconnectingStatusIsReportedWhenOpenVpnGoesIntoReconnectingState() {
assert.NoError(tc.T(), tc.connManager.Connect(myID, activeProviderID, ConnectParams{}))
assert.NoError(tc.T(), tc.connManager.Connect(myID, activeProviderID, activeServiceType, ConnectParams{}))
tc.fakeConnectionFactory.mockConnection.reportState(reconnectingState)
waitABit()
assert.Equal(tc.T(), statusReconnecting(), tc.connManager.Status())
}

func (tc *testContext) TestDoubleDisconnectResultsInError() {
assert.NoError(tc.T(), tc.connManager.Connect(myID, activeProviderID, ConnectParams{}))
assert.NoError(tc.T(), tc.connManager.Connect(myID, activeProviderID, activeServiceType, ConnectParams{}))
assert.Equal(tc.T(), statusConnected(establishedSessionID), tc.connManager.Status())
assert.NoError(tc.T(), tc.connManager.Disconnect())
waitABit()
Expand All @@ -195,27 +195,27 @@ func (tc *testContext) TestDoubleDisconnectResultsInError() {
}

func (tc *testContext) TestTwoConnectDisconnectCyclesReturnNoError() {
assert.NoError(tc.T(), tc.connManager.Connect(myID, activeProviderID, ConnectParams{}))
assert.NoError(tc.T(), tc.connManager.Connect(myID, activeProviderID, activeServiceType, ConnectParams{}))
assert.Equal(tc.T(), statusConnected(establishedSessionID), tc.connManager.Status())
assert.NoError(tc.T(), tc.connManager.Disconnect())
waitABit()
assert.Equal(tc.T(), statusNotConnected(), tc.connManager.Status())

assert.NoError(tc.T(), tc.connManager.Connect(myID, activeProviderID, ConnectParams{}))
assert.NoError(tc.T(), tc.connManager.Connect(myID, activeProviderID, activeServiceType, ConnectParams{}))
assert.Equal(tc.T(), statusConnected(establishedSessionID), tc.connManager.Status())
assert.NoError(tc.T(), tc.connManager.Disconnect())
waitABit()
assert.Equal(tc.T(), statusNotConnected(), tc.connManager.Status())

}

func (tc *testContext) TestConnectFailsIfConnectionFactoryReturnsError() {
func (tc *testContext) TestConnectFailsIfOpenvpnFactoryReturnsError() {
tc.fakeConnectionFactory.mockError = errors.New("failed to create vpn instance")
assert.Error(tc.T(), tc.connManager.Connect(myID, activeProviderID, ConnectParams{}))
assert.Error(tc.T(), tc.connManager.Connect(myID, activeProviderID, activeServiceType, ConnectParams{}))
}

func (tc *testContext) TestStatusIsConnectedWhenConnectCommandReturnsWithoutError() {
tc.connManager.Connect(myID, activeProviderID, ConnectParams{})
tc.connManager.Connect(myID, activeProviderID, activeServiceType, ConnectParams{})
assert.Equal(tc.T(), statusConnected(establishedSessionID), tc.connManager.Status())
}

Expand All @@ -226,7 +226,7 @@ func (tc *testContext) TestConnectingInProgressCanBeCanceled() {
var err error
go func() {
defer connectWaiter.Done()
err = tc.connManager.Connect(myID, activeProviderID, ConnectParams{})
err = tc.connManager.Connect(myID, activeProviderID, activeServiceType, ConnectParams{})
}()

waitABit()
Expand All @@ -247,7 +247,7 @@ func (tc *testContext) TestConnectMethodReturnsErrorIfConnectionExitsDuringConne
var err error
go func() {
defer connectWaiter.Done()
err = tc.connManager.Connect(myID, activeProviderID, ConnectParams{})
err = tc.connManager.Connect(myID, activeProviderID, activeServiceType, ConnectParams{})
}()
waitABit()
tc.fakeConnectionFactory.mockConnection.reportState(processExited)
Expand All @@ -256,15 +256,15 @@ func (tc *testContext) TestConnectMethodReturnsErrorIfConnectionExitsDuringConne
}

func (tc *testContext) Test_PromiseIssuer_WhenManagerMadeConnectionIsStarted() {
err := tc.connManager.Connect(myID, activeProviderID, ConnectParams{})
err := tc.connManager.Connect(myID, activeProviderID, activeServiceType, ConnectParams{})
assert.NoError(tc.T(), err)
assert.True(tc.T(), tc.fakePromiseIssuer.startCalled)
}

func (tc *testContext) Test_PromiseIssuer_OnConnectErrorIsStopped() {
tc.fakeConnectionFactory.mockError = errors.New("fatal connection error")

err := tc.connManager.Connect(myID, activeProviderID, ConnectParams{})
err := tc.connManager.Connect(myID, activeProviderID, activeServiceType, ConnectParams{})
assert.Error(tc.T(), err)
assert.True(tc.T(), tc.fakePromiseIssuer.stopCalled)
}
Expand Down
2 changes: 1 addition & 1 deletion e2e/connection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ func consumerConnectFlow(t *testing.T, tequilapi *tequilapi_client.Client, consu
})
assert.NoError(t, err)

connectionStatus, err = tequilapi.Connect(consumerID, proposal.ProviderID, endpoints.ConnectOptions{true})
connectionStatus, err = tequilapi.Connect(consumerID, proposal.ProviderID, "openvpn", endpoints.ConnectOptions{true})
assert.NoError(t, err)

err = waitForCondition(func() (bool, error) {
Expand Down
2 changes: 1 addition & 1 deletion server/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import (
type Client interface {
RegisterIdentity(id identity.Identity, signer identity.Signer) (err error)

FindProposals(providerID string) (proposals []dto_discovery.ServiceProposal, err error)
FindProposals(providerID string, serviceType string) (proposals []dto_discovery.ServiceProposal, err error)
RegisterProposal(proposal dto_discovery.ServiceProposal, signer identity.Signer) (err error)
UnregisterProposal(proposal dto_discovery.ServiceProposal, signer identity.Signer) (err error)
PingProposal(proposal dto_discovery.ServiceProposal, signer identity.Signer) (err error)
Expand Down
5 changes: 2 additions & 3 deletions server/mysterium_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,12 +133,12 @@ func (mApi *mysteriumAPI) PingProposal(proposal dto_discovery.ServiceProposal, s
}

// FindProposals fetches currently active service proposals from discovery
func (mApi *mysteriumAPI) FindProposals(providerID string) ([]dto_discovery.ServiceProposal, error) {
func (mApi *mysteriumAPI) FindProposals(providerID string, serviceType string) ([]dto_discovery.ServiceProposal, error) {
values := url.Values{}
if providerID != "" {
values.Set("node_key", providerID)
values.Set("service_type", serviceType)
}

req, err := requests.NewGetRequest(mApi.discoveryAPIAddress, "proposals", values)
if err != nil {
return nil, err
Expand All @@ -151,7 +151,6 @@ func (mApi *mysteriumAPI) FindProposals(providerID string) ([]dto_discovery.Serv
}

log.Info(mysteriumAPILogPrefix, "Proposals fetched: ", len(proposalsResponse.Proposals))

return proposalsResponse.Proposals, nil
}

Expand Down
12 changes: 8 additions & 4 deletions server/mysterium_api_fake.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,15 +79,19 @@ func (client *ClientFake) PingProposal(proposal dto_discovery.ServiceProposal, s
}

// FindProposals fetches announced proposals by given filters
func (client *ClientFake) FindProposals(providerID string) (proposals []dto_discovery.ServiceProposal, err error) {
func (client *ClientFake) FindProposals(providerID string, serviceType string) (proposals []dto_discovery.ServiceProposal, err error) {
log.Info(mysteriumAPILogPrefix, "Fake proposals requested for provider: ", providerID)

for _, proposal := range client.proposalsMock {
var filterMatched = true
var providerMatched = true
if providerID != "" {
filterMatched = filterMatched && (providerID == proposal.ProviderID)
providerMatched = providerMatched && (providerID == proposal.ProviderID)
}
if filterMatched {
var serviceMatched = true
if serviceType != "" {
serviceMatched = serviceMatched && (serviceType == proposal.ServiceType)
}
if providerMatched && serviceMatched {
proposals = append(proposals, proposal)
}
}
Expand Down
30 changes: 30 additions & 0 deletions services/noop/bootstrap.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package noop

import (
"encoding/json"

dto_discovery "github.com/mysteriumnetwork/node/service_discovery/dto"
)

// Bootstrap is called on program initialization time and registers various deserializers related to opepnvpn service
func Bootstrap() {
dto_discovery.RegisterServiceDefinitionUnserializer(
ServiceType,
func(rawDefinition *json.RawMessage) (dto_discovery.ServiceDefinition, error) {
var definition ServiceDefinition
err := json.Unmarshal(*rawDefinition, &definition)

return definition, err
},
)

dto_discovery.RegisterPaymentMethodUnserializer(
PaymentMethodNoop,
func(rawDefinition *json.RawMessage) (dto_discovery.PaymentMethod, error) {
var method PaymentNoop
err := json.Unmarshal(*rawDefinition, &method)

return method, err
},
)
}
20 changes: 18 additions & 2 deletions services/noop/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,33 +18,49 @@
package noop

import (
"sync"
"time"

"github.com/mysteriumnetwork/node/core/connection"
)

// Connection which does no real tunneling
type Connection struct {
stateChannel connection.StateChannel
isRunning bool
noopConnection sync.WaitGroup
stateChannel connection.StateChannel
}

// Start implements the connection.Connection interface
func (c *Connection) Start() error {
c.noopConnection.Add(1)
c.isRunning = true

c.stateChannel <- connection.Connecting

time.Sleep(5 * time.Second)
c.stateChannel <- connection.Connected

return nil
}

// Wait implements the connection.Connection interface
func (c *Connection) Wait() error {
if c.isRunning {
c.noopConnection.Wait()
}
return nil
}

// Stop implements the connection.Connection interface
func (c *Connection) Stop() {
if !c.isRunning {
return
}

c.isRunning = false
c.stateChannel <- connection.Disconnecting
time.Sleep(2 * time.Second)
c.stateChannel <- connection.NotConnected
c.noopConnection.Done()
close(c.stateChannel)
}
Loading

0 comments on commit 003c239

Please sign in to comment.