當前位置: 首頁>>代碼示例>>Golang>>正文


Golang e3x.Channel類代碼示例

本文整理匯總了Golang中github.com/telehash/gogotelehash/e3x.Channel的典型用法代碼示例。如果您正苦於以下問題:Golang Channel類的具體用法?Golang Channel怎麽用?Golang Channel使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。


在下文中一共展示了Channel類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。

示例1: handleSkipSuccessor

func (t *transport) handleSkipSuccessor(ch *e3x.Channel) {
	var (
		err    error
		stream io.ReadWriteCloser
		req    struct {
			Target string
			Self   *completeVnode
		}
	)

	defer ch.Close()

	stream = newStream(ch)

	err = json.NewDecoder(stream).Decode(&req)
	if err != nil {
		// log
		// tracef("(SkipSuccessor) error: %s", err)
		return
	}

	rpc := t.lookupRPC(req.Target)
	if rpc == nil {
		// log
		// tracef("(SkipSuccessor) error: %s", "no RPC")
		return
	}

	err = rpc.SkipSuccessor(t.internalVnode(req.Self))
	if err != nil {
		// log
		// tracef("(SkipSuccessor) error: %s", err)
		return
	}
}
開發者ID:utamaro,項目名稱:gogotelehash,代碼行數:35,代碼來源:chord.go

示例2: handleListVnodes

func (t *transport) handleListVnodes(ch *e3x.Channel) {
	var (
		err error
		res []*completeVnode
	)

	defer ch.Close()

	_, err = ch.ReadPacket()
	if err != nil {
		// log error
		// tracef("error: %s", err)
		return
	}

	for _, rpc := range t.localVnodes {
		res = append(res, t.completeVnode(rpc.vn))
	}

	err = json.NewEncoder(newStream(ch)).Encode(&res)
	if err != nil {
		// log error
		// tracef("error: %s", err)
		return
	}

	// tracef("handle.ListVnodes() => %s", res)
}
開發者ID:utamaro,項目名稱:gogotelehash,代碼行數:28,代碼來源:chord.go

示例3: ListVnodes

// Gets a list of the vnodes on the box
func (t *transport) ListVnodes(hn string) ([]*chord.Vnode, error) {
	var (
		addr *e3x.Addr
		ch   *e3x.Channel
		res  []*completeVnode
		err  error
	)

	addr = t.lookupAddr(hashname.H(hn))
	if addr == nil {
		return nil, e3x.ErrNoAddress
	}

	ch, err = t.e.Open(addr, "chord.list", true)
	if err != nil {
		return nil, err
	}

	defer ch.Close()

	// ch.SetReadDeadline(time.Now().Add(30*time.Second))
	// ch.SetWriteDeadline(time.Now().Add(30*time.Second))

	err = ch.WritePacket(&lob.Packet{})
	if err != nil {
		return nil, err
	}

	err = json.NewDecoder(newStream(ch)).Decode(&res)
	if err != nil {
		return nil, err
	}

	return t.internalVnodes(res), nil
}
開發者ID:utamaro,項目名稱:gogotelehash,代碼行數:36,代碼來源:chord.go

示例4: FindSuccessors

// Find a successor
func (t *transport) FindSuccessors(vn *chord.Vnode, n int, k []byte) ([]*chord.Vnode, error) {
	var (
		addr   *e3x.Addr
		ch     *e3x.Channel
		stream io.ReadWriteCloser
		res    []*completeVnode
		err    error

		req = struct {
			Target string
			N      int
			K      []byte
		}{vn.String(), n, k}
	)

	// tracef("FindSuccessors(target:Vnode(%q))", vn.String())

	addr = t.lookupAddr(hashname.H(vn.Host))
	if addr == nil {
		return nil, e3x.ErrNoAddress
	}

	ch, err = t.e.Open(addr, "chord.successors.find", true)
	if err != nil {
		return nil, err
	}

	defer ch.Close()

	// ch.SetReadDeadline(time.Now().Add(30*time.Second))
	// ch.SetWriteDeadline(time.Now().Add(30*time.Second))

	stream = newStream(ch)

	err = json.NewEncoder(stream).Encode(&req)
	if err != nil {
		// tracef("(FindSuccessors) error: %s", err)
		return nil, err
	}

	err = json.NewDecoder(stream).Decode(&res)
	if err != nil {
		// tracef("(FindSuccessors) error: %s", err)
		return nil, err
	}

	return t.internalVnodes(res), nil
}
開發者ID:utamaro,項目名稱:gogotelehash,代碼行數:49,代碼來源:chord.go

示例5: handleNotify

func (t *transport) handleNotify(ch *e3x.Channel) {
	var (
		err    error
		stream io.ReadWriteCloser
		req    struct {
			Target string
			Self   *completeVnode
		}
		vnodes []*chord.Vnode
		res    []*completeVnode
	)

	defer ch.Close()

	stream = newStream(ch)

	err = json.NewDecoder(stream).Decode(&req)
	if err != nil {
		// log
		// tracef("(Notify) error: %s", err)
		return
	}

	rpc := t.lookupRPC(req.Target)
	if rpc == nil {
		// log
		// tracef("(Notify) error: %s", "no RPC")
		return
	}

	vnodes, err = rpc.Notify(t.internalVnode(req.Self))
	if err != nil {
		// log
		// tracef("(Notify) error: %s", err)
		return
	}

	res = t.completeVnodes(vnodes)

	err = json.NewEncoder(stream).Encode(&res)
	if err != nil {
		// log
		// tracef("(Notify) error: %s", err)
		return
	}

	// tracef("handle.Notify(target:Vnode(%q), self:Vnode(%q)) => []Vnode(%v)", req.Target, req.Self.Id, res)
}
開發者ID:utamaro,項目名稱:gogotelehash,代碼行數:48,代碼來源:chord.go

示例6: RoundTrip

func (rt *RoundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
	var (
		hashname = hashname.H(req.URL.Host)
		c        *e3x.Channel
		resp     *http.Response
		err      error
	)

	x := rt.Endpoint.GetExchange(hashname)
	if x == nil {
		return nil, e3x.UnreachableEndpointError(hashname)
	}

	c, err = x.Open("thtp", true)
	if err != nil {
		c.Close()
		return nil, err
	}

	err = rt.writeRequest(req, c)
	if err != nil {
		c.Close()
		return nil, err
	}

	resp, err = rt.readResponse(c)
	if err != nil {
		c.Close()
		return nil, err
	}

	resp.Request = req
	return resp, nil
}
開發者ID:utamaro,項目名稱:gogotelehash,代碼行數:34,代碼來源:round_tripper.go

示例7: handleGetPredecessor

func (t *transport) handleGetPredecessor(ch *e3x.Channel) {
	var (
		err   error
		pkt   *lob.Packet
		id    string
		vnode *chord.Vnode
		res   *completeVnode
	)

	defer ch.Close()

	pkt, err = ch.ReadPacket()
	if err != nil {
		// log error
		// tracef("error: %s", err)
		return
	}

	id, _ = pkt.Header().GetString("vn")
	rpc := t.lookupRPC(id)
	if rpc == nil {
		// log
		// tracef("error: %s", "no RPC")
		return
	}

	vnode, err = rpc.GetPredecessor()
	if err != nil {
		// log
		// tracef("error: %s", err)
		return
	}

	res = t.completeVnode(vnode)
	err = json.NewEncoder(newStream(ch)).Encode(&res)
	if err != nil {
		// log
		// tracef("error: %s", err)
		return
	}

	if res != nil {
		// tracef("handle.GetPredecessor(Vnode(%q)) => Vnode(%q)", id, res.Id)
	}
}
開發者ID:utamaro,項目名稱:gogotelehash,代碼行數:45,代碼來源:chord.go

示例8: Notify

// Notify our successor of ourselves
func (t *transport) Notify(target, self *chord.Vnode) ([]*chord.Vnode, error) {
	var (
		addr   *e3x.Addr
		ch     *e3x.Channel
		stream io.ReadWriteCloser
		res    []*completeVnode
		err    error

		req = struct {
			Target string
			Self   *completeVnode
		}{target.String(), t.completeVnode(self)}
	)

	addr = t.lookupAddr(hashname.H(target.Host))
	if addr == nil {
		return nil, e3x.ErrNoAddress
	}

	ch, err = t.e.Open(addr, "chord.notify", true)
	if err != nil {
		return nil, err
	}

	defer ch.Close()

	// ch.SetReadDeadline(time.Now().Add(30*time.Second))
	// ch.SetWriteDeadline(time.Now().Add(30*time.Second))

	stream = newStream(ch)

	err = json.NewEncoder(stream).Encode(&req)
	if err != nil {
		return nil, err
	}

	err = json.NewDecoder(stream).Decode(&res)
	if err != nil {
		return nil, err
	}

	// tracef("Notify(target:Vnode(%q), self:Vnode(%q)) => []Vnode(%v)", target.String(), self.String(), res)
	return t.internalVnodes(res), nil
}
開發者ID:utamaro,項目名稱:gogotelehash,代碼行數:45,代碼來源:chord.go

示例9: GetPredecessor

// Request a nodes predecessor
func (t *transport) GetPredecessor(vn *chord.Vnode) (*chord.Vnode, error) {
	var (
		addr *e3x.Addr
		ch   *e3x.Channel
		pkt  *lob.Packet
		res  *completeVnode
		err  error
	)

	addr = t.lookupAddr(hashname.H(vn.Host))
	if addr == nil {
		return nil, e3x.ErrNoAddress
	}

	ch, err = t.e.Open(addr, "chord.predecessor.get", true)
	if err != nil {
		return nil, err
	}

	defer ch.Close()

	// ch.SetReadDeadline(time.Now().Add(30*time.Second))
	// ch.SetWriteDeadline(time.Now().Add(30*time.Second))

	pkt = &lob.Packet{}
	pkt.Header().SetString("vn", vn.String())
	err = ch.WritePacket(pkt)
	if err != nil {
		return nil, err
	}

	err = json.NewDecoder(newStream(ch)).Decode(&res)
	if err != nil {
		return nil, err
	}

	if res != nil {
		// tracef("GetPredecessor(Vnode(%q)) => Vnode(%q)", vn.String(), res.Id)
	}
	return t.internalVnode(res), nil
}
開發者ID:utamaro,項目名稱:gogotelehash,代碼行數:42,代碼來源:chord.go

示例10: handleFindSuccessors

func (t *transport) handleFindSuccessors(ch *e3x.Channel) {
	var (
		err    error
		stream io.ReadWriteCloser
		req    struct {
			Target string
			N      int
			K      []byte
		}
		res []*chord.Vnode
	)

	defer ch.Close()

	stream = newStream(ch)

	err = json.NewDecoder(stream).Decode(&req)
	if err != nil {
		// log
		// tracef("(FindSuccessors) error: %s", err)
		return
	}

	rpc := t.lookupRPC(req.Target)
	if rpc != nil {
		res, err = rpc.FindSuccessors(req.N, req.K)
		if err != nil {
			// log
			// tracef("(FindSuccessors) error: %s", err)
			return
		}
	}

	err = json.NewEncoder(stream).Encode(t.completeVnodes(res))
	if err != nil {
		// log
		// tracef("(FindSuccessors) error: %s", err)
		return
	}
}
開發者ID:utamaro,項目名稱:gogotelehash,代碼行數:40,代碼來源:chord.go

示例11: handlePing

func (t *transport) handlePing(ch *e3x.Channel) {
	var (
		err   error
		pkt   *lob.Packet
		id    string
		alive bool
	)

	defer ch.Close()

	pkt, err = ch.ReadPacket()
	if err != nil {
		// log error
		// tracef("error: %s", err)
		return
	}

	id, _ = pkt.Header().GetString("vn")
	rpc := t.lookupRPC(id)
	if rpc == nil {
		alive = false
	} else {
		alive = true
	}

	pkt = &lob.Packet{}
	pkt.Header().SetBool("alive", alive)
	err = ch.WritePacket(pkt)
	if err != nil {
		// log error
		// tracef("error: %s", err)
		return
	}
}
開發者ID:utamaro,項目名稱:gogotelehash,代碼行數:34,代碼來源:chord.go

示例12: serveTelehash

func (mod *module) serveTelehash(c *e3x.Channel) {
	defer c.Close()

	defer func() {
		if err := recover(); err != nil {
			const size = 64 << 10
			buf := make([]byte, size)
			buf = buf[:runtime.Stack(buf, false)]
			mod.logf("thtp: panic serving %s: %v\n%s", c.RemoteHashname(), err, buf)
		}
	}()

	req, err := mod.readRequest(c)
	if err != nil {
		return
	}

	rw := newResponseWriter(c)
	defer rw.Flush()

	mod.handler.ServeHTTP(rw, req)
}
開發者ID:utamaro,項目名稱:gogotelehash,代碼行數:22,代碼來源:server.go

示例13: handlePathRequest

func (mod *module) handlePathRequest(c *e3x.Channel) {
	defer c.Kill()

	pkt, err := c.ReadPacket()
	if err != nil {
		return // ignore
	}

	// decode paths known by peer and add them as candidates
	if header, found := pkt.Header().Get("paths"); found {
		data, err := json.Marshal(header)
		if err != nil {
			return // ignore
		}

		var entries []json.RawMessage
		err = json.Unmarshal(data, &entries)
		if err != nil {
			return // ignore
		}

		for _, entry := range entries {
			addr, err := transports.DecodeAddr(entry)
			if err != nil {
			}
			if err == nil {
				c.Exchange().AddPathCandidate(addr)
			}
		}
	}

	var pipes = c.Exchange().KnownPipes()

	for _, pipe := range pipes {
		pkt := &lob.Packet{}
		pkt.Header().Set("path", pipe.RemoteAddr())
		c.WritePacketTo(pkt, pipe)
	}
}
開發者ID:utamaro,項目名稱:gogotelehash,代碼行數:39,代碼來源:module.go

示例14: SkipSuccessor

// Instructs a node to skip a given successor. Used to leave.
func (t *transport) SkipSuccessor(target, self *chord.Vnode) error {
	var (
		addr   *e3x.Addr
		ch     *e3x.Channel
		stream io.ReadWriteCloser
		err    error

		req = struct {
			Target string
			Self   *completeVnode
		}{target.String(), t.completeVnode(self)}
	)

	addr = t.lookupAddr(hashname.H(target.Host))
	if addr == nil {
		return e3x.ErrNoAddress
	}

	ch, err = t.e.Open(addr, "chord.successor.skip", true)
	if err != nil {
		return err
	}

	defer ch.Close()

	// ch.SetReadDeadline(time.Now().Add(30*time.Second))
	// ch.SetWriteDeadline(time.Now().Add(30*time.Second))

	stream = newStream(ch)

	err = json.NewEncoder(stream).Encode(&req)
	if err != nil {
		return err
	}

	return nil
}
開發者ID:utamaro,項目名稱:gogotelehash,代碼行數:38,代碼來源:chord.go

示例15: Ping

// Ping a Vnode, check for liveness
func (t *transport) Ping(vn *chord.Vnode) (bool, error) {
	var (
		addr  *e3x.Addr
		ch    *e3x.Channel
		pkt   *lob.Packet
		alive bool
		err   error
	)

	addr = t.lookupAddr(hashname.H(vn.Host))
	if addr == nil {
		return false, e3x.ErrNoAddress
	}

	ch, err = t.e.Open(addr, "chord.ping", true)
	if err != nil {
		return false, err
	}

	defer ch.Close()

	// ch.SetReadDeadline(time.Now().Add(30*time.Second))
	// ch.SetWriteDeadline(time.Now().Add(30*time.Second))

	pkt = &lob.Packet{}
	pkt.Header().SetString("vn", vn.String())
	err = ch.WritePacket(pkt)
	if err != nil {
		return false, err
	}

	pkt, err = ch.ReadPacket()
	if err != nil {
		return false, err
	}
	alive, _ = pkt.Header().GetBool("alive")
	// tracef("Ping(Vnode(%q)) => %v", vn.String(), alive)
	return alive, nil
}
開發者ID:utamaro,項目名稱:gogotelehash,代碼行數:40,代碼來源:chord.go


注:本文中的github.com/telehash/gogotelehash/e3x.Channel類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。