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 }
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 }
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 }
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 }