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


Golang agent.Comm類代碼示例

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


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

示例1: Turn

func (self *RandomBot) Turn(comm agent.Comm) {
	defer func() { self.time += 1 }()

	if self.Time()%250 == 0 {
		x := pseudo_rand.Intn(2) - 1
		y := pseudo_rand.Intn(2) - 1
		comm.Move(x, y)
		self.log("info", "moved", x, y)
	}

	if self.Id() == 8 && self.Time() == 500 {
		self.send.Send([]byte("Hello there Number 1."), 1)
	}

	self.hello.Run(comm)
	self.route.Run(self.hello.Neighbors(), comm)
	m := self.send.Run(self.route.Routes(), comm)

	if m != nil {
		self.log("info", self.Time(), "got a message", string([]byte(m.Body())))
	}
	if self.Time()%100 == 9 {
		//         self.log("info", self.Time(), "neighbors", self.hello.Neighbors())

		//         self.log("info", self.Time(), "reachable", self.route.Reachable())
		//         s := fmt.Sprintf("\nRoute Table (%v):\n", self.agent.Id())
		//         for i := uint32(1); i <= 8; i++ {
		//             if route, has := self.routes[i]; has {
		//                 s += fmt.Sprint(route, "\n")
		//             }
		//         }
		//         self.log("info", s)
	}
}
開發者ID:jmptrader,項目名稱:Tecellate,代碼行數:34,代碼來源:random_move.go

示例2: Turn

func (self *Simple) Turn(comm agent.Comm) {
	if !comm.Move(agent.NewMove(1, 0)) {
		fmt.Println("Move Failed!")
	}
	if !comm.Move(agent.NewMove(1, 0)) {
		fmt.Println("Move Failed!")
	}
	return
}
開發者ID:andradeandrey,項目名稱:Tecellate,代碼行數:9,代碼來源:simple.go

示例3: send_message

func (self *SendMachine) send_message(comm agent.Comm) (sent, isack bool) {

	find := func() (*DataGram, bool) {
		for i := 0; i < self.sendq.Len(); i++ {
			msg, ok := self.sendq.Dequeue()
			if !ok {
				break
			}
			if _, has := self.routes[msg.DestAddr]; !has {
				self.sendq.Queue(msg)
			} else {
				return msg, true
			}
		}
		return nil, false
	}
	msg, found := find()
	if !found {
		return false, false
	}

	self.sendq.QueueFront(msg)

	next := self.routes[msg.DestAddr].NextAddr
	var pkt *Packet
	if msg.IsAck() {
		pkt = NewPacket(Commands["ACK"], self.agent.Id(), next)
	} else {
		pkt = NewPacket(Commands["MESSAGE"], self.agent.Id(), next)
	}
	pkt.SetBody(msg.Bytes())
	bytes := pkt.Bytes()
	comm.Broadcast(self.freq, bytes)
	if !msg.IsAck() {
		self.last_checksum = msg.ComputeChecksum()
	}
	self.last = bytes
	self.log("info", self.agent.Time(), "sent", pkt, msg)
	return true, msg.IsAck()
}
開發者ID:jmptrader,項目名稱:Tecellate,代碼行數:40,代碼來源:send_machine.go

示例4: PerformListens

func (self *SendMachine) PerformListens(comm agent.Comm) *DataGram {
	switch self.state {
	case 1:
		return nil
	}
	pkt := MakePacket(comm.Listen(self.freq))
	if !pkt.ValidateChecksum() {
		return nil
	}
	ok, cmd, _ := pkt.Cmd()
	if !ok {
		return nil
	}
	switch cmd {
	case Commands["MESSAGE"]:
		myaddr := uint32(self.agent.Id())
		to := pkt.ToField()
		from := pkt.FromField()
		body := pkt.GetBody(PacketBodySize)
		self.qlog("heard", from, "sent to", to, "pkt", pkt, MakeDataGram(body))
		if to == myaddr {
			msg := MakeDataGram(body)
			if msg.ValidateChecksum() {
				ack := NewAckGram(msg.ComputeChecksum(), myaddr, from)
				self.sendq.QueueFront(ack)
				//                     ack_pkt := NewPacket(Commands["ACK"], myaddr, from)
				//                     ack_pkt.SetBody(ack.Bytes())
				//                     bytes := ack_pkt.Bytes()
				//                     comm.Broadcast(self.freq, bytes)
				if msg.DestAddr == myaddr {
					return msg
				}
				self.sendq.Queue(msg)
			}
		}
	}
	return nil
}
開發者ID:jmptrader,項目名稱:Tecellate,代碼行數:38,代碼來源:send_machine.go

示例5: Turn

func (self *Configurable) Turn(comm agent.Comm) {
	broadcasted := comm.Broadcast(23, []byte("hello_world"))
	if self.LogBroadcast {
		comm.Log("Broadcast success:", broadcasted)
	}
	comm.Move(self.XVelocity, self.YVelocity)
	if self.LogMove {
		// comm.Logf("Pos: %v (%t)", self.Position(), success)
	}
	if self.LogListen {
		comm.Log("Heard:", string(comm.Listen(23)))
	}
	if self.LogCollect {
		comm.Log("Collected: ", comm.Collect())
	}
	if self.LogLook {
		comm.Log("Look: ", comm.Collect())
	}
	if self.LogPrevious {
		comm.Log("Previous: ", comm.PrevResult())
	}
	if self.LogEnergy {
		comm.Log("Energy:", comm.Energy())
	}
	return
}
開發者ID:jmptrader,項目名稱:Tecellate,代碼行數:26,代碼來源:configurable.go

示例6: confirm_acked

func (self *SendMachine) confirm_acked(comm agent.Comm) (confirm bool) {

	cur := comm.Listen(self.freq)
	if self.last.Eq(cur) {
		self.qlog("heard my own packet")
		self.heard_last = true
		return false
	}

	if self.heard_last == false {
		self.wait -= 1
		if self.wait == 0 {
			self.state = 0
			self.backoff = self.backoff * (pseudo_rand.Float64()*2 + 1)
			self.wait = uint32(self.backoff)
			self.qlog("didn't hear last waiting...", self.wait)
		}
		return false
	}

	self.ack_wait -= 1
	if self.ack_wait == 0 {
		self.ack_backoff = self.ack_backoff * (pseudo_rand.Float64()*2 + 1)
		self.ack_wait = uint32(self.ack_backoff)
		self.state = 0
		return false
	}

	pkt := MakePacket(cur)
	if !pkt.ValidateChecksum() {
		return false
	}
	ok, cmd, _ := pkt.Cmd()
	if !ok {
		return false
	}
	self.log("info", self.agent.Time(), "got possible ack pkt", pkt)
	switch cmd {
	case Commands["ACK"]:
		self.log("info", self.agent.Time(), "it is an ack")
		myaddr := self.agent.Id()
		to := pkt.ToField()
		body := pkt.GetBody(PacketBodySize)
		self.log("info", self.agent.Time(), "it is to me?", myaddr, to)
		if to == myaddr {
			msg := MakeDataGram(body)
			self.log("info", self.agent.Time(), "the datagram", msg)
			if msg.ValidateChecksum() {
				self.log("info", self.agent.Time(), "it validates")
				self.log("info", self.agent.Time(), "it dest", msg.DestAddr, "myaddr", myaddr)
				if msg.DestAddr == myaddr {
					self.log("info", self.agent.Time(), "it dest == myaddr")
					bytes := msg.Body()[0:4]
					self.log("info", self.agent.Time(), "bytes", bytes, "last", self.last_checksum)
					if self.last_checksum.Eq(bytes) {
						return true
					}
				}
			}
		}
	}
	return false
}
開發者ID:jmptrader,項目名稱:Tecellate,代碼行數:63,代碼來源:send_machine.go

示例7: confirm_last

func (self *SendMachine) confirm_last(comm agent.Comm) (confirm bool) {
	bytes := comm.Listen(self.freq)
	confirm = self.last.Eq(bytes)
	self.log("info", self.agent.Time(), "confirm_last", confirm)
	return
}
開發者ID:jmptrader,項目名稱:Tecellate,代碼行數:6,代碼來源:send_machine.go


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