本文整理汇总了Golang中github.com/jbenet/go-multiaddr.NewMultiaddr函数的典型用法代码示例。如果您正苦于以下问题:Golang NewMultiaddr函数的具体用法?Golang NewMultiaddr怎么用?Golang NewMultiaddr使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了NewMultiaddr函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: main
func main() {
ip := os.Getenv("OPENSHIFT_GO_IP")
if ip == "" {
ip = "127.0.0.1"
}
port := os.Getenv("OPENSHIFT_GO_PORT")
if port == "" {
port = "8000"
}
m, err := ma.NewMultiaddr(fmt.Sprintf("/ip4/%s/tcp/%s/ws/echo", ip, port))
if err != nil {
panic(err)
}
go serve(m, func(s string) string {
return fmt.Sprintf("you're the \"%s\"\n", s)
})
m, err = ma.NewMultiaddr(fmt.Sprintf("/ip4/%s/tcp/%s/ws/notecho", ip, port))
if err != nil {
panic(err)
}
serve(m, func(s string) string {
return "not speaking to you\n"
})
}
示例2: FromIP
// FromIP converts a net.IP type to a Multiaddr.
func FromIP(ip net.IP) (ma.Multiaddr, error) {
switch {
case ip.To4() != nil:
return ma.NewMultiaddr("/ip4/" + ip.String())
case ip.To16() != nil:
return ma.NewMultiaddr("/ip6/" + ip.String())
default:
return nil, fmt.Errorf("incorrect network addr conversion")
}
}
示例3: FromIP
// FromIP converts a net.IP type to a Multiaddr.
func FromIP(ip net.IP) (ma.Multiaddr, error) {
switch {
case ip.To4() != nil:
return ma.NewMultiaddr("/ip4/" + ip.String())
case ip.To16() != nil:
return ma.NewMultiaddr("/ip6/" + ip.String())
default:
return nil, errIncorrectNetAddr
}
}
示例4: newMultiaddr
func newMultiaddr(t *testing.T, m string) ma.Multiaddr {
maddr, err := ma.NewMultiaddr(m)
if err != nil {
t.Fatal("failed to construct multiaddr:", m, err)
}
return maddr
}
示例5: newAddrOrFatal
func newAddrOrFatal(t *testing.T, s string) ma.Multiaddr {
a, err := ma.NewMultiaddr(s)
if err != nil {
t.Fatal("error parsing multiaddr", err)
}
return a
}
示例6: doInterface
func doInterface() {
m, _ := ma.NewMultiaddr("/ip4/127.0.0.1")
ps := []IPeer{
Peer{"me", [][]byte{m.Bytes()}, nil},
Peer{"they", [][]byte{m.Bytes()}, map[string]interface{}{"age": 3}},
}
fmt.Printf("Encoding: %+v\n", ps)
var buf bytes.Buffer
mx := mux.StandardMux()
mx.Encoder(&buf).Encode(ps)
fmt.Printf("Encoded buffer: %s\n", buf.String())
// Now we'll decode the []Peer structure into []IPeer slice
// without mentioning the type Peer.
dec := mx.Decoder(&buf)
typ := reflect.SliceOf(reflect.TypeOf(ps[0]))
val, err := reflectDecode(dec, typ)
if err != nil {
panic(err)
}
v := make([]IPeer, val.Len())
for i := 0; i < val.Len(); i++ {
v[i] = val.Index(i).Interface().(IPeer)
}
fmt.Printf("Decoded: %+v\n", v)
}
示例7: newMultiaddr
func newMultiaddr(t *testing.T, s string) ma.Multiaddr {
maddr, err := ma.NewMultiaddr(s)
if err != nil {
t.Fatal(err)
}
return maddr
}
示例8: BuildSwarmAddrs
// Builds and returns the Multiaddrs for the swarm to listen on
func BuildSwarmAddrs(port int) [][]byte {
// Get all interface addresses
all, _ := manet.InterfaceMultiaddrs()
// Filter out loopback and link-local addresses
var filtered []ma.Multiaddr
for _, m := range all {
if manet.IsIPLoopback(m) {
continue
}
if manet.IsIP6LinkLocal(m) {
continue
}
filtered = append(filtered, m)
}
// Add tcp/<port> to each address and convert to byte representation
prt, err := ma.NewMultiaddr(fmt.Sprintf("/tcp/%d", port))
if err != nil {
panic(err)
}
var listenAddrs [][]byte
for _, a := range filtered {
listenAddrs = append(listenAddrs, a.Encapsulate(prt).Bytes())
}
return listenAddrs
}
示例9: TestDialBadAddrs
func TestDialBadAddrs(t *testing.T) {
m := func(s string) ma.Multiaddr {
maddr, err := ma.NewMultiaddr(s)
if err != nil {
t.Fatal(err)
}
return maddr
}
ctx := context.Background()
s := makeSwarms(ctx, t, 1)[0]
test := func(a ma.Multiaddr) {
p := testutil.RandPeerIDFatal(t)
s.peers.AddAddr(p, a, peer.PermanentAddrTTL)
if _, err := s.Dial(ctx, p); err == nil {
t.Error("swarm should not dial: %s", m)
}
}
test(m("/ip6/fe80::1")) // link local
test(m("/ip6/fe80::100")) // link local
test(m("/ip4/127.0.0.1/udp/1234/utp")) // utp
}
示例10: MultiaddrsFromServiceEntry
func MultiaddrsFromServiceEntry(en *mdns.ServiceEntry) [][]byte {
var addrs []ma.Multiaddr
// Parse IP addresses
addr, err := manet.FromIP(en.AddrV4)
if err == nil {
addrs = append(addrs, addr)
}
addr, err = manet.FromIP(en.AddrV6)
if err == nil {
addrs = append(addrs, addr)
}
var bytes [][]byte
for _, addr := range addrs {
// Append port
prt, err := ma.NewMultiaddr(fmt.Sprintf("/tcp/%d", en.Port))
if err != nil {
continue
}
addr = addr.Encapsulate(prt)
bytes = append(bytes, addr.Bytes())
}
return bytes
}
示例11: TestBadProto
func TestBadProto(t *testing.T) {
m, err := ma.NewMultiaddr("/ip4/127.0.0.1/tcp/8002")
if err != nil {
t.Fatal(err)
}
gw1 := NewGateway()
defer gw1.Close()
err = gw1.ListenAll([][]byte{m.Bytes()})
if err != nil {
t.Fatal(err)
}
pn1 := gw1.NewProtoNet("/foo")
defer pn1.Close()
gw2 := NewGateway()
defer gw2.Close()
pn2 := gw2.NewProtoNet("/bar")
defer pn2.Close()
dest := &PeerInfo{
ID: "bob",
MAddrs: [][]byte{m.Bytes()},
}
_, err = pn2.Dial(dest)
if err == nil {
t.Fatal("expected error from ProtoNet.Dial, got nil")
}
}
示例12: MA
func MA(t *testing.T, m string) ma.Multiaddr {
maddr, err := ma.NewMultiaddr(m)
if err != nil {
t.Fatal(err)
}
return maddr
}
示例13: init
func init() {
// initialize ZeroLocalTCPAddress
maddr, err := ma.NewMultiaddr("/ip4/127.0.0.1/tcp/0")
if err != nil {
panic(err)
}
ZeroLocalTCPAddress = maddr
}
示例14: address
func address(addr string) ma.Multiaddr {
m, err := ma.NewMultiaddr(addr)
if err != nil {
die(err)
}
return m
}
示例15: mkAddr
func mkAddr(t *testing.T, s string) ma.Multiaddr {
a, err := ma.NewMultiaddr(s)
if err != nil {
t.Fatal(err)
}
return a
}