本文整理匯總了Golang中gx/ipfs/QmcobAGsCjYt5DXoq9et9L8yR8er7o7Cu3DTvpaq12jYSz/go-multiaddr.NewMultiaddr函數的典型用法代碼示例。如果您正苦於以下問題:Golang NewMultiaddr函數的具體用法?Golang NewMultiaddr怎麽用?Golang NewMultiaddr使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了NewMultiaddr函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: 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
}
}
示例2: TestThinWaist
func TestThinWaist(t *testing.T) {
addrs := map[string]bool{
"/ip4/127.0.0.1/udp/1234": true,
"/ip4/127.0.0.1/tcp/1234": true,
"/ip4/127.0.0.1/udp/1234/utp": true,
"/ip4/127.0.0.1/udp/1234/tcp/1234": true,
"/ip4/127.0.0.1/tcp/12345/ip4/1.2.3.4": true,
"/ip6/::1/tcp/80": true,
"/ip6/::1/udp/80": true,
"/ip6/::1": true,
"/ip6/::1/utp": false,
"/tcp/1234/ip4/1.2.3.4": false,
"/tcp/1234": false,
"/tcp/1234/utp": false,
"/tcp/1234/udp/1234": false,
"/ip4/1.2.3.4/ip4/2.3.4.5": true,
"/ip6/::1/ip4/2.3.4.5": true,
}
for a, res := range addrs {
m, err := ma.NewMultiaddr(a)
if err != nil {
t.Fatalf("failed to construct Multiaddr: %s", a)
}
if IsThinWaist(m) != res {
t.Fatalf("IsThinWaist(%s) != %v", a, res)
}
}
}
示例3: newMultiaddr
func newMultiaddr(t *testing.T, s string) ma.Multiaddr {
maddr, err := ma.NewMultiaddr(s)
if err != nil {
t.Fatal(err)
}
return maddr
}
示例4: TestDialArgs
func TestDialArgs(t *testing.T) {
test := func(e_maddr, e_nw, e_host string) {
m, err := ma.NewMultiaddr(e_maddr)
if err != nil {
t.Fatal("failed to construct", "/ip4/127.0.0.1/udp/1234", e_maddr)
}
nw, host, err := DialArgs(m)
if err != nil {
t.Fatal("failed to get dial args", e_maddr, m, err)
}
if nw != e_nw {
t.Error("failed to get udp network Dial Arg", e_nw, nw)
}
if host != e_host {
t.Error("failed to get host:port Dial Arg", e_host, host)
}
}
test("/ip4/127.0.0.1/udp/1234", "udp4", "127.0.0.1:1234")
test("/ip4/127.0.0.1/tcp/4321", "tcp4", "127.0.0.1:4321")
test("/ip4/127.0.0.1/udp/1234/utp", "utp4", "127.0.0.1:1234")
test("/ip6/::1/udp/1234", "udp6", "[::1]:1234")
test("/ip6/::1/tcp/4321", "tcp6", "[::1]:4321")
test("/ip6/::1/udp/1234/utp", "utp6", "[::1]:1234")
}
示例5: address
func address(addr string) ma.Multiaddr {
m, err := ma.NewMultiaddr(addr)
if err != nil {
die(err)
}
return m
}
示例6: init
func init() {
// initialize ZeroLocalTCPAddress
maddr, err := ma.NewMultiaddr("/ip4/127.0.0.1/tcp/0")
if err != nil {
panic(err)
}
ZeroLocalTCPAddress = maddr
}
示例7: serveHTTPGateway
// serveHTTPGateway collects options, creates listener, prints status message and starts serving requests
func serveHTTPGateway(req cmds.Request) (error, <-chan error) {
cfg, err := req.InvocContext().GetConfig()
if err != nil {
return fmt.Errorf("serveHTTPGateway: GetConfig() failed: %s", err), nil
}
gatewayMaddr, err := ma.NewMultiaddr(cfg.Addresses.Gateway)
if err != nil {
return fmt.Errorf("serveHTTPGateway: invalid gateway address: %q (err: %s)", cfg.Addresses.Gateway, err), nil
}
writable, writableOptionFound, err := req.Option(writableKwd).Bool()
if err != nil {
return fmt.Errorf("serveHTTPGateway: req.Option(%s) failed: %s", writableKwd, err), nil
}
if !writableOptionFound {
writable = cfg.Gateway.Writable
}
gwLis, err := manet.Listen(gatewayMaddr)
if err != nil {
return fmt.Errorf("serveHTTPGateway: manet.Listen(%s) failed: %s", gatewayMaddr, err), nil
}
// we might have listened to /tcp/0 - lets see what we are listing on
gatewayMaddr = gwLis.Multiaddr()
if writable {
fmt.Printf("Gateway (writable) server listening on %s\n", gatewayMaddr)
} else {
fmt.Printf("Gateway (readonly) server listening on %s\n", gatewayMaddr)
}
var opts = []corehttp.ServeOption{
corehttp.MetricsCollectionOption("gateway"),
corehttp.CommandsROOption(*req.InvocContext()),
corehttp.VersionOption(),
corehttp.IPNSHostnameOption(),
corehttp.GatewayOption(writable, cfg.Gateway.PathPrefixes),
}
if len(cfg.Gateway.RootRedirect) > 0 {
opts = append(opts, corehttp.RedirectOption("", cfg.Gateway.RootRedirect))
}
node, err := req.InvocContext().ConstructNode()
if err != nil {
return fmt.Errorf("serveHTTPGateway: ConstructNode() failed: %s", err), nil
}
errc := make(chan error)
go func() {
errc <- corehttp.Serve(node, gwLis.NetListener(), opts...)
close(errc)
}()
return nil, errc
}
示例8: TestParseMultiaddrBad
func TestParseMultiaddrBad(t *testing.T) {
for _, b := range bad {
m, err := ma.NewMultiaddr(b)
if err != nil {
continue // skip these.
}
if _, err := ParseMultiaddr(m); err == nil {
t.Error("succeeded in parsing", m)
}
}
}
示例9: ParseString
// ParseString parses a string representation of an address into an IPFSAddr
func ParseString(str string) (a IPFSAddr, err error) {
if str == "" {
return nil, ErrInvalidAddr
}
m, err := ma.NewMultiaddr(str)
if err != nil {
return nil, err
}
return ParseMultiaddr(m)
}
示例10: listenAddresses
func listenAddresses(cfg *config.Config) ([]ma.Multiaddr, error) {
var listen []ma.Multiaddr
for _, addr := range cfg.Addresses.Swarm {
maddr, err := ma.NewMultiaddr(addr)
if err != nil {
return nil, fmt.Errorf("Failure to parse config.Addresses.Swarm: %s", cfg.Addresses.Swarm)
}
listen = append(listen, maddr)
}
return listen, nil
}
示例11: getApiClient
// getApiClient checks the repo, and the given options, checking for
// a running API service. if there is one, it returns a client.
// otherwise, it returns errApiNotRunning, or another error.
func getApiClient(repoPath, apiAddrStr string) (cmdsHttp.Client, error) {
if apiAddrStr == "" {
var err error
if apiAddrStr, err = fsrepo.APIAddr(repoPath); err != nil {
return nil, err
}
}
addr, err := ma.NewMultiaddr(apiAddrStr)
if err != nil {
return nil, err
}
return apiClientForAddr(addr)
}
示例12: ListenAndServe
// ListenAndServe runs an HTTP server listening at |listeningMultiAddr| with
// the given serve options. The address must be provided in multiaddr format.
//
// TODO intelligently parse address strings in other formats so long as they
// unambiguously map to a valid multiaddr. e.g. for convenience, ":8080" should
// map to "/ip4/0.0.0.0/tcp/8080".
func ListenAndServe(n *core.IpfsNode, listeningMultiAddr string, options ...ServeOption) error {
addr, err := ma.NewMultiaddr(listeningMultiAddr)
if err != nil {
return err
}
list, err := manet.Listen(addr)
if err != nil {
return err
}
// we might have listened to /tcp/0 - lets see what we are listing on
addr = list.Multiaddr()
fmt.Printf("API server listening on %s\n", addr)
return Serve(n, list.NetListener(), options...)
}
示例13: main
func main() {
flag.Parse()
// extract address from host flag
addr, err := ma.NewMultiaddr(*host)
if err != nil {
log.Fatal("NewMultiaddr() failed: ", err)
}
p := addr.Protocols()
if len(p) < 2 {
log.Fatal("need two protocols in host flag (/ip/tcp): ", addr)
}
_, host, err := manet.DialArgs(addr)
if err != nil {
log.Fatal("manet.DialArgs() failed: ", err)
}
if *verbose { // lower log level
logging.SetDebugLogging()
}
// construct url to dial
var u url.URL
u.Scheme = "http"
u.Host = host
u.Path = *endpoint
// show what we got
start := time.Now()
log.Debug("starting at %s, tries: %d, timeout: %s, url: %s", start, *tries, *timeout, u)
for *tries > 0 {
err := checkOK(http.Get(u.String()))
if err == nil {
log.Debugf("ok - endpoint reachable with %d tries remaining, took %s", *tries, time.Since(start))
os.Exit(0)
}
log.Debug("get failed: ", err)
time.Sleep(*timeout)
*tries--
}
log.Error("failed.")
os.Exit(1)
}
示例14: NewShell
func NewShell(url string) *Shell {
if a, err := ma.NewMultiaddr(url); err == nil {
_, host, err := manet.DialArgs(a)
if err == nil {
url = host
}
}
return &Shell{
url: url,
httpcli: &gohttp.Client{
Transport: &gohttp.Transport{
DisableKeepAlives: true,
},
},
}
}
示例15: 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
}