/
main.go
60 lines (49 loc) · 1.18 KB
/
main.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
package main
import (
"flag"
"fmt"
"time"
"github.com/465583030/dagchain"
)
var laddr = flag.String("l", "", "")
var saddr = flag.String("s", "", "")
var messaging = flag.Bool("m", false, "")
var id = flag.String("i", "", "")
func main() {
flag.Parse()
send := make(chan interface{}, 1)
recv := make(chan interface{}, 1)
// start the p2p node
go func() {
err := dagchain.StartNode(*laddr, *saddr, send, recv)
if err != nil {
panic("node start panic:" + err.Error())
}
}()
// wait 1 second for p2p node started
time.Sleep(1 * time.Second)
// query 10 nodes address from p2p network
addrs := dagchain.QueryNodes(10)
// excute some actions with the nodes address,like downloads blockchain from these nodes
// all depends on yourself
fmt.Println("query nodes:", addrs)
// send message to all the other nodes
if *messaging {
data := "hello-" + *id
go func() {
for {
send <- data
time.Sleep(5 * time.Second)
fmt.Println("send message:", data)
}
}()
}
// receive message from other nodes
for {
select {
case r := <-recv:
res := r.(*dagchain.Request)
fmt.Printf("receive message: %v from other node: \"%s\" \n", res.Data, res.From)
}
}
}