Skip to content

Commit

Permalink
Multiple services seem to work now
Browse files Browse the repository at this point in the history
  • Loading branch information
vkuznecovas committed Nov 13, 2018
1 parent 003c239 commit d1f3899
Show file tree
Hide file tree
Showing 9 changed files with 50 additions and 18 deletions.
18 changes: 9 additions & 9 deletions cmd/di.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,10 +244,13 @@ func (di *Dependencies) bootstrapServiceComponents(nodeOptions node.Options) {
di.SignerFactory,
)

discoveryService := discovery.NewService(di.IdentityRegistry, di.IdentityRegistration, di.MysteriumClient, di.SignerFactory)
newDialogWaiter := func(providerID identity.Identity) communication.DialogWaiter {
di.ServiceRegistry = service.NewRegistry()
di.ServiceSessionStorage = session.NewStorageMemory()

newDialogWaiter := func(providerID identity.Identity, serviceType string) communication.DialogWaiter {
address := nats_discovery.NewAddressGenerate(di.NetworkDefinition.BrokerAddress, providerID, serviceType)
return nats_dialog.NewDialogWaiter(
nats_discovery.NewAddressGenerate(di.NetworkDefinition.BrokerAddress, providerID),
address,
di.SignerFactory(providerID),
di.IdentityRegistry,
)
Expand All @@ -263,18 +266,15 @@ func (di *Dependencies) bootstrapServiceComponents(nodeOptions node.Options) {
return session.NewDialogHandler(sessionManagerFactory)
}

di.ServiceRegistry = service.NewRegistry()
di.ServiceSessionStorage = session.NewStorageMemory()

if len(nodeOptions.ServiceTypes) > 0 {
serviceManagerMap := make(map[string]service.RunnableService, len(nodeOptions.ServiceTypes))
for _, serviceType := range nodeOptions.ServiceTypes {
serviceManagerMap[serviceType] = service.NewManager(
for i := range nodeOptions.ServiceTypes {
serviceManagerMap[nodeOptions.ServiceTypes[i]] = service.NewManager(
identityHandler,
di.ServiceRegistry.Create,
newDialogWaiter,
newDialogHandler,
discoveryService,
discovery.NewService(di.IdentityRegistry, di.IdentityRegistration, di.MysteriumClient, di.SignerFactory),
)
}
di.ServiceRunner = service.NewRunner(serviceManagerMap)
Expand Down
6 changes: 3 additions & 3 deletions communication/nats/discovery/address.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ func NewAddress(topic string, addresses ...string) *AddressNATS {
}

// NewAddressGenerate generates NATS address for current node
func NewAddressGenerate(brokerIP string, myID identity.Identity) *AddressNATS {
func NewAddressGenerate(brokerIP string, myID identity.Identity, serviceType string) *AddressNATS {
address := fmt.Sprintf("nats://%s:%d", brokerIP, BrokerPort)

return NewAddress(myID.Address, address)
topic := fmt.Sprintf("%v.%v", myID.Address, serviceType)
return NewAddress(topic, address)
}

// NewAddressForContact extracts NATS address from given contact structure
Expand Down
4 changes: 2 additions & 2 deletions communication/nats/discovery/address_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,13 @@ func TestNewAddress(t *testing.T) {
func TestNewAddressGenerate(t *testing.T) {
myID := identity.FromAddress("provider1")
brokerIP := "127.0.0.1"
address := NewAddressGenerate(brokerIP, myID)
address := NewAddressGenerate(brokerIP, myID, "noop")

assert.Equal(
t,
&AddressNATS{
servers: []string{"nats://" + brokerIP + ":4222"},
topic: "provider1",
topic: "provider1.noop",
},
address,
)
Expand Down
7 changes: 4 additions & 3 deletions core/service/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,11 @@ type Service interface {
Start(providerID identity.Identity) (dto_discovery.ServiceProposal, session.ConfigProvider, error)
Wait() error
Stop() error
GetType() string
}

// DialogWaiterFactory initiates communication channel which waits for incoming dialogs
type DialogWaiterFactory func(providerID identity.Identity) communication.DialogWaiter
type DialogWaiterFactory func(providerID identity.Identity, serviceType string) communication.DialogWaiter

// DialogHandlerFactory initiates instance which is able to handle incoming dialogs
type DialogHandlerFactory func(dto_discovery.ServiceProposal, session.ConfigProvider) communication.DialogHandler
Expand All @@ -75,7 +76,7 @@ func NewManager(
type Manager struct {
identityHandler identity_selector.Handler

dialogWaiterFactory func(identity identity.Identity) communication.DialogWaiter
dialogWaiterFactory DialogWaiterFactory
dialogWaiter communication.DialogWaiter
dialogHandlerFactory DialogHandlerFactory

Expand All @@ -102,7 +103,7 @@ func (manager *Manager) Start(options Options) (err error) {
return err
}

manager.dialogWaiter = manager.dialogWaiterFactory(providerID)
manager.dialogWaiter = manager.dialogWaiterFactory(providerID, manager.service.GetType())
providerContact, err := manager.dialogWaiter.Start()
if err != nil {
return err
Expand Down
4 changes: 4 additions & 0 deletions core/service/stub_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,7 @@ func (service *serviceFake) Wait() error {
func (service *serviceFake) Stop() error {
return nil
}

func (service *serviceFake) GetType() string {
return "fake"
}
2 changes: 1 addition & 1 deletion server/mysterium_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ func (mApi *mysteriumAPI) PingProposal(proposal dto_discovery.ServiceProposal, s

err = mApi.doRequest(req)
if err == nil {
log.Info(mysteriumAPILogPrefix, "Proposal pinged for node: ", proposal.ProviderID)
log.Info(mysteriumAPILogPrefix, "Proposal pinged for node: ", proposal.ProviderID, " service type: ", proposal.ServiceType)
}
return err
}
Expand Down
17 changes: 17 additions & 0 deletions services/noop/bootstrap.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,20 @@
/*
* Copyright (C) 2018 The "MysteriumNetwork/node" Authors.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package noop

import (
Expand Down
5 changes: 5 additions & 0 deletions services/noop/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,8 @@ func (manager *Manager) Stop() error {
log.Info(logPrefix, "Noop service stopped")
return nil
}

// GetType returns the service type
func (manager *Manager) GetType() string {
return ServiceType
}
5 changes: 5 additions & 0 deletions services/openvpn/service/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,3 +153,8 @@ func vpnStateCallback(state openvpn.State) {
log.Info(logPrefix, "Openvpn service exited")
}
}

// GetType returns the service type
func (manager *Manager) GetType() string {
return openvpn_service.ServiceType
}

0 comments on commit d1f3899

Please sign in to comment.