提交 bec54c25 作者: Łukasz Magiera

Rename PTP to P2P

License: MIT
Signed-off-by: 's avatarŁukasz Magiera <magik6k@gmail.com>
上级 2f999d4b
...@@ -14,14 +14,14 @@ import ( ...@@ -14,14 +14,14 @@ import (
ma "gx/ipfs/QmcyqRMCAXVtYPS4DiBrA7sezL9rRGfW8Ctx7cywL4TXJj/go-multiaddr" ma "gx/ipfs/QmcyqRMCAXVtYPS4DiBrA7sezL9rRGfW8Ctx7cywL4TXJj/go-multiaddr"
) )
// PTPListenerInfoOutput is output type of ls command // P2PListenerInfoOutput is output type of ls command
type PTPListenerInfoOutput struct { type P2PListenerInfoOutput struct {
Protocol string Protocol string
Address string Address string
} }
// PTPStreamInfoOutput is output type of streams command // P2PStreamInfoOutput is output type of streams command
type PTPStreamInfoOutput struct { type P2PStreamInfoOutput struct {
HandlerID string HandlerID string
Protocol string Protocol string
LocalPeer string LocalPeer string
...@@ -30,18 +30,18 @@ type PTPStreamInfoOutput struct { ...@@ -30,18 +30,18 @@ type PTPStreamInfoOutput struct {
RemoteAddress string RemoteAddress string
} }
// PTPLsOutput is output type of ls command // P2PLsOutput is output type of ls command
type PTPLsOutput struct { type P2PLsOutput struct {
Listeners []PTPListenerInfoOutput Listeners []P2PListenerInfoOutput
} }
// PTPStreamsOutput is output type of streams command // P2PStreamsOutput is output type of streams command
type PTPStreamsOutput struct { type P2PStreamsOutput struct {
Streams []PTPStreamInfoOutput Streams []P2PStreamInfoOutput
} }
// PTPCmd is the 'ipfs ptp' command // P2PCmd is the 'ipfs ppp' command
var PTPCmd = &cmds.Command{ var P2PCmd = &cmds.Command{
Helptext: cmds.HelpText{ Helptext: cmds.HelpText{
Tagline: "Libp2p stream mounting.", Tagline: "Libp2p stream mounting.",
ShortDescription: ` ShortDescription: `
...@@ -51,40 +51,40 @@ Note: this command is experimental and subject to change as usecases and APIs ar ...@@ -51,40 +51,40 @@ Note: this command is experimental and subject to change as usecases and APIs ar
}, },
Subcommands: map[string]*cmds.Command{ Subcommands: map[string]*cmds.Command{
"listener": ptpListenerCmd, "listener": p2pListenerCmd,
"stream": ptpStreamCmd, "stream": p2pStreamCmd,
}, },
} }
// ptpListenerCmd is the 'ipfs ptp listener' command // p2pListenerCmd is the 'ipfs p2p listener' command
var ptpListenerCmd = &cmds.Command{ var p2pListenerCmd = &cmds.Command{
Helptext: cmds.HelpText{ Helptext: cmds.HelpText{
Tagline: "P2P listener management.", Tagline: "P2P listener management.",
ShortDescription: "Create and manage listener p2p endpoints", ShortDescription: "Create and manage listener p2p endpoints",
}, },
Subcommands: map[string]*cmds.Command{ Subcommands: map[string]*cmds.Command{
"ls": ptpListenerLsCmd, "ls": p2pListenerLsCmd,
"open": ptpListenerListenCmd, "open": p2pListenerListenCmd,
"close": ptpListenerCloseCmd, "close": p2pListenerCloseCmd,
}, },
} }
// ptpStreamCmd is the 'ipfs ptp stream' command // p2pStreamCmd is the 'ipfs p2p stream' command
var ptpStreamCmd = &cmds.Command{ var p2pStreamCmd = &cmds.Command{
Helptext: cmds.HelpText{ Helptext: cmds.HelpText{
Tagline: "P2P stream management.", Tagline: "P2P stream management.",
ShortDescription: "Create and manage p2p streams", ShortDescription: "Create and manage p2p streams",
}, },
Subcommands: map[string]*cmds.Command{ Subcommands: map[string]*cmds.Command{
"ls": ptpStreamLsCmd, "ls": p2pStreamLsCmd,
"dial": ptpStreamDialCmd, "dial": p2pStreamDialCmd,
"close": ptpStreamCloseCmd, "close": p2pStreamCloseCmd,
}, },
} }
var ptpListenerLsCmd = &cmds.Command{ var p2pListenerLsCmd = &cmds.Command{
Helptext: cmds.HelpText{ Helptext: cmds.HelpText{
Tagline: "List active p2p listeners.", Tagline: "List active p2p listeners.",
}, },
...@@ -99,10 +99,10 @@ var ptpListenerLsCmd = &cmds.Command{ ...@@ -99,10 +99,10 @@ var ptpListenerLsCmd = &cmds.Command{
return return
} }
output := &PTPLsOutput{} output := &P2PLsOutput{}
for _, listener := range n.PTP.Listeners.Listeners { for _, listener := range n.P2P.Listeners.Listeners {
output.Listeners = append(output.Listeners, PTPListenerInfoOutput{ output.Listeners = append(output.Listeners, P2PListenerInfoOutput{
Protocol: listener.Protocol, Protocol: listener.Protocol,
Address: listener.Address.String(), Address: listener.Address.String(),
}) })
...@@ -110,11 +110,11 @@ var ptpListenerLsCmd = &cmds.Command{ ...@@ -110,11 +110,11 @@ var ptpListenerLsCmd = &cmds.Command{
res.SetOutput(output) res.SetOutput(output)
}, },
Type: PTPLsOutput{}, Type: P2PLsOutput{},
Marshalers: cmds.MarshalerMap{ Marshalers: cmds.MarshalerMap{
cmds.Text: func(res cmds.Response) (io.Reader, error) { cmds.Text: func(res cmds.Response) (io.Reader, error) {
headers, _, _ := res.Request().Option("headers").Bool() headers, _, _ := res.Request().Option("headers").Bool()
list, _ := res.Output().(*PTPLsOutput) list, _ := res.Output().(*P2PLsOutput)
buf := new(bytes.Buffer) buf := new(bytes.Buffer)
w := tabwriter.NewWriter(buf, 1, 2, 1, ' ', 0) w := tabwriter.NewWriter(buf, 1, 2, 1, ' ', 0)
for _, listener := range list.Listeners { for _, listener := range list.Listeners {
...@@ -131,7 +131,7 @@ var ptpListenerLsCmd = &cmds.Command{ ...@@ -131,7 +131,7 @@ var ptpListenerLsCmd = &cmds.Command{
}, },
} }
var ptpStreamLsCmd = &cmds.Command{ var p2pStreamLsCmd = &cmds.Command{
Helptext: cmds.HelpText{ Helptext: cmds.HelpText{
Tagline: "List active p2p streams.", Tagline: "List active p2p streams.",
}, },
...@@ -145,10 +145,10 @@ var ptpStreamLsCmd = &cmds.Command{ ...@@ -145,10 +145,10 @@ var ptpStreamLsCmd = &cmds.Command{
return return
} }
output := &PTPStreamsOutput{} output := &P2PStreamsOutput{}
for _, s := range n.PTP.Streams.Streams { for _, s := range n.P2P.Streams.Streams {
output.Streams = append(output.Streams, PTPStreamInfoOutput{ output.Streams = append(output.Streams, P2PStreamInfoOutput{
HandlerID: strconv.FormatUint(s.HandlerID, 10), HandlerID: strconv.FormatUint(s.HandlerID, 10),
Protocol: s.Protocol, Protocol: s.Protocol,
...@@ -163,11 +163,11 @@ var ptpStreamLsCmd = &cmds.Command{ ...@@ -163,11 +163,11 @@ var ptpStreamLsCmd = &cmds.Command{
res.SetOutput(output) res.SetOutput(output)
}, },
Type: PTPStreamsOutput{}, Type: P2PStreamsOutput{},
Marshalers: cmds.MarshalerMap{ Marshalers: cmds.MarshalerMap{
cmds.Text: func(res cmds.Response) (io.Reader, error) { cmds.Text: func(res cmds.Response) (io.Reader, error) {
headers, _, _ := res.Request().Option("headers").Bool() headers, _, _ := res.Request().Option("headers").Bool()
list, _ := res.Output().(*PTPStreamsOutput) list, _ := res.Output().(*P2PStreamsOutput)
buf := new(bytes.Buffer) buf := new(bytes.Buffer)
w := tabwriter.NewWriter(buf, 1, 2, 1, ' ', 0) w := tabwriter.NewWriter(buf, 1, 2, 1, ' ', 0)
for _, stream := range list.Streams { for _, stream := range list.Streams {
...@@ -184,7 +184,7 @@ var ptpStreamLsCmd = &cmds.Command{ ...@@ -184,7 +184,7 @@ var ptpStreamLsCmd = &cmds.Command{
}, },
} }
var ptpListenerListenCmd = &cmds.Command{ var p2pListenerListenCmd = &cmds.Command{
Helptext: cmds.HelpText{ Helptext: cmds.HelpText{
Tagline: "Forward p2p connections to a network multiaddr.", Tagline: "Forward p2p connections to a network multiaddr.",
ShortDescription: ` ShortDescription: `
...@@ -204,8 +204,8 @@ Note that the connections originate from the ipfs daemon process. ...@@ -204,8 +204,8 @@ Note that the connections originate from the ipfs daemon process.
return return
} }
proto := "/ptp/" + req.Arguments()[0] proto := "/p2p/" + req.Arguments()[0]
if n.PTP.CheckProtoExists(proto) { if n.P2P.CheckProtoExists(proto) {
res.SetError(errors.New("protocol handler already registered"), cmds.ErrNormal) res.SetError(errors.New("protocol handler already registered"), cmds.ErrNormal)
return return
} }
...@@ -216,21 +216,21 @@ Note that the connections originate from the ipfs daemon process. ...@@ -216,21 +216,21 @@ Note that the connections originate from the ipfs daemon process.
return return
} }
_, err = n.PTP.NewListener(n.Context(), proto, addr) _, err = n.P2P.NewListener(n.Context(), proto, addr)
if err != nil { if err != nil {
res.SetError(err, cmds.ErrNormal) res.SetError(err, cmds.ErrNormal)
return return
} }
// Successful response. // Successful response.
res.SetOutput(&PTPListenerInfoOutput{ res.SetOutput(&P2PListenerInfoOutput{
Protocol: proto, Protocol: proto,
Address: addr.String(), Address: addr.String(),
}) })
}, },
} }
var ptpStreamDialCmd = &cmds.Command{ var p2pStreamDialCmd = &cmds.Command{
Helptext: cmds.HelpText{ Helptext: cmds.HelpText{
Tagline: "Dial to a p2p listener.", Tagline: "Dial to a p2p listener.",
...@@ -260,7 +260,7 @@ transparently connect to a p2p service. ...@@ -260,7 +260,7 @@ transparently connect to a p2p service.
return return
} }
proto := "/ptp/" + req.Arguments()[1] proto := "/p2p/" + req.Arguments()[1]
bindAddr, _ := ma.NewMultiaddr("/ip4/127.0.0.1/tcp/0") bindAddr, _ := ma.NewMultiaddr("/ip4/127.0.0.1/tcp/0")
if len(req.Arguments()) == 3 { if len(req.Arguments()) == 3 {
...@@ -271,13 +271,13 @@ transparently connect to a p2p service. ...@@ -271,13 +271,13 @@ transparently connect to a p2p service.
} }
} }
listenerInfo, err := n.PTP.Dial(n.Context(), addr, peer, proto, bindAddr) listenerInfo, err := n.P2P.Dial(n.Context(), addr, peer, proto, bindAddr)
if err != nil { if err != nil {
res.SetError(err, cmds.ErrNormal) res.SetError(err, cmds.ErrNormal)
return return
} }
output := PTPListenerInfoOutput{ output := P2PListenerInfoOutput{
Protocol: listenerInfo.Protocol, Protocol: listenerInfo.Protocol,
Address: listenerInfo.Address.String(), Address: listenerInfo.Address.String(),
} }
...@@ -286,7 +286,7 @@ transparently connect to a p2p service. ...@@ -286,7 +286,7 @@ transparently connect to a p2p service.
}, },
} }
var ptpListenerCloseCmd = &cmds.Command{ var p2pListenerCloseCmd = &cmds.Command{
Helptext: cmds.HelpText{ Helptext: cmds.HelpText{
Tagline: "Close active p2p listener.", Tagline: "Close active p2p listener.",
}, },
...@@ -312,10 +312,10 @@ var ptpListenerCloseCmd = &cmds.Command{ ...@@ -312,10 +312,10 @@ var ptpListenerCloseCmd = &cmds.Command{
return return
} }
proto = "/ptp/" + req.Arguments()[0] proto = "/p2p/" + req.Arguments()[0]
} }
for _, listener := range n.PTP.Listeners.Listeners { for _, listener := range n.P2P.Listeners.Listeners {
if !closeAll && listener.Protocol != proto { if !closeAll && listener.Protocol != proto {
continue continue
} }
...@@ -327,7 +327,7 @@ var ptpListenerCloseCmd = &cmds.Command{ ...@@ -327,7 +327,7 @@ var ptpListenerCloseCmd = &cmds.Command{
}, },
} }
var ptpStreamCloseCmd = &cmds.Command{ var p2pStreamCloseCmd = &cmds.Command{
Helptext: cmds.HelpText{ Helptext: cmds.HelpText{
Tagline: "Close active p2p stream.", Tagline: "Close active p2p stream.",
}, },
...@@ -360,7 +360,7 @@ var ptpStreamCloseCmd = &cmds.Command{ ...@@ -360,7 +360,7 @@ var ptpStreamCloseCmd = &cmds.Command{
} }
} }
for _, stream := range n.PTP.Streams.Streams { for _, stream := range n.P2P.Streams.Streams {
if !closeAll && handlerID != stream.HandlerID { if !closeAll && handlerID != stream.HandlerID {
continue continue
} }
......
...@@ -47,7 +47,7 @@ ADVANCED COMMANDS ...@@ -47,7 +47,7 @@ ADVANCED COMMANDS
pin Pin objects to local storage pin Pin objects to local storage
repo Manipulate the IPFS repository repo Manipulate the IPFS repository
stats Various operational stats stats Various operational stats
ptp Libp2p stream mounting p2p Libp2p stream mounting
filestore Manage the filestore (experimental) filestore Manage the filestore (experimental)
NETWORK COMMANDS NETWORK COMMANDS
...@@ -114,7 +114,7 @@ var rootSubcommands = map[string]*cmds.Command{ ...@@ -114,7 +114,7 @@ var rootSubcommands = map[string]*cmds.Command{
"object": ocmd.ObjectCmd, "object": ocmd.ObjectCmd,
"pin": PinCmd, "pin": PinCmd,
"ping": PingCmd, "ping": PingCmd,
"ptp": PTPCmd, "p2p": P2PCmd,
"pubsub": PubsubCmd, "pubsub": PubsubCmd,
"refs": RefsCmd, "refs": RefsCmd,
"repo": RepoCmd, "repo": RepoCmd,
......
...@@ -33,9 +33,9 @@ import ( ...@@ -33,9 +33,9 @@ import (
mfs "github.com/ipfs/go-ipfs/mfs" mfs "github.com/ipfs/go-ipfs/mfs"
namesys "github.com/ipfs/go-ipfs/namesys" namesys "github.com/ipfs/go-ipfs/namesys"
ipnsrp "github.com/ipfs/go-ipfs/namesys/republisher" ipnsrp "github.com/ipfs/go-ipfs/namesys/republisher"
p2p "github.com/ipfs/go-ipfs/p2p"
path "github.com/ipfs/go-ipfs/path" path "github.com/ipfs/go-ipfs/path"
pin "github.com/ipfs/go-ipfs/pin" pin "github.com/ipfs/go-ipfs/pin"
ptp "github.com/ipfs/go-ipfs/ptp"
repo "github.com/ipfs/go-ipfs/repo" repo "github.com/ipfs/go-ipfs/repo"
config "github.com/ipfs/go-ipfs/repo/config" config "github.com/ipfs/go-ipfs/repo/config"
nilrouting "github.com/ipfs/go-ipfs/routing/none" nilrouting "github.com/ipfs/go-ipfs/routing/none"
...@@ -131,7 +131,7 @@ type IpfsNode struct { ...@@ -131,7 +131,7 @@ type IpfsNode struct {
IpnsRepub *ipnsrp.Republisher IpnsRepub *ipnsrp.Republisher
Floodsub *floodsub.PubSub Floodsub *floodsub.PubSub
PTP *ptp.PTP P2P *p2p.P2P
proc goprocess.Process proc goprocess.Process
ctx context.Context ctx context.Context
...@@ -247,7 +247,7 @@ func (n *IpfsNode) startOnlineServices(ctx context.Context, routingOption Routin ...@@ -247,7 +247,7 @@ func (n *IpfsNode) startOnlineServices(ctx context.Context, routingOption Routin
n.Floodsub = floodsub.NewFloodSub(ctx, peerhost) n.Floodsub = floodsub.NewFloodSub(ctx, peerhost)
} }
n.PTP = ptp.NewPTP(n.Identity, n.PeerHost, n.Peerstore) n.P2P = p2p.NewP2P(n.Identity, n.PeerHost, n.Peerstore)
// setup local discovery // setup local discovery
if do != nil { if do != nil {
......
package ptp package p2p
import ( import (
"context" "context"
...@@ -14,8 +14,8 @@ import ( ...@@ -14,8 +14,8 @@ import (
manet "gx/ipfs/Qmf1Gq7N45Rpuw7ev47uWgH6dLPtdnvcMRNPkVBwqjLJg2/go-multiaddr-net" manet "gx/ipfs/Qmf1Gq7N45Rpuw7ev47uWgH6dLPtdnvcMRNPkVBwqjLJg2/go-multiaddr-net"
) )
// PTP structure holds information on currently running streams/listeners // P2P structure holds information on currently running streams/listeners
type PTP struct { type P2P struct {
Listeners ListenerRegistry Listeners ListenerRegistry
Streams StreamRegistry Streams StreamRegistry
...@@ -24,38 +24,38 @@ type PTP struct { ...@@ -24,38 +24,38 @@ type PTP struct {
peerstore pstore.Peerstore peerstore pstore.Peerstore
} }
// NewPTP creates new PTP struct // NewP2P creates new P2P struct
func NewPTP(identity peer.ID, peerHost p2phost.Host, peerstore pstore.Peerstore) *PTP { func NewP2P(identity peer.ID, peerHost p2phost.Host, peerstore pstore.Peerstore) *P2P {
return &PTP{ return &P2P{
identity: identity, identity: identity,
peerHost: peerHost, peerHost: peerHost,
peerstore: peerstore, peerstore: peerstore,
} }
} }
func (ptp *PTP) newStreamTo(ctx2 context.Context, p peer.ID, protocol string) (net.Stream, error) { func (p2p *P2P) newStreamTo(ctx2 context.Context, p peer.ID, protocol string) (net.Stream, error) {
ctx, cancel := context.WithTimeout(ctx2, time.Second*30) //TODO: configurable? ctx, cancel := context.WithTimeout(ctx2, time.Second*30) //TODO: configurable?
defer cancel() defer cancel()
err := ptp.peerHost.Connect(ctx, pstore.PeerInfo{ID: p}) err := p2p.peerHost.Connect(ctx, pstore.PeerInfo{ID: p})
if err != nil { if err != nil {
return nil, err return nil, err
} }
return ptp.peerHost.NewStream(ctx2, p, pro.ID(protocol)) return p2p.peerHost.NewStream(ctx2, p, pro.ID(protocol))
} }
// Dial creates new P2P stream to a remote listener // Dial creates new P2P stream to a remote listener
func (ptp *PTP) Dial(ctx context.Context, addr ma.Multiaddr, peer peer.ID, proto string, bindAddr ma.Multiaddr) (*ListenerInfo, error) { func (p2p *P2P) Dial(ctx context.Context, addr ma.Multiaddr, peer peer.ID, proto string, bindAddr ma.Multiaddr) (*ListenerInfo, error) {
lnet, _, err := manet.DialArgs(bindAddr) lnet, _, err := manet.DialArgs(bindAddr)
if err != nil { if err != nil {
return nil, err return nil, err
} }
listenerInfo := ListenerInfo{ listenerInfo := ListenerInfo{
Identity: ptp.identity, Identity: p2p.identity,
Protocol: proto, Protocol: proto,
} }
remote, err := ptp.newStreamTo(ctx, peer, proto) remote, err := p2p.newStreamTo(ctx, peer, proto)
if err != nil { if err != nil {
return nil, err return nil, err
} }
...@@ -74,7 +74,7 @@ func (ptp *PTP) Dial(ctx context.Context, addr ma.Multiaddr, peer peer.ID, proto ...@@ -74,7 +74,7 @@ func (ptp *PTP) Dial(ctx context.Context, addr ma.Multiaddr, peer peer.ID, proto
listenerInfo.Closer = listener listenerInfo.Closer = listener
listenerInfo.Running = true listenerInfo.Running = true
go ptp.doAccept(&listenerInfo, remote, listener) go p2p.doAccept(&listenerInfo, remote, listener)
default: default:
return nil, errors.New("unsupported protocol: " + lnet) return nil, errors.New("unsupported protocol: " + lnet)
...@@ -83,7 +83,7 @@ func (ptp *PTP) Dial(ctx context.Context, addr ma.Multiaddr, peer peer.ID, proto ...@@ -83,7 +83,7 @@ func (ptp *PTP) Dial(ctx context.Context, addr ma.Multiaddr, peer peer.ID, proto
return &listenerInfo, nil return &listenerInfo, nil
} }
func (ptp *PTP) doAccept(listenerInfo *ListenerInfo, remote net.Stream, listener manet.Listener) { func (p2p *P2P) doAccept(listenerInfo *ListenerInfo, remote net.Stream, listener manet.Listener) {
defer listener.Close() defer listener.Close()
local, err := listener.Accept() local, err := listener.Accept()
...@@ -103,10 +103,10 @@ func (ptp *PTP) doAccept(listenerInfo *ListenerInfo, remote net.Stream, listener ...@@ -103,10 +103,10 @@ func (ptp *PTP) doAccept(listenerInfo *ListenerInfo, remote net.Stream, listener
Local: local, Local: local,
Remote: remote, Remote: remote,
Registry: &ptp.Streams, Registry: &p2p.Streams,
} }
ptp.Streams.Register(&stream) p2p.Streams.Register(&stream)
stream.startStreaming() stream.startStreaming()
} }
...@@ -143,18 +143,18 @@ func (il *P2PListener) Close() error { ...@@ -143,18 +143,18 @@ func (il *P2PListener) Close() error {
} }
// Listen creates new P2PListener // Listen creates new P2PListener
func (ptp *PTP) registerStreamHandler(ctx2 context.Context, protocol string) (*P2PListener, error) { func (p2p *P2P) registerStreamHandler(ctx2 context.Context, protocol string) (*P2PListener, error) {
ctx, cancel := context.WithCancel(ctx2) ctx, cancel := context.WithCancel(ctx2)
list := &P2PListener{ list := &P2PListener{
peerHost: ptp.peerHost, peerHost: p2p.peerHost,
proto: pro.ID(protocol), proto: pro.ID(protocol),
conCh: make(chan net.Stream), conCh: make(chan net.Stream),
ctx: ctx, ctx: ctx,
cancel: cancel, cancel: cancel,
} }
ptp.peerHost.SetStreamHandler(list.proto, func(s net.Stream) { p2p.peerHost.SetStreamHandler(list.proto, func(s net.Stream) {
select { select {
case list.conCh <- s: case list.conCh <- s:
case <-ctx.Done(): case <-ctx.Done():
...@@ -165,30 +165,30 @@ func (ptp *PTP) registerStreamHandler(ctx2 context.Context, protocol string) (*P ...@@ -165,30 +165,30 @@ func (ptp *PTP) registerStreamHandler(ctx2 context.Context, protocol string) (*P
return list, nil return list, nil
} }
// NewListener creates new ptp listener // NewListener creates new p2p listener
func (ptp *PTP) NewListener(ctx context.Context, proto string, addr ma.Multiaddr) (*ListenerInfo, error) { func (p2p *P2P) NewListener(ctx context.Context, proto string, addr ma.Multiaddr) (*ListenerInfo, error) {
listener, err := ptp.registerStreamHandler(ctx, proto) listener, err := p2p.registerStreamHandler(ctx, proto)
if err != nil { if err != nil {
return nil, err return nil, err
} }
listenerInfo := ListenerInfo{ listenerInfo := ListenerInfo{
Identity: ptp.identity, Identity: p2p.identity,
Protocol: proto, Protocol: proto,
Address: addr, Address: addr,
Closer: listener, Closer: listener,
Running: true, Running: true,
Registry: &ptp.Listeners, Registry: &p2p.Listeners,
} }
go ptp.acceptStreams(&listenerInfo, listener) go p2p.acceptStreams(&listenerInfo, listener)
ptp.Listeners.Register(&listenerInfo) p2p.Listeners.Register(&listenerInfo)
return &listenerInfo, nil return &listenerInfo, nil
} }
func (ptp *PTP) acceptStreams(listenerInfo *ListenerInfo, listener Listener) { func (p2p *P2P) acceptStreams(listenerInfo *ListenerInfo, listener Listener) {
for listenerInfo.Running { for listenerInfo.Running {
remote, err := listener.Accept() remote, err := listener.Accept()
if err != nil { if err != nil {
...@@ -214,19 +214,19 @@ func (ptp *PTP) acceptStreams(listenerInfo *ListenerInfo, listener Listener) { ...@@ -214,19 +214,19 @@ func (ptp *PTP) acceptStreams(listenerInfo *ListenerInfo, listener Listener) {
Local: local, Local: local,
Remote: remote, Remote: remote,
Registry: &ptp.Streams, Registry: &p2p.Streams,
} }
ptp.Streams.Register(&stream) p2p.Streams.Register(&stream)
stream.startStreaming() stream.startStreaming()
} }
ptp.Listeners.Deregister(listenerInfo.Protocol) p2p.Listeners.Deregister(listenerInfo.Protocol)
} }
// CheckProtoExists checks whether a protocol handler is registered to // CheckProtoExists checks whether a protocol handler is registered to
// mux handler // mux handler
func (ptp *PTP) CheckProtoExists(proto string) bool { func (p2p *P2P) CheckProtoExists(proto string) bool {
protos := ptp.peerHost.Mux().Protocols() protos := p2p.peerHost.Mux().Protocols()
for _, p := range protos { for _, p := range protos {
if p != proto { if p != proto {
......
package ptp package p2p
import ( import (
"fmt" "fmt"
......
#!/bin/sh #!/bin/sh
test_description="Test experimental ptp commands" test_description="Test experimental p2p commands"
. lib/test-lib.sh . lib/test-lib.sh
...@@ -27,7 +27,7 @@ test_expect_success "test ports are closed" ' ...@@ -27,7 +27,7 @@ test_expect_success "test ports are closed" '
' '
test_must_fail 'fail without config option being enabled' ' test_must_fail 'fail without config option being enabled' '
ipfsi 0 ptp stream ls ipfsi 0 p2p stream ls
' '
test_expect_success "enable filestore config setting" ' test_expect_success "enable filestore config setting" '
...@@ -35,15 +35,15 @@ test_expect_success "enable filestore config setting" ' ...@@ -35,15 +35,15 @@ test_expect_success "enable filestore config setting" '
ipfsi 1 config --json Experimental.Libp2pStreamMounting true ipfsi 1 config --json Experimental.Libp2pStreamMounting true
' '
test_expect_success 'start ptp listener' ' test_expect_success 'start p2p listener' '
ipfsi 0 ptp listener open ptp-test /ip4/127.0.0.1/tcp/10101 2>&1 > listener-stdouterr.log ipfsi 0 p2p listener open p2p-test /ip4/127.0.0.1/tcp/10101 2>&1 > listener-stdouterr.log
' '
test_expect_success 'Test server to client communications' ' test_expect_success 'Test server to client communications' '
ma-pipe-unidir --listen send /ip4/127.0.0.1/tcp/10101 < test0.bin & ma-pipe-unidir --listen send /ip4/127.0.0.1/tcp/10101 < test0.bin &
SERVER_PID=$! SERVER_PID=$!
ipfsi 1 ptp stream dial $PEERID_0 ptp-test /ip4/127.0.0.1/tcp/10102 2>&1 > dialer-stdouterr.log && ipfsi 1 p2p stream dial $PEERID_0 p2p-test /ip4/127.0.0.1/tcp/10102 2>&1 > dialer-stdouterr.log &&
ma-pipe-unidir recv /ip4/127.0.0.1/tcp/10102 > client.out && ma-pipe-unidir recv /ip4/127.0.0.1/tcp/10102 > client.out &&
wait $SERVER_PID wait $SERVER_PID
' '
...@@ -52,7 +52,7 @@ test_expect_success 'Test client to server communications' ' ...@@ -52,7 +52,7 @@ test_expect_success 'Test client to server communications' '
ma-pipe-unidir --listen recv /ip4/127.0.0.1/tcp/10101 > server.out & ma-pipe-unidir --listen recv /ip4/127.0.0.1/tcp/10101 > server.out &
SERVER_PID=$! SERVER_PID=$!
ipfsi 1 ptp stream dial $PEERID_0 ptp-test /ip4/127.0.0.1/tcp/10102 2>&1 > dialer-stdouterr.log && ipfsi 1 p2p stream dial $PEERID_0 p2p-test /ip4/127.0.0.1/tcp/10102 2>&1 > dialer-stdouterr.log &&
ma-pipe-unidir send /ip4/127.0.0.1/tcp/10102 < test1.bin ma-pipe-unidir send /ip4/127.0.0.1/tcp/10102 < test1.bin
wait $SERVER_PID wait $SERVER_PID
' '
...@@ -65,90 +65,90 @@ test_expect_success 'client to server output looks good' ' ...@@ -65,90 +65,90 @@ test_expect_success 'client to server output looks good' '
test_cmp server.out test1.bin test_cmp server.out test1.bin
' '
test_expect_success "'ipfs listener ptp ls' succeeds" ' test_expect_success "'ipfs listener p2p ls' succeeds" '
echo "/ip4/127.0.0.1/tcp/10101 /ptp/ptp-test" > expected && echo "/ip4/127.0.0.1/tcp/10101 /p2p/p2p-test" > expected &&
ipfsi 0 ptp listener ls > actual ipfsi 0 p2p listener ls > actual
' '
test_expect_success "'ipfs ptp listener ls' output looks good" ' test_expect_success "'ipfs p2p listener ls' output looks good" '
test_cmp expected actual test_cmp expected actual
' '
test_expect_success "Cannot re-register app handler" ' test_expect_success "Cannot re-register app handler" '
(! ipfsi 0 ptp listener open ptp-test /ip4/127.0.0.1/tcp/10101) (! ipfsi 0 p2p listener open p2p-test /ip4/127.0.0.1/tcp/10101)
' '
test_expect_success "'ipfs ptp stream ls' output is empty" ' test_expect_success "'ipfs p2p stream ls' output is empty" '
ipfsi 0 ptp stream ls > actual && ipfsi 0 p2p stream ls > actual &&
test_must_be_empty actual test_must_be_empty actual
' '
test_expect_success "Setup: Idle stream" ' test_expect_success "Setup: Idle stream" '
ma-pipe-unidir --listen --pidFile=listener.pid recv /ip4/127.0.0.1/tcp/10101 & ma-pipe-unidir --listen --pidFile=listener.pid recv /ip4/127.0.0.1/tcp/10101 &
ipfsi 1 ptp stream dial $PEERID_0 ptp-test /ip4/127.0.0.1/tcp/10102 2>&1 > dialer-stdouterr.log && ipfsi 1 p2p stream dial $PEERID_0 p2p-test /ip4/127.0.0.1/tcp/10102 2>&1 > dialer-stdouterr.log &&
ma-pipe-unidir --pidFile=client.pid recv /ip4/127.0.0.1/tcp/10102 & ma-pipe-unidir --pidFile=client.pid recv /ip4/127.0.0.1/tcp/10102 &
go-sleep 500ms && go-sleep 500ms &&
kill -0 $(cat listener.pid) && kill -0 $(cat client.pid) kill -0 $(cat listener.pid) && kill -0 $(cat client.pid)
' '
test_expect_success "'ipfs ptp stream ls' succeeds" ' test_expect_success "'ipfs p2p stream ls' succeeds" '
echo "2 /ptp/ptp-test /ip4/127.0.0.1/tcp/10101 $PEERID_1" > expected echo "2 /p2p/p2p-test /ip4/127.0.0.1/tcp/10101 $PEERID_1" > expected
ipfsi 0 ptp stream ls > actual ipfsi 0 p2p stream ls > actual
' '
test_expect_success "'ipfs ptp stream ls' output looks good" ' test_expect_success "'ipfs p2p stream ls' output looks good" '
test_cmp expected actual test_cmp expected actual
' '
test_expect_success "'ipfs ptp stream close' closes stream" ' test_expect_success "'ipfs p2p stream close' closes stream" '
ipfsi 0 ptp stream close 2 && ipfsi 0 p2p stream close 2 &&
ipfsi 0 ptp stream ls > actual && ipfsi 0 p2p stream ls > actual &&
[ ! -f listener.pid ] && [ ! -f client.pid ] && [ ! -f listener.pid ] && [ ! -f client.pid ] &&
test_must_be_empty actual test_must_be_empty actual
' '
test_expect_success "'ipfs ptp listener close' closes app handler" ' test_expect_success "'ipfs p2p listener close' closes app handler" '
ipfsi 0 ptp listener close ptp-test && ipfsi 0 p2p listener close p2p-test &&
ipfsi 0 ptp listener ls > actual && ipfsi 0 p2p listener ls > actual &&
test_must_be_empty actual test_must_be_empty actual
' '
test_expect_success "Setup: Idle stream(2)" ' test_expect_success "Setup: Idle stream(2)" '
ma-pipe-unidir --listen --pidFile=listener.pid recv /ip4/127.0.0.1/tcp/10101 & ma-pipe-unidir --listen --pidFile=listener.pid recv /ip4/127.0.0.1/tcp/10101 &
ipfsi 0 ptp listener open ptp-test2 /ip4/127.0.0.1/tcp/10101 2>&1 > listener-stdouterr.log && ipfsi 0 p2p listener open p2p-test2 /ip4/127.0.0.1/tcp/10101 2>&1 > listener-stdouterr.log &&
ipfsi 1 ptp stream dial $PEERID_0 ptp-test2 /ip4/127.0.0.1/tcp/10102 2>&1 > dialer-stdouterr.log && ipfsi 1 p2p stream dial $PEERID_0 p2p-test2 /ip4/127.0.0.1/tcp/10102 2>&1 > dialer-stdouterr.log &&
ma-pipe-unidir --pidFile=client.pid recv /ip4/127.0.0.1/tcp/10102 & ma-pipe-unidir --pidFile=client.pid recv /ip4/127.0.0.1/tcp/10102 &
go-sleep 500ms && go-sleep 500ms &&
kill -0 $(cat listener.pid) && kill -0 $(cat client.pid) kill -0 $(cat listener.pid) && kill -0 $(cat client.pid)
' '
test_expect_success "'ipfs ptp stream ls' succeeds(2)" ' test_expect_success "'ipfs p2p stream ls' succeeds(2)" '
echo "3 /ptp/ptp-test2 /ip4/127.0.0.1/tcp/10101 $PEERID_1" > expected echo "3 /p2p/p2p-test2 /ip4/127.0.0.1/tcp/10101 $PEERID_1" > expected
ipfsi 0 ptp stream ls > actual ipfsi 0 p2p stream ls > actual
test_cmp expected actual test_cmp expected actual
' '
test_expect_success "'ipfs ptp listener close -a' closes app handlers" ' test_expect_success "'ipfs p2p listener close -a' closes app handlers" '
ipfsi 0 ptp listener close -a && ipfsi 0 p2p listener close -a &&
ipfsi 0 ptp listener ls > actual && ipfsi 0 p2p listener ls > actual &&
test_must_be_empty actual test_must_be_empty actual
' '
test_expect_success "'ipfs ptp stream close -a' closes streams" ' test_expect_success "'ipfs p2p stream close -a' closes streams" '
ipfsi 0 ptp stream close -a && ipfsi 0 p2p stream close -a &&
ipfsi 0 ptp stream ls > actual && ipfsi 0 p2p stream ls > actual &&
[ ! -f listener.pid ] && [ ! -f client.pid ] && [ ! -f listener.pid ] && [ ! -f client.pid ] &&
test_must_be_empty actual test_must_be_empty actual
' '
test_expect_success "'ipfs ptp listener close' closes app numeric handlers" ' test_expect_success "'ipfs p2p listener close' closes app numeric handlers" '
ipfsi 0 ptp listener open 1234 /ip4/127.0.0.1/tcp/10101 && ipfsi 0 p2p listener open 1234 /ip4/127.0.0.1/tcp/10101 &&
ipfsi 0 ptp listener close 1234 && ipfsi 0 p2p listener close 1234 &&
ipfsi 0 ptp listener ls > actual && ipfsi 0 p2p listener ls > actual &&
test_must_be_empty actual test_must_be_empty actual
' '
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论