-
Notifications
You must be signed in to change notification settings - Fork 0
/
gossiper.go
99 lines (80 loc) · 3.3 KB
/
gossiper.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package main
import (
"flag"
"fmt"
"log"
"net"
"os"
"strconv"
"strings"
"time"
"github.com/paulnicolet/tibcoin/gossipernode"
)
const DEFAULT_RTIMER = "60"
const DEFAULT_GUI_PORT = "8080"
func main() {
logger := log.New(os.Stderr, "[Gossiper] ", log.Ltime|log.Lshortfile)
name, uiPort, guiPort, gossipAddr, peers, rtimer, noforward, tibcoin, miner, err := parseInput(os.Args)
if err != nil {
logger.Fatal(err)
}
gossiper, err := gossipernode.NewGossiper(name, uiPort, guiPort, gossipAddr, peers, rtimer, noforward, tibcoin, miner)
if err != nil {
logger.Fatal(err)
}
err = gossiper.Start()
}
func parseInput(args []string) (string, string, string, *net.UDPAddr, []*net.UDPAddr, *time.Duration, bool, bool, bool, error) {
name := flag.String("name", "", "Gossiper's name")
uiPort := flag.String("UIPort", "", "Port listening for message from CLI client.")
guiPort := flag.String("GUIPort", "", "Port used by webserver for GUI.")
gossipPort := flag.String("gossipAddr", "", "Port or address listening for message from other gossipers.")
peers := flag.String("peers", "", "List of connected peers of the form <ip>:<port>, separated by commas.")
rtimer := flag.String("rtimer", DEFAULT_RTIMER, "Interval between two routing rumor messages in second.")
noforward := flag.Bool("noforward", false, "Indicate if the gossiper should forward rumors.")
tibcoin := flag.Bool("tibcoin", false, "Indicate if we should generate a new private/public key pair for joining the tibcoin network.")
miner := flag.Bool("miner", false, "Indicate if we should mine or not while being a node in the tibcoin network; only works if '-tibcoin' is also present.")
flag.Parse()
// Check missing flags
if *name == "" || *uiPort == "" || *gossipPort == "" {
return "", "", "", nil, nil, nil, false, false, false, fmt.Errorf("Missing command line arguments, please see -help for format specifications.")
}
// Check listening port format
if _, err := strconv.Atoi(*uiPort); err != nil {
return "", "", "", nil, nil, nil, false, false, false, fmt.Errorf("Invalid UI port number: %s", *uiPort)
}
if *guiPort == "" {
*guiPort = DEFAULT_GUI_PORT
} else if _, err := strconv.Atoi(*guiPort); err != nil {
return "", "", "", nil, nil, nil, false, false, false, fmt.Errorf("Invalid gui port number: %s", *guiPort)
}
// Convert gossipPort to address if necessary
if len(strings.Split(*gossipPort, ":")) == 1 {
*gossipPort = gossipernode.LocalIP(*gossipPort)
}
gossipAddr, err := net.ResolveUDPAddr("udp", *gossipPort)
if err != nil {
return "", "", "", nil, nil, nil, false, false, false, err
}
// Convert to duration
timerDuration, err := time.ParseDuration(*rtimer + "s")
if err != nil {
return "", "", "", nil, nil, nil, false, false, false, fmt.Errorf("Invalid duration (in second): %s", *rtimer)
}
// Create peers map
var addresses []*net.UDPAddr
if *peers != "" {
for _, node := range strings.Split(*peers, ",") {
addr, err := net.ResolveUDPAddr("udp", node)
if err != nil {
return "", "", "", nil, nil, nil, false, false, false, err
}
addresses = append(addresses, addr)
}
}
// Ensures '-miner' only if '-tibcoin'
if *miner && !*tibcoin {
return "", "", "", nil, nil, nil, false, false, false, err
}
return *name, *uiPort, *guiPort, gossipAddr, addresses, &timerDuration, *noforward, *tibcoin, *miner, nil
}