提交 035d600f 作者: Juan Batiz-Benet 提交者: Brian Tiger Chow

Godeps: use in net + new multiaddr

上级 06b651c4
{
"ImportPath": "github.com/jbenet/go-ipfs",
"GoVersion": "go1.3.1",
"GoVersion": "go1.3",
"Packages": [
"./..."
],
......@@ -20,6 +20,11 @@
"Rev": "00a7d3b31bbab5795b4a51933c04fc2768242970"
},
{
"ImportPath": "code.google.com/p/go.net/context",
"Comment": "null-144",
"Rev": "ad01a6fcc8a19d3a4478c836895ffe883bd2ceab"
},
{
"ImportPath": "code.google.com/p/gogoprotobuf/proto",
"Rev": "6c980277330804e94257ac7ef70a3adbe1641059"
},
......@@ -55,8 +60,8 @@
},
{
"ImportPath": "github.com/jbenet/go-multiaddr",
"Comment": "0.1.2",
"Rev": "b90678896b52c3e5a4c8176805c6facc3fe3eb82"
"Comment": "0.1.2-2-g0624ab3",
"Rev": "0624ab3bf754d013585c5d07f0100ba34901a689"
},
{
"ImportPath": "github.com/jbenet/go-multihash",
......
// Copyright 2014 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package context_test
import (
"fmt"
"time"
"github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
)
func ExampleWithTimeout() {
// Pass a context with a timeout to tell a blocking function that it
// should abandon its work after the timeout elapses.
ctx, _ := context.WithTimeout(context.Background(), 100*time.Millisecond)
select {
case <-time.After(200 * time.Millisecond):
fmt.Println("overslept")
case <-ctx.Done():
fmt.Println(ctx.Err()) // prints "context deadline exceeded"
}
// Output:
// context deadline exceeded
}
......@@ -66,12 +66,14 @@ func bytesToString(b []byte) (ret string, err error) {
func addressStringToBytes(p *Protocol, s string) []byte {
switch p.Code {
// ipv4,6
case 4, 41:
case P_IP4: // ipv4
return net.ParseIP(s).To4()
case P_IP6: // ipv6
return net.ParseIP(s).To16()
// tcp udp dccp sctp
case 6, 17, 33, 132:
case P_TCP, P_UDP, P_DCCP, P_SCTP:
b := make([]byte, 2)
i, err := strconv.Atoi(s)
if err == nil {
......@@ -87,11 +89,11 @@ func addressBytesToString(p *Protocol, b []byte) string {
switch p.Code {
// ipv4,6
case 4, 41:
case P_IP4, P_IP6:
return net.IP(b).String()
// tcp udp dccp sctp
case 6, 17, 33, 132:
case P_TCP, P_UDP, P_DCCP, P_SCTP:
i := binary.BigEndian.Uint16(b)
return strconv.Itoa(int(i))
}
......
package multiaddr
import (
"fmt"
"net"
)
var errIncorrectNetAddr = fmt.Errorf("incorrect network addr conversion")
// FromNetAddr converts a net.Addr type to a Multiaddr.
func FromNetAddr(a net.Addr) (*Multiaddr, error) {
switch a.Network() {
case "tcp", "tcp4", "tcp6":
ac, ok := a.(*net.TCPAddr)
if !ok {
return nil, errIncorrectNetAddr
}
// Get IP Addr
ipm, err := FromIP(ac.IP)
if err != nil {
return nil, errIncorrectNetAddr
}
// Get TCP Addr
tcpm, err := NewMultiaddr(fmt.Sprintf("/tcp/%d", ac.Port))
if err != nil {
return nil, errIncorrectNetAddr
}
// Encapsulate
return ipm.Encapsulate(tcpm), nil
case "udp", "upd4", "udp6":
ac, ok := a.(*net.UDPAddr)
if !ok {
return nil, errIncorrectNetAddr
}
// Get IP Addr
ipm, err := FromIP(ac.IP)
if err != nil {
return nil, errIncorrectNetAddr
}
// Get UDP Addr
udpm, err := NewMultiaddr(fmt.Sprintf("/udp/%d", ac.Port))
if err != nil {
return nil, errIncorrectNetAddr
}
// Encapsulate
return ipm.Encapsulate(udpm), nil
case "ip", "ip4", "ip6":
ac, ok := a.(*net.IPAddr)
if !ok {
return nil, errIncorrectNetAddr
}
return FromIP(ac.IP)
default:
return nil, fmt.Errorf("unknown network %v", a.Network())
}
}
// FromIP converts a net.IP type to a Multiaddr.
func FromIP(ip net.IP) (*Multiaddr, error) {
switch {
case ip.To4() != nil:
return NewMultiaddr("/ip4/" + ip.String())
case ip.To16() != nil:
return NewMultiaddr("/ip6/" + ip.String())
default:
return nil, errIncorrectNetAddr
}
}
package multiaddr
import (
"net"
"testing"
)
type GenFunc func() (*Multiaddr, error)
func testConvert(t *testing.T, s string, gen GenFunc) {
m, err := gen()
if err != nil {
t.Fatal("failed to generate.")
}
if s2, _ := m.String(); err != nil || s2 != s {
t.Fatal("failed to convert: " + s + " != " + s2)
}
}
func TestFromIP4(t *testing.T) {
testConvert(t, "/ip4/10.20.30.40", func() (*Multiaddr, error) {
return FromIP(net.ParseIP("10.20.30.40"))
})
}
func TestFromIP6(t *testing.T) {
testConvert(t, "/ip6/2001:4860:0:2001::68", func() (*Multiaddr, error) {
return FromIP(net.ParseIP("2001:4860:0:2001::68"))
})
}
func TestFromTCP(t *testing.T) {
testConvert(t, "/ip4/10.20.30.40/tcp/1234", func() (*Multiaddr, error) {
return FromNetAddr(&net.TCPAddr{
IP: net.ParseIP("10.20.30.40"),
Port: 1234,
})
})
}
func TestFromUDP(t *testing.T) {
testConvert(t, "/ip4/10.20.30.40/udp/1234", func() (*Multiaddr, error) {
return FromNetAddr(&net.UDPAddr{
IP: net.ParseIP("10.20.30.40"),
Port: 1234,
})
})
}
......@@ -77,8 +77,8 @@ func NewIpfsNode(cfg *config.Config, online bool) (*IpfsNode, error) {
var (
net *swarm.Swarm
// TODO: refactor so we can use IpfsRouting interface instead of being DHT-specific
route* dht.IpfsDHT
swap *bitswap.BitSwap
route *dht.IpfsDHT
swap *bitswap.BitSwap
)
if online {
......@@ -134,7 +134,7 @@ func initIdentity(cfg *config.Config) (*peer.Peer, error) {
return nil, err
}
addresses = []*ma.Multiaddr{ maddr }
addresses = []*ma.Multiaddr{maddr}
}
skb, err := base64.StdEncoding.DecodeString(cfg.Identity.PrivKey)
......
......@@ -3,7 +3,7 @@ package message
import (
peer "github.com/jbenet/go-ipfs/peer"
proto "code.google.com/p/goprotobuf/proto"
proto "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/goprotobuf/proto"
)
// Message represents a packet of information sent to or received from a
......
......@@ -6,8 +6,8 @@ import (
msg "github.com/jbenet/go-ipfs/net/message"
u "github.com/jbenet/go-ipfs/util"
context "code.google.com/p/go.net/context"
proto "code.google.com/p/goprotobuf/proto"
context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
proto "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/goprotobuf/proto"
)
// Protocol objects produce + consume raw data. They are added to the Muxer
......
......@@ -13,7 +13,7 @@ It has these top-level messages:
*/
package mux
import proto "code.google.com/p/gogoprotobuf/proto"
import proto "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/gogoprotobuf/proto"
import json "encoding/json"
import math "math"
......
......@@ -9,9 +9,9 @@ import (
msg "github.com/jbenet/go-ipfs/net/message"
peer "github.com/jbenet/go-ipfs/peer"
u "github.com/jbenet/go-ipfs/util"
mh "github.com/jbenet/go-multihash"
mh "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multihash"
context "code.google.com/p/go.net/context"
context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
)
type TestProtocol struct {
......
......@@ -6,7 +6,7 @@ import (
msg "github.com/jbenet/go-ipfs/net/message"
peer "github.com/jbenet/go-ipfs/peer"
proto "code.google.com/p/goprotobuf/proto"
proto "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/goprotobuf/proto"
)
const (
......
......@@ -13,7 +13,7 @@ It has these top-level messages:
*/
package service
import proto "code.google.com/p/gogoprotobuf/proto"
import proto "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/gogoprotobuf/proto"
import json "encoding/json"
import math "math"
......
......@@ -7,7 +7,7 @@ import (
msg "github.com/jbenet/go-ipfs/net/message"
u "github.com/jbenet/go-ipfs/util"
context "code.google.com/p/go.net/context"
context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
)
// Handler is an interface that objects must implement in order to handle
......
......@@ -8,8 +8,8 @@ import (
msg "github.com/jbenet/go-ipfs/net/message"
peer "github.com/jbenet/go-ipfs/peer"
context "code.google.com/p/go.net/context"
mh "github.com/jbenet/go-multihash"
context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
mh "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multihash"
)
// ReverseHandler reverses all Data it receives and sends it back.
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论