Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 245a1a3

Browse files
author
Lars Gierth
committedNov 17, 2015
WIP
License: MIT Signed-off-by: Lars Gierth <larsg@systemli.org>
1 parent 3a2ac8f commit 245a1a3

File tree

7 files changed

+150
-27
lines changed

7 files changed

+150
-27
lines changed
 

‎core/builder.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ func setupNode(ctx context.Context, n *IpfsNode, cfg *BuildCfg) error {
139139
if err != nil {
140140
return err
141141
}
142-
do := setupDiscoveryOption(rcfg.Discovery)
142+
do := setupDiscoveryOptions(rcfg.Discovery)
143143
if err := n.startOnlineServices(ctx, cfg.Routing, cfg.Host, do); err != nil {
144144
return err
145145
}

‎core/core.go

+21-10
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ type IpfsNode struct {
9595
DAG merkledag.DAGService // the merkle dag service, get/add objects.
9696
Resolver *path.Resolver // the path resolution system
9797
Reporter metrics.Reporter
98-
Discovery discovery.Service
98+
Discovery []discovery.Service
9999

100100
// Online
101101
PeerHost p2phost.Host // the network host (server+client)
@@ -124,7 +124,7 @@ type Mounts struct {
124124
Ipns mount.Mount
125125
}
126126

127-
func (n *IpfsNode) startOnlineServices(ctx context.Context, routingOption RoutingOption, hostOption HostOption, do DiscoveryOption) error {
127+
func (n *IpfsNode) startOnlineServices(ctx context.Context, routingOption RoutingOption, hostOption HostOption, discoveryOpts []DiscoveryOption) error {
128128

129129
if n.PeerHost != nil { // already online.
130130
return errors.New("node already online")
@@ -170,29 +170,40 @@ func (n *IpfsNode) startOnlineServices(ctx context.Context, routingOption Routin
170170
go n.Reprovider.ProvideEvery(ctx, kReprovideFrequency)
171171

172172
// setup local discovery
173-
if do != nil {
174-
service, err := do(n.PeerHost)
175-
if err != nil {
176-
log.Error("mdns error: ", err)
173+
for _, opt := range discoveryOpts {
174+
if service, err := opt(n.PeerHost); err != nil {
175+
return err
177176
} else {
178177
service.RegisterNotifee(n)
179-
n.Discovery = service
178+
n.Discovery = append(n.Discovery, service)
180179
}
181180
}
182181

183182
return n.Bootstrap(DefaultBootstrapConfig)
184183
}
185184

186-
func setupDiscoveryOption(d config.Discovery) DiscoveryOption {
185+
func setupDiscoveryOptions(d config.Discovery) DiscoveryOption {
186+
opts := []DiscoveryOption{}
187+
187188
if d.MDNS.Enabled {
188-
return func(h p2phost.Host) (discovery.Service, error) {
189+
opt := func(h p2phost.Host) (discovery.Service, error) {
189190
if d.MDNS.Interval == 0 {
190191
d.MDNS.Interval = 5
191192
}
192193
return discovery.NewMdnsService(h, time.Duration(d.MDNS.Interval)*time.Second)
193194
}
195+
opts = append(opts, opt)
194196
}
195-
return nil
197+
if d.Cjdns.Enabled {
198+
opt := func(h p2phost.Host) (discovery.Service, error) {
199+
if d.Cjdns.Interval == 0 {
200+
d.Cjdns.Interval = 5
201+
}
202+
return discovery.NewCjdnsService(h, time.Duration(d.Cjdns.Interval)*time.Second)
203+
}
204+
opts = append(opts, opt)
205+
}
206+
return opts
196207
}
197208

198209
func (n *IpfsNode) HandlePeerFound(p peer.PeerInfo) {

‎p2p/discovery/cjdns.go

+92
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
package discovery
2+
3+
// mdns introduced in https://github.com/ipfs/go-ipfs/pull/1117
4+
5+
import (
6+
"sync"
7+
"time"
8+
9+
"github.com/ipfs/go-ipfs/p2p/host"
10+
11+
"github.com/ehmry/go-cjdns/admin"
12+
"github.com/ehmry/go-cjdns/key"
13+
)
14+
15+
type cjdnsService struct {
16+
admin *admin.Conn
17+
host host.Host
18+
lk sync.Mutex
19+
notifees []Notifee
20+
interval time.Duration
21+
}
22+
23+
func NewCjdnsService(host host.Host, interval time.Duration) (Service, error) {
24+
cjdnsConfig := &admin.CjdnsAdminConfig{
25+
Addr: "127.0.0.1",
26+
Port: 11234,
27+
Password: "NONE",
28+
}
29+
admin, err := admin.Connect(cjdnsConfig)
30+
if err != nil {
31+
log.Error("cjdns connect error: ", err)
32+
return nil, err
33+
}
34+
35+
log.Debug("cjdns connect")
36+
37+
service := &cjdnsService{
38+
admin: admin,
39+
host: host,
40+
interval: interval,
41+
}
42+
43+
go service.pollPeerStats()
44+
45+
return service, nil
46+
}
47+
48+
func (cjdns *cjdnsService) Close() error {
49+
return nil
50+
}
51+
52+
func (cjdns *cjdnsService) pollPeerStats() {
53+
ticker := time.NewTicker(cjdns.interval)
54+
for {
55+
select {
56+
case <-ticker.C:
57+
results, err := cjdns.admin.InterfaceController_peerStats()
58+
if err != nil {
59+
log.Error("cjdns peerstats error: ", err)
60+
}
61+
62+
for _, peer := range results {
63+
k, err := key.DecodePublic(peer.PublicKey.String())
64+
if err != nil {
65+
log.Error("malformed key: %s", peer.PublicKey.String())
66+
}
67+
log.Debugf("cjdns peer: %s", k.IP())
68+
}
69+
}
70+
}
71+
}
72+
73+
func (c *cjdnsService) RegisterNotifee(n Notifee) {
74+
c.lk.Lock()
75+
c.notifees = append(c.notifees, n)
76+
c.lk.Unlock()
77+
}
78+
79+
func (c *cjdnsService) UnregisterNotifee(n Notifee) {
80+
c.lk.Lock()
81+
found := -1
82+
for i, notif := range c.notifees {
83+
if notif == n {
84+
found = i
85+
break
86+
}
87+
}
88+
if found != -1 {
89+
c.notifees = append(c.notifees[:found], c.notifees[found+1:]...)
90+
}
91+
c.lk.Unlock()
92+
}

‎p2p/discovery/discovery.go

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package discovery
2+
3+
import (
4+
"io"
5+
6+
"github.com/ipfs/go-ipfs/p2p/peer"
7+
"github.com/ipfs/go-ipfs/util"
8+
)
9+
10+
var log = util.Logger("discovery")
11+
12+
type Service interface {
13+
io.Closer
14+
RegisterNotifee(Notifee)
15+
UnregisterNotifee(Notifee)
16+
}
17+
18+
type Notifee interface {
19+
HandlePeerFound(peer.PeerInfo)
20+
}

‎p2p/discovery/mdns.go

-11
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package discovery
22

33
import (
44
"errors"
5-
"io"
65
"io/ioutil"
76
golog "log"
87
"net"
@@ -22,16 +21,6 @@ var log = logging.Logger("mdns")
2221

2322
const ServiceTag = "discovery.ipfs.io"
2423

25-
type Service interface {
26-
io.Closer
27-
RegisterNotifee(Notifee)
28-
UnregisterNotifee(Notifee)
29-
}
30-
31-
type Notifee interface {
32-
HandlePeerFound(peer.PeerInfo)
33-
}
34-
3524
type mdnsService struct {
3625
server *mdns.Server
3726
service *mdns.MDNSService

‎repo/config/discovery.go

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
package config
22

33
type Discovery struct {
4-
MDNS MDNS
4+
MDNS MDNS
5+
Cjdns Cjdns
56
}
67

78
type MDNS struct {
@@ -10,3 +11,8 @@ type MDNS struct {
1011
// Time in seconds between discovery rounds
1112
Interval int
1213
}
14+
15+
type Cjdns struct {
16+
Enabled bool
17+
Interval int
18+
}

‎repo/config/init.go

+9-4
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,15 @@ func Init(out io.Writer, nBitsForKeypair int) (*Config, error) {
4949
SupernodeRouting: *snr,
5050
Datastore: *ds,
5151
Identity: identity,
52-
Discovery: Discovery{MDNS{
53-
Enabled: true,
54-
Interval: 10,
55-
}},
52+
Discovery: Discovery{
53+
MDNS{
54+
Enabled: true,
55+
Interval: 10,
56+
},
57+
Cjdns{
58+
Enabled: true,
59+
Interval: 10,
60+
}},
5661

5762
// setup the node mount points.
5863
Mounts: Mounts{

0 commit comments

Comments
 (0)
Please sign in to comment.