aboutsummaryrefslogtreecommitdiffstats
path: root/simulation/peer-server.go
blob: 90bcf3391c75dd6f5a4cd7cbb2c92c68dccad2cd (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
// Copyright 2018 The dexon-consensus-core Authors
// This file is part of the dexon-consensus-core library.
//
// The dexon-consensus-core 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 dexon-consensus-core 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 dexon-consensus-core library. If not, see
// <http://www.gnu.org/licenses/>.

package simulation

import (
    "encoding/json"
    "fmt"
    "log"
    "net"
    "net/http"
    "sync"

    "github.com/dexon-foundation/dexon-consensus-core/core/types"
    "github.com/dexon-foundation/dexon-consensus-core/simulation/config"
)

// PeerServer is the main object for maintaining peer list.
type PeerServer struct {
    peers   map[types.ValidatorID]string
    peersMu sync.Mutex
}

// NewPeerServer returns a new peer server.
func NewPeerServer() *PeerServer {
    return &PeerServer{
        peers: make(map[types.ValidatorID]string),
    }
}

// Run starts the peer server.
func (p *PeerServer) Run(configPath string) {
    cfg, err := config.Read(configPath)
    if err != nil {
        panic(err)
    }

    resetHandler := func(w http.ResponseWriter, r *http.Request) {
        p.peersMu.Lock()
        defer p.peersMu.Unlock()

        p.peers = make(map[types.ValidatorID]string)
        log.Printf("Peer server has been reset.")
    }

    joinHandler := func(w http.ResponseWriter, r *http.Request) {
        idString := r.Header.Get("ID")
        portString := r.Header.Get("PORT")

        id := types.ValidatorID{}
        id.UnmarshalText([]byte(idString))

        p.peersMu.Lock()
        defer p.peersMu.Unlock()

        host, _, _ := net.SplitHostPort(r.RemoteAddr)
        p.peers[id] = net.JoinHostPort(host, portString)
        log.Printf("Peer %s joined from %s", id, p.peers[id])
    }

    peersHandler := func(w http.ResponseWriter, r *http.Request) {
        p.peersMu.Lock()
        defer p.peersMu.Unlock()

        if len(p.peers) != cfg.Validator.Num {
            w.WriteHeader(http.StatusNotFound)
            return
        }

        jsonText, err := json.Marshal(p.peers)
        if err != nil {
            w.WriteHeader(http.StatusInternalServerError)
            return
        }

        w.Header().Set("Content-Type", "application/json")
        w.Write(jsonText)
    }

    infoHandler := func(w http.ResponseWriter, r *http.Request) {
        p.peersMu.Lock()
        defer p.peersMu.Unlock()

        jsonText, err := json.Marshal(p.peers)
        if err != nil {
            w.WriteHeader(http.StatusInternalServerError)
            return
        }

        w.Header().Set("Content-Type", "application/json")
        w.Write(jsonText)
    }

    http.HandleFunc("/reset", resetHandler)
    http.HandleFunc("/join", joinHandler)
    http.HandleFunc("/peers", peersHandler)
    http.HandleFunc("/info", infoHandler)

    addr := fmt.Sprintf("0.0.0.0:%d", peerPort)
    log.Printf("Peer server started at %s", addr)

    http.ListenAndServe(addr, nil)
}