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

Golang common.Message类代码示例

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

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



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

示例1: HandleMessage

func (tfh *TestFileHandler) HandleMessage(payload []byte) {
	m := new(common.Message)
	m.Destination = tfh.dest
	m.Payload = []byte(tfh.data)
	tfh.tcpServ.SendMessage(m)

	tfh.done <- true
}
开发者ID:Radzell,项目名称:Sia,代码行数:8,代码来源:client_test.go


示例2: JoinSia

// Announce ourself to the bootstrap address, who will announce us to the quorum
func (s *State) JoinSia() (err error) {
	// Send join message to bootstrap address
	m := new(common.Message)
	m.Destination.Id = bootstrapId
	m.Destination.Host = bootstrapHost
	m.Destination.Port = bootstrapPort
	m.Payload = append([]byte(string(joinSia)), s.self.marshal()...)
	err = s.messageRouter.SendMessage(m)
	return
}
开发者ID:Radzell,项目名称:Sia,代码行数:11,代码来源:state.go


示例3: broadcast

// Takes a payload and sends it in a message to every participant in the quorum
func (s *State) broadcast(payload []byte) {
	s.participantsLock.RLock()
	for i := range s.participants {
		if s.participants[i] != nil {
			m := new(common.Message)
			m.Payload = payload
			m.Destination = s.participants[i].address
			err := s.messageRouter.SendMessage(m)
			if err != nil {
				log.Errorln("messageSender returning an error")
			}
		}
	}
	s.participantsLock.RUnlock()
}
开发者ID:Radzell,项目名称:Sia,代码行数:16,代码来源:state.go


示例4: DownloadFile

// DownloadFile retrieves the erasure-coded segments corresponding to a given file from a quorum.
// It reconstructs the original file from the segments using erasure.RebuildSector().
func DownloadFile(mr common.MessageRouter, fileHash crypto.Hash, length int, k int, quorum [common.QuorumSize]common.Address) (fileData []byte, err error) {
	// spawn a separate thread for each segment
	for i := range quorum {
		go func() {
			// send request
			m := new(common.Message)
			m.Destination = quorum[i]
			m.Payload = []byte{0x01}
			m.Payload = append(m.Payload, fileHash[:]...)
			mr.SendMessage(m)
			// wait for response
		}()
	}
	return
}
开发者ID:Radzell,项目名称:Sia,代码行数:17,代码来源:client.go


示例5: UploadFile

// TestTCPDownloadFile tests the NewTCPServer and DownloadFile functions.
// NewTCPServer must properly initialize a TCP server.
// UploadFile splits a file into erasure-coded segments and distributes them across a quorum.
// k is the number of non-redundant segments.
// The file is padded to satisfy the erasure-coding requirements that:
//     len(fileData) = k*bytesPerSegment, and:
//     bytesPerSegment % 64 = 0
func UploadFile(mr common.MessageRouter, file *os.File, k int, quorum [common.QuorumSize]common.Address) (bytesPerSegment int, err error) {
	// read file
	fileInfo, err := file.Stat()
	if err != nil {
		return
	}
	if fileInfo.Size() > int64(common.QuorumSize*common.MaxSegmentSize) {
		err = fmt.Errorf("File exceeds maximum per-quorum size")
		return
	}
	fileData := make([]byte, fileInfo.Size())
	_, err = io.ReadFull(file, fileData)
	if err != nil {
		return
	}

	// calculate EncodeRing parameters, padding file if necessary
	bytesPerSegment = len(fileData) / k
	if bytesPerSegment%64 != 0 {
		bytesPerSegment += 64 - (bytesPerSegment % 64)
		padding := k*bytesPerSegment - len(fileData)
		fileData = append(fileData, bytes.Repeat([]byte{0x00}, padding)...)
	}

	// create erasure-coded segments
	segments, err := erasure.EncodeRing(k, bytesPerSegment, fileData)
	if err != nil {
		return
	}

	// for now we just send segment i to node i
	// this may need to be randomized for security
	for i := range quorum {
		m := new(common.Message)
		m.Destination = quorum[i]
		m.Payload = append([]byte{byte(i)}, []byte(segments[i])...)
		err = mr.SendMessage(m)
		if err != nil {
			return
		}
	}

	return
}
开发者ID:Radzell,项目名称:Sia,代码行数:51,代码来源:client.go


示例6: announceSignedHeartbeat

func (s *State) announceSignedHeartbeat(sh *signedHeartbeat) {
	for i := range s.participants {
		if s.participants[i] != nil {
			payload, err := sh.marshal()
			if err != nil {
				log.Fatalln(err)
			}

			m := new(common.Message)
			m.Payload = append([]byte{byte(1)}, payload...)
			m.Destination = s.participants[i].Address
			//time.Sleep(time.Millisecond) // prevents panics. No idea where original source of bug is.
			err = s.messageSender.SendMessage(m)
			if err != nil {
				log.Fatalln("Error while sending message")
			}
		}
	}
}
开发者ID:jaked122,项目名称:Sia,代码行数:19,代码来源:consensus.go


示例7: addNewParticipant

// Add a participant to the state, tell the participant about ourselves
func (s *State) addNewParticipant(payload []byte) {
	// extract index and participant object from payload
	participantIndex := payload[0]
	p, err := unmarshalParticipant(payload[1:])
	if err != nil {
		return
	}

	// for this participant, make the heartbeat map and add the default heartbeat
	hb := new(heartbeat)
	emptyHash, err := crypto.CalculateTruncatedHash(hb.entropyStage2[:])
	hb.entropyStage1 = emptyHash
	s.heartbeatsLock.Lock()
	s.participantsLock.Lock()
	s.heartbeats[participantIndex] = make(map[crypto.TruncatedHash]*heartbeat)
	s.heartbeats[participantIndex][emptyHash] = hb
	s.heartbeatsLock.Unlock()

	if *p == *s.self {
		// add our self object to the correct index in participants
		s.participants[participantIndex] = s.self
		s.tickingLock.Lock()
		s.ticking = true
		s.tickingLock.Unlock()

		go s.tick()
	} else {
		// add the participant to participants
		s.participants[participantIndex] = p

		// tell the new guy about ourselves
		m := new(common.Message)
		m.Destination = p.address
		m.Payload = append([]byte(string(newParticipant)), s.self.marshal()...)
		s.messageRouter.SendMessage(m)
	}
	s.participantsLock.Unlock()
}
开发者ID:Radzell,项目名称:Sia,代码行数:39,代码来源:state.go


示例8: SendOutAnnounce

func (a *Announce) SendOutAnnounce(recipients []*Participant) error {
	//TODO Add code to actually send out to participants when it works.
	server, err := network.NewTCPServer(7777)
	if err != nil {
		log.Fatal("TCP Server not initialized")
	}
	for _, i := range recipients {
		s, err := json.Marshal(a)
		s = []byte(string(rune(0)) + string(s))
		if err != nil {
			log.Fatal(err)
		}
		c := new(common.Message)
		c.Payload = s
		c.Destination = i.Address
		err = server.SendMessage(c)
		if err != nil {
			log.Fatal(err)
		}
	}

	return nil
}
开发者ID:jaked122,项目名称:Sia,代码行数:23,代码来源:announce.go



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


鲜花

握手

雷人

路过

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

请发表评论

全部评论

专题导读
上一篇:
Golang common.User类代码示例发布时间:2022-05-24
下一篇:
Golang common.Context类代码示例发布时间:2022-05-24
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

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

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

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