commit
3d98f675f3
|
@ -75,7 +75,7 @@ const (
|
|||
|
||||
/* AddrBook - concurrency safe peer address manager */
|
||||
type AddrBook struct {
|
||||
QuitService
|
||||
BaseService
|
||||
|
||||
mtx sync.Mutex
|
||||
filePath string
|
||||
|
@ -106,7 +106,7 @@ func NewAddrBook(filePath string, routabilityStrict bool) *AddrBook {
|
|||
routabilityStrict: routabilityStrict,
|
||||
}
|
||||
am.init()
|
||||
am.QuitService = *NewQuitService(log, "AddrBook", am)
|
||||
am.BaseService = *NewBaseService(log, "AddrBook", am)
|
||||
return am
|
||||
}
|
||||
|
||||
|
@ -126,7 +126,7 @@ func (a *AddrBook) init() {
|
|||
}
|
||||
|
||||
func (a *AddrBook) OnStart() error {
|
||||
a.QuitService.OnStart()
|
||||
a.BaseService.OnStart()
|
||||
a.loadFromFile(a.filePath)
|
||||
a.wg.Add(1)
|
||||
go a.saveRoutine()
|
||||
|
@ -134,7 +134,7 @@ func (a *AddrBook) OnStart() error {
|
|||
}
|
||||
|
||||
func (a *AddrBook) OnStop() {
|
||||
a.QuitService.OnStop()
|
||||
a.BaseService.OnStop()
|
||||
a.wg.Wait()
|
||||
}
|
||||
|
||||
|
|
40
switch.go
40
switch.go
|
@ -26,13 +26,13 @@ type Reactor interface {
|
|||
//--------------------------------------
|
||||
|
||||
type BaseReactor struct {
|
||||
QuitService // Provides Start, Stop, .Quit
|
||||
BaseService // Provides Start, Stop, .Quit
|
||||
Switch *Switch
|
||||
}
|
||||
|
||||
func NewBaseReactor(log log15.Logger, name string, impl Reactor) *BaseReactor {
|
||||
return &BaseReactor{
|
||||
QuitService: *NewQuitService(log, name, impl),
|
||||
BaseService: *NewBaseService(log, name, impl),
|
||||
Switch: nil,
|
||||
}
|
||||
}
|
||||
|
@ -195,8 +195,10 @@ func (sw *Switch) OnStop() {
|
|||
// NOTE: This performs a blocking handshake before the peer is added.
|
||||
// CONTRACT: Iff error is returned, peer is nil, and conn is immediately closed.
|
||||
func (sw *Switch) AddPeerWithConnection(conn net.Conn, outbound bool) (*Peer, error) {
|
||||
|
||||
// Filter by addr (ie. ip:port)
|
||||
if err := sw.FilterConnByAddr(conn.RemoteAddr()); err != nil {
|
||||
conn.Close()
|
||||
return nil, err
|
||||
}
|
||||
|
||||
|
@ -217,6 +219,7 @@ func (sw *Switch) AddPeerWithConnection(conn net.Conn, outbound bool) (*Peer, er
|
|||
|
||||
// Filter by p2p-key
|
||||
if err := sw.FilterConnByPubKey(sconn.(*SecretConnection).RemotePubKey()); err != nil {
|
||||
sconn.Close()
|
||||
return nil, err
|
||||
}
|
||||
|
||||
|
@ -466,30 +469,45 @@ func MakeConnectedSwitches(n int, initSwitch func(int, *Switch) *Switch, connect
|
|||
switches[i] = makeSwitch(i, "testing", "123.123.123", initSwitch)
|
||||
}
|
||||
|
||||
if err := StartSwitches(switches); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
for i := 0; i < n; i++ {
|
||||
for j := i; j < n; j++ {
|
||||
connect(switches, i, j)
|
||||
}
|
||||
}
|
||||
// Wait for things to happen, peers to get added...
|
||||
// TODO: better
|
||||
time.Sleep(100 * time.Millisecond * time.Duration(n*n))
|
||||
|
||||
if err := StartSwitches(switches); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
return switches
|
||||
}
|
||||
|
||||
var PanicOnAddPeerErr = false
|
||||
|
||||
// Will connect switches i and j via net.Pipe()
|
||||
// Blocks until a conection is established.
|
||||
// NOTE: caller ensures i and j are within bounds
|
||||
func Connect2Switches(switches []*Switch, i, j int) {
|
||||
switchI := switches[i]
|
||||
switchJ := switches[j]
|
||||
c1, c2 := net.Pipe()
|
||||
go switchI.AddPeerWithConnection(c1, false) // AddPeer is blocking, requires handshake.
|
||||
go switchJ.AddPeerWithConnection(c2, true)
|
||||
doneCh := make(chan struct{})
|
||||
go func() {
|
||||
_, err := switchI.AddPeerWithConnection(c1, false) // AddPeer is blocking, requires handshake.
|
||||
if PanicOnAddPeerErr && err != nil {
|
||||
panic(err)
|
||||
}
|
||||
doneCh <- struct{}{}
|
||||
}()
|
||||
go func() {
|
||||
_, err := switchJ.AddPeerWithConnection(c2, true)
|
||||
if PanicOnAddPeerErr && err != nil {
|
||||
panic(err)
|
||||
}
|
||||
doneCh <- struct{}{}
|
||||
}()
|
||||
<-doneCh
|
||||
<-doneCh
|
||||
}
|
||||
|
||||
func StartSwitches(switches []*Switch) error {
|
||||
|
|
2
types.go
2
types.go
|
@ -21,7 +21,7 @@ type NodeInfo struct {
|
|||
Other []string `json:"other"` // other application specific data
|
||||
}
|
||||
|
||||
// CONTRACT: two nodes are compactible if the major/minor versions match and network match
|
||||
// CONTRACT: two nodes are compatible if the major/minor versions match and network match
|
||||
func (info *NodeInfo) CompatibleWith(other *NodeInfo) error {
|
||||
iMajor, iMinor, _, iErr := splitVersion(info.Version)
|
||||
oMajor, oMinor, _, oErr := splitVersion(other.Version)
|
||||
|
|
|
@ -1,3 +1,3 @@
|
|||
package p2p
|
||||
|
||||
const Version = "0.3.4" // filter by addr or pubkey
|
||||
const Version = "0.3.5" // minor fixes
|
||||
|
|
Loading…
Reference in New Issue