Skip to content

Commit

Permalink
PTP API: Make code more object oriented, use less node
Browse files Browse the repository at this point in the history
License: MIT
Signed-off-by: Łukasz Magiera <[email protected]>
  • Loading branch information
magik6k committed Jun 3, 2017
1 parent ad9ae35 commit 712a522
Show file tree
Hide file tree
Showing 10 changed files with 327 additions and 360 deletions.
51 changes: 25 additions & 26 deletions core/commands/ptp.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,12 @@ import (

cmds "github.com/ipfs/go-ipfs/commands"
core "github.com/ipfs/go-ipfs/core"
ptpnet "github.com/ipfs/go-ipfs/ptp/net"

ma "gx/ipfs/QmcyqRMCAXVtYPS4DiBrA7sezL9rRGfW8Ctx7cywL4TXJj/go-multiaddr"
)

// PTPAppInfoOutput is output type of ls command
type PTPAppInfoOutput struct {
// PTPListenerInfoOutput is output type of ls command
type PTPListenerInfoOutput struct {
Protocol string
Address string
}
Expand All @@ -33,7 +32,7 @@ type PTPStreamInfoOutput struct {

// PTPLsOutput is output type of ls command
type PTPLsOutput struct {
Apps []PTPAppInfoOutput
Listeners []PTPListenerInfoOutput
}

// PTPStreamsOutput is output type of streams command
Expand Down Expand Up @@ -87,10 +86,10 @@ var ptpLsCmd = &cmds.Command{

output := &PTPLsOutput{}

for _, app := range n.PTP.Apps.Apps {
output.Apps = append(output.Apps, PTPAppInfoOutput{
Protocol: app.Protocol,
Address: app.Address.String(),
for _, listener := range n.PTP.Listeners.Listeners {
output.Listeners = append(output.Listeners, PTPListenerInfoOutput{
Protocol: listener.Protocol,
Address: listener.Address.String(),
})
}

Expand All @@ -103,12 +102,12 @@ var ptpLsCmd = &cmds.Command{
list, _ := res.Output().(*PTPLsOutput)
buf := new(bytes.Buffer)
w := tabwriter.NewWriter(buf, 1, 2, 1, ' ', 0)
for _, app := range list.Apps {
for _, listener := range list.Listeners {
if headers {
fmt.Fprintln(w, "Address\tProtocol")
}

fmt.Fprintf(w, "%s\t%s\n", app.Address, app.Protocol)
fmt.Fprintf(w, "%s\t%s\n", listener.Address, listener.Protocol)
}
w.Flush()

Expand Down Expand Up @@ -183,9 +182,9 @@ var ptpStreamsCmd = &cmds.Command{

var ptpListenCmd = &cmds.Command{
Helptext: cmds.HelpText{
Tagline: "Create application protocol listener and proxy to network multiaddr.",
Tagline: "Forward p2p connections to a network multiaddr.",
ShortDescription: `
Register a p2p connection handler and proxies the connections to a specified address.
Register a p2p connection handler and forward the connections to a specified address.
Note that the connections originate from the ipfs daemon process.
`,
Expand All @@ -212,8 +211,8 @@ Note that the connections originate from the ipfs daemon process.
return
}

proto := "/app/" + req.Arguments()[0]
if ptpnet.CheckProtoExists(n, proto) {
proto := "/ptp/" + req.Arguments()[0]
if n.PTP.CheckProtoExists(proto) {
res.SetError(errors.New("protocol handler already registered"), cmds.ErrNormal)
return
}
Expand All @@ -224,14 +223,14 @@ Note that the connections originate from the ipfs daemon process.
return
}

_, err = ptpnet.NewListener(n, proto, addr)
_, err = n.PTP.NewListener(n.Context(), proto, addr)
if err != nil {
res.SetError(err, cmds.ErrNormal)
return
}

// Successful response.
res.SetOutput(&PTPAppInfoOutput{
res.SetOutput(&PTPListenerInfoOutput{
Protocol: proto,
Address: addr.String(),
})
Expand All @@ -253,7 +252,7 @@ transparently connect to a p2p service.
Arguments: []cmds.Argument{
cmds.StringArg("Peer", true, false, "Remote peer to connect to"),
cmds.StringArg("Protocol", true, false, "Protocol identifier."),
cmds.StringArg("BindAddress", false, false, "Address to listen for application/s (default: /ip4/127.0.0.1/tcp/0)."),
cmds.StringArg("BindAddress", false, false, "Address to listen for connection/s (default: /ip4/127.0.0.1/tcp/0)."),
},
Run: func(req cmds.Request, res cmds.Response) {
n, err := req.InvocContext().GetNode()
Expand All @@ -279,7 +278,7 @@ transparently connect to a p2p service.
return
}

proto := "/app/" + req.Arguments()[1]
proto := "/ptp/" + req.Arguments()[1]

bindAddr, _ := ma.NewMultiaddr("/ip4/127.0.0.1/tcp/0")
if len(req.Arguments()) == 3 {
Expand All @@ -290,15 +289,15 @@ transparently connect to a p2p service.
}
}

app, err := ptpnet.Dial(n, addr, peer, proto, bindAddr)
listenerInfo, err := n.PTP.Dial(n.Context(), addr, peer, proto, bindAddr)
if err != nil {
res.SetError(err, cmds.ErrNormal)
return
}

output := PTPAppInfoOutput{
Protocol: app.Protocol,
Address: app.Address.String(),
output := PTPListenerInfoOutput{
Protocol: listenerInfo.Protocol,
Address: listenerInfo.Address.String(),
}

res.SetOutput(&output)
Expand Down Expand Up @@ -348,7 +347,7 @@ var ptpCloseCmd = &cmds.Command{

handlerID, err = strconv.ParseUint(req.Arguments()[0], 10, 64)
if err != nil {
proto = "/app/" + req.Arguments()[0]
proto = "/ptp/" + req.Arguments()[0]
} else {
useHandlerID = true
}
Expand All @@ -367,11 +366,11 @@ var ptpCloseCmd = &cmds.Command{
}

if closeAll || !useHandlerID {
for _, app := range n.PTP.Apps.Apps {
if !closeAll && app.Protocol != proto {
for _, listener := range n.PTP.Listeners.Listeners {
if !closeAll && listener.Protocol != proto {
continue
}
app.Close()
listener.Close()
if !closeAll {
break
}
Expand Down
4 changes: 2 additions & 2 deletions core/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (

bstore "github.com/ipfs/go-ipfs/blocks/blockstore"
bserv "github.com/ipfs/go-ipfs/blockservice"
ptp "github.com/ipfs/go-ipfs/ptp"
exchange "github.com/ipfs/go-ipfs/exchange"
bitswap "github.com/ipfs/go-ipfs/exchange/bitswap"
bsnet "github.com/ipfs/go-ipfs/exchange/bitswap/network"
Expand All @@ -36,6 +35,7 @@ import (
ipnsrp "github.com/ipfs/go-ipfs/namesys/republisher"
path "github.com/ipfs/go-ipfs/path"
pin "github.com/ipfs/go-ipfs/pin"
ptp "github.com/ipfs/go-ipfs/ptp"
repo "github.com/ipfs/go-ipfs/repo"
config "github.com/ipfs/go-ipfs/repo/config"
nilrouting "github.com/ipfs/go-ipfs/routing/none"
Expand Down Expand Up @@ -248,7 +248,7 @@ func (n *IpfsNode) startOnlineServices(ctx context.Context, routingOption Routin
n.Floodsub = floodsub.NewFloodSub(ctx, peerhost)
}

n.PTP = ptp.NewPTP()
n.PTP = ptp.NewPTP(n.Identity, n.PeerHost, n.Peerstore)

// setup local discovery
if do != nil {
Expand Down
82 changes: 0 additions & 82 deletions ptp/net/dial.go

This file was deleted.

67 changes: 0 additions & 67 deletions ptp/net/listen.go

This file was deleted.

Loading

0 comments on commit 712a522

Please sign in to comment.