From 5c50386d772f75d1cae77a6e33011fd28a09e3b1 Mon Sep 17 00:00:00 2001 From: Fabio Barone Date: Tue, 25 Apr 2017 13:23:00 -0500 Subject: [PATCH 1/3] p2p/simulations: GetResourceIdForController for ResourceController; GET //nodes/ --- p2p/simulations/session_controller.go | 11 ++++ swarm/network/simulations/overlay.go | 76 ++++++++++++++++++--------- 2 files changed, 61 insertions(+), 26 deletions(-) diff --git a/p2p/simulations/session_controller.go b/p2p/simulations/session_controller.go index 1dadad5fddab..f8e1a97d4a61 100644 --- a/p2p/simulations/session_controller.go +++ b/p2p/simulations/session_controller.go @@ -173,6 +173,17 @@ func (self *ResourceController) SetResource(id string, c Controller) { } } +func (self *ResourceController) GetResourceIdForController(controller Controller) (string, error) { + self.lock.Lock() + defer self.lock.Unlock() + for id, c := range self.controllers { + if c == controller { + return id, nil + } + } + return "", fmt.Errorf("Id for Controller not found") +} + func (self *ResourceController) DeleteResource(id string) { delete(self.controllers, id) } diff --git a/swarm/network/simulations/overlay.go b/swarm/network/simulations/overlay.go index 0f08331133cc..3bff84fde3b8 100644 --- a/swarm/network/simulations/overlay.go +++ b/swarm/network/simulations/overlay.go @@ -9,7 +9,7 @@ import ( "fmt" "math/rand" "os" - "reflect" + // "reflect" "runtime" "time" @@ -118,7 +118,7 @@ func nethook(conf *simulations.NetworkConfig) (simulations.NetworkControl, *simu net := NewNetwork(simulations.NewNetwork(conf)) //ids := p2ptest.RandomNodeIds(10) - ids := adapters.RandomNodeIds(3) + ids := adapters.RandomNodeIds(10) for i, id := range ids { net.NewNode(&simulations.NodeConfig{Id: id}) @@ -144,42 +144,66 @@ func nethook(conf *simulations.NetworkConfig) (simulations.NetworkControl, *simu // net.Stop(id) } }() - // for i, id := range ids { - // n := 3000 + i*1000 - // go func() { - // for { - // // n := rand.Intn(5000) - // // n := 3000 - // time.Sleep(time.Duration(n) * time.Millisecond) - // log.Debug(fmt.Sprintf("node %v shutting down", id)) - // net.Stop(id) - // // n = rand.Intn(5000) - // n = 2000 - // time.Sleep(time.Duration(n) * time.Millisecond) - // log.Debug(fmt.Sprintf("node %v starting up", id)) - // net.Start(id) - // n = 5000 - // } - // }() - // } + + for i, id := range ids { + n := 3000 + i*1000 + go func(id *adapters.NodeId) { + for { + // n := rand.Intn(5000) + // n := 3000 + time.Sleep(time.Duration(n) * time.Millisecond) + log.Debug(fmt.Sprintf("node %v shutting down", id)) + net.Stop(id) + // n = rand.Intn(5000) + n = 2000 + time.Sleep(time.Duration(n) * time.Millisecond) + log.Debug(fmt.Sprintf("node %v starting up", id)) + net.Start(id) + n = 5000 + } + }(id) + } + nodes := simulations.NewResourceContoller( &simulations.ResourceHandlers{ + //GET //nodes -- returns all nodes' kademlia table Retrieve: &simulations.ResourceHandler{ Handle: func(msg interface{}, parent *simulations.ResourceController) (interface{}, error) { - ids := msg.([]string) var results []string for _, id := range ids { - if len(id) != 128 { - return nil, fmt.Errorf("Nodes controller expects 128 bytes size hex id") - } - pp := net.GetNode(adapters.NewNodeIdFromHex(id)).Adapter().(*SimNode).hive + pp := net.GetNode(id).Adapter().(*SimNode).hive results = append(results, pp.String()) } return results, nil }, - Type: reflect.TypeOf([]string{}), // this is input not output param structure + //Type: reflect.TypeOf([]string{}), // this is input not output param structure }, }) + for _, id := range ids { + idc := simulations.NewResourceContoller( + &simulations.ResourceHandlers{ + //GET //nodes/ -- returns 's kademlia table + Retrieve: &simulations.ResourceHandler{ + Handle: func(msg interface{}, parent *simulations.ResourceController) (interface{}, error) { + nodeId, err := nodes.GetResourceIdForController(parent) + if err != nil { + return nil, fmt.Errorf("Node could not be found") + } + if len(nodeId) != 128 { + return nil, fmt.Errorf("Node length must be 128") + } + pp := net.GetNode(adapters.NewNodeIdFromHex(nodeId)).Adapter().(*SimNode).hive + if pp != nil { + return pp.String(), nil + } + //this shouldn't happen anymore, but just in case + return nil, fmt.Errorf("Node not found") + }, + //Type: reflect.TypeOf([]string{}), // this is input not output param structure + }, + }) + nodes.SetResource(id.String(), idc) + } return net, nodes } From c0b16731c256eff9a7c13e2dfbd9a55d2c7842b6 Mon Sep 17 00:00:00 2001 From: Fabio Barone Date: Tue, 25 Apr 2017 16:29:52 -0500 Subject: [PATCH 2/3] p2p/simulations: renamed all occurrence of cytoscape --- p2p/simulations/network.go | 18 +++++----- p2p/simulations/session_controller_test.go | 2 +- .../{cytoscape.go => sim_events.go} | 36 +++++++++---------- 3 files changed, 28 insertions(+), 28 deletions(-) rename p2p/simulations/{cytoscape.go => sim_events.go} (72%) diff --git a/p2p/simulations/network.go b/p2p/simulations/network.go index 1cc481c2443d..e1254bb97bbb 100644 --- a/p2p/simulations/network.go +++ b/p2p/simulations/network.go @@ -66,7 +66,7 @@ type NetworkController struct { } // ServeStream subscribes to network events and sends them to the client as a -// stream of Server-Sent-Events, with each event being a JSON encoded CyUpdate +// stream of Server-Sent-Events, with each event being a JSON encoded SimUpdate // object func (n *NetworkController) ServeStream(w http.ResponseWriter, req *http.Request) { sub := n.events.Subscribe(ConnectivityEvents...) @@ -96,8 +96,8 @@ func (n *NetworkController) ServeStream(w http.ResponseWriter, req *http.Request for { select { case event := <-ch: - // convert the event to a CyUpdate - update, err := NewCyUpdate(event) + // convert the event to a SimUpdate + update, err := NewSimUpdate(event) if err != nil { write("error", err.Error()) return @@ -107,7 +107,7 @@ func (n *NetworkController) ServeStream(w http.ResponseWriter, req *http.Request write("error", err.Error()) return } - write("cyupdate", string(data)) + write("simupdate", string(data)) case <-clientGone: return } @@ -138,17 +138,17 @@ func NewNetworkController(net NetworkControl, nodesController *ResourceControlle Retrieve: &ResourceHandler{ Handle: func(msg interface{}, parent *ResourceController) (interface{}, error) { log.Trace(fmt.Sprintf("msg: %v", msg)) - cyConfig, ok := msg.(*CyConfig) + simConfig, ok := msg.(*SimConfig) if ok { - return UpdateCy(cyConfig, journal) + return UpdateSim(simConfig, journal) } snapshotConfig, ok := msg.(*SnapshotConfig) if ok { return Snapshot(snapshotConfig, journal) } - return nil, fmt.Errorf("invalid json body: must be CyConfig or SnapshotConfig") + return nil, fmt.Errorf("invalid json body: must be SimConfig or SnapshotConfig") }, - Type: reflect.TypeOf(&CyConfig{}), + Type: reflect.TypeOf(&SimConfig{}), }, // DELETE // Destroy: &ResourceHandler{ @@ -441,7 +441,7 @@ type NodeConfig struct { // TODO: ignored for now type QueryConfig struct { - Format string // "cy.update", "journal", + Format string // "sim.update", "journal", } type Know struct { diff --git a/p2p/simulations/session_controller_test.go b/p2p/simulations/session_controller_test.go index 92312fe40110..c7e3d120f720 100644 --- a/p2p/simulations/session_controller_test.go +++ b/p2p/simulations/session_controller_test.go @@ -153,7 +153,7 @@ func TestUpdate(t *testing.T) { "remove": [], "message": [] }` - s, _ := json.Marshal(&CyConfig{}) + s, _ := json.Marshal(&SimConfig{}) resp := testResponse(t, "GET", url(port, "0"), bytes.NewReader(s)) if string(resp) != exp { t.Fatalf("incorrect response body. got\n'%v', expected\n'%v'", string(resp), exp) diff --git a/p2p/simulations/cytoscape.go b/p2p/simulations/sim_events.go similarity index 72% rename from p2p/simulations/cytoscape.go rename to p2p/simulations/sim_events.go index 37f3fe795657..1a472ddff46d 100644 --- a/p2p/simulations/cytoscape.go +++ b/p2p/simulations/sim_events.go @@ -6,19 +6,19 @@ import ( "github.com/ethereum/go-ethereum/event" ) -// TODO: to implement cytoscape global behav -type CyConfig struct { +// TODO: to implement simulation global behav +type SimConfig struct { } -type CyData struct { +type SimData struct { Id string `json:"id"` Source string `json:"source,omitempty"` Target string `json:"target,omitempty"` Up bool `json:"up"` } -type CyElement struct { - Data *CyData `json:"data"` +type SimElement struct { + Data *SimData `json:"data"` Classes string `json:"classes,omitempty"` Group string `json:"group"` // selected: false, // whether the element is selected (default false) @@ -27,19 +27,19 @@ type CyElement struct { // grabbable: true, // whether the node can be grabbed and moved by the user } -type CyUpdate struct { - Add []*CyElement `json:"add"` - Remove []*CyElement `json:"remove"` - Message []*CyElement `json:"message"` +type SimUpdate struct { + Add []*SimElement `json:"add"` + Remove []*SimElement `json:"remove"` + Message []*SimElement `json:"message"` } -func NewCyUpdate(e *event.TypeMuxEvent) (*CyUpdate, error) { - var update CyUpdate - var el *CyElement +func NewSimUpdate(e *event.TypeMuxEvent) (*SimUpdate, error) { + var update SimUpdate + var el *SimElement entry := e.Data var action string if ev, ok := entry.(*NodeEvent); ok { - el = &CyElement{Group: "nodes", Data: &CyData{Id: ev.node.Id.String()}} + el = &SimElement{Group: "nodes", Data: &SimData{Id: ev.node.Id.String()}} action = ev.Action } else if ev, ok := entry.(*MsgEvent); ok { msg := ev.msg @@ -47,7 +47,7 @@ func NewCyUpdate(e *event.TypeMuxEvent) (*CyUpdate, error) { var source, target string source = msg.One.String() target = msg.Other.String() - el = &CyElement{Group: "msgs", Data: &CyData{Id: id, Source: source, Target: target}} + el = &SimElement{Group: "msgs", Data: &SimData{Id: id, Source: source, Target: target}} action = ev.Action } else if ev, ok := entry.(*ConnEvent); ok { // mutually exclusive directed edge (caller -> callee) @@ -61,7 +61,7 @@ func NewCyUpdate(e *event.TypeMuxEvent) (*CyUpdate, error) { source = conn.One.String() target = conn.Other.String() } - el = &CyElement{Group: "edges", Data: &CyData{Id: id, Source: source, Target: target}} + el = &SimElement{Group: "edges", Data: &SimData{Id: id, Source: source, Target: target}} action = ev.Action } else { return nil, fmt.Errorf("unknown event type: %T", entry) @@ -84,10 +84,10 @@ func NewCyUpdate(e *event.TypeMuxEvent) (*CyUpdate, error) { return &update, nil } -func UpdateCy(conf *CyConfig, j *Journal) (*CyUpdate, error) { - var update CyUpdate +func UpdateSim(conf *SimConfig, j *Journal) (*SimUpdate, error) { + var update SimUpdate j.Read(func(e *event.TypeMuxEvent) bool { - u, err := NewCyUpdate(e) + u, err := NewSimUpdate(e) if err != nil { panic(err.Error()) } From fdb6611d386354c0e966a077edc137da675d9053 Mon Sep 17 00:00:00 2001 From: Fabio Barone Date: Wed, 26 Apr 2017 12:04:37 -0500 Subject: [PATCH 3/3] p2p/simulations: removed GetResourceIdFromController --- p2p/simulations/session_controller.go | 11 ----------- swarm/network/simulations/overlay.go | 9 +-------- 2 files changed, 1 insertion(+), 19 deletions(-) diff --git a/p2p/simulations/session_controller.go b/p2p/simulations/session_controller.go index f8e1a97d4a61..1dadad5fddab 100644 --- a/p2p/simulations/session_controller.go +++ b/p2p/simulations/session_controller.go @@ -173,17 +173,6 @@ func (self *ResourceController) SetResource(id string, c Controller) { } } -func (self *ResourceController) GetResourceIdForController(controller Controller) (string, error) { - self.lock.Lock() - defer self.lock.Unlock() - for id, c := range self.controllers { - if c == controller { - return id, nil - } - } - return "", fmt.Errorf("Id for Controller not found") -} - func (self *ResourceController) DeleteResource(id string) { delete(self.controllers, id) } diff --git a/swarm/network/simulations/overlay.go b/swarm/network/simulations/overlay.go index 3bff84fde3b8..fb3c211f0e92 100644 --- a/swarm/network/simulations/overlay.go +++ b/swarm/network/simulations/overlay.go @@ -185,14 +185,7 @@ func nethook(conf *simulations.NetworkConfig) (simulations.NetworkControl, *simu //GET //nodes/ -- returns 's kademlia table Retrieve: &simulations.ResourceHandler{ Handle: func(msg interface{}, parent *simulations.ResourceController) (interface{}, error) { - nodeId, err := nodes.GetResourceIdForController(parent) - if err != nil { - return nil, fmt.Errorf("Node could not be found") - } - if len(nodeId) != 128 { - return nil, fmt.Errorf("Node length must be 128") - } - pp := net.GetNode(adapters.NewNodeIdFromHex(nodeId)).Adapter().(*SimNode).hive + pp := net.GetNode(id).Adapter().(*SimNode).hive if pp != nil { return pp.String(), nil }