mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-12 15:03:45 +00:00
p2p/simulations: GetResourceIdForController for ResourceController; GET /<networkId>/nodes/<nodeId>
This commit is contained in:
parent
9fc8097768
commit
f02b408dd2
2 changed files with 61 additions and 26 deletions
|
|
@ -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) {
|
func (self *ResourceController) DeleteResource(id string) {
|
||||||
delete(self.controllers, id)
|
delete(self.controllers, id)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -9,7 +9,7 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
"os"
|
"os"
|
||||||
"reflect"
|
// "reflect"
|
||||||
"runtime"
|
"runtime"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
|
@ -118,7 +118,7 @@ func nethook(conf *simulations.NetworkConfig) (simulations.NetworkControl, *simu
|
||||||
net := NewNetwork(simulations.NewNetwork(conf))
|
net := NewNetwork(simulations.NewNetwork(conf))
|
||||||
|
|
||||||
//ids := p2ptest.RandomNodeIds(10)
|
//ids := p2ptest.RandomNodeIds(10)
|
||||||
ids := adapters.RandomNodeIds(3)
|
ids := adapters.RandomNodeIds(10)
|
||||||
|
|
||||||
for i, id := range ids {
|
for i, id := range ids {
|
||||||
net.NewNode(&simulations.NodeConfig{Id: id})
|
net.NewNode(&simulations.NodeConfig{Id: id})
|
||||||
|
|
@ -144,42 +144,66 @@ func nethook(conf *simulations.NetworkConfig) (simulations.NetworkControl, *simu
|
||||||
// net.Stop(id)
|
// net.Stop(id)
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
// for i, id := range ids {
|
|
||||||
// n := 3000 + i*1000
|
for i, id := range ids {
|
||||||
// go func() {
|
n := 3000 + i*1000
|
||||||
// for {
|
go func(id *adapters.NodeId) {
|
||||||
// // n := rand.Intn(5000)
|
for {
|
||||||
// // n := 3000
|
// n := rand.Intn(5000)
|
||||||
// time.Sleep(time.Duration(n) * time.Millisecond)
|
// n := 3000
|
||||||
// log.Debug(fmt.Sprintf("node %v shutting down", id))
|
time.Sleep(time.Duration(n) * time.Millisecond)
|
||||||
// net.Stop(id)
|
log.Debug(fmt.Sprintf("node %v shutting down", id))
|
||||||
// // n = rand.Intn(5000)
|
net.Stop(id)
|
||||||
// n = 2000
|
// n = rand.Intn(5000)
|
||||||
// time.Sleep(time.Duration(n) * time.Millisecond)
|
n = 2000
|
||||||
// log.Debug(fmt.Sprintf("node %v starting up", id))
|
time.Sleep(time.Duration(n) * time.Millisecond)
|
||||||
// net.Start(id)
|
log.Debug(fmt.Sprintf("node %v starting up", id))
|
||||||
// n = 5000
|
net.Start(id)
|
||||||
// }
|
n = 5000
|
||||||
// }()
|
}
|
||||||
// }
|
}(id)
|
||||||
|
}
|
||||||
|
|
||||||
nodes := simulations.NewResourceContoller(
|
nodes := simulations.NewResourceContoller(
|
||||||
&simulations.ResourceHandlers{
|
&simulations.ResourceHandlers{
|
||||||
|
//GET /<networkId>/nodes -- returns all nodes' kademlia table
|
||||||
Retrieve: &simulations.ResourceHandler{
|
Retrieve: &simulations.ResourceHandler{
|
||||||
Handle: func(msg interface{}, parent *simulations.ResourceController) (interface{}, error) {
|
Handle: func(msg interface{}, parent *simulations.ResourceController) (interface{}, error) {
|
||||||
ids := msg.([]string)
|
|
||||||
var results []string
|
var results []string
|
||||||
for _, id := range ids {
|
for _, id := range ids {
|
||||||
if len(id) != 128 {
|
pp := net.GetNode(id).Adapter().(*SimNode).hive
|
||||||
return nil, fmt.Errorf("Nodes controller expects 128 bytes size hex id")
|
|
||||||
}
|
|
||||||
pp := net.GetNode(adapters.NewNodeIdFromHex(id)).Adapter().(*SimNode).hive
|
|
||||||
results = append(results, pp.String())
|
results = append(results, pp.String())
|
||||||
}
|
}
|
||||||
return results, nil
|
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) {
|
||||||
|
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
|
return net, nodes
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue