func NewIDService(h host.Host) *IDService { s := &IDService{ Host: h, currid: make(map[inet.Conn]chan struct{}), } h.SetStreamHandler(ID, s.RequestHandler) return s }
func NewRelayService(h host.Host, sh inet.StreamHandler) *RelayService { s := &RelayService{ host: h, handler: sh, } h.SetStreamHandler(ID, s.requestHandler) return s }
func testHasProtocolVersions(t *testing.T, h host.Host, p peer.ID) { v, err := h.Peerstore().Get(p, "ProtocolVersion") if v == nil { t.Error("no protocol version") return } if v.(string) != identify.LibP2PVersion { t.Error("protocol mismatch", err) } v, err = h.Peerstore().Get(p, "AgentVersion") if v.(string) != identify.ClientVersion { t.Error("agent version mismatch", err) } }
func getDialableListenAddrs(ph host.Host) ([]*net.TCPAddr, error) { var out []*net.TCPAddr for _, addr := range ph.Addrs() { na, err := manet.ToNetAddr(addr) if err != nil { continue } tcp, ok := na.(*net.TCPAddr) if ok { out = append(out, tcp) } } if len(out) == 0 { return nil, errors.New("failed to find good external addr from peerhost") } return out, nil }
func testKnowsAddrs(t *testing.T, h host.Host, p peer.ID, expected []ma.Multiaddr) { actual := h.Peerstore().Addrs(p) if len(actual) != len(expected) { t.Error("dont have the same addresses") } have := map[string]struct{}{} for _, addr := range actual { have[addr.String()] = struct{}{} } for _, addr := range expected { if _, found := have[addr.String()]; !found { t.Errorf("%s did not have addr for %s: %s", h.ID(), p, addr) // panic("ahhhhhhh") } } }
func NewMdnsService(peerhost host.Host, interval time.Duration) (Service, error) { // TODO: dont let mdns use logging... golog.SetOutput(ioutil.Discard) var ipaddrs []net.IP port := 4001 addrs, err := getDialableListenAddrs(peerhost) if err != nil { log.Warning(err) } else { port = addrs[0].Port for _, a := range addrs { ipaddrs = append(ipaddrs, a.IP) } } myid := peerhost.ID().Pretty() info := []string{myid} service, err := mdns.NewMDNSService(myid, ServiceTag, "", "", port, ipaddrs, info) if err != nil { return nil, err } // Create the mDNS server, defer shutdown server, err := mdns.NewServer(&mdns.Config{Zone: service}) if err != nil { return nil, err } s := &mdnsService{ server: server, service: service, host: peerhost, interval: interval, } go s.pollForEntries() return s, nil }
func NewPingService(h host.Host) *PingService { ps := &PingService{h} h.SetStreamHandler(ID, ps.PingHandler) return ps }