|
| 1 | +// Deprecated: This package has moved into go-libp2p as a sub-package, github.com/libp2p/go-libp2p/p2p/host/blank. |
1 | 2 | package blankhost
|
2 | 3 |
|
3 | 4 | import (
|
4 |
| - "context" |
5 |
| - "errors" |
6 |
| - "fmt" |
7 |
| - "io" |
| 5 | + blankhost "github.com/libp2p/go-libp2p/p2p/host/blank" |
8 | 6 |
|
9 | 7 | "github.com/libp2p/go-libp2p-core/connmgr"
|
10 | 8 | "github.com/libp2p/go-libp2p-core/event"
|
11 |
| - "github.com/libp2p/go-libp2p-core/host" |
12 | 9 | "github.com/libp2p/go-libp2p-core/network"
|
13 |
| - "github.com/libp2p/go-libp2p-core/peer" |
14 |
| - "github.com/libp2p/go-libp2p-core/peerstore" |
15 |
| - "github.com/libp2p/go-libp2p-core/protocol" |
16 |
| - "github.com/libp2p/go-libp2p-core/record" |
17 |
| - |
18 |
| - "github.com/libp2p/go-eventbus" |
19 |
| - |
20 |
| - logging "github.com/ipfs/go-log/v2" |
21 |
| - |
22 |
| - ma "github.com/multiformats/go-multiaddr" |
23 |
| - mstream "github.com/multiformats/go-multistream" |
24 | 10 | )
|
25 | 11 |
|
26 |
| -var log = logging.Logger("blankhost") |
27 |
| - |
28 | 12 | // BlankHost is the thinnest implementation of the host.Host interface
|
29 |
| -type BlankHost struct { |
30 |
| - n network.Network |
31 |
| - mux *mstream.MultistreamMuxer |
32 |
| - cmgr connmgr.ConnManager |
33 |
| - eventbus event.Bus |
34 |
| - emitters struct { |
35 |
| - evtLocalProtocolsUpdated event.Emitter |
36 |
| - } |
37 |
| -} |
38 |
| - |
39 |
| -type config struct { |
40 |
| - cmgr connmgr.ConnManager |
41 |
| - eventBus event.Bus |
42 |
| -} |
| 13 | +// Deprecated: use github.com/libp2p/go-libp2p/p2p/host/blank.BlankHost instead. |
| 14 | +type BlankHost = blankhost.BlankHost |
43 | 15 |
|
44 |
| -type Option = func(cfg *config) |
| 16 | +// Deprecated: use github.com/libp2p/go-libp2p/p2p/host/blank.Option instead. |
| 17 | +type Option = blankhost.Option |
45 | 18 |
|
| 19 | +// Deprecated: use github.com/libp2p/go-libp2p/p2p/host/blank.WithConnectionManager instead. |
46 | 20 | func WithConnectionManager(cmgr connmgr.ConnManager) Option {
|
47 |
| - return func(cfg *config) { |
48 |
| - cfg.cmgr = cmgr |
49 |
| - } |
| 21 | + return blankhost.WithConnectionManager(cmgr) |
50 | 22 | }
|
51 | 23 |
|
| 24 | +// Deprecated: use github.com/libp2p/go-libp2p/p2p/host/blank.WithEventBus instead. |
52 | 25 | func WithEventBus(eventBus event.Bus) Option {
|
53 |
| - return func(cfg *config) { |
54 |
| - cfg.eventBus = eventBus |
55 |
| - } |
| 26 | + return blankhost.WithEventBus(eventBus) |
56 | 27 | }
|
57 | 28 |
|
| 29 | +// Deprecated: use github.com/libp2p/go-libp2p/p2p/host/blank.NewBlankHost instead. |
58 | 30 | func NewBlankHost(n network.Network, options ...Option) *BlankHost {
|
59 |
| - cfg := config{ |
60 |
| - cmgr: &connmgr.NullConnMgr{}, |
61 |
| - } |
62 |
| - for _, opt := range options { |
63 |
| - opt(&cfg) |
64 |
| - } |
65 |
| - |
66 |
| - bh := &BlankHost{ |
67 |
| - n: n, |
68 |
| - cmgr: cfg.cmgr, |
69 |
| - mux: mstream.NewMultistreamMuxer(), |
70 |
| - } |
71 |
| - if bh.eventbus == nil { |
72 |
| - bh.eventbus = eventbus.NewBus() |
73 |
| - } |
74 |
| - |
75 |
| - // subscribe the connection manager to network notifications (has no effect with NullConnMgr) |
76 |
| - n.Notify(bh.cmgr.Notifee()) |
77 |
| - |
78 |
| - var err error |
79 |
| - if bh.emitters.evtLocalProtocolsUpdated, err = bh.eventbus.Emitter(&event.EvtLocalProtocolsUpdated{}); err != nil { |
80 |
| - return nil |
81 |
| - } |
82 |
| - evtPeerConnectednessChanged, err := bh.eventbus.Emitter(&event.EvtPeerConnectednessChanged{}) |
83 |
| - if err != nil { |
84 |
| - return nil |
85 |
| - } |
86 |
| - n.Notify(newPeerConnectWatcher(evtPeerConnectednessChanged)) |
87 |
| - |
88 |
| - n.SetStreamHandler(bh.newStreamHandler) |
89 |
| - |
90 |
| - // persist a signed peer record for self to the peerstore. |
91 |
| - if err := bh.initSignedRecord(); err != nil { |
92 |
| - log.Errorf("error creating blank host, err=%s", err) |
93 |
| - return nil |
94 |
| - } |
95 |
| - |
96 |
| - return bh |
97 |
| -} |
98 |
| - |
99 |
| -func (bh *BlankHost) initSignedRecord() error { |
100 |
| - cab, ok := peerstore.GetCertifiedAddrBook(bh.n.Peerstore()) |
101 |
| - if !ok { |
102 |
| - log.Error("peerstore does not support signed records") |
103 |
| - return errors.New("peerstore does not support signed records") |
104 |
| - } |
105 |
| - rec := peer.PeerRecordFromAddrInfo(peer.AddrInfo{ID: bh.ID(), Addrs: bh.Addrs()}) |
106 |
| - ev, err := record.Seal(rec, bh.Peerstore().PrivKey(bh.ID())) |
107 |
| - if err != nil { |
108 |
| - log.Errorf("failed to create signed record for self, err=%s", err) |
109 |
| - return fmt.Errorf("failed to create signed record for self, err=%s", err) |
110 |
| - } |
111 |
| - _, err = cab.ConsumePeerRecord(ev, peerstore.PermanentAddrTTL) |
112 |
| - if err != nil { |
113 |
| - log.Errorf("failed to persist signed record to peerstore,err=%s", err) |
114 |
| - return fmt.Errorf("failed to persist signed record for self, err=%s", err) |
115 |
| - } |
116 |
| - return err |
117 |
| -} |
118 |
| - |
119 |
| -var _ host.Host = (*BlankHost)(nil) |
120 |
| - |
121 |
| -func (bh *BlankHost) Addrs() []ma.Multiaddr { |
122 |
| - addrs, err := bh.n.InterfaceListenAddresses() |
123 |
| - if err != nil { |
124 |
| - log.Debug("error retrieving network interface addrs: ", err) |
125 |
| - return nil |
126 |
| - } |
127 |
| - |
128 |
| - return addrs |
129 |
| -} |
130 |
| - |
131 |
| -func (bh *BlankHost) Close() error { |
132 |
| - return bh.n.Close() |
133 |
| -} |
134 |
| - |
135 |
| -func (bh *BlankHost) Connect(ctx context.Context, ai peer.AddrInfo) error { |
136 |
| - // absorb addresses into peerstore |
137 |
| - bh.Peerstore().AddAddrs(ai.ID, ai.Addrs, peerstore.TempAddrTTL) |
138 |
| - |
139 |
| - cs := bh.n.ConnsToPeer(ai.ID) |
140 |
| - if len(cs) > 0 { |
141 |
| - return nil |
142 |
| - } |
143 |
| - |
144 |
| - _, err := bh.Network().DialPeer(ctx, ai.ID) |
145 |
| - return err |
146 |
| -} |
147 |
| - |
148 |
| -func (bh *BlankHost) Peerstore() peerstore.Peerstore { |
149 |
| - return bh.n.Peerstore() |
150 |
| -} |
151 |
| - |
152 |
| -func (bh *BlankHost) ID() peer.ID { |
153 |
| - return bh.n.LocalPeer() |
154 |
| -} |
155 |
| - |
156 |
| -func (bh *BlankHost) NewStream(ctx context.Context, p peer.ID, protos ...protocol.ID) (network.Stream, error) { |
157 |
| - s, err := bh.n.NewStream(ctx, p) |
158 |
| - if err != nil { |
159 |
| - return nil, err |
160 |
| - } |
161 |
| - |
162 |
| - var protoStrs []string |
163 |
| - for _, pid := range protos { |
164 |
| - protoStrs = append(protoStrs, string(pid)) |
165 |
| - } |
166 |
| - |
167 |
| - selected, err := mstream.SelectOneOf(protoStrs, s) |
168 |
| - if err != nil { |
169 |
| - s.Reset() |
170 |
| - return nil, err |
171 |
| - } |
172 |
| - |
173 |
| - selpid := protocol.ID(selected) |
174 |
| - s.SetProtocol(selpid) |
175 |
| - bh.Peerstore().AddProtocols(p, selected) |
176 |
| - |
177 |
| - return s, nil |
178 |
| -} |
179 |
| - |
180 |
| -func (bh *BlankHost) RemoveStreamHandler(pid protocol.ID) { |
181 |
| - bh.Mux().RemoveHandler(string(pid)) |
182 |
| - bh.emitters.evtLocalProtocolsUpdated.Emit(event.EvtLocalProtocolsUpdated{ |
183 |
| - Removed: []protocol.ID{pid}, |
184 |
| - }) |
185 |
| -} |
186 |
| - |
187 |
| -func (bh *BlankHost) SetStreamHandler(pid protocol.ID, handler network.StreamHandler) { |
188 |
| - bh.Mux().AddHandler(string(pid), func(p string, rwc io.ReadWriteCloser) error { |
189 |
| - is := rwc.(network.Stream) |
190 |
| - is.SetProtocol(protocol.ID(p)) |
191 |
| - handler(is) |
192 |
| - return nil |
193 |
| - }) |
194 |
| - bh.emitters.evtLocalProtocolsUpdated.Emit(event.EvtLocalProtocolsUpdated{ |
195 |
| - Added: []protocol.ID{pid}, |
196 |
| - }) |
197 |
| -} |
198 |
| - |
199 |
| -func (bh *BlankHost) SetStreamHandlerMatch(pid protocol.ID, m func(string) bool, handler network.StreamHandler) { |
200 |
| - bh.Mux().AddHandlerWithFunc(string(pid), m, func(p string, rwc io.ReadWriteCloser) error { |
201 |
| - is := rwc.(network.Stream) |
202 |
| - is.SetProtocol(protocol.ID(p)) |
203 |
| - handler(is) |
204 |
| - return nil |
205 |
| - }) |
206 |
| - bh.emitters.evtLocalProtocolsUpdated.Emit(event.EvtLocalProtocolsUpdated{ |
207 |
| - Added: []protocol.ID{pid}, |
208 |
| - }) |
209 |
| -} |
210 |
| - |
211 |
| -// newStreamHandler is the remote-opened stream handler for network.Network |
212 |
| -func (bh *BlankHost) newStreamHandler(s network.Stream) { |
213 |
| - protoID, handle, err := bh.Mux().Negotiate(s) |
214 |
| - if err != nil { |
215 |
| - log.Infow("protocol negotiation failed", "error", err) |
216 |
| - s.Reset() |
217 |
| - return |
218 |
| - } |
219 |
| - |
220 |
| - s.SetProtocol(protocol.ID(protoID)) |
221 |
| - |
222 |
| - go handle(protoID, s) |
223 |
| -} |
224 |
| - |
225 |
| -// TODO: i'm not sure this really needs to be here |
226 |
| -func (bh *BlankHost) Mux() protocol.Switch { |
227 |
| - return bh.mux |
228 |
| -} |
229 |
| - |
230 |
| -// TODO: also not sure this fits... Might be better ways around this (leaky abstractions) |
231 |
| -func (bh *BlankHost) Network() network.Network { |
232 |
| - return bh.n |
233 |
| -} |
234 |
| - |
235 |
| -func (bh *BlankHost) ConnManager() connmgr.ConnManager { |
236 |
| - return bh.cmgr |
237 |
| -} |
238 |
| - |
239 |
| -func (bh *BlankHost) EventBus() event.Bus { |
240 |
| - return bh.eventbus |
| 31 | + return blankhost.NewBlankHost(n, options...) |
241 | 32 | }
|
0 commit comments