diff options
author | Janos Guljas <janos@resenje.org> | 2017-12-14 17:35:49 +0800 |
---|---|---|
committer | Janos Guljas <janos@resenje.org> | 2017-12-14 17:36:12 +0800 |
commit | 47a801455966298d1d1519eebb955024c8f02b84 (patch) | |
tree | 5b8144ba4844092dc0bae599fc3314eaacf1a7b4 /p2p/simulations/http.go | |
parent | 19982f946735948478b6b7e7706f1b615f171d0d (diff) | |
parent | 3654aeaa4f87452ac5bc801a18808189595e2ef8 (diff) | |
download | dexon-47a801455966298d1d1519eebb955024c8f02b84.tar dexon-47a801455966298d1d1519eebb955024c8f02b84.tar.gz dexon-47a801455966298d1d1519eebb955024c8f02b84.tar.bz2 dexon-47a801455966298d1d1519eebb955024c8f02b84.tar.lz dexon-47a801455966298d1d1519eebb955024c8f02b84.tar.xz dexon-47a801455966298d1d1519eebb955024c8f02b84.tar.zst dexon-47a801455966298d1d1519eebb955024c8f02b84.zip |
cmd/swarm: Merge branch 'master' into multiple-ens-endpoints
Fix a conflict in cmd/swarm envVarsOverride function.
Diffstat (limited to 'p2p/simulations/http.go')
-rw-r--r-- | p2p/simulations/http.go | 64 |
1 files changed, 62 insertions, 2 deletions
diff --git a/p2p/simulations/http.go b/p2p/simulations/http.go index 3fa8b9292..97dd742e8 100644 --- a/p2p/simulations/http.go +++ b/p2p/simulations/http.go @@ -27,6 +27,7 @@ import ( "net/http" "strconv" "strings" + "sync" "github.com/ethereum/go-ethereum/event" "github.com/ethereum/go-ethereum/p2p" @@ -263,8 +264,10 @@ func (c *Client) Send(method, path string, in, out interface{}) error { // Server is an HTTP server providing an API to manage a simulation network type Server struct { - router *httprouter.Router - network *Network + router *httprouter.Router + network *Network + mockerStop chan struct{} // when set, stops the current mocker + mockerMtx sync.Mutex // synchronises access to the mockerStop field } // NewServer returns a new simulation API server @@ -278,6 +281,10 @@ func NewServer(network *Network) *Server { s.GET("/", s.GetNetwork) s.POST("/start", s.StartNetwork) s.POST("/stop", s.StopNetwork) + s.POST("/mocker/start", s.StartMocker) + s.POST("/mocker/stop", s.StopMocker) + s.GET("/mocker", s.GetMockers) + s.POST("/reset", s.ResetNetwork) s.GET("/events", s.StreamNetworkEvents) s.GET("/snapshot", s.CreateSnapshot) s.POST("/snapshot", s.LoadSnapshot) @@ -318,6 +325,59 @@ func (s *Server) StopNetwork(w http.ResponseWriter, req *http.Request) { w.WriteHeader(http.StatusOK) } +// StartMocker starts the mocker node simulation +func (s *Server) StartMocker(w http.ResponseWriter, req *http.Request) { + s.mockerMtx.Lock() + defer s.mockerMtx.Unlock() + if s.mockerStop != nil { + http.Error(w, "mocker already running", http.StatusInternalServerError) + return + } + mockerType := req.FormValue("mocker-type") + mockerFn := LookupMocker(mockerType) + if mockerFn == nil { + http.Error(w, fmt.Sprintf("unknown mocker type %q", mockerType), http.StatusBadRequest) + return + } + nodeCount, err := strconv.Atoi(req.FormValue("node-count")) + if err != nil { + http.Error(w, "invalid node-count provided", http.StatusBadRequest) + return + } + s.mockerStop = make(chan struct{}) + go mockerFn(s.network, s.mockerStop, nodeCount) + + w.WriteHeader(http.StatusOK) +} + +// StopMocker stops the mocker node simulation +func (s *Server) StopMocker(w http.ResponseWriter, req *http.Request) { + s.mockerMtx.Lock() + defer s.mockerMtx.Unlock() + if s.mockerStop == nil { + http.Error(w, "stop channel not initialized", http.StatusInternalServerError) + return + } + close(s.mockerStop) + s.mockerStop = nil + + w.WriteHeader(http.StatusOK) +} + +// GetMockerList returns a list of available mockers +func (s *Server) GetMockers(w http.ResponseWriter, req *http.Request) { + + list := GetMockerList() + s.JSON(w, http.StatusOK, list) +} + +// ResetNetwork resets all properties of a network to its initial (empty) state +func (s *Server) ResetNetwork(w http.ResponseWriter, req *http.Request) { + s.network.Reset() + + w.WriteHeader(http.StatusOK) +} + // StreamNetworkEvents streams network events as a server-sent-events stream func (s *Server) StreamNetworkEvents(w http.ResponseWriter, req *http.Request) { events := make(chan *Event) |