dht: Improve on on Msg methods

This commit is contained in:
Matt Joiner 2015-08-18 02:11:09 +10:00
parent b536975580
commit 3204e276f2
3 changed files with 19 additions and 6 deletions

View File

@ -178,7 +178,7 @@ func (me *Announce) getPeers(addr dHTAddr) error {
nodeInfo := NodeInfo{ nodeInfo := NodeInfo{
Addr: t.remoteAddr, Addr: t.remoteAddr,
} }
copy(nodeInfo.ID[:], m.ID()) copy(nodeInfo.ID[:], m.SenderID())
select { select {
case me.values <- PeersValues{ case me.values <- PeersValues{
Peers: vs, Peers: vs,

View File

@ -289,11 +289,24 @@ func (m Msg) T() (t string) {
return return
} }
func (m Msg) ID() string { func (m Msg) Args() map[string]interface{} {
defer func() { defer func() {
recover() recover()
}() }()
return m[m["y"].(string)].(map[string]interface{})["id"].(string) return m["a"].(map[string]interface{})
}
func (m Msg) SenderID() string {
defer func() {
recover()
}()
switch m["y"].(string) {
case "q":
return m.Args()["id"].(string)
case "r":
return m["r"].(map[string]interface{})["id"].(string)
}
return ""
} }
// Suggested nodes in a response. // Suggested nodes in a response.
@ -647,7 +660,7 @@ func (s *Server) processPacket(b []byte, addr dHTAddr) {
//log.Printf("unexpected message: %#v", d) //log.Printf("unexpected message: %#v", d)
return return
} }
node := s.getNode(addr, d.ID()) node := s.getNode(addr, d.SenderID())
node.lastGotResponse = time.Now() node.lastGotResponse = time.Now()
// TODO: Update node ID as this is an authoritative packet. // TODO: Update node ID as this is an authoritative packet.
go t.handleResponse(d) go t.handleResponse(d)
@ -1090,7 +1103,7 @@ func (s *Server) getPeers(addr dHTAddr, infoHash string) (t *Transaction, err er
s.liftNodes(m) s.liftNodes(m)
at, ok := m.AnnounceToken() at, ok := m.AnnounceToken()
if ok { if ok {
s.getNode(addr, m.ID()).announceToken = at s.getNode(addr, m.SenderID()).announceToken = at
} }
}) })
return return

View File

@ -145,7 +145,7 @@ func TestPing(t *testing.T) {
defer tn.Close() defer tn.Close()
ok := make(chan bool) ok := make(chan bool)
tn.SetResponseHandler(func(msg Msg) { tn.SetResponseHandler(func(msg Msg) {
ok <- msg.ID() == srv0.ID() ok <- msg.SenderID() == srv0.ID()
}) })
if !<-ok { if !<-ok {
t.FailNow() t.FailNow()