Browse Source

with seed node

pull/9/head
Jae Kwon 11 years ago
parent
commit
cc9ea407aa
3 changed files with 55 additions and 37 deletions
  1. +25
    -5
      config/config.go
  2. +28
    -32
      main.go
  3. +2
    -0
      p2p/addrbook.go

+ 25
- 5
config/config.go View File

@ -3,6 +3,7 @@ package config
import (
"encoding/json"
"errors"
"flag"
"fmt"
"io/ioutil"
"log"
@ -18,8 +19,14 @@ import (
var AppDir = os.Getenv("HOME") + "/.tendermint"
var Config Config_
func init() {
func initFlags(printHelp *bool) {
flag.BoolVar(printHelp, "help", false, "Print this help message.")
flag.StringVar(&Config.IP, "ip", Config.IP, "Listen IP. (0.0.0.0 means any)")
flag.IntVar(&Config.Port, "port", Config.Port, "Listen port. (0 means any)")
flag.StringVar(&Config.Seed, "seed", Config.Seed, "Address of seed node")
}
func init() {
configFile := AppDir + "/config.json"
// try to read configuration. if missing, write default
@ -41,13 +48,25 @@ func init() {
if err != nil {
log.Panicf("Invalid configuration file %s: %v", configFile, err)
}
// try to parse arg flags, which can override file configuration.
var printHelp bool
initFlags(&printHelp)
flag.Parse()
if printHelp {
fmt.Println("----------------------------------")
flag.PrintDefaults()
fmt.Println("----------------------------------")
os.Exit(0)
}
}
/* Default configuration */
var defaultConfig = Config_{
Host: "127.0.0.1",
IP: "0.0.0.0",
Port: 8770,
Seed: "",
Db: DbConfig{
Type: "level",
Dir: AppDir + "/data",
@ -58,8 +77,9 @@ var defaultConfig = Config_{
/* Configuration types */
type Config_ struct {
Host string
IP string
Port int
Seed string
Db DbConfig
Twilio TwilioConfig
}
@ -78,8 +98,8 @@ type DbConfig struct {
}
func (cfg *Config_) validate() error {
if cfg.Host == "" {
return errors.New("Host must be set")
if cfg.IP == "" {
return errors.New("IP must be set")
}
if cfg.Port == 0 {
return errors.New("Port must be set")


+ 28
- 32
main.go View File

@ -1,6 +1,7 @@
package main
import (
"fmt"
"os"
"os/signal"
"time"
@ -94,6 +95,24 @@ func (n *Node) AddListener(l p2p.Listener) {
}()
}
// threadsafe
func (n *Node) DialPeerWithAddress(addr *p2p.NetAddress) (*p2p.Peer, error) {
log.Infof("Dialing peer @ %v", addr)
n.dialing.Set(addr.String(), addr)
n.book.MarkAttempt(addr)
conn, err := addr.DialTimeout(peerDialTimeoutSeconds * time.Second)
n.dialing.Delete(addr.String())
if err != nil {
return nil, err
}
peer, err := n.sw.AddPeerWithConnection(conn, true)
if err != nil {
return nil, err
}
n.initPeer(peer)
return peer, nil
}
// Ensures that sufficient peers are connected.
func (n *Node) ensurePeers() {
numPeers := n.sw.NumOutboundPeers()
@ -122,23 +141,7 @@ func (n *Node) ensurePeers() {
if picked == nil {
continue
}
n.dialing.Set(picked.String(), picked)
n.book.MarkAttempt(picked)
go func() {
log.Infof("Dialing addr: %v", picked)
conn, err := picked.DialTimeout(peerDialTimeoutSeconds * time.Second)
n.dialing.Delete(picked.String())
if err != nil {
// ignore error.
return
}
peer, err := n.sw.AddPeerWithConnection(conn, true)
if err != nil {
log.Warnf("Error trying to add new outbound peer connection:%v", err)
return
}
n.initPeer(peer)
}()
go n.DialPeerWithAddress(picked)
}
}
@ -171,30 +174,23 @@ func (n *Node) Stop() {
func main() {
// Create & start node
n := NewNode()
l := p2p.NewDefaultListener("tcp", ":8001")
l := p2p.NewDefaultListener("tcp", fmt.Sprintf("%v:%v", config.Config.IP, config.Config.Port))
n.AddListener(l)
n.Start()
if false {
// TODO: replace with a global list of addresses.
// 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 := n.sw.AddPeerWithConnection(conn, true)
// Seed?
if config.Config.Seed != "" {
peer, err := n.DialPeerWithAddress(p2p.NewNetAddressString(config.Config.Seed))
if err != nil {
log.Infof("Error adding peer with connection: %v", err)
log.Errorf("Error dialing seed: %v", err)
return
}
log.Infof("Connected to peer: %v", peer)
// TODO remove
log.Infof("Connected to seed: %v", peer)
}
// Sleep forever
// Sleep
trapSignal()
select {}
}


+ 2
- 0
p2p/addrbook.go View File

@ -540,6 +540,8 @@ func (a *AddrBook) expireNew(bucketIdx int) {
}
// Promotes an address from new to old.
// TODO: Move to old probabilistically.
// The better a node is, the less likely it should be evicted from an old bucket.
func (a *AddrBook) moveToOld(ka *knownAddress) {
// Sanity check
if ka.isOld() {


Loading…
Cancel
Save