2014-08-01 13:21:51 -07:00
|
|
|
package dht
|
|
|
|
|
2014-08-09 22:28:46 -07:00
|
|
|
import (
|
2014-08-19 19:14:52 -07:00
|
|
|
"code.google.com/p/goprotobuf/proto"
|
2014-08-09 22:28:46 -07:00
|
|
|
peer "github.com/jbenet/go-ipfs/peer"
|
|
|
|
)
|
|
|
|
|
2014-08-16 23:03:36 -07:00
|
|
|
// Message is a a helper struct which makes working with protbuf types easier
|
|
|
|
type Message struct {
|
2014-08-08 18:09:21 -07:00
|
|
|
Type PBDHTMessage_MessageType
|
|
|
|
Key string
|
|
|
|
Value []byte
|
2014-08-01 13:21:51 -07:00
|
|
|
Response bool
|
2014-08-28 16:48:00 -07:00
|
|
|
ID string
|
2014-08-08 18:09:21 -07:00
|
|
|
Success bool
|
2014-08-09 22:28:46 -07:00
|
|
|
Peers []*peer.Peer
|
|
|
|
}
|
|
|
|
|
|
|
|
func peerInfo(p *peer.Peer) *PBDHTMessage_PBPeer {
|
|
|
|
pbp := new(PBDHTMessage_PBPeer)
|
2014-08-19 19:14:52 -07:00
|
|
|
if len(p.Addresses) == 0 || p.Addresses[0] == nil {
|
|
|
|
pbp.Addr = proto.String("")
|
|
|
|
} else {
|
|
|
|
addr, err := p.Addresses[0].String()
|
|
|
|
if err != nil {
|
|
|
|
//Temp: what situations could cause this?
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
pbp.Addr = &addr
|
2014-08-09 22:28:46 -07:00
|
|
|
}
|
|
|
|
pid := string(p.ID)
|
|
|
|
pbp.Id = &pid
|
|
|
|
return pbp
|
2014-08-01 13:21:51 -07:00
|
|
|
}
|
|
|
|
|
2014-08-16 23:03:36 -07:00
|
|
|
// ToProtobuf takes a Message and produces a protobuf with it.
|
2014-08-12 15:37:26 -07:00
|
|
|
// TODO: building the protobuf message this way is a little wasteful
|
|
|
|
// Unused fields wont be omitted, find a better way to do this
|
2014-08-16 23:03:36 -07:00
|
|
|
func (m *Message) ToProtobuf() *PBDHTMessage {
|
2014-08-08 18:09:21 -07:00
|
|
|
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
|
2014-08-16 23:03:36 -07:00
|
|
|
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
|
|
|
|
}
|