Commit f12d4f94 authored by Janos Guljas's avatar Janos Guljas

separate p2p Streamer from p2p Service

parent 8984999f
...@@ -22,6 +22,7 @@ func main() { ...@@ -22,6 +22,7 @@ func main() {
//var idht *dht.IpfsDHT //var idht *dht.IpfsDHT
// Construct P2P service.
s, err := libp2p.New(ctx, libp2p.Options{ s, err := libp2p.New(ctx, libp2p.Options{
// Routing: func(h host.Host) (r routing.PeerRouting, err error) { // Routing: func(h host.Host) (r routing.PeerRouting, err error) {
// idht, err = dht.New(ctx, h) // idht, err = dht.New(ctx, h)
...@@ -32,11 +33,16 @@ func main() { ...@@ -32,11 +33,16 @@ func main() {
log.Fatal("p2p service: ", err) log.Fatal("p2p service: ", err)
} }
pingPong, err := pingpong.New(s) // Construct protocols.
if err != nil { pingPong := pingpong.New(s)
// Add protocols to the P2P service.
if err = s.AddProtocol(pingPong.Protocol()); err != nil {
log.Fatal("pingpong service: ", err) log.Fatal("pingpong service: ", err)
} }
// Bellow is only demo code.
addrs, err := s.Addresses() addrs, err := s.Addresses()
if err != nil { if err != nil {
log.Fatal("get server addresses: ", err) log.Fatal("get server addresses: ", err)
......
...@@ -11,6 +11,9 @@ import ( ...@@ -11,6 +11,9 @@ import (
type Service interface { type Service interface {
AddProtocol(ProtocolSpec) error AddProtocol(ProtocolSpec) error
Connect(ctx context.Context, addr ma.Multiaddr) (peerID string, err error) Connect(ctx context.Context, addr ma.Multiaddr) (peerID string, err error)
}
type Streamer interface {
NewStream(ctx context.Context, peerID, protocol, stream, version string) (Stream, error) NewStream(ctx context.Context, peerID, protocol, stream, version string) (Stream, error)
} }
......
...@@ -20,15 +20,15 @@ const ( ...@@ -20,15 +20,15 @@ const (
) )
type Service struct { type Service struct {
p2p p2p.Service streamer p2p.Streamer
} }
func New(p2ps p2p.Service) (s *Service, err error) { func New(streamer p2p.Streamer) *Service {
s = &Service{ return &Service{streamer: streamer}
p2p: p2ps, }
}
if err := p2ps.AddProtocol(p2p.ProtocolSpec{ func (s *Service) Protocol() p2p.ProtocolSpec {
return p2p.ProtocolSpec{
Name: protocolName, Name: protocolName,
StreamSpecs: []p2p.StreamSpec{ StreamSpecs: []p2p.StreamSpec{
{ {
...@@ -37,10 +37,7 @@ func New(p2ps p2p.Service) (s *Service, err error) { ...@@ -37,10 +37,7 @@ func New(p2ps p2p.Service) (s *Service, err error) {
Handler: s.Handler, Handler: s.Handler,
}, },
}, },
}); err != nil {
return nil, err
} }
return s, nil
} }
func (s *Service) Handler(p p2p.Peer) { func (s *Service) Handler(p p2p.Peer) {
...@@ -68,7 +65,7 @@ func (s *Service) Handler(p p2p.Peer) { ...@@ -68,7 +65,7 @@ func (s *Service) Handler(p p2p.Peer) {
} }
func (s *Service) Ping(ctx context.Context, peerID string, msgs ...string) (rtt time.Duration, err error) { func (s *Service) Ping(ctx context.Context, peerID string, msgs ...string) (rtt time.Duration, err error) {
stream, err := s.p2p.NewStream(ctx, peerID, protocolName, streamName, streamVersion) stream, err := s.streamer.NewStream(ctx, peerID, protocolName, streamName, streamVersion)
if err != nil { if err != nil {
return 0, fmt.Errorf("new stream: %w", err) return 0, fmt.Errorf("new stream: %w", err)
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment