[stream] added synchronize service in service manager

pull/3588/head
Jacky Wang 4 years ago
parent 72286f09ec
commit 0df102f14f
No known key found for this signature in database
GPG Key ID: 1085CE5F4FF5842C
  1. 5
      api/service/manager.go
  2. 37
      api/service/synchronize/service.go
  3. 7
      p2p/host.go

@ -82,6 +82,11 @@ func (m *Manager) GetServices() []Service {
return m.services
}
// GetService get the specified service
func (m *Manager) GetService(t Type) Service {
return m.serviceMap[t]
}
// StartServices run all registered services. If one of the starting service returns
// an error, closing all started services.
func (m *Manager) StartServices() (err error) {

@ -0,0 +1,37 @@
package synchronize
import (
"github.com/ethereum/go-ethereum/rpc"
"github.com/harmony-one/harmony/core"
"github.com/harmony-one/harmony/hmy/downloader"
"github.com/harmony-one/harmony/p2p"
)
// Service is simply a adapter of Downloaders, which support block synchronization
type Service struct {
Downloaders *downloader.Downloaders
}
// NewService creates the a new downloader service
func NewService(host p2p.Host, bcs []*core.BlockChain, config downloader.Config) *Service {
return &Service{
Downloaders: downloader.NewDownloaders(host, bcs, config),
}
}
// Start start the service
func (s *Service) Start() error {
s.Downloaders.Start()
return nil
}
// Stop stop the service
func (s *Service) Stop() error {
s.Downloaders.Close()
return nil
}
// APIs return all APIs of the service
func (s *Service) APIs() []rpc.API {
return nil
}

@ -10,6 +10,8 @@ import (
"strings"
"sync"
"github.com/libp2p/go-libp2p-core/protocol"
"github.com/harmony-one/bls/ffi/go/bls"
nodeconfig "github.com/harmony-one/harmony/internal/configs/node"
"github.com/harmony-one/harmony/internal/utils"
@ -230,8 +232,9 @@ func (host *HostV2) C() (int, int, int) {
// AddStreamProtocol adds the stream protocols to the host to be started and closed
// when the host starts or close
func (host *HostV2) AddStreamProtocol(protocols ...sttypes.Protocol) {
for _, protocol := range protocols {
host.streamProtos = append(host.streamProtos, protocol)
for _, proto := range protocols {
host.streamProtos = append(host.streamProtos, proto)
host.h.SetStreamHandlerMatch(protocol.ID(proto.ProtoID()), proto.Match, proto.HandleStream)
}
}

Loading…
Cancel
Save