Skip to content

Commit

Permalink
Merge pull request ethereum#72 from holisticode/rename_cy
Browse files Browse the repository at this point in the history
Rename cytoscape
  • Loading branch information
zelig authored Apr 27, 2017
2 parents fb458ff + fdb6611 commit 5c003a3
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 54 deletions.
18 changes: 9 additions & 9 deletions p2p/simulations/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -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...)
Expand Down Expand Up @@ -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
Expand All @@ -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
}
Expand Down Expand Up @@ -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 /<networkId>/
Destroy: &ResourceHandler{
Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion p2p/simulations/session_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
36 changes: 18 additions & 18 deletions p2p/simulations/cytoscape.go → p2p/simulations/sim_events.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -27,27 +27,27 @@ 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
id := ConnLabel(msg.One, msg.Other)
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)
Expand All @@ -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)
Expand All @@ -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())
}
Expand Down
69 changes: 43 additions & 26 deletions swarm/network/simulations/overlay.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"fmt"
"math/rand"
"os"
"reflect"
// "reflect"
"runtime"
"time"

Expand Down Expand Up @@ -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})
Expand All @@ -144,42 +144,59 @@ 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 /<networkId>/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 /<networkId>/nodes/<nodeId> -- returns <nodeId>'s kademlia table
Retrieve: &simulations.ResourceHandler{
Handle: func(msg interface{}, parent *simulations.ResourceController) (interface{}, error) {
pp := net.GetNode(id).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
}

Expand Down

0 comments on commit 5c003a3

Please sign in to comment.