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) } }
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 }
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() }
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 *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) 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 }