本文整理汇总了Golang中github.com/jbenet/go-multiaddr.NewMultiaddr函数的典型用法代码示例。如果您正苦于以下问题:Golang NewMultiaddr函数的具体用法?Golang NewMultiaddr怎么用?Golang NewMultiaddr使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了NewMultiaddr函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的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"
})
}
开发者ID:Gaboose,项目名称:manet-echo,代码行数:27,代码来源:main.go
示例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")
}
}
开发者ID:Gaboose,项目名称:go-multiaddr-net,代码行数:11,代码来源:ip.go
示例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
}
}
开发者ID:Gaboose,项目名称:manet-echo,代码行数:11,代码来源:ip.go
示例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
}
开发者ID:Gaboose,项目名称:manet-echo,代码行数:7,代码来源:net_test.go
示例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
}
开发者ID:tilgovi,项目名称:go-libp2p,代码行数:7,代码来源:addrsrcs_test.go
示例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)
}
开发者ID:Gaboose,项目名称:examples,代码行数:30,代码来源:main.go
示例7: newMultiaddr
func newMultiaddr(t *testing.T, s string) ma.Multiaddr {
maddr, err := ma.NewMultiaddr(s)
if err != nil {
t.Fatal(err)
}
return maddr
}
开发者ID:tilgovi,项目名称:go-libp2p,代码行数:7,代码来源:addr_test.go
示例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
}
开发者ID:Gaboose,项目名称:go-pubsub,代码行数:29,代码来源:daemon.go
示例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
}
开发者ID:tilgovi,项目名称:go-libp2p,代码行数:25,代码来源:swarm_addr_test.go
示例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
}
开发者ID:Gaboose,项目名称:go-pubsub,代码行数:27,代码来源:mdns.go
示例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")
}
}
开发者ID:Gaboose,项目名称:go-pubsub,代码行数:31,代码来源:protonet_test.go
示例12: MA
func MA(t *testing.T, m string) ma.Multiaddr {
maddr, err := ma.NewMultiaddr(m)
if err != nil {
t.Fatal(err)
}
return maddr
}
开发者ID:tilgovi,项目名称:go-libp2p,代码行数:7,代码来源:addr_manager_test.go
示例13: init
func init() {
// initialize ZeroLocalTCPAddress
maddr, err := ma.NewMultiaddr("/ip4/127.0.0.1/tcp/0")
if err != nil {
panic(err)
}
ZeroLocalTCPAddress = maddr
}
开发者ID:tilgovi,项目名称:go-libp2p,代码行数:8,代码来源:gen.go
示例14: address
func address(addr string) ma.Multiaddr {
m, err := ma.NewMultiaddr(addr)
if err != nil {
die(err)
}
return m
}
开发者ID:Gaboose,项目名称:go-pubsub,代码行数:8,代码来源:multiaddr.go
示例15: mkAddr
func mkAddr(t *testing.T, s string) ma.Multiaddr {
a, err := ma.NewMultiaddr(s)
if err != nil {
t.Fatal(err)
}
return a
}
开发者ID:tilgovi,项目名称:go-libp2p,代码行数:8,代码来源:dial_test.go
示例16: FromTCPAddr
// FromTCPAddr converts a *net.TCPAddr type to a Multiaddr.
func FromTCPAddr(addr *net.TCPAddr) (ma.Multiaddr, error) {
ipm, err := impl.FromIP(addr.IP)
if err != nil {
return nil, err
}
tcpm, err := ma.NewMultiaddr(fmt.Sprintf("/tcp/%d", addr.Port))
if err != nil {
return nil, err
}
return ipm.Encapsulate(tcpm), nil
}
开发者ID:Gaboose,项目名称:go-multiaddr-net,代码行数:14,代码来源:net.go
示例17: setupPeer
func setupPeer(id string, addr string) (*peer.Peer, error) {
tcp, err := ma.NewMultiaddr(addr)
if err != nil {
return nil, err
}
mh, err := mh.FromHexString(id)
if err != nil {
return nil, err
}
p := &peer.Peer{ID: peer.ID(mh)}
p.AddAddress(tcp)
return p, nil
}
开发者ID:Bren2010,项目名称:go-ipfs,代码行数:15,代码来源:conn_test.go
示例18: RandLocalTCPAddress
// RandLocalTCPAddress returns a random multiaddr. it suppresses errors
// for nice composability-- do check the address isn't nil.
//
// Note: for real network tests, use ZeroLocalTCPAddress so the kernel
// assigns an unused TCP port. otherwise you may get clashes. This
// function remains here so that p2p/net/mock (which does not touch the
// real network) can assign different addresses to peers.
func RandLocalTCPAddress() ma.Multiaddr {
// chances are it will work out, but it **might** fail if the port is in use
// most ports above 10000 aren't in use by long running processes, so yay.
// (maybe there should be a range of "loopback" ports that are guaranteed
// to be open for the process, but naturally can only talk to self.)
lastPort.Lock()
if lastPort.port == 0 {
lastPort.port = 10000 + SeededRand.Intn(50000)
}
port := lastPort.port
lastPort.port++
lastPort.Unlock()
addr := fmt.Sprintf("/ip4/127.0.0.1/tcp/%d", port)
maddr, _ := ma.NewMultiaddr(addr)
return maddr
}
开发者ID:tilgovi,项目名称:go-libp2p,代码行数:26,代码来源:gen.go
示例19: makeDummyHost
// create a 'Host' with a random peer to listen on the given address
func makeDummyHost(listen string) (host.Host, error) {
addr, err := ma.NewMultiaddr(listen)
if err != nil {
return nil, err
}
pid, err := testutil.RandPeerID()
if err != nil {
return nil, err
}
// bandwidth counter, should be optional in the future
bwc := metrics.NewBandwidthCounter()
// create a new swarm to be used by the service host
netw, err := swarm.NewNetwork(context.Background(), []ma.Multiaddr{addr}, pid, pstore.NewPeerstore(), bwc)
if err != nil {
return nil, err
}
log.Printf("I am %s/ipfs/%s\n", addr, pid.Pretty())
return bhost.New(netw), nil
}
开发者ID:whyrusleeping,项目名称:libp2p-examples,代码行数:24,代码来源:main.go
示例20: ExternalAddr
func (m *mapping) ExternalAddr() (ma.Multiaddr, error) {
if time.Now().Sub(m.cacheTime) < CacheTime {
return m.cached, nil
}
if m.ExternalPort() == 0 { // dont even try right now.
return nil, ErrNoMapping
}
ip, err := m.nat.nat.GetExternalAddress()
if err != nil {
return nil, err
}
ipmaddr, err := manet.FromIP(ip)
if err != nil {
return nil, fmt.Errorf("error parsing ip")
}
// call m.ExternalPort again, as mapping may have changed under our feet. (tocttou)
extport := m.ExternalPort()
if extport == 0 {
return nil, ErrNoMapping
}
tcp, err := ma.NewMultiaddr(fmt.Sprintf("/%s/%d", m.Protocol(), extport))
if err != nil {
return nil, err
}
maddr2 := ipmaddr.Encapsulate(tcp)
m.cached = maddr2
m.cacheTime = time.Now()
return maddr2, nil
}
开发者ID:tilgovi,项目名称:go-libp2p,代码行数:36,代码来源:nat.go
注:本文中的github.com/jbenet/go-multiaddr.NewMultiaddr函数示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论