1
1
package shell
2
2
3
3
import (
4
- "encoding/base64 "
4
+ "encoding/binary "
5
5
"encoding/json"
6
- )
7
6
8
- // floodsub uses base64 encoding for just about everything.
9
- // To Decode the base64 while also decoding the JSON the type b64String was added.
10
- type b64String string
7
+ "github.com/libp2p/go- floodsub"
8
+ "github.com/libp2p/go-libp2p-peer"
9
+ )
11
10
12
- // UnmarshalJSON implements the json.Unmarshaler interface.
13
- func (bs * b64String ) UnmarshalJSON (in []byte ) error {
14
- var b64 string
11
+ // PubSubRecord is a record received via PubSub.
12
+ type PubSubRecord interface {
13
+ // From returns the peer ID of the node that published this record
14
+ From () peer.ID
15
15
16
- err := json .Unmarshal (in , & b64 )
17
- if err != nil {
18
- return err
19
- }
16
+ // Data returns the data field
17
+ Data () []byte
20
18
21
- bsStr , err := base64 .StdEncoding .DecodeString (b64 )
19
+ // SeqNo is the sequence number of this record
20
+ SeqNo () int64
22
21
23
- * bs = b64String ( bsStr )
24
- return err
22
+ //TopicIDs is the list of topics this record belongs to
23
+ TopicIDs () [] string
25
24
}
26
25
27
- func (bs * b64String ) Marshal () (string , error ) {
28
- jsonBytes , err := json .Marshal (
29
- base64 .StdEncoding .EncodeToString (
30
- []byte (* bs )))
26
+ type floodsubRecord struct {
27
+ msg * floodsub.Message
28
+ }
31
29
32
- return string (jsonBytes ), err
30
+ func (r floodsubRecord ) From () peer.ID {
31
+ return r .msg .GetFrom ()
33
32
}
34
33
35
- ///
34
+ func (r floodsubRecord ) Data () []byte {
35
+ return r .msg .GetData ()
36
+ }
36
37
37
- // PubSubRecord is a record received via PubSub.
38
- type PubSubRecord struct {
39
- From string `json:"from"`
40
- Data b64String `json:"data"`
41
- SeqNo b64String `json:"seqno"`
42
- TopicIDs []string `json:"topicIDs"`
38
+ func (r floodsubRecord ) SeqNo () int64 {
39
+ return int64 (binary .BigEndian .Uint64 (r .msg .GetSeqno ()))
43
40
}
44
41
45
- // DataString returns the string representation of the data field.
46
- func (r PubSubRecord ) DataString () string {
47
- return string (r .Data )
42
+ func (r floodsubRecord ) TopicIDs () []string {
43
+ return r .msg .GetTopicIDs ()
48
44
}
49
45
50
46
///
@@ -64,17 +60,17 @@ func newPubSubSubscription(resp *Response) *PubSubSubscription {
64
60
}
65
61
66
62
// Next waits for the next record and returns that.
67
- func (s * PubSubSubscription ) Next () (* PubSubRecord , error ) {
63
+ func (s * PubSubSubscription ) Next () (PubSubRecord , error ) {
68
64
if s .resp .Error != nil {
69
65
return nil , s .resp .Error
70
66
}
71
67
72
68
d := json .NewDecoder (s .resp .Output )
73
69
74
- r := & PubSubRecord {}
70
+ r := & floodsub. Message {}
75
71
err := d .Decode (r )
76
72
77
- return r , err
73
+ return floodsubRecord { msg : r } , err
78
74
}
79
75
80
76
// Cancel cancels the given subscription.
0 commit comments