-
Notifications
You must be signed in to change notification settings - Fork 1
/
filters.go
149 lines (113 loc) · 3.38 KB
/
filters.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package quasar
import (
"errors"
"fmt"
"time"
atbf "github.com/Bit-Nation/go-libp2p-quasar/atbf"
pb "github.com/Bit-Nation/go-libp2p-quasar/pb"
net "github.com/libp2p/go-libp2p-net"
)
// fetch filters for given stream
func (p *protocol) pullFilters(str net.Stream, timeOut time.Duration) (*pb.PullFiltersResponse, error) {
m := pb.Message{
Type: pb.Message_PULL_FILTER_REQUEST,
}
resp, err := p.request(&m, str, timeOut)
if err != nil {
return nil, err
}
if resp.msg.Type != pb.Message_PULL_FILTER_RESPONSE {
return nil, errors.New(fmt.Sprintf("expected to get a PULL_FILTER_RESPONSE but got: %d", resp.msg.Type))
}
return resp.msg.PullFiltersResponse, nil
}
// push filter to a given stream / node
func (p *protocol) pushFilters(str net.Stream, timeOut time.Duration, filter *atbf.AttenuatedBloomFilter) error {
rawFilter, err := filter.Marshal()
if err != nil {
return err
}
m := pb.Message{
Type: pb.Message_PUSH_FILTER_REQUEST,
PushFilterRequest: &pb.PushFiltersRequest{
AttenuateBloomFilter: rawFilter,
},
}
resp, err := p.request(&m, str, timeOut)
if err != nil {
return err
}
if resp.msg.Type != pb.Message_PUSH_FILTER_RESPONE {
return errors.New(fmt.Sprintf("expected to get a PUSH_FILTER_RESPONE but got: %d", resp.msg.Type))
}
return nil
}
// handle filter request
func (p *protocol) handlePullFilterRequest(msg *pb.Message, str net.Stream) error {
// check if message is correct
if msg.Type != pb.Message_PULL_FILTER_REQUEST {
return errors.New(fmt.Sprintf("got %s instead of PULL_FILTER_REQUEST", msg.Type))
}
// marshal the whole thing
rawFilter, err := p.filter.Marshal()
if err != nil {
return err
}
// send response to back to peer
return p.respond(msg.RequestID, &pb.Message{
Type: pb.Message_PULL_FILTER_RESPONSE,
PullFiltersResponse: &pb.PullFiltersResponse{
AttenuateBloomFilter: rawFilter,
},
}, str)
}
// handle pull filter response
func (p *protocol) handlePullFilterResponse(msg *pb.Message) error {
// check if message is correct
if msg.Type != pb.Message_PULL_FILTER_RESPONSE {
return errors.New(fmt.Sprintf("got %s instead of PULL_FILTER_RESPONSE", msg.Type))
}
req, err := p.cutRequest(msg.RequestID)
if err != nil {
return err
}
req.respChan <- response{
msg: msg,
}
return nil
}
// handle push filter response
func (p *protocol) handlePushFilterResponse(msg *pb.Message) error {
// check if message is correct
if msg.Type != pb.Message_PUSH_FILTER_RESPONE {
return errors.New(fmt.Sprintf("got %s instead of PUSH_FILTER_RESPONE", msg.Type))
}
req, err := p.cutRequest(msg.RequestID)
if err != nil {
return err
}
req.respChan <- response{
msg: msg,
}
return nil
}
// handle push filter request
func (p *protocol) handlePushFilterRequest(msg *pb.Message, str net.Stream) error {
// check if message is correct
if msg.Type != pb.Message_PUSH_FILTER_REQUEST {
return errors.New(fmt.Sprintf("got %s instead of PUSH_FILTER_REQUEST", msg.Type))
}
// merge our filter with the received one
recFilter := &atbf.AttenuatedBloomFilter{}
if err := recFilter.Unmarshal(msg.PushFilterRequest.AttenuateBloomFilter); err != nil {
return err
}
if err := p.filter.Merge(recFilter); err != nil {
return err
}
// send response to back to peer
return p.respond(msg.RequestID, &pb.Message{
Type: pb.Message_PUSH_FILTER_RESPONE,
PushFilterResponse: &pb.PushFiltersResponse{},
}, str)
}