mirror of
https://github.com/fluencelabs/tendermint
synced 2025-06-14 22:01:20 +00:00
created "Node"
This commit is contained in:
@ -2,7 +2,10 @@ package common
|
||||
|
||||
import "time"
|
||||
|
||||
/* RepeatTimer */
|
||||
/*
|
||||
RepeatTimer repeatedly sends a struct{}{} to .Ch after each "dur" period.
|
||||
It's good for keeping connections alive.
|
||||
*/
|
||||
type RepeatTimer struct {
|
||||
Ch chan struct{}
|
||||
quit chan struct{}
|
||||
@ -26,6 +29,7 @@ func (t *RepeatTimer) fireHandler() {
|
||||
}
|
||||
}
|
||||
|
||||
// Wait the duration again before firing.
|
||||
func (t *RepeatTimer) Reset() {
|
||||
t.timer.Reset(t.dur)
|
||||
}
|
||||
|
@ -5,7 +5,10 @@ import (
|
||||
"time"
|
||||
)
|
||||
|
||||
/* Throttler */
|
||||
/*
|
||||
Throttler sends a struct{}{} to .Ch "dur" after the last .Set().
|
||||
It's good for ensuring that something happens last after a burst of events.
|
||||
*/
|
||||
type Throttler struct {
|
||||
Ch chan struct{}
|
||||
quit chan struct{}
|
||||
@ -19,6 +22,7 @@ func NewThrottler(dur time.Duration) *Throttler {
|
||||
var quit = make(chan struct{})
|
||||
var t = &Throttler{Ch: ch, dur: dur, quit: quit}
|
||||
t.timer = time.AfterFunc(dur, t.fireHandler)
|
||||
t.timer.Stop()
|
||||
return t
|
||||
}
|
||||
|
||||
|
2
log.go
2
log.go
@ -16,7 +16,7 @@ func init() {
|
||||
</outputs>
|
||||
<formats>
|
||||
<format id="main" format="%Date/%Time [%LEV] %Msg%n"/>
|
||||
<format id="colored" format="%EscM(46)%Level%EscM(49) %EscM(36)%File%EscM(39) %Msg%n%EscM(0)"/>
|
||||
<format id="colored" format="%Time %EscM(46)%Level%EscM(49) %EscM(36)%File%EscM(39) %Msg%n%EscM(0)"/>
|
||||
</formats>
|
||||
</seelog>`
|
||||
|
||||
|
107
main.go
107
main.go
@ -8,8 +8,21 @@ import (
|
||||
"github.com/tendermint/tendermint/p2p"
|
||||
)
|
||||
|
||||
func main() {
|
||||
const (
|
||||
minNumPeers = 10
|
||||
maxNumPeers = 20
|
||||
|
||||
ensurePeersPeriodSeconds = 30
|
||||
peerDialTimeoutSeconds = 30
|
||||
)
|
||||
|
||||
type Node struct {
|
||||
sw *p2p.Switch
|
||||
book *p2p.AddressBook
|
||||
quit chan struct{}
|
||||
}
|
||||
|
||||
func NewNode() *Node {
|
||||
// Define channels for our app
|
||||
chDescs := []p2p.ChannelDescriptor{
|
||||
p2p.ChannelDescriptor{
|
||||
@ -33,43 +46,107 @@ func main() {
|
||||
RecvBufferSize: 1000,
|
||||
},
|
||||
}
|
||||
|
||||
// Create the switch
|
||||
sw := p2p.NewSwitch(chDescs)
|
||||
book := p2p.NewAddrBook(config.AppDir + "/addrbook.json")
|
||||
|
||||
// Create a listener for incoming connections
|
||||
l := p2p.NewDefaultListener("tcp", ":8001")
|
||||
return &New{
|
||||
sw: sw,
|
||||
book: book,
|
||||
}
|
||||
}
|
||||
|
||||
func (n *Node) Start() {
|
||||
n.sw.Start()
|
||||
n.book.Start()
|
||||
go p2p.PexHandler(sw, book)
|
||||
go n.ensurePeersHandler(sw, book)
|
||||
}
|
||||
|
||||
func (n *Node) initPeer(peer *Peer) {
|
||||
if peer.IsOutgoing() {
|
||||
// TODO: initiate PEX
|
||||
}
|
||||
}
|
||||
|
||||
// Add a Listener to accept incoming peer connections.
|
||||
func (n *Node) AddListener(l Listener) {
|
||||
go func() {
|
||||
for {
|
||||
inConn, ok := <-l.Connections()
|
||||
if !ok {
|
||||
break
|
||||
}
|
||||
peer, err := sw.AddPeerWithConnection(inConn, false)
|
||||
peer, err := n.sw.AddPeerWithConnection(inConn, false)
|
||||
if err != nil {
|
||||
log.Infof("Ignoring error from incoming connection: %v\n%v",
|
||||
peer, err)
|
||||
continue
|
||||
}
|
||||
initPeer(peer)
|
||||
n.initPeer(peer)
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
// Open our address book
|
||||
book := p2p.NewAddrBook(config.AppDir + "/addrbook.json")
|
||||
// Ensures that sufficient peers are connected.
|
||||
func (n *Node) ensurePeers() {
|
||||
numPeers := len(n.sw.Peers())
|
||||
if numPeers < minNumPeers {
|
||||
// XXX
|
||||
}
|
||||
}
|
||||
|
||||
// Start PEX
|
||||
go p2p.PexHandler(sw, book)
|
||||
func (n *Node) ensurePeersHandler() {
|
||||
timer := NewRepeatTimer(ensurePeersPeriodSeconds * time.Second)
|
||||
FOR_LOOP:
|
||||
for {
|
||||
select {
|
||||
case <-timer.Ch:
|
||||
n.ensurePeers()
|
||||
case <-n.quit:
|
||||
break FOR_LOOP
|
||||
}
|
||||
}
|
||||
|
||||
// cleanup
|
||||
timer.Stop()
|
||||
}
|
||||
|
||||
func (n *Node) Stop() {
|
||||
// TODO: gracefully disconnect from peers.
|
||||
n.sw.Stop()
|
||||
n.book.Stop()
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
func main() {
|
||||
|
||||
n := NewNode()
|
||||
l := p2p.NewDefaultListener("tcp", ":8001")
|
||||
n.AddListener()
|
||||
|
||||
if false {
|
||||
// TODO remove
|
||||
// let's connect to 66.175.218.199
|
||||
conn, err := p2p.NewNetAddressString("66.175.218.199:8001").Dial()
|
||||
if err != nil {
|
||||
log.Infof("Error connecting to it: %v", err)
|
||||
return
|
||||
}
|
||||
peer, err := sw.AddPeerWithConnection(conn, true)
|
||||
if err != nil {
|
||||
log.Infof("Error adding peer with connection: %v", err)
|
||||
return
|
||||
}
|
||||
log.Infof("Connected to peer: %v", peer)
|
||||
// TODO remove
|
||||
}
|
||||
|
||||
// Sleep forever
|
||||
trapSignal()
|
||||
select {}
|
||||
}
|
||||
|
||||
func initPeer(peer *p2p.Peer) {
|
||||
// TODO: ask for more peers if we need them.
|
||||
}
|
||||
|
||||
func trapSignal() {
|
||||
c := make(chan os.Signal, 1)
|
||||
signal.Notify(c, os.Interrupt)
|
||||
|
@ -95,6 +95,14 @@ func (na *NetAddress) Dial() (*Connection, error) {
|
||||
return NewConnection(conn), nil
|
||||
}
|
||||
|
||||
func (na *NetAddress) DialTimeout(timeout time.Duration) (*Connection, error) {
|
||||
conn, err := net.DialTimeout("tcp", na.String(), timeout)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return NewConnection(conn), nil
|
||||
}
|
||||
|
||||
func (na *NetAddress) Routable() bool {
|
||||
// TODO(oga) bitcoind doesn't include RFC3849 here, but should we?
|
||||
return na.Valid() && !(na.RFC1918() || na.RFC3927() || na.RFC4862() ||
|
||||
|
@ -55,6 +55,10 @@ func (p *Peer) stop() {
|
||||
}
|
||||
}
|
||||
|
||||
func (p *Peer) IsOutgoing() bool {
|
||||
return p.outgoing
|
||||
}
|
||||
|
||||
func (p *Peer) LocalAddress() *NetAddress {
|
||||
return p.conn.LocalAddress()
|
||||
}
|
||||
|
@ -49,6 +49,9 @@ func NewSwitch(channels []ChannelDescriptor) *Switch {
|
||||
return s
|
||||
}
|
||||
|
||||
func (s *Switch) Start() {
|
||||
}
|
||||
|
||||
func (s *Switch) Stop() {
|
||||
log.Infof("Stopping switch")
|
||||
if atomic.CompareAndSwapUint32(&s.stopped, 0, 1) {
|
||||
|
3
rpc/rpc.go
Normal file
3
rpc/rpc.go
Normal file
@ -0,0 +1,3 @@
|
||||
package rpc
|
||||
|
||||
// TODO
|
Reference in New Issue
Block a user