2014-08-01 13:21:51 -07:00
|
|
|
package dht
|
|
|
|
|
2014-08-09 22:28:46 -07:00
|
|
|
import (
|
|
|
|
peer "github.com/jbenet/go-ipfs/peer"
|
|
|
|
)
|
|
|
|
|
2014-08-01 13:21:51 -07:00
|
|
|
// A helper struct to make working with protbuf types easier
|
2014-08-08 18:09:21 -07:00
|
|
|
type DHTMessage struct {
|
|
|
|
Type PBDHTMessage_MessageType
|
|
|
|
Key string
|
|
|
|
Value []byte
|
2014-08-01 13:21:51 -07:00
|
|
|
Response bool
|
2014-08-08 18:09:21 -07:00
|
|
|
Id uint64
|
|
|
|
Success bool
|
2014-08-09 22:28:46 -07:00
|
|
|
Peers []*peer.Peer
|
|
|
|
}
|
|
|
|
|
|
|
|
func peerInfo(p *peer.Peer) *PBDHTMessage_PBPeer {
|
|
|
|
pbp := new(PBDHTMessage_PBPeer)
|
|
|
|
addr, err := p.Addresses[0].String()
|
|
|
|
if err != nil {
|
|
|
|
//Temp: what situations could cause this?
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
pbp.Addr = &addr
|
|
|
|
pid := string(p.ID)
|
|
|
|
pbp.Id = &pid
|
|
|
|
return pbp
|
2014-08-01 13:21:51 -07:00
|
|
|
}
|
|
|
|
|
2014-08-08 18:09:21 -07:00
|
|
|
func (m *DHTMessage) ToProtobuf() *PBDHTMessage {
|
|
|
|
pmes := new(PBDHTMessage)
|
2014-08-01 13:21:51 -07:00
|
|
|
if m.Value != nil {
|
|
|
|
pmes.Value = m.Value
|
|
|
|
}
|
|
|
|
|
|
|
|
pmes.Type = &m.Type
|
|
|
|
pmes.Key = &m.Key
|
|
|
|
pmes.Response = &m.Response
|
|
|
|
pmes.Id = &m.Id
|
2014-08-06 18:37:45 -07:00
|
|
|
pmes.Success = &m.Success
|
2014-08-09 22:28:46 -07:00
|
|
|
for _, p := range m.Peers {
|
|
|
|
pmes.Peers = append(pmes.Peers, peerInfo(p))
|
|
|
|
}
|
2014-08-01 13:21:51 -07:00
|
|
|
|
|
|
|
return pmes
|
|
|
|
}
|