Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
Menu
Open sidebar
adam.huang
go-libp2p
Commits
4493535d
Commit
4493535d
authored
Apr 22, 2015
by
Jeromy
Browse files
implement a config option for mdns
parent
bc183f1a
Changes
1
Show whitespace changes
Inline
Side-by-side
discovery/mdns.go
View file @
4493535d
package
discovery
package
discovery
import
(
import
(
"errors"
"fmt"
"fmt"
"io"
"io"
"io/ioutil"
"io/ioutil"
golog
"log"
golog
"log"
"net"
"net"
"strconv"
//
"strconv"
"strings"
//
"strings"
"sync"
"sync"
"time"
"time"
...
@@ -22,7 +23,6 @@ import (
...
@@ -22,7 +23,6 @@ import (
var
log
=
u
.
Logger
(
"mdns"
)
var
log
=
u
.
Logger
(
"mdns"
)
const
LookupFrequency
=
time
.
Second
*
5
const
ServiceTag
=
"discovery.ipfs.io"
const
ServiceTag
=
"discovery.ipfs.io"
type
Service
interface
{
type
Service
interface
{
...
@@ -42,33 +42,45 @@ type mdnsService struct {
...
@@ -42,33 +42,45 @@ type mdnsService struct {
lk
sync
.
Mutex
lk
sync
.
Mutex
notifees
[]
Notifee
notifees
[]
Notifee
interval
time
.
Duration
}
}
func
NewMdnsService
(
peerhost
host
.
Host
)
(
Service
,
error
)
{
func
getDialableListenAddr
(
ph
host
.
Host
)
(
*
net
.
TCPAddr
,
error
)
{
for
_
,
addr
:=
range
ph
.
Addrs
()
{
na
,
err
:=
manet
.
ToNetAddr
(
addr
)
if
err
!=
nil
{
continue
}
tcp
,
ok
:=
na
.
(
*
net
.
TCPAddr
)
if
ok
{
return
tcp
,
nil
}
}
return
nil
,
errors
.
New
(
"failed to find good external addr from peerhost"
)
}
func
NewMdnsService
(
peerhost
host
.
Host
,
interval
time
.
Duration
)
(
Service
,
error
)
{
// TODO: dont let mdns use logging...
// TODO: dont let mdns use logging...
golog
.
SetOutput
(
ioutil
.
Discard
)
golog
.
SetOutput
(
ioutil
.
Discard
)
// determine my local swarm port
var
ipaddrs
[]
net
.
IP
port
:=
4001
port
:=
4001
for
_
,
addr
:=
range
peerhost
.
Addrs
()
{
parts
:=
strings
.
Split
(
addr
.
String
(),
"/"
)
addr
,
err
:=
getDialableListenAddr
(
peerhost
)
fmt
.
Println
(
"parts len: "
,
len
(
parts
))
if
len
(
parts
)
==
5
&&
parts
[
3
]
==
"tcp"
{
n
,
err
:=
strconv
.
Atoi
(
parts
[
4
])
if
err
!=
nil
{
if
err
!=
nil
{
return
nil
,
err
log
.
Warning
(
err
)
}
}
else
{
port
=
n
ipaddrs
=
[]
net
.
IP
{
addr
.
IP
}
break
port
=
addr
.
Port
}
}
}
fmt
.
Println
(
"using port: "
,
port
)
fmt
.
Println
(
"using port: "
,
port
)
myid
:=
peerhost
.
ID
()
.
Pretty
()
myid
:=
peerhost
.
ID
()
.
Pretty
()
info
:=
[]
string
{
myid
}
info
:=
[]
string
{
myid
}
service
,
err
:=
mdns
.
NewMDNSService
(
myid
,
ServiceTag
,
""
,
""
,
port
,
nil
,
info
)
service
,
err
:=
mdns
.
NewMDNSService
(
myid
,
ServiceTag
,
""
,
""
,
port
,
ipaddrs
,
info
)
if
err
!=
nil
{
if
err
!=
nil
{
return
nil
,
err
return
nil
,
err
}
}
...
@@ -83,6 +95,7 @@ func NewMdnsService(peerhost host.Host) (Service, error) {
...
@@ -83,6 +95,7 @@ func NewMdnsService(peerhost host.Host) (Service, error) {
server
:
server
,
server
:
server
,
service
:
service
,
service
:
service
,
host
:
peerhost
,
host
:
peerhost
,
interval
:
interval
,
}
}
go
s
.
pollForEntries
()
go
s
.
pollForEntries
()
...
@@ -95,7 +108,7 @@ func (m *mdnsService) Close() error {
...
@@ -95,7 +108,7 @@ func (m *mdnsService) Close() error {
}
}
func
(
m
*
mdnsService
)
pollForEntries
()
{
func
(
m
*
mdnsService
)
pollForEntries
()
{
ticker
:=
time
.
NewTicker
(
LookupFrequency
)
ticker
:=
time
.
NewTicker
(
m
.
interval
)
for
{
for
{
select
{
select
{
case
<-
ticker
.
C
:
case
<-
ticker
.
C
:
...
@@ -110,7 +123,7 @@ func (m *mdnsService) pollForEntries() {
...
@@ -110,7 +123,7 @@ func (m *mdnsService) pollForEntries() {
qp
.
Domain
=
"local"
qp
.
Domain
=
"local"
qp
.
Entries
=
entriesCh
qp
.
Entries
=
entriesCh
qp
.
Service
=
ServiceTag
qp
.
Service
=
ServiceTag
qp
.
Timeout
=
time
.
Second
*
3
qp
.
Timeout
=
time
.
Second
*
5
err
:=
mdns
.
Query
(
&
qp
)
err
:=
mdns
.
Query
(
&
qp
)
if
err
!=
nil
{
if
err
!=
nil
{
...
@@ -122,6 +135,7 @@ func (m *mdnsService) pollForEntries() {
...
@@ -122,6 +135,7 @@ func (m *mdnsService) pollForEntries() {
}
}
func
(
m
*
mdnsService
)
handleEntry
(
e
*
mdns
.
ServiceEntry
)
{
func
(
m
*
mdnsService
)
handleEntry
(
e
*
mdns
.
ServiceEntry
)
{
fmt
.
Println
(
"handling entry!"
)
mpeer
,
err
:=
peer
.
IDB58Decode
(
e
.
Info
)
mpeer
,
err
:=
peer
.
IDB58Decode
(
e
.
Info
)
if
err
!=
nil
{
if
err
!=
nil
{
log
.
Warning
(
"Error parsing peer ID from mdns entry: "
,
err
)
log
.
Warning
(
"Error parsing peer ID from mdns entry: "
,
err
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment