aboutsummaryrefslogtreecommitdiffstats
path: root/p2p/simulations/examples/ping-pong.go
blob: 597cc950c9796fd5c7e44501dc92b221a1385d8e (plain) (blame)
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
// Copyright 2017 The go-ethereum Authors
// This file is part of the go-ethereum library.
//
// The go-ethereum library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The go-ethereum library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.

package main

import (
    "flag"
    "fmt"
    "io/ioutil"
    "net/http"
    "os"
    "sync/atomic"
    "time"

    "github.com/ethereum/go-ethereum/log"
    "github.com/ethereum/go-ethereum/node"
    "github.com/ethereum/go-ethereum/p2p"
    "github.com/ethereum/go-ethereum/p2p/enode"
    "github.com/ethereum/go-ethereum/p2p/simulations"
    "github.com/ethereum/go-ethereum/p2p/simulations/adapters"
    "github.com/ethereum/go-ethereum/rpc"
)

var adapterType = flag.String("adapter", "sim", `node adapter to use (one of "sim", "exec" or "docker")`)

// main() starts a simulation network which contains nodes running a simple
// ping-pong protocol
func main() {
    flag.Parse()

    // set the log level to Trace
    log.Root().SetHandler(log.LvlFilterHandler(log.LvlTrace, log.StreamHandler(os.Stderr, log.TerminalFormat(false))))

    // register a single ping-pong service
    services := map[string]adapters.ServiceFunc{
        "ping-pong": func(ctx *adapters.ServiceContext) (node.Service, error) {
            return newPingPongService(ctx.Config.ID), nil
        },
    }
    adapters.RegisterServices(services)

    // create the NodeAdapter
    var adapter adapters.NodeAdapter

    switch *adapterType {

    case "sim":
        log.Info("using sim adapter")
        adapter = adapters.NewSimAdapter(services)

    case "exec":
        tmpdir, err := ioutil.TempDir("", "p2p-example")
        if err != nil {
            log.Crit("error creating temp dir", "err", err)
        }
        defer os.RemoveAll(tmpdir)
        log.Info("using exec adapter", "tmpdir", tmpdir)
        adapter = adapters.NewExecAdapter(tmpdir)

    case "docker":
        log.Info("using docker adapter")
        var err error
        adapter, err = adapters.NewDockerAdapter()
        if err != nil {
            log.Crit("error creating docker adapter", "err", err)
        }

    default:
        log.Crit(fmt.Sprintf("unknown node adapter %q", *adapterType))
    }

    // start the HTTP API
    log.Info("starting simulation server on 0.0.0.0:8888...")
    network := simulations.NewNetwork(adapter, &simulations.NetworkConfig{
        DefaultService: "ping-pong",
    })
    if err := http.ListenAndServe(":8888", simulations.NewServer(network)); err != nil {
        log.Crit("error starting simulation server", "err", err)
    }
}

// pingPongService runs a ping-pong protocol between nodes where each node
// sends a ping to all its connected peers every 10s and receives a pong in
// return
type pingPongService struct {
    id       enode.ID
    log      log.Logger
    received int64
}

func newPingPongService(id enode.ID) *pingPongService {
    return &pingPongService{
        id:  id,
        log: log.New("node.id", id),
    }
}

func (p *pingPongService) Protocols() []p2p.Protocol {
    return []p2p.Protocol{{
        Name:     "ping-pong",
        Version:  1,
        Length:   2,
        Run:      p.Run,
        NodeInfo: p.Info,
    }}
}

func (p *pingPongService) APIs() []rpc.API {
    return nil
}

func (p *pingPongService) Start(server *p2p.Server) error {
    p.log.Info("ping-pong service starting")
    return nil
}

func (p *pingPongService) Stop() error {
    p.log.Info("ping-pong service stopping")
    return nil
}

func (p *pingPongService) Info() interface{} {
    return struct {
        Received int64 `json:"received"`
    }{
        atomic.LoadInt64(&p.received),
    }
}

const (
    pingMsgCode = iota
    pongMsgCode
)

// Run implements the ping-pong protocol which sends ping messages to the peer
// at 10s intervals, and responds to pings with pong messages.
func (p *pingPongService) Run(peer *p2p.Peer, rw p2p.MsgReadWriter) error {
    log := p.log.New("peer.id", peer.ID())

    errC := make(chan error)
    go func() {
        for range time.Tick(10 * time.Second) {
            log.Info("sending ping")
            if err := p2p.Send(rw, pingMsgCode, "PING"); err != nil {
                errC <- err
                return
            }
        }
    }()
    go func() {
        for {
            msg, err := rw.ReadMsg()
            if err != nil {
                errC <- err
                return
            }
            payload, err := ioutil.ReadAll(msg.Payload)
            if err != nil {
                errC <- err
                return
            }
            log.Info("received message", "msg.code", msg.Code, "msg.payload", string(payload))
            atomic.AddInt64(&p.received, 1)
            if msg.Code == pingMsgCode {
                log.Info("sending pong")
                go p2p.Send(rw, pongMsgCode, "PONG")
            }
        }
    }()
    return <-errC
}