]> Sergey Matveev's repositories - btrtrc.git/blob - dht/msg.go
dht: Make Msg a struct with bencode tags
[btrtrc.git] / dht / msg.go
1 package dht
2
3 import (
4         "fmt"
5
6         "github.com/anacrolix/torrent/util"
7 )
8
9 // The unmarshalled KRPC dict message.
10 type Msg struct {
11         Q string `bencode:"q,omitempty"`
12         A *struct {
13                 ID       string `bencode:"id"`
14                 InfoHash string `bencode:"info_hash"`
15                 Target   string `bencode:"target"`
16         } `bencode:"a,omitempty"`
17         T string     `bencode:"t"`
18         Y string     `bencode:"y"`
19         R *Return    `bencode:"r,omitempty"`
20         E *KRPCError `bencode:"e,omitempty"`
21 }
22
23 type Return struct {
24         ID     string              `bencode:"id"`
25         Nodes  CompactIPv4NodeInfo `bencode:"nodes,omitempty"`
26         Token  string              `bencode:"token"`
27         Values []util.CompactPeer  `bencode:"values,omitempty"`
28 }
29
30 var _ fmt.Stringer = Msg{}
31
32 func (m Msg) String() string {
33         return fmt.Sprintf("%#v", m)
34 }
35
36 // The node ID of the source of this Msg.
37 func (m Msg) SenderID() string {
38         switch m.Y {
39         case "q":
40                 return m.A.ID
41         case "r":
42                 return m.R.ID
43         }
44         return ""
45 }
46
47 func (m Msg) Error() *KRPCError {
48         if m.Y != "e" {
49                 return nil
50         }
51         return m.E
52 }