diff --git a/p2p/simulations/http.go b/p2p/simulations/http.go index acc3888b1a..a0f30c7460 100644 --- a/p2p/simulations/http.go +++ b/p2p/simulations/http.go @@ -59,6 +59,21 @@ func (c *Client) GetNetwork(networkID string) (*Network, error) { return network, c.Get(fmt.Sprintf("/networks/%s", networkID), network) } +// StartNetwork starts all existing nodes in a simulation network +func (c *Client) StartNetwork(networkID string) error { + return c.Post(fmt.Sprintf("/networks/%s/start", networkID), nil, nil) +} + +// StopNetwork stops all existing nodes in a simulation network +func (c *Client) StopNetwork(networkID string) error { + return c.Post(fmt.Sprintf("/networks/%s/stop", networkID), nil, nil) +} + +// DeleteNetwork stops and deletes a simulation network +func (c *Client) DeleteNetwork(networkID string) error { + return c.Delete(fmt.Sprintf("/networks/%s", networkID)) +} + // CreateSnapshot creates a network snapshot func (c *Client) CreateSnapshot(networkID string) (*Snapshot, error) { snap := &Snapshot{} @@ -276,6 +291,9 @@ func NewServer(config *ServerConfig) *Server { s.POST("/networks", s.CreateNetwork) s.GET("/networks", s.GetNetworks) s.GET("/networks/:netid", s.GetNetwork) + s.POST("/networks/:netid/start", s.StartNetwork) + s.POST("/networks/:netid/stop", s.StopNetwork) + s.DELETE("/networks/:netid", s.DeleteNetwork) s.GET("/networks/:netid/events", s.StreamNetworkEvents) s.GET("/networks/:netid/snapshot", s.CreateSnapshot) s.POST("/networks/:netid/snapshot", s.LoadSnapshot) @@ -341,6 +359,46 @@ func (s *Server) GetNetwork(w http.ResponseWriter, req *http.Request) { s.JSON(w, http.StatusOK, network) } +// StartNetwork starts all nodes in a network +func (s *Server) StartNetwork(w http.ResponseWriter, req *http.Request) { + network := req.Context().Value("network").(*Network) + + if err := network.StartAll(); err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + + w.WriteHeader(http.StatusOK) +} + +// StopNetwork stops all nodes in a network +func (s *Server) StopNetwork(w http.ResponseWriter, req *http.Request) { + network := req.Context().Value("network").(*Network) + + if err := network.StopAll(); err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + + w.WriteHeader(http.StatusOK) +} + +// DeleteNetwork stops all nodes in a network and deletes it +func (s *Server) DeleteNetwork(w http.ResponseWriter, req *http.Request) { + network := req.Context().Value("network").(*Network) + + if err := network.StopAll(); err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + + s.mtx.Lock() + delete(s.networks, network.Id) + s.mtx.Unlock() + + w.WriteHeader(http.StatusOK) +} + //Get the info for a particular mocker func (s *Server) GetMocker(w http.ResponseWriter, req *http.Request) { m := make(map[string]string) diff --git a/p2p/simulations/network.go b/p2p/simulations/network.go index 4254ff4ba0..77cd4b9cbd 100644 --- a/p2p/simulations/network.go +++ b/p2p/simulations/network.go @@ -260,6 +260,30 @@ func (self *Conn) nodesUp() error { return nil } +func (self *Network) StartAll() error { + for _, node := range self.Nodes { + if node.Up { + continue + } + if err := self.Start(node.ID()); err != nil { + return err + } + } + return nil +} + +func (self *Network) StopAll() error { + for _, node := range self.Nodes { + if !node.Up { + continue + } + if err := self.Stop(node.ID()); err != nil { + return err + } + } + return nil +} + // Start(id) starts up the node (relevant only for instance with own p2p or remote) func (self *Network) Start(id *adapters.NodeId) error { return self.startWithSnapshot(id, nil)