mirror of
https://github.com/vacp2p/mvds.git
synced 2026-01-09 12:07:55 -05:00
split channel
This commit is contained in:
40
main.go
40
main.go
@@ -4,16 +4,13 @@ import (
|
|||||||
"crypto/ecdsa"
|
"crypto/ecdsa"
|
||||||
"crypto/elliptic"
|
"crypto/elliptic"
|
||||||
"crypto/rand"
|
"crypto/rand"
|
||||||
"errors"
|
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
math "math/rand"
|
math "math/rand"
|
||||||
"sync"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/status-im/mvds/node"
|
"github.com/status-im/mvds/node"
|
||||||
"github.com/status-im/mvds/protobuf"
|
|
||||||
"github.com/status-im/mvds/state"
|
"github.com/status-im/mvds/state"
|
||||||
"github.com/status-im/mvds/store"
|
"github.com/status-im/mvds/store"
|
||||||
"github.com/status-im/mvds/transport"
|
"github.com/status-im/mvds/transport"
|
||||||
@@ -28,32 +25,6 @@ var (
|
|||||||
interactive int
|
interactive int
|
||||||
)
|
)
|
||||||
|
|
||||||
type Transport struct {
|
|
||||||
sync.Mutex
|
|
||||||
|
|
||||||
in <-chan transport.Packet
|
|
||||||
out map[state.PeerID]chan<- transport.Packet
|
|
||||||
}
|
|
||||||
|
|
||||||
func (t *Transport) Watch() transport.Packet {
|
|
||||||
return <-t.in
|
|
||||||
}
|
|
||||||
|
|
||||||
func (t *Transport) Send(group state.GroupID, sender state.PeerID, peer state.PeerID, payload protobuf.Payload) error {
|
|
||||||
math.Seed(time.Now().UnixNano())
|
|
||||||
if math.Intn(100) < offline {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
c, ok := t.out[peer]
|
|
||||||
if !ok {
|
|
||||||
return errors.New("peer unknown")
|
|
||||||
}
|
|
||||||
|
|
||||||
c <- transport.Packet{Group: group, Sender: sender, Payload: payload}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
flag.IntVar(&offline, "offline", 90, "percentage of time a node is offline")
|
flag.IntVar(&offline, "offline", 90, "percentage of time a node is offline")
|
||||||
flag.IntVar(&nodeCount, "nodes", 3, "amount of nodes")
|
flag.IntVar(&nodeCount, "nodes", 3, "amount of nodes")
|
||||||
@@ -68,16 +39,13 @@ func main() {
|
|||||||
|
|
||||||
// @todo validate flags
|
// @todo validate flags
|
||||||
|
|
||||||
transports := make([]*Transport, 0)
|
transports := make([]*transport.ChannelTransport, 0)
|
||||||
input := make([]chan transport.Packet, 0)
|
input := make([]chan transport.Packet, 0)
|
||||||
nodes := make([]*node.Node, 0)
|
nodes := make([]*node.Node, 0)
|
||||||
for i := 0; i < nodeCount; i++ {
|
for i := 0; i < nodeCount; i++ {
|
||||||
in := make(chan transport.Packet)
|
in := make(chan transport.Packet)
|
||||||
|
|
||||||
t := &Transport{
|
t := transport.NewChannelTransport(offline, in)
|
||||||
in: in,
|
|
||||||
out: make(map[state.PeerID]chan<- transport.Packet),
|
|
||||||
}
|
|
||||||
|
|
||||||
input = append(input, in)
|
input = append(input, in)
|
||||||
transports = append(transports, t)
|
transports = append(transports, t)
|
||||||
@@ -102,7 +70,7 @@ func main() {
|
|||||||
for _, p := range peers {
|
for _, p := range peers {
|
||||||
peer := nodes[p].ID
|
peer := nodes[p].ID
|
||||||
|
|
||||||
transports[i].out[peer] = input[p]
|
transports[i].AddOutput(peer, input[p])
|
||||||
n.AddPeer(group, peer)
|
n.AddPeer(group, peer)
|
||||||
|
|
||||||
log.Printf("%x sharing with %x", n.ID[:4], peer[:4])
|
log.Printf("%x sharing with %x", n.ID[:4], peer[:4])
|
||||||
@@ -139,7 +107,7 @@ OUTER:
|
|||||||
return peers
|
return peers
|
||||||
}
|
}
|
||||||
|
|
||||||
func createNode(transport *Transport, id state.PeerID, mode node.Mode) *node.Node {
|
func createNode(transport transport.Transport, id state.PeerID, mode node.Mode) *node.Node {
|
||||||
ds := store.NewDummyStore()
|
ds := store.NewDummyStore()
|
||||||
return node.NewNode(
|
return node.NewNode(
|
||||||
&ds,
|
&ds,
|
||||||
|
|||||||
52
transport/channel_transport.go
Normal file
52
transport/channel_transport.go
Normal file
@@ -0,0 +1,52 @@
|
|||||||
|
package transport
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
math "math/rand"
|
||||||
|
"sync"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/status-im/mvds/protobuf"
|
||||||
|
"github.com/status-im/mvds/state"
|
||||||
|
)
|
||||||
|
|
||||||
|
// ChannelTransport implements a basic MVDS transport using channels for basic testing purposes.
|
||||||
|
type ChannelTransport struct {
|
||||||
|
sync.Mutex
|
||||||
|
|
||||||
|
offline int
|
||||||
|
|
||||||
|
in <-chan Packet
|
||||||
|
out map[state.PeerID]chan<- Packet
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewChannelTransport(offline int, in <-chan Packet) *ChannelTransport {
|
||||||
|
return &ChannelTransport{
|
||||||
|
offline: offline,
|
||||||
|
in: in,
|
||||||
|
out: make(map[state.PeerID]chan<- Packet),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *ChannelTransport) AddOutput(id state.PeerID, c chan<-Packet) {
|
||||||
|
t.out[id] = c
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *ChannelTransport) Watch() Packet {
|
||||||
|
return <-t.in
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *ChannelTransport) Send(group state.GroupID, sender state.PeerID, peer state.PeerID, payload protobuf.Payload) error {
|
||||||
|
math.Seed(time.Now().UnixNano())
|
||||||
|
if math.Intn(100) < t.offline {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
c, ok := t.out[peer]
|
||||||
|
if !ok {
|
||||||
|
return errors.New("peer unknown")
|
||||||
|
}
|
||||||
|
|
||||||
|
c <- Packet{Group: group, Sender: sender, Payload: payload}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user