Make tunnel channel clients key-able. Neighbour map simplified.
tunClient is a map of tunnel clients keyed on tunnel channel name. Neighbour map is now a cimple map of nodes which contains its nodes.
This commit is contained in:
parent
d09b7dbbef
commit
db8e2620cb
@ -29,10 +29,12 @@ var (
|
|||||||
|
|
||||||
// node is network node
|
// node is network node
|
||||||
type node struct {
|
type node struct {
|
||||||
// Id is node id
|
// id is node id
|
||||||
Id string
|
id string
|
||||||
// Address is node address
|
// address is node address
|
||||||
Address string
|
address string
|
||||||
|
// neighbours are node neightbours
|
||||||
|
neighbours map[string]*node
|
||||||
}
|
}
|
||||||
|
|
||||||
// network implements Network interface
|
// network implements Network interface
|
||||||
@ -50,10 +52,8 @@ type network struct {
|
|||||||
// client is network client
|
// client is network client
|
||||||
client client.Client
|
client client.Client
|
||||||
|
|
||||||
// ctrlClient is ControlChannel client
|
// tunClient is a mao of tunnel clients keyed over channel names
|
||||||
ctrlClient transport.Client
|
tunClient map[string]transport.Client
|
||||||
// netClient is NetwrokChannel client
|
|
||||||
netClient transport.Client
|
|
||||||
|
|
||||||
sync.RWMutex
|
sync.RWMutex
|
||||||
// connected marks the network as connected
|
// connected marks the network as connected
|
||||||
@ -61,7 +61,7 @@ type network struct {
|
|||||||
// closed closes the network
|
// closed closes the network
|
||||||
closed chan bool
|
closed chan bool
|
||||||
// neighbours maps the node neighbourhood
|
// neighbours maps the node neighbourhood
|
||||||
neighbours map[node]map[node]bool
|
neighbours map[string]*node
|
||||||
}
|
}
|
||||||
|
|
||||||
// newNetwork returns a new network node
|
// newNetwork returns a new network node
|
||||||
@ -112,7 +112,8 @@ func newNetwork(opts ...Option) Network {
|
|||||||
Tunnel: options.Tunnel,
|
Tunnel: options.Tunnel,
|
||||||
server: server,
|
server: server,
|
||||||
client: client,
|
client: client,
|
||||||
neighbours: make(map[node]map[node]bool),
|
tunClient: make(map[string]transport.Client),
|
||||||
|
neighbours: make(map[string]*node),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -228,12 +229,13 @@ func (n *network) processNetChan(l tunnel.Listener) {
|
|||||||
if pbNetConnect.Node.Id == n.options.Id {
|
if pbNetConnect.Node.Id == n.options.Id {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
neighbour := node{
|
neighbour := &node{
|
||||||
Id: pbNetConnect.Node.Id,
|
id: pbNetConnect.Node.Id,
|
||||||
Address: pbNetConnect.Node.Address,
|
address: pbNetConnect.Node.Address,
|
||||||
|
neighbours: make(map[string]*node),
|
||||||
}
|
}
|
||||||
n.Lock()
|
n.Lock()
|
||||||
n.neighbours[neighbour] = make(map[node]bool)
|
n.neighbours[neighbour.id] = neighbour
|
||||||
n.Unlock()
|
n.Unlock()
|
||||||
case "neighbour":
|
case "neighbour":
|
||||||
pbNetNeighbour := &pbNet.Neighbour{}
|
pbNetNeighbour := &pbNet.Neighbour{}
|
||||||
@ -245,20 +247,21 @@ func (n *network) processNetChan(l tunnel.Listener) {
|
|||||||
if pbNetNeighbour.Node.Id == n.options.Id {
|
if pbNetNeighbour.Node.Id == n.options.Id {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
neighbour := node{
|
neighbour := &node{
|
||||||
Id: pbNetNeighbour.Node.Id,
|
id: pbNetNeighbour.Node.Id,
|
||||||
Address: pbNetNeighbour.Node.Address,
|
address: pbNetNeighbour.Node.Address,
|
||||||
|
neighbours: make(map[string]*node),
|
||||||
}
|
}
|
||||||
n.Lock()
|
n.Lock()
|
||||||
// we override the existing neighbour map
|
// we override the existing neighbour map
|
||||||
n.neighbours[neighbour] = make(map[node]bool)
|
n.neighbours[neighbour.id] = neighbour
|
||||||
// store the neighbouring node and its neighbours
|
// store the neighbouring node and its neighbours
|
||||||
for _, pbNeighbour := range pbNetNeighbour.Neighbours {
|
for _, pbNeighbour := range pbNetNeighbour.Neighbours {
|
||||||
neighbourNode := node{
|
neighbourNode := &node{
|
||||||
Id: pbNeighbour.Id,
|
id: pbNeighbour.Id,
|
||||||
Address: pbNeighbour.Address,
|
address: pbNeighbour.Address,
|
||||||
}
|
}
|
||||||
n.neighbours[neighbour][neighbourNode] = true
|
n.neighbours[neighbour.id].neighbours[neighbourNode.id] = neighbourNode
|
||||||
}
|
}
|
||||||
n.Unlock()
|
n.Unlock()
|
||||||
case "close":
|
case "close":
|
||||||
@ -271,12 +274,8 @@ func (n *network) processNetChan(l tunnel.Listener) {
|
|||||||
if pbNetClose.Node.Id == n.options.Id {
|
if pbNetClose.Node.Id == n.options.Id {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
node := node{
|
|
||||||
Id: pbNetClose.Node.Id,
|
|
||||||
Address: pbNetClose.Node.Address,
|
|
||||||
}
|
|
||||||
n.Lock()
|
n.Lock()
|
||||||
delete(n.neighbours, node)
|
delete(n.neighbours, pbNetClose.Node.Id)
|
||||||
n.Unlock()
|
n.Unlock()
|
||||||
}
|
}
|
||||||
case <-n.closed:
|
case <-n.closed:
|
||||||
@ -298,10 +297,10 @@ func (n *network) announce(client transport.Client) {
|
|||||||
n.RLock()
|
n.RLock()
|
||||||
nodes := make([]*pbNet.Node, len(n.neighbours))
|
nodes := make([]*pbNet.Node, len(n.neighbours))
|
||||||
i := 0
|
i := 0
|
||||||
for node, _ := range n.neighbours {
|
for id, _ := range n.neighbours {
|
||||||
pbNode := &pbNet.Node{
|
pbNode := &pbNet.Node{
|
||||||
Id: node.Id,
|
Id: id,
|
||||||
Address: node.Address,
|
Address: n.neighbours[id].address,
|
||||||
}
|
}
|
||||||
nodes[i] = pbNode
|
nodes[i] = pbNode
|
||||||
}
|
}
|
||||||
@ -521,7 +520,7 @@ func (n *network) Connect() error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
n.ctrlClient = ctrlClient
|
n.tunClient[ControlChannel] = ctrlClient
|
||||||
|
|
||||||
// listen on ControlChannel
|
// listen on ControlChannel
|
||||||
ctrlListener, err := n.Tunnel.Listen(ControlChannel)
|
ctrlListener, err := n.Tunnel.Listen(ControlChannel)
|
||||||
@ -535,7 +534,7 @@ func (n *network) Connect() error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
n.netClient = netClient
|
n.tunClient[NetworkChannel] = netClient
|
||||||
|
|
||||||
// listen on NetworkChannel
|
// listen on NetworkChannel
|
||||||
netListener, err := n.Tunnel.Listen(NetworkChannel)
|
netListener, err := n.Tunnel.Listen(NetworkChannel)
|
||||||
@ -641,7 +640,7 @@ func (n *network) Close() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// send close message only if we managed to connect to NetworkChannel
|
// send close message only if we managed to connect to NetworkChannel
|
||||||
if n.netClient != nil {
|
if netClient, ok := n.tunClient[NetworkChannel]; ok {
|
||||||
// send connect message to NetworkChannel
|
// send connect message to NetworkChannel
|
||||||
node := &pbNet.Node{
|
node := &pbNet.Node{
|
||||||
Id: n.options.Id,
|
Id: n.options.Id,
|
||||||
@ -661,7 +660,7 @@ func (n *network) Close() error {
|
|||||||
Body: body,
|
Body: body,
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := n.netClient.Send(&m); err != nil {
|
if err := netClient.Send(&m); err != nil {
|
||||||
log.Debugf("Network failed to send close messsage: %v", err)
|
log.Debugf("Network failed to send close messsage: %v", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user