@@ -4,12 +4,15 @@ import (
4
4
"context"
5
5
"io"
6
6
7
+ "github.com/libp2p/go-libp2p-core/connmgr"
8
+ "github.com/libp2p/go-libp2p-core/host"
9
+ "github.com/libp2p/go-libp2p-core/network"
10
+ "github.com/libp2p/go-libp2p-core/peer"
11
+ "github.com/libp2p/go-libp2p-core/peerstore"
12
+ "github.com/libp2p/go-libp2p-core/protocol"
13
+
7
14
logging "github.com/ipfs/go-log"
8
- core "github.com/libp2p/go-libp2p-core"
9
- connmgr "github.com/libp2p/go-libp2p-core/connmgr"
10
- network "github.com/libp2p/go-libp2p-core/network"
11
- pstore "github.com/libp2p/go-libp2p-core/peerstore"
12
- protocol "github.com/libp2p/go-libp2p-core/protocol"
15
+
13
16
ma "github.com/multiformats/go-multiaddr"
14
17
mstream "github.com/multiformats/go-multistream"
15
18
)
@@ -18,12 +21,12 @@ var log = logging.Logger("blankhost")
18
21
19
22
// BlankHost is the thinnest implementation of the host.Host interface
20
23
type BlankHost struct {
21
- n core .Network
24
+ n network .Network
22
25
mux * mstream.MultistreamMuxer
23
26
cmgr connmgr.ConnManager
24
27
}
25
28
26
- func NewBlankHost (n core .Network ) * BlankHost {
29
+ func NewBlankHost (n network .Network ) * BlankHost {
27
30
bh := & BlankHost {
28
31
n : n ,
29
32
cmgr : & connmgr.NullConnMgr {},
@@ -34,7 +37,7 @@ func NewBlankHost(n core.Network) *BlankHost {
34
37
return bh
35
38
}
36
39
37
- var _ core .Host = (* BlankHost )(nil )
40
+ var _ host .Host = (* BlankHost )(nil )
38
41
39
42
func (bh * BlankHost ) Addrs () []ma.Multiaddr {
40
43
addrs , err := bh .n .InterfaceListenAddresses ()
@@ -50,28 +53,28 @@ func (bh *BlankHost) Close() error {
50
53
return bh .n .Close ()
51
54
}
52
55
53
- func (bh * BlankHost ) Connect (ctx context.Context , pi core. PeerAddrInfo ) error {
56
+ func (bh * BlankHost ) Connect (ctx context.Context , ai peer. AddrInfo ) error {
54
57
// absorb addresses into peerstore
55
- bh .Peerstore ().AddAddrs (pi .ID , pi .Addrs , pstore .TempAddrTTL )
58
+ bh .Peerstore ().AddAddrs (ai .ID , ai .Addrs , peerstore .TempAddrTTL )
56
59
57
- cs := bh .n .ConnsToPeer (pi .ID )
60
+ cs := bh .n .ConnsToPeer (ai .ID )
58
61
if len (cs ) > 0 {
59
62
return nil
60
63
}
61
64
62
- _ , err := bh .Network ().DialPeer (ctx , pi .ID )
65
+ _ , err := bh .Network ().DialPeer (ctx , ai .ID )
63
66
return err
64
67
}
65
68
66
- func (bh * BlankHost ) Peerstore () pstore .Peerstore {
69
+ func (bh * BlankHost ) Peerstore () peerstore .Peerstore {
67
70
return bh .n .Peerstore ()
68
71
}
69
72
70
- func (bh * BlankHost ) ID () core. PeerID {
73
+ func (bh * BlankHost ) ID () peer. ID {
71
74
return bh .n .LocalPeer ()
72
75
}
73
76
74
- func (bh * BlankHost ) NewStream (ctx context.Context , p core. PeerID , protos ... core. ProtocolID ) (core .Stream , error ) {
77
+ func (bh * BlankHost ) NewStream (ctx context.Context , p peer. ID , protos ... protocol. ID ) (network .Stream , error ) {
75
78
s , err := bh .n .NewStream (ctx , p )
76
79
if err != nil {
77
80
return nil , err
@@ -95,32 +98,32 @@ func (bh *BlankHost) NewStream(ctx context.Context, p core.PeerID, protos ...cor
95
98
return s , nil
96
99
}
97
100
98
- func (bh * BlankHost ) RemoveStreamHandler (p core. ProtocolID ) {
101
+ func (bh * BlankHost ) RemoveStreamHandler (p protocol. ID ) {
99
102
bh .Mux ().RemoveHandler (string (p ))
100
103
}
101
104
102
- func (bh * BlankHost ) SetStreamHandler (pid core. ProtocolID , handler network.StreamHandler ) {
105
+ func (bh * BlankHost ) SetStreamHandler (pid protocol. ID , handler network.StreamHandler ) {
103
106
bh .Mux ().AddHandler (string (pid ), func (p string , rwc io.ReadWriteCloser ) error {
104
- is := rwc .(core .Stream )
107
+ is := rwc .(network .Stream )
105
108
is .SetProtocol (protocol .ID (p ))
106
109
handler (is )
107
110
return nil
108
111
})
109
112
}
110
113
111
- func (bh * BlankHost ) SetStreamHandlerMatch (pid core. ProtocolID , m func (string ) bool , handler network.StreamHandler ) {
114
+ func (bh * BlankHost ) SetStreamHandlerMatch (pid protocol. ID , m func (string ) bool , handler network.StreamHandler ) {
112
115
bh .Mux ().AddHandlerWithFunc (string (pid ), m , func (p string , rwc io.ReadWriteCloser ) error {
113
- is := rwc .(core .Stream )
116
+ is := rwc .(network .Stream )
114
117
is .SetProtocol (protocol .ID (p ))
115
118
handler (is )
116
119
return nil
117
120
})
118
121
}
119
122
120
- // newStreamHandler is the remote-opened stream handler for core .Network
121
- func (h * BlankHost ) newStreamHandler (s core .Stream ) {
123
+ // newStreamHandler is the remote-opened stream handler for network .Network
124
+ func (bh * BlankHost ) newStreamHandler (s network .Stream ) {
122
125
123
- protoID , handle , err := h .Mux ().Negotiate (s )
126
+ protoID , handle , err := bh .Mux ().Negotiate (s )
124
127
if err != nil {
125
128
log .Warning ("protocol mux failed: %s" , err )
126
129
s .Close ()
@@ -138,7 +141,7 @@ func (bh *BlankHost) Mux() protocol.Switch {
138
141
}
139
142
140
143
// TODO: also not sure this fits... Might be better ways around this (leaky abstractions)
141
- func (bh * BlankHost ) Network () core .Network {
144
+ func (bh * BlankHost ) Network () network .Network {
142
145
return bh .n
143
146
}
144
147
0 commit comments