• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

Golang protocol.Message类代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了Golang中github.com/degdb/degdb/protocol.Message的典型用法代码示例。如果您正苦于以下问题:Golang Message类的具体用法?Golang Message怎么用?Golang Message使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。



在下文中一共展示了Message类的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Golang代码示例。

示例1: handleHandshake

func (s *Server) handleHandshake(conn *Conn, msg *protocol.Message) {
	handshake := msg.GetHandshake()
	conn.Peer = handshake.GetSender()

	s.peersLock.RLock()
	peer := s.Peers[conn.Peer.Id]
	s.peersLock.RUnlock()

	if peer != nil {
		s.Printf("Ignoring duplicate peer %s.", conn.PrettyID())
		if err := conn.Close(); err != nil && err != io.EOF {
			s.Printf("ERR closing connection %s", err)
		}
		return
	}

	s.peersLock.Lock()
	s.Peers[conn.Peer.Id] = conn
	s.peersLock.Unlock()

	s.Print(color.GreenString("New peer %s", conn.PrettyID()))
	if !handshake.Response {
		if err := s.sendHandshake(conn, true); err != nil {
			s.Printf("ERR sendHandshake %s", err)
		}
	} else {
		if err := s.sendPeerRequest(conn); err != nil {
			s.Printf("ERR sendPeerRequest %s", err)
		}
	}
	go s.connHeartbeat(conn)
}
开发者ID:javajax,项目名称:degdb,代码行数:32,代码来源:protocol.go


示例2: Request

// Request sends a message on a connection and waits for a response.
// Returns error network.Timeout if no response in 10 seconds.
func (c *Conn) Request(m *protocol.Message) (*protocol.Message, error) {
	m.Id = uint64(rand.Int63())
	m.ResponseRequired = true
	if err := c.Send(m); err != nil {
		return nil, err
	}

	timeout := make(chan bool, 1)
	go func() {
		time.Sleep(10 * time.Second)
		timeout <- true
	}()
	resp := make(chan *protocol.Message, 1)
	c.expectedMessages[m.Id] = resp

	var msg *protocol.Message
	var err error
	select {
	case msg = <-resp:
	case <-timeout:
		err = Timeout
	}
	delete(c.expectedMessages, m.Id)
	return msg, err
}
开发者ID:nonempty,项目名称:degdb,代码行数:27,代码来源:conn.go


示例3: handlePeerNotify

func (s *Server) handlePeerNotify(conn *Conn, msg *protocol.Message) {
	peers := msg.GetPeerNotify().Peers
	for _, peer := range peers {
		if _, ok := s.Peers[peer.Id]; !ok {
			s.Peers[peer.Id] = nil
			s.Connect(peer.Id)
		}
	}
}
开发者ID:nunb,项目名称:degdb,代码行数:9,代码来源:network.go


示例4: Send

// Send a message on the specified connection. Consider Request.
func (c *Conn) Send(m *protocol.Message) error {
	msg, err := m.Marshal()
	if err != nil {
		return err
	}
	packet := make([]byte, len(msg)+4)
	binary.BigEndian.PutUint32(packet, uint32(len(msg)))
	copy(packet[4:], msg)
	_, err = c.Conn.Write(packet)
	return err
}
开发者ID:nonempty,项目名称:degdb,代码行数:12,代码来源:conn.go


示例5: handlePeerNotify

func (s *Server) handlePeerNotify(conn *Conn, msg *protocol.Message) {
	conn.peerRequest <- true
	s.Printf("PeerNotify %+v", conn)
	peers := msg.GetPeerNotify().Peers
	for _, peer := range peers {
		if _, ok := s.Peers[peer.Id]; !ok {
			s.Peers[peer.Id] = nil
			s.Connect(peer.Id)
		}
	}
}
开发者ID:raj347,项目名称:degdb,代码行数:11,代码来源:network.go


示例6: Send

// Send a message to the specified connection.
func (c *Conn) Send(m *protocol.Message) error {
	msg, err := m.Marshal()
	if err != nil {
		return err
	}
	packet := make([]byte, len(msg)+4)
	binary.BigEndian.PutUint32(packet, uint32(len(msg)))
	copy(packet[4:], msg)

	if _, err := c.Write(packet); err != nil {
		return err
	}
	c.server.Printf("Message: -> %s, %+v", c.PrettyID(), m.GetMessage())
	return nil
}
开发者ID:raj347,项目名称:degdb,代码行数:16,代码来源:network.go


示例7: Broadcast

// Broadcast sends a message to all peers with that have the hash in their keyspace.
func (s *Server) Broadcast(hash *uint64, msg *protocol.Message) error {
	alreadySentTo := make(map[uint64]bool)
	if msg.Gossip {
		for _, to := range msg.SentTo {
			alreadySentTo[to] = true
		}
	}
	sentTo := []uint64{murmur3.Sum64([]byte(s.LocalPeer().Id))}
	var toPeers []*Conn
	for _, peer := range s.Peers {
		peerHash := murmur3.Sum64([]byte(peer.Peer.Id))
		if (hash == nil || peer.Peer.GetKeyspace().Includes(*hash)) && !alreadySentTo[peerHash] {
			sentTo = append(sentTo, peerHash)
			toPeers = append(toPeers, peer)
		}
	}
	if msg.Gossip {
		msg.SentTo = append(msg.SentTo, sentTo...)
	}
	for _, peer := range toPeers {
		s.Printf("Broadcasting to %s", peer.Peer.Id)
		if err := peer.Send(msg); err != nil {
			return err
		}
	}
	return nil
}
开发者ID:raj347,项目名称:degdb,代码行数:28,代码来源:network.go


示例8: handleQueryRequest

func (s *server) handleQueryRequest(conn *network.Conn, msg *protocol.Message) {
	triples, err := s.ExecuteQuery(msg.GetQueryRequest())
	resp := &protocol.Message{
		Message: &protocol.Message_QueryResponse{
			QueryResponse: &protocol.QueryResponse{
				Triples: triples,
			},
		},
	}
	if err != nil {
		resp.Error = err.Error()
	}
	if err := conn.RespondTo(msg, resp); err != nil {
		s.Printf("ERR send QueryResponse %s", err)
	}
}
开发者ID:nonempty,项目名称:degdb,代码行数:16,代码来源:binary.go


示例9: handleHandshake

func (s *Server) handleHandshake(conn *Conn, msg *protocol.Message) {
	handshake := msg.GetHandshake()
	conn.Peer = handshake.GetSender()
	s.Peers[conn.Peer.Id] = conn
	s.Printf("New peer %s", conn.Peer.Id)
	if !handshake.Response {
		if err := s.sendHandshake(conn, true); err != nil {
			log.Printf("ERR sendHandshake %s", err)
		}
	} else {
		msg := &protocol.Message{Message: &protocol.Message_PeerRequest{
			PeerRequest: &protocol.PeerRequest{
				Limit: -1,
				//Keyspace: s.LocalPeer().Keyspace,
			}}}
		if err := conn.Send(msg); err != nil {
			log.Printf("ERR sending PeerRequest: %s", err)
		}
	}
}
开发者ID:nunb,项目名称:degdb,代码行数:20,代码来源:network.go


示例10: handlePeerNotify

func (s *Server) handlePeerNotify(conn *Conn, msg *protocol.Message) {
	conn.peerRequest <- true
	peers := msg.GetPeerNotify().Peers
	for _, peer := range peers {
		s.peersLock.RLock()
		_, ok := s.Peers[peer.Id]
		s.peersLock.RUnlock()

		if ok {
			continue
		}

		s.peersLock.Lock()
		s.Peers[peer.Id] = nil
		s.peersLock.Unlock()

		if err := s.Connect(peer.Id); err != nil {
			s.Printf("ERR failed to connect to peer %s", err)
		}
	}
}
开发者ID:javajax,项目名称:degdb,代码行数:21,代码来源:protocol.go


示例11: handleInsertTriples

func (s *server) handleInsertTriples(conn *network.Conn, msg *protocol.Message) {
	triples := msg.GetInsertTriples().Triples
	localKS := s.network.LocalPeer().Keyspace

	var validTriples []*protocol.Triple
	idHashes := make(map[string]uint64)
	for _, triple := range triples {
		hash, ok := idHashes[triple.Subj]
		if !ok {
			hash = murmur3.Sum64([]byte(triple.Subj))
			idHashes[triple.Subj] = hash
		}
		if !localKS.Includes(hash) {
			s.Printf("ERR insert triple dropped due to keyspace %#v from %#v", triple, conn.Peer)
			// TODO(d4l3k): Follow up on bad triple by reannouncing keyspace.
			continue
		}
		validTriples = append(validTriples, triple)
	}
	s.ts.Insert(validTriples)
}
开发者ID:nonempty,项目名称:degdb,代码行数:21,代码来源:binary.go


示例12: handlePeerRequest

func (s *Server) handlePeerRequest(conn *Conn, msg *protocol.Message) {
	// TODO(d4l3k): Handle keyspace check.
	req := msg.GetPeerRequest()

	var peers []*protocol.Peer
	for id, v := range s.Peers {
		if conn.Peer.Id == id || v == nil {
			continue
		}
		peers = append(peers, v.Peer)
		if req.Limit > 0 && int32(len(peers)) >= req.Limit {
			break
		}
	}
	err := conn.Send(&protocol.Message{Message: &protocol.Message_PeerNotify{
		PeerNotify: &protocol.PeerNotify{
			Peers: peers,
		}}})
	if err != nil {
		log.Printf("ERR sending PeerNotify: %s", err)
	}
}
开发者ID:raj347,项目名称:degdb,代码行数:22,代码来源:network.go


示例13: RespondTo

// RespondTo sends `resp` as a response to the request `to`.
func (c *Conn) RespondTo(to *protocol.Message, resp *protocol.Message) error {
	resp.ResponseTo = to.Id
	return c.Send(resp)
}
开发者ID:nonempty,项目名称:degdb,代码行数:5,代码来源:conn.go


示例14: ExecuteQuery

func (s *server) ExecuteQuery(q *protocol.QueryRequest) ([]*protocol.Triple, error) {
	var triples []*protocol.Triple
	switch q.Type {
	case protocol.BASIC:
		for i, step := range q.Steps {
			if i != 0 {
				var midTriples []*protocol.Triple
				for _, triple := range triples {
					midTriples = append(midTriples, &protocol.Triple{
						Subj: triple.Obj,
					})
				}
				step = &protocol.ArrayOp{
					Arguments: []*protocol.ArrayOp{step},
					Mode:      protocol.AND,
					Triples:   midTriples,
				}
			}

			// External request and is already sharded.
			if q.Sharded {
				return s.ts.QueryArrayOp(step, int(q.Limit))
			}

			var wg sync.WaitGroup
			var triplesLock sync.RWMutex
			triples = nil
			shards := query.ShardQueryByHash(step)

			// Unrooted queries
			if arrayOp, ok := shards[0]; ok {
				// TODO localnode
				set := s.network.MinimumCoveringPeers()
				s.Printf("Minimum covering set %+v", set)
				_ = arrayOp
				wg.Add(len(set))
				req := basicReq(arrayOp)
				var err error
				for _, conn := range set {
					conn := conn
					go func() {
						var msg *protocol.Message
						msg, err = conn.Request(req)
						if err != nil {
							return
						}
						triplesLock.Lock()
						// TODO(d4l3k): Deduplicate triples
						triples = append(triples, msg.GetQueryResponse().Triples...)
						triplesLock.Unlock()
						done := make(chan bool, 1)
						go func() {
							wg.Done()
							done <- true
						}()
						go func() {
							time.Sleep(10 * time.Second)
							done <- true
						}()
						<-done
					}()
				}
				wg.Wait()
				return triples, err
			}

			// Rooted queries
			for hash, arrayOp := range shards {
				if hash == 0 {
					return nil, query.ErrUnRooted
				}
				if s.network.LocalPeer().Keyspace.Includes(hash) {
					trips, err := s.ts.QueryArrayOp(arrayOp, int(q.Limit))
					if err != nil {
						return nil, err
					}
					triples = append(triples, trips...)
					continue
				}
			Peers:
				for _, conn := range s.network.Peers {
					if conn == nil || conn.Peer == nil {
						continue
					}
					if conn.Peer.Keyspace.Includes(hash) {
						req := basicReq(arrayOp)
						// TODO(d4l3k) Parallelize
						msg, err := conn.Request(req)
						if err != nil {
							return nil, err
						}
						triples = append(triples, msg.GetQueryResponse().Triples...)
						break Peers
					}
				}
			}
		}

	//case protocol.GREMLIN:
	//case protocol.MQL:
//.........这里部分代码省略.........
开发者ID:nonempty,项目名称:degdb,代码行数:101,代码来源:query.go



注:本文中的github.com/degdb/degdb/protocol.Message类示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Golang client.CreateHTTPClient函数代码示例发布时间:2022-05-23
下一篇:
Golang protocol.Keyspace类代码示例发布时间:2022-05-23
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap