2019-08-20 12:48:51 +01:00
|
|
|
package network
|
|
|
|
|
|
|
|
import (
|
2019-09-05 17:18:16 +01:00
|
|
|
"errors"
|
2019-09-17 17:55:42 +01:00
|
|
|
"fmt"
|
|
|
|
"hash/fnv"
|
2019-08-20 21:12:21 +01:00
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
2019-08-20 21:15:02 +01:00
|
|
|
"github.com/golang/protobuf/proto"
|
2019-08-20 12:48:51 +01:00
|
|
|
"github.com/micro/go-micro/client"
|
2019-08-21 19:16:18 +01:00
|
|
|
rtr "github.com/micro/go-micro/client/selector/router"
|
2019-08-28 20:11:19 +01:00
|
|
|
pbNet "github.com/micro/go-micro/network/proto"
|
2019-08-20 21:12:21 +01:00
|
|
|
"github.com/micro/go-micro/proxy"
|
|
|
|
"github.com/micro/go-micro/router"
|
2019-08-28 20:11:19 +01:00
|
|
|
pbRtr "github.com/micro/go-micro/router/proto"
|
2019-08-20 12:48:51 +01:00
|
|
|
"github.com/micro/go-micro/server"
|
2019-08-20 21:12:21 +01:00
|
|
|
"github.com/micro/go-micro/transport"
|
|
|
|
"github.com/micro/go-micro/tunnel"
|
2019-08-30 00:04:46 +01:00
|
|
|
tun "github.com/micro/go-micro/tunnel/transport"
|
2019-08-20 21:12:21 +01:00
|
|
|
"github.com/micro/go-micro/util/log"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2019-08-28 20:11:19 +01:00
|
|
|
// NetworkChannel is the name of the tunnel channel for passing network messages
|
|
|
|
NetworkChannel = "network"
|
|
|
|
// ControlChannel is the name of the tunnel channel for passing control message
|
2019-08-21 19:16:18 +01:00
|
|
|
ControlChannel = "control"
|
2019-08-27 11:36:46 +01:00
|
|
|
// DefaultLink is default network link
|
|
|
|
DefaultLink = "network"
|
2019-09-05 17:44:47 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2019-09-05 17:43:59 +01:00
|
|
|
// ErrClientNotFound is returned when client for tunnel channel could not be found
|
|
|
|
ErrClientNotFound = errors.New("client not found")
|
2019-08-20 12:48:51 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// network implements Network interface
|
|
|
|
type network struct {
|
2019-09-02 12:39:26 +01:00
|
|
|
// node is network node
|
|
|
|
*node
|
2019-08-20 12:48:51 +01:00
|
|
|
// options configure the network
|
|
|
|
options Options
|
2019-08-20 21:12:21 +01:00
|
|
|
// rtr is network router
|
2019-09-25 12:56:52 +01:00
|
|
|
router router.Router
|
2019-08-20 21:12:21 +01:00
|
|
|
// prx is network proxy
|
2019-09-25 12:56:52 +01:00
|
|
|
proxy proxy.Proxy
|
2019-08-20 21:12:21 +01:00
|
|
|
// tun is network tunnel
|
2019-09-25 12:56:52 +01:00
|
|
|
tunnel tunnel.Tunnel
|
2019-08-27 23:08:35 +01:00
|
|
|
// server is network server
|
|
|
|
server server.Server
|
2019-08-20 21:12:21 +01:00
|
|
|
// client is network client
|
|
|
|
client client.Client
|
|
|
|
|
2019-09-02 11:42:45 +01:00
|
|
|
// tunClient is a map of tunnel clients keyed over tunnel channel names
|
2019-08-28 23:11:26 +01:00
|
|
|
tunClient map[string]transport.Client
|
2019-08-28 20:11:19 +01:00
|
|
|
|
2019-08-20 21:12:21 +01:00
|
|
|
sync.RWMutex
|
|
|
|
// connected marks the network as connected
|
|
|
|
connected bool
|
|
|
|
// closed closes the network
|
|
|
|
closed chan bool
|
2019-08-20 12:48:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// newNetwork returns a new network node
|
|
|
|
func newNetwork(opts ...Option) Network {
|
|
|
|
options := DefaultOptions()
|
|
|
|
|
|
|
|
for _, o := range opts {
|
|
|
|
o(&options)
|
|
|
|
}
|
|
|
|
|
2019-08-20 21:12:21 +01:00
|
|
|
// init tunnel address to the network bind address
|
|
|
|
options.Tunnel.Init(
|
|
|
|
tunnel.Address(options.Address),
|
2019-09-19 16:32:15 +01:00
|
|
|
tunnel.Nodes(options.Peers...),
|
2019-08-20 21:12:21 +01:00
|
|
|
)
|
|
|
|
|
2019-08-27 23:08:35 +01:00
|
|
|
// init router Id to the network id
|
|
|
|
options.Router.Init(
|
|
|
|
router.Id(options.Id),
|
|
|
|
)
|
|
|
|
|
2019-08-20 21:12:21 +01:00
|
|
|
// create tunnel client with tunnel transport
|
2019-08-30 00:04:46 +01:00
|
|
|
tunTransport := tun.NewTransport(
|
|
|
|
tun.WithTunnel(options.Tunnel),
|
2019-08-20 21:12:21 +01:00
|
|
|
)
|
|
|
|
|
2019-10-02 15:22:44 +01:00
|
|
|
// set the address to a hashed address
|
|
|
|
hasher := fnv.New64()
|
|
|
|
hasher.Write([]byte(options.Address + options.Id))
|
|
|
|
address := fmt.Sprintf("%d", hasher.Sum64())
|
|
|
|
|
2019-09-18 18:56:02 +01:00
|
|
|
// set the address to advertise
|
2019-10-02 15:22:44 +01:00
|
|
|
var advertise string
|
2019-10-03 17:37:29 +01:00
|
|
|
var peerAddress string
|
2019-10-02 15:22:44 +01:00
|
|
|
|
2019-09-18 18:56:02 +01:00
|
|
|
if len(options.Advertise) > 0 {
|
2019-10-02 15:22:44 +01:00
|
|
|
advertise = options.Advertise
|
2019-10-03 17:37:29 +01:00
|
|
|
peerAddress = options.Advertise
|
2019-10-02 15:22:44 +01:00
|
|
|
} else {
|
|
|
|
advertise = options.Address
|
2019-10-03 17:37:29 +01:00
|
|
|
peerAddress = address
|
2019-09-18 18:56:02 +01:00
|
|
|
}
|
|
|
|
|
2019-08-27 23:08:35 +01:00
|
|
|
// server is network server
|
|
|
|
server := server.NewServer(
|
|
|
|
server.Id(options.Id),
|
2019-10-03 18:35:54 +01:00
|
|
|
server.Address(peerAddress),
|
2019-10-02 15:22:44 +01:00
|
|
|
server.Advertise(advertise),
|
2019-08-21 19:22:50 +01:00
|
|
|
server.Name(options.Name),
|
2019-08-20 21:12:21 +01:00
|
|
|
server.Transport(tunTransport),
|
|
|
|
)
|
|
|
|
|
|
|
|
// client is network client
|
|
|
|
client := client.NewClient(
|
|
|
|
client.Transport(tunTransport),
|
2019-08-21 19:16:18 +01:00
|
|
|
client.Selector(
|
|
|
|
rtr.NewSelector(
|
|
|
|
rtr.WithRouter(options.Router),
|
|
|
|
),
|
|
|
|
),
|
2019-08-20 21:12:21 +01:00
|
|
|
)
|
|
|
|
|
2019-09-02 12:39:26 +01:00
|
|
|
network := &network{
|
|
|
|
node: &node{
|
2019-09-10 01:14:23 +01:00
|
|
|
id: options.Id,
|
2019-10-03 17:37:29 +01:00
|
|
|
address: peerAddress,
|
2019-09-10 01:14:23 +01:00
|
|
|
peers: make(map[string]*node),
|
2019-09-02 12:39:26 +01:00
|
|
|
},
|
|
|
|
options: options,
|
2019-09-25 12:56:52 +01:00
|
|
|
router: options.Router,
|
|
|
|
proxy: options.Proxy,
|
|
|
|
tunnel: options.Tunnel,
|
2019-09-02 12:39:26 +01:00
|
|
|
server: server,
|
|
|
|
client: client,
|
|
|
|
tunClient: make(map[string]transport.Client),
|
2019-08-20 12:48:51 +01:00
|
|
|
}
|
2019-09-02 12:39:26 +01:00
|
|
|
|
|
|
|
network.node.network = network
|
|
|
|
|
|
|
|
return network
|
2019-08-20 12:48:51 +01:00
|
|
|
}
|
|
|
|
|
2019-09-02 11:42:45 +01:00
|
|
|
// Options returns network options
|
|
|
|
func (n *network) Options() Options {
|
2019-09-13 03:02:20 +01:00
|
|
|
n.RLock()
|
|
|
|
defer n.RUnlock()
|
|
|
|
|
2019-09-02 11:42:45 +01:00
|
|
|
options := n.options
|
|
|
|
|
|
|
|
return options
|
|
|
|
}
|
|
|
|
|
2019-08-20 12:48:51 +01:00
|
|
|
// Name returns network name
|
|
|
|
func (n *network) Name() string {
|
|
|
|
return n.options.Name
|
|
|
|
}
|
|
|
|
|
2019-08-28 20:11:19 +01:00
|
|
|
// resolveNodes resolves network nodes to addresses
|
2019-08-20 21:12:21 +01:00
|
|
|
func (n *network) resolveNodes() ([]string, error) {
|
|
|
|
// resolve the network address to network nodes
|
|
|
|
records, err := n.options.Resolver.Resolve(n.options.Name)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-08-29 14:53:30 +01:00
|
|
|
nodeMap := make(map[string]bool)
|
|
|
|
|
2019-08-20 21:12:21 +01:00
|
|
|
// collect network node addresses
|
2019-08-29 14:53:30 +01:00
|
|
|
var nodes []string
|
2019-08-29 15:09:01 +01:00
|
|
|
for _, record := range records {
|
|
|
|
nodes = append(nodes, record.Address)
|
2019-08-29 14:53:30 +01:00
|
|
|
nodeMap[record.Address] = true
|
|
|
|
}
|
|
|
|
|
|
|
|
// append seed nodes if we have them
|
2019-09-19 16:32:15 +01:00
|
|
|
for _, node := range n.options.Peers {
|
2019-08-29 14:53:30 +01:00
|
|
|
if _, ok := nodeMap[node]; !ok {
|
|
|
|
nodes = append(nodes, node)
|
|
|
|
}
|
2019-08-20 21:12:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return nodes, nil
|
|
|
|
}
|
|
|
|
|
2019-08-28 20:11:19 +01:00
|
|
|
// resolve continuously resolves network nodes and initializes network tunnel with resolved addresses
|
2019-08-20 21:12:21 +01:00
|
|
|
func (n *network) resolve() {
|
|
|
|
resolve := time.NewTicker(ResolveTime)
|
|
|
|
defer resolve.Stop()
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-n.closed:
|
|
|
|
return
|
|
|
|
case <-resolve.C:
|
|
|
|
nodes, err := n.resolveNodes()
|
|
|
|
if err != nil {
|
|
|
|
log.Debugf("Network failed to resolve nodes: %v", err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
// initialize the tunnel
|
2019-09-25 12:56:52 +01:00
|
|
|
n.tunnel.Init(
|
2019-08-20 21:12:21 +01:00
|
|
|
tunnel.Nodes(nodes...),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-28 20:11:19 +01:00
|
|
|
// handleNetConn handles network announcement messages
|
2019-08-30 20:05:00 +01:00
|
|
|
func (n *network) handleNetConn(sess tunnel.Session, msg chan *transport.Message) {
|
2019-08-20 21:12:21 +01:00
|
|
|
for {
|
|
|
|
m := new(transport.Message)
|
2019-08-30 20:05:00 +01:00
|
|
|
if err := sess.Recv(m); err != nil {
|
2019-08-28 20:11:19 +01:00
|
|
|
log.Debugf("Network tunnel [%s] receive error: %v", NetworkChannel, err)
|
2019-10-07 19:07:56 +01:00
|
|
|
if sessErr := sess.Close(); sessErr != nil {
|
|
|
|
log.Debugf("Network tunnel [%s] closing connection error: %v", sessErr)
|
|
|
|
}
|
2019-08-20 21:12:21 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-08-22 13:17:32 +01:00
|
|
|
select {
|
|
|
|
case msg <- m:
|
|
|
|
case <-n.closed:
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-08-20 21:12:21 +01:00
|
|
|
|
2019-08-28 20:11:19 +01:00
|
|
|
// acceptNetConn accepts connections from NetworkChannel
|
|
|
|
func (n *network) acceptNetConn(l tunnel.Listener, recv chan *transport.Message) {
|
|
|
|
for {
|
|
|
|
// accept a connection
|
|
|
|
conn, err := l.Accept()
|
|
|
|
if err != nil {
|
|
|
|
log.Debugf("Network tunnel [%s] accept error: %v", NetworkChannel, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-n.closed:
|
|
|
|
return
|
|
|
|
default:
|
|
|
|
// go handle NetworkChannel connection
|
|
|
|
go n.handleNetConn(conn, recv)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// processNetChan processes messages received on NetworkChannel
|
2019-09-05 16:04:44 +01:00
|
|
|
func (n *network) processNetChan(client transport.Client, listener tunnel.Listener) {
|
2019-08-28 20:11:19 +01:00
|
|
|
// receive network message queue
|
2019-08-22 13:17:32 +01:00
|
|
|
recv := make(chan *transport.Message, 128)
|
|
|
|
|
2019-08-28 20:11:19 +01:00
|
|
|
// accept NetworkChannel connections
|
2019-09-05 16:04:44 +01:00
|
|
|
go n.acceptNetConn(listener, recv)
|
2019-08-28 20:11:19 +01:00
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case m := <-recv:
|
|
|
|
// switch on type of message and take action
|
|
|
|
switch m.Header["Micro-Method"] {
|
|
|
|
case "connect":
|
2019-09-04 18:02:13 +01:00
|
|
|
// mark the time the message has been received
|
|
|
|
now := time.Now()
|
2019-08-28 20:11:19 +01:00
|
|
|
pbNetConnect := &pbNet.Connect{}
|
|
|
|
if err := proto.Unmarshal(m.Body, pbNetConnect); err != nil {
|
|
|
|
log.Debugf("Network tunnel [%s] connect unmarshal error: %v", NetworkChannel, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
// don't process your own messages
|
|
|
|
if pbNetConnect.Node.Id == n.options.Id {
|
|
|
|
continue
|
|
|
|
}
|
2019-09-05 00:16:22 +01:00
|
|
|
log.Debugf("Network received connect message from: %s", pbNetConnect.Node.Id)
|
2019-09-13 03:02:20 +01:00
|
|
|
peer := &node{
|
2019-09-10 01:14:23 +01:00
|
|
|
id: pbNetConnect.Node.Id,
|
|
|
|
address: pbNetConnect.Node.Address,
|
|
|
|
peers: make(map[string]*node),
|
|
|
|
lastSeen: now,
|
2019-08-28 20:11:19 +01:00
|
|
|
}
|
2019-09-16 19:22:55 +01:00
|
|
|
if err := n.node.AddPeer(peer); err == ErrPeerExists {
|
2019-09-13 03:02:20 +01:00
|
|
|
log.Debugf("Network peer exists, refreshing: %s", peer.id)
|
2019-09-13 18:46:24 +01:00
|
|
|
// update lastSeen time for the existing node
|
2019-09-16 19:22:55 +01:00
|
|
|
if err := n.RefreshPeer(peer.id, now); err != nil {
|
|
|
|
log.Debugf("Network failed refreshing peer %s: %v", peer.id, err)
|
2019-09-13 03:02:20 +01:00
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
// get node peers down to MaxDepth encoded in protobuf
|
|
|
|
msg := PeersToProto(n.node, MaxDepth)
|
2019-09-05 17:18:16 +01:00
|
|
|
// advertise yourself to the network
|
2019-09-11 15:55:32 +01:00
|
|
|
if err := n.sendMsg("peer", msg, NetworkChannel); err != nil {
|
2019-09-10 01:14:23 +01:00
|
|
|
log.Debugf("Network failed to advertise peers: %v", err)
|
2019-09-05 16:04:44 +01:00
|
|
|
}
|
|
|
|
// advertise all the routes when a new node has connected
|
2019-09-25 12:56:52 +01:00
|
|
|
if err := n.router.Solicit(); err != nil {
|
2019-09-05 16:04:44 +01:00
|
|
|
log.Debugf("Network failed to solicit routes: %s", err)
|
|
|
|
}
|
2019-09-10 01:14:23 +01:00
|
|
|
case "peer":
|
2019-09-04 18:02:13 +01:00
|
|
|
// mark the time the message has been received
|
|
|
|
now := time.Now()
|
2019-09-10 01:14:23 +01:00
|
|
|
pbNetPeer := &pbNet.Peer{}
|
|
|
|
if err := proto.Unmarshal(m.Body, pbNetPeer); err != nil {
|
|
|
|
log.Debugf("Network tunnel [%s] peer unmarshal error: %v", NetworkChannel, err)
|
2019-08-28 20:11:19 +01:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
// don't process your own messages
|
2019-09-10 01:14:23 +01:00
|
|
|
if pbNetPeer.Node.Id == n.options.Id {
|
2019-08-28 20:11:19 +01:00
|
|
|
continue
|
|
|
|
}
|
2019-09-10 01:14:23 +01:00
|
|
|
log.Debugf("Network received peer message from: %s", pbNetPeer.Node.Id)
|
2019-09-13 03:02:20 +01:00
|
|
|
peer := &node{
|
|
|
|
id: pbNetPeer.Node.Id,
|
|
|
|
address: pbNetPeer.Node.Address,
|
|
|
|
peers: make(map[string]*node),
|
|
|
|
lastSeen: now,
|
|
|
|
}
|
2019-09-25 14:30:35 +01:00
|
|
|
if err := n.node.AddPeer(peer); err == nil {
|
2019-09-10 01:14:23 +01:00
|
|
|
// send a solicit message when discovering new peer
|
2019-09-11 15:55:32 +01:00
|
|
|
msg := &pbRtr.Solicit{
|
|
|
|
Id: n.options.Id,
|
|
|
|
}
|
|
|
|
if err := n.sendMsg("solicit", msg, ControlChannel); err != nil {
|
2019-09-09 23:34:27 +01:00
|
|
|
log.Debugf("Network failed to send solicit message: %s", err)
|
|
|
|
}
|
|
|
|
continue
|
2019-09-25 14:30:35 +01:00
|
|
|
// we're expecting any error to be ErrPeerExists
|
|
|
|
} else if err != ErrPeerExists {
|
|
|
|
log.Debugf("Network got error adding peer %v", err)
|
|
|
|
continue
|
2019-09-04 18:02:13 +01:00
|
|
|
}
|
2019-09-25 14:30:35 +01:00
|
|
|
|
2019-09-13 03:02:20 +01:00
|
|
|
log.Debugf("Network peer exists, refreshing: %s", pbNetPeer.Node.Id)
|
|
|
|
// update lastSeen time for the peer
|
2019-09-16 19:22:55 +01:00
|
|
|
if err := n.RefreshPeer(pbNetPeer.Node.Id, now); err != nil {
|
|
|
|
log.Debugf("Network failed refreshing peer %s: %v", pbNetPeer.Node.Id, err)
|
2019-09-09 23:34:27 +01:00
|
|
|
}
|
2019-09-25 14:30:35 +01:00
|
|
|
|
2019-09-13 18:46:24 +01:00
|
|
|
// NOTE: we don't unpack MaxDepth toplogy
|
2019-09-13 03:02:20 +01:00
|
|
|
peer = UnpackPeerTopology(pbNetPeer, now, MaxDepth-1)
|
|
|
|
log.Debugf("Network updating topology of node: %s", n.node.id)
|
2019-09-16 19:22:55 +01:00
|
|
|
if err := n.node.UpdatePeer(peer); err != nil {
|
|
|
|
log.Debugf("Network failed to update peers: %v", err)
|
2019-09-03 16:39:27 +01:00
|
|
|
}
|
2019-08-28 20:11:19 +01:00
|
|
|
case "close":
|
|
|
|
pbNetClose := &pbNet.Close{}
|
|
|
|
if err := proto.Unmarshal(m.Body, pbNetClose); err != nil {
|
|
|
|
log.Debugf("Network tunnel [%s] close unmarshal error: %v", NetworkChannel, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
// don't process your own messages
|
|
|
|
if pbNetClose.Node.Id == n.options.Id {
|
|
|
|
continue
|
|
|
|
}
|
2019-09-05 00:16:22 +01:00
|
|
|
log.Debugf("Network received close message from: %s", pbNetClose.Node.Id)
|
2019-09-15 12:16:08 +01:00
|
|
|
peer := &node{
|
|
|
|
id: pbNetClose.Node.Id,
|
|
|
|
address: pbNetClose.Node.Address,
|
|
|
|
}
|
2019-09-16 19:22:55 +01:00
|
|
|
if err := n.DeletePeerNode(peer.id); err != nil {
|
|
|
|
log.Debugf("Network failed to delete node %s routes: %v", peer.id, err)
|
|
|
|
}
|
|
|
|
if err := n.prunePeerRoutes(peer); err != nil {
|
|
|
|
log.Debugf("Network failed pruning peer %s routes: %v", peer.id, err)
|
2019-09-03 10:00:14 +01:00
|
|
|
}
|
2019-08-28 20:11:19 +01:00
|
|
|
}
|
|
|
|
case <-n.closed:
|
|
|
|
return
|
|
|
|
}
|
2019-08-22 13:17:32 +01:00
|
|
|
}
|
2019-08-28 20:11:19 +01:00
|
|
|
}
|
2019-08-22 13:17:32 +01:00
|
|
|
|
2019-09-05 17:18:16 +01:00
|
|
|
// sendMsg sends a message to the tunnel channel
|
2019-09-11 15:55:32 +01:00
|
|
|
func (n *network) sendMsg(method string, msg proto.Message, channel string) error {
|
|
|
|
body, err := proto.Marshal(msg)
|
2019-09-05 16:04:44 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// create transport message and chuck it down the pipe
|
|
|
|
m := transport.Message{
|
|
|
|
Header: map[string]string{
|
2019-09-11 15:55:32 +01:00
|
|
|
"Micro-Method": method,
|
2019-09-05 16:04:44 +01:00
|
|
|
},
|
|
|
|
Body: body,
|
|
|
|
}
|
|
|
|
|
2019-09-09 18:47:10 +01:00
|
|
|
// check if the channel client is initialized
|
2019-09-05 17:18:16 +01:00
|
|
|
n.RLock()
|
|
|
|
client, ok := n.tunClient[channel]
|
2019-09-09 18:47:10 +01:00
|
|
|
if !ok || client == nil {
|
2019-09-05 17:18:16 +01:00
|
|
|
n.RUnlock()
|
2019-09-05 17:43:59 +01:00
|
|
|
return ErrClientNotFound
|
2019-09-05 17:18:16 +01:00
|
|
|
}
|
|
|
|
n.RUnlock()
|
|
|
|
|
2019-09-11 15:55:32 +01:00
|
|
|
log.Debugf("Network sending %s message from: %s", method, n.options.Id)
|
2019-09-05 16:04:44 +01:00
|
|
|
if err := client.Send(&m); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-09-10 01:14:23 +01:00
|
|
|
// announce announces node peers to the network
|
2019-08-28 20:11:19 +01:00
|
|
|
func (n *network) announce(client transport.Client) {
|
|
|
|
announce := time.NewTicker(AnnounceTime)
|
|
|
|
defer announce.Stop()
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-n.closed:
|
|
|
|
return
|
|
|
|
case <-announce.C:
|
2019-09-13 03:02:20 +01:00
|
|
|
msg := PeersToProto(n.node, MaxDepth)
|
2019-09-05 16:04:44 +01:00
|
|
|
// advertise yourself to the network
|
2019-09-11 15:55:32 +01:00
|
|
|
if err := n.sendMsg("peer", msg, NetworkChannel); err != nil {
|
2019-09-10 01:14:23 +01:00
|
|
|
log.Debugf("Network failed to advertise peers: %v", err)
|
2019-08-28 20:11:19 +01:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-15 12:16:08 +01:00
|
|
|
// pruneRoutes prunes routes return by given query
|
|
|
|
func (n *network) pruneRoutes(q router.Query) error {
|
2019-09-25 12:56:52 +01:00
|
|
|
routes, err := n.router.Table().Query(q)
|
2019-09-03 10:00:14 +01:00
|
|
|
if err != nil && err != router.ErrRouteNotFound {
|
|
|
|
return err
|
|
|
|
}
|
2019-09-15 12:16:08 +01:00
|
|
|
|
2019-09-03 10:00:14 +01:00
|
|
|
for _, route := range routes {
|
2019-09-25 12:56:52 +01:00
|
|
|
if err := n.router.Table().Delete(route); err != nil && err != router.ErrRouteNotFound {
|
2019-09-03 10:00:14 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-09-15 12:16:08 +01:00
|
|
|
// pruneNodeRoutes prunes routes that were either originated by or routable via given node
|
|
|
|
func (n *network) prunePeerRoutes(peer *node) error {
|
|
|
|
// lookup all routes originated by router
|
|
|
|
q := router.NewQuery(
|
|
|
|
router.QueryRouter(peer.id),
|
|
|
|
)
|
|
|
|
if err := n.pruneRoutes(q); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// lookup all routes routable via gw
|
|
|
|
q = router.NewQuery(
|
2019-09-17 16:31:33 +01:00
|
|
|
router.QueryGateway(peer.id),
|
2019-09-15 12:16:08 +01:00
|
|
|
)
|
|
|
|
if err := n.pruneRoutes(q); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// prune deltes node peers that have not been seen for longer than PruneTime seconds
|
|
|
|
// prune also removes all the routes either originated by or routable by the stale nodes
|
2019-09-03 10:00:14 +01:00
|
|
|
func (n *network) prune() {
|
|
|
|
prune := time.NewTicker(PruneTime)
|
|
|
|
defer prune.Stop()
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-n.closed:
|
|
|
|
return
|
|
|
|
case <-prune.C:
|
2019-09-16 19:22:55 +01:00
|
|
|
pruned := n.PruneStalePeerNodes(PruneTime)
|
|
|
|
for id, peer := range pruned {
|
|
|
|
log.Debugf("Network peer exceeded prune time: %s", id)
|
|
|
|
if err := n.prunePeerRoutes(peer); err != nil {
|
|
|
|
log.Debugf("Network failed pruning peer %s routes: %v", id, err)
|
2019-09-03 10:00:14 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-28 20:11:19 +01:00
|
|
|
// handleCtrlConn handles ControlChannel connections
|
2019-08-30 20:05:00 +01:00
|
|
|
func (n *network) handleCtrlConn(sess tunnel.Session, msg chan *transport.Message) {
|
2019-08-28 20:11:19 +01:00
|
|
|
for {
|
|
|
|
m := new(transport.Message)
|
2019-08-30 20:05:00 +01:00
|
|
|
if err := sess.Recv(m); err != nil {
|
2019-08-28 20:11:19 +01:00
|
|
|
// TODO: should we bail here?
|
|
|
|
log.Debugf("Network tunnel advert receive error: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
|
|
|
case msg <- m:
|
|
|
|
case <-n.closed:
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// acceptCtrlConn accepts connections from ControlChannel
|
|
|
|
func (n *network) acceptCtrlConn(l tunnel.Listener, recv chan *transport.Message) {
|
|
|
|
for {
|
|
|
|
// accept a connection
|
|
|
|
conn, err := l.Accept()
|
|
|
|
if err != nil {
|
|
|
|
// TODO: handle this
|
|
|
|
log.Debugf("Network tunnel [%s] accept error: %v", ControlChannel, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-n.closed:
|
|
|
|
return
|
|
|
|
default:
|
|
|
|
// go handle ControlChannel connection
|
|
|
|
go n.handleCtrlConn(conn, recv)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-29 11:45:47 +01:00
|
|
|
// setRouteMetric calculates metric of the route and updates it in place
|
|
|
|
// - Local route metric is 1
|
2019-09-10 01:14:23 +01:00
|
|
|
// - Routes with ID of adjacent nodes are 10
|
|
|
|
// - Routes by peers of the advertiser are 100
|
2019-08-29 11:45:47 +01:00
|
|
|
// - Routes beyond your neighbourhood are 1000
|
|
|
|
func (n *network) setRouteMetric(route *router.Route) {
|
|
|
|
// we are the origin of the route
|
|
|
|
if route.Router == n.options.Id {
|
|
|
|
route.Metric = 1
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-09-10 01:14:23 +01:00
|
|
|
// check if the route origin is our peer
|
|
|
|
if _, ok := n.peers[route.Router]; ok {
|
2019-08-29 11:45:47 +01:00
|
|
|
route.Metric = 10
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-09-10 01:14:23 +01:00
|
|
|
// check if the route origin is the peer of our peer
|
|
|
|
for _, peer := range n.peers {
|
|
|
|
for id := range peer.peers {
|
2019-08-29 11:45:47 +01:00
|
|
|
if route.Router == id {
|
|
|
|
route.Metric = 100
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// the origin of the route is beyond our neighbourhood
|
|
|
|
route.Metric = 1000
|
|
|
|
}
|
|
|
|
|
|
|
|
// processCtrlChan processes messages received on ControlChannel
|
2019-09-05 16:04:44 +01:00
|
|
|
func (n *network) processCtrlChan(client transport.Client, listener tunnel.Listener) {
|
2019-08-28 20:11:19 +01:00
|
|
|
// receive control message queue
|
|
|
|
recv := make(chan *transport.Message, 128)
|
|
|
|
|
|
|
|
// accept ControlChannel cconnections
|
2019-09-05 16:04:44 +01:00
|
|
|
go n.acceptCtrlConn(listener, recv)
|
2019-08-22 13:17:32 +01:00
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case m := <-recv:
|
|
|
|
// switch on type of message and take action
|
|
|
|
switch m.Header["Micro-Method"] {
|
|
|
|
case "advert":
|
2019-08-28 20:11:19 +01:00
|
|
|
pbRtrAdvert := &pbRtr.Advert{}
|
|
|
|
if err := proto.Unmarshal(m.Body, pbRtrAdvert); err != nil {
|
2019-08-30 00:04:46 +01:00
|
|
|
log.Debugf("Network fail to unmarshal advert message: %v", err)
|
2019-08-22 13:17:32 +01:00
|
|
|
continue
|
2019-08-20 21:12:21 +01:00
|
|
|
}
|
2019-09-04 18:02:13 +01:00
|
|
|
// don't process your own messages
|
|
|
|
if pbRtrAdvert.Id == n.options.Id {
|
|
|
|
continue
|
|
|
|
}
|
2019-09-17 17:55:42 +01:00
|
|
|
log.Debugf("Network received advert message from: %s", pbRtrAdvert.Id)
|
2019-09-13 18:46:24 +01:00
|
|
|
// loookup advertising node in our peer topology
|
|
|
|
advertNode := n.node.GetPeerNode(pbRtrAdvert.Id)
|
2019-09-13 03:02:20 +01:00
|
|
|
if advertNode == nil {
|
2019-09-13 18:46:24 +01:00
|
|
|
// if we can't find the node in our topology (MaxDepth) we skipp prcessing adverts
|
2019-09-13 03:02:20 +01:00
|
|
|
log.Debugf("Network skipping advert message from unknown peer: %s", pbRtrAdvert.Id)
|
|
|
|
continue
|
2019-08-30 00:04:46 +01:00
|
|
|
}
|
|
|
|
|
2019-08-22 13:17:32 +01:00
|
|
|
var events []*router.Event
|
2019-08-28 20:11:19 +01:00
|
|
|
for _, event := range pbRtrAdvert.Events {
|
2019-08-30 00:04:46 +01:00
|
|
|
// we know the advertising node is not the origin of the route
|
2019-09-13 18:46:24 +01:00
|
|
|
if pbRtrAdvert.Id != event.Route.Router {
|
2019-09-10 01:14:23 +01:00
|
|
|
// if the origin router is not the advertising node peer
|
2019-08-30 00:04:46 +01:00
|
|
|
// we can't rule out potential routing loops so we bail here
|
2019-09-13 11:55:53 -07:00
|
|
|
if peer := advertNode.GetPeerNode(event.Route.Router); peer == nil {
|
2019-09-13 18:46:24 +01:00
|
|
|
log.Debugf("Network skipping advert message from peer: %s", pbRtrAdvert.Id)
|
2019-08-30 00:04:46 +01:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
2019-08-22 13:17:32 +01:00
|
|
|
route := router.Route{
|
|
|
|
Service: event.Route.Service,
|
|
|
|
Address: event.Route.Address,
|
|
|
|
Gateway: event.Route.Gateway,
|
|
|
|
Network: event.Route.Network,
|
2019-08-28 08:41:19 +01:00
|
|
|
Router: event.Route.Router,
|
2019-08-22 13:17:32 +01:00
|
|
|
Link: event.Route.Link,
|
|
|
|
Metric: int(event.Route.Metric),
|
|
|
|
}
|
2019-08-29 11:45:47 +01:00
|
|
|
// set the route metric
|
2019-09-13 03:02:20 +01:00
|
|
|
n.node.RLock()
|
2019-08-29 11:45:47 +01:00
|
|
|
n.setRouteMetric(&route)
|
2019-09-13 03:02:20 +01:00
|
|
|
n.node.RUnlock()
|
2019-08-29 11:45:47 +01:00
|
|
|
// throw away metric bigger than 1000
|
|
|
|
if route.Metric > 1000 {
|
2019-09-06 15:12:23 +01:00
|
|
|
log.Debugf("Network route metric %d dropping node: %s", route.Metric, route.Router)
|
2019-08-29 11:45:47 +01:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
// create router event
|
2019-08-22 13:17:32 +01:00
|
|
|
e := &router.Event{
|
|
|
|
Type: router.EventType(event.Type),
|
2019-08-28 20:11:19 +01:00
|
|
|
Timestamp: time.Unix(0, pbRtrAdvert.Timestamp),
|
2019-08-22 13:17:32 +01:00
|
|
|
Route: route,
|
|
|
|
}
|
|
|
|
events = append(events, e)
|
|
|
|
}
|
2019-09-13 20:46:14 +01:00
|
|
|
// if no events are eligible for processing continue
|
|
|
|
if len(events) == 0 {
|
|
|
|
log.Debugf("Network no events to be processed by router: %s", n.options.Id)
|
|
|
|
continue
|
|
|
|
}
|
2019-09-13 03:02:20 +01:00
|
|
|
// create an advert and process it
|
2019-08-22 13:17:32 +01:00
|
|
|
advert := &router.Advert{
|
2019-08-28 20:11:19 +01:00
|
|
|
Id: pbRtrAdvert.Id,
|
|
|
|
Type: router.AdvertType(pbRtrAdvert.Type),
|
|
|
|
Timestamp: time.Unix(0, pbRtrAdvert.Timestamp),
|
|
|
|
TTL: time.Duration(pbRtrAdvert.Ttl),
|
2019-08-22 13:17:32 +01:00
|
|
|
Events: events,
|
2019-08-20 21:12:21 +01:00
|
|
|
}
|
|
|
|
|
2019-09-26 11:56:30 +01:00
|
|
|
log.Debugf("Network router %s processing advert: %s", n.Id(), advert.Id)
|
2019-09-25 12:56:52 +01:00
|
|
|
if err := n.router.Process(advert); err != nil {
|
2019-08-22 13:17:32 +01:00
|
|
|
log.Debugf("Network failed to process advert %s: %v", advert.Id, err)
|
|
|
|
}
|
2019-09-05 16:04:44 +01:00
|
|
|
case "solicit":
|
2019-09-11 15:55:32 +01:00
|
|
|
pbRtrSolicit := &pbRtr.Solicit{}
|
|
|
|
if err := proto.Unmarshal(m.Body, pbRtrSolicit); err != nil {
|
2019-09-06 15:05:58 +01:00
|
|
|
log.Debugf("Network fail to unmarshal solicit message: %v", err)
|
|
|
|
continue
|
|
|
|
}
|
2019-09-11 15:55:32 +01:00
|
|
|
log.Debugf("Network received solicit message from: %s", pbRtrSolicit.Id)
|
|
|
|
// ignore solicitation when requested by you
|
|
|
|
if pbRtrSolicit.Id == n.options.Id {
|
2019-09-06 15:05:58 +01:00
|
|
|
continue
|
|
|
|
}
|
2019-09-13 03:02:20 +01:00
|
|
|
log.Debugf("Network router flushing routes for: %s", pbRtrSolicit.Id)
|
2019-09-05 16:04:44 +01:00
|
|
|
// advertise all the routes when a new node has connected
|
2019-09-25 12:56:52 +01:00
|
|
|
if err := n.router.Solicit(); err != nil {
|
2019-09-05 16:04:44 +01:00
|
|
|
log.Debugf("Network failed to solicit routes: %s", err)
|
|
|
|
}
|
2019-08-20 21:12:21 +01:00
|
|
|
}
|
2019-08-22 13:17:32 +01:00
|
|
|
case <-n.closed:
|
|
|
|
return
|
2019-08-20 21:12:21 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// advertise advertises routes to the network
|
|
|
|
func (n *network) advertise(client transport.Client, advertChan <-chan *router.Advert) {
|
2019-09-17 17:55:42 +01:00
|
|
|
hasher := fnv.New64()
|
2019-08-20 21:12:21 +01:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
// process local adverts and randomly fire them at other nodes
|
|
|
|
case advert := <-advertChan:
|
|
|
|
// create a proto advert
|
2019-08-28 20:11:19 +01:00
|
|
|
var events []*pbRtr.Event
|
2019-08-20 21:12:21 +01:00
|
|
|
for _, event := range advert.Events {
|
2019-09-20 09:40:55 +01:00
|
|
|
// the routes service address
|
|
|
|
address := event.Route.Address
|
|
|
|
|
|
|
|
// only hash the address if we're advertising our own local routes
|
|
|
|
if event.Route.Router == advert.Id {
|
|
|
|
// hash the service before advertising it
|
|
|
|
hasher.Reset()
|
|
|
|
hasher.Write([]byte(event.Route.Address + n.node.id))
|
|
|
|
address = fmt.Sprintf("%d", hasher.Sum64())
|
|
|
|
}
|
|
|
|
|
|
|
|
// NOTE: we override Gateway, Link and Address here
|
|
|
|
// TODO: should we avoid overriding gateway?
|
2019-08-28 20:11:19 +01:00
|
|
|
route := &pbRtr.Route{
|
2019-08-20 21:12:21 +01:00
|
|
|
Service: event.Route.Service,
|
2019-09-20 09:40:55 +01:00
|
|
|
Address: address,
|
2019-10-02 15:52:31 +01:00
|
|
|
Gateway: n.node.Address(),
|
2019-08-23 21:08:18 +01:00
|
|
|
Network: event.Route.Network,
|
2019-08-28 08:41:19 +01:00
|
|
|
Router: event.Route.Router,
|
2019-08-27 11:36:46 +01:00
|
|
|
Link: DefaultLink,
|
2019-08-20 21:12:21 +01:00
|
|
|
Metric: int64(event.Route.Metric),
|
|
|
|
}
|
2019-08-28 20:11:19 +01:00
|
|
|
e := &pbRtr.Event{
|
|
|
|
Type: pbRtr.EventType(event.Type),
|
2019-08-20 21:12:21 +01:00
|
|
|
Timestamp: event.Timestamp.UnixNano(),
|
|
|
|
Route: route,
|
|
|
|
}
|
|
|
|
events = append(events, e)
|
|
|
|
}
|
2019-09-11 15:55:32 +01:00
|
|
|
msg := &pbRtr.Advert{
|
2019-08-20 21:12:21 +01:00
|
|
|
Id: advert.Id,
|
2019-08-28 20:11:19 +01:00
|
|
|
Type: pbRtr.AdvertType(advert.Type),
|
2019-08-20 21:12:21 +01:00
|
|
|
Timestamp: advert.Timestamp.UnixNano(),
|
|
|
|
Events: events,
|
|
|
|
}
|
2019-09-13 03:02:20 +01:00
|
|
|
if err := n.sendMsg("advert", msg, ControlChannel); err != nil {
|
2019-09-11 15:55:32 +01:00
|
|
|
log.Debugf("Network failed to advertise routes: %v", err)
|
2019-08-20 21:12:21 +01:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
case <-n.closed:
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-20 12:48:51 +01:00
|
|
|
// Connect connects the network
|
|
|
|
func (n *network) Connect() error {
|
2019-09-13 18:46:24 +01:00
|
|
|
n.Lock()
|
2019-08-20 21:12:21 +01:00
|
|
|
// return if already connected
|
|
|
|
if n.connected {
|
2019-09-13 18:46:24 +01:00
|
|
|
n.Unlock()
|
2019-08-20 21:12:21 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// try to resolve network nodes
|
|
|
|
nodes, err := n.resolveNodes()
|
|
|
|
if err != nil {
|
2019-08-23 17:48:14 +01:00
|
|
|
log.Debugf("Network failed to resolve nodes: %v", err)
|
2019-08-20 21:12:21 +01:00
|
|
|
}
|
|
|
|
|
2019-10-07 19:07:56 +01:00
|
|
|
// initialize the tunnel to resolved nodes
|
|
|
|
n.tunnel.Init(
|
|
|
|
tunnel.Nodes(nodes...),
|
|
|
|
)
|
|
|
|
|
2019-08-20 21:12:21 +01:00
|
|
|
// connect network tunnel
|
2019-09-25 12:56:52 +01:00
|
|
|
if err := n.tunnel.Connect(); err != nil {
|
2019-09-13 03:31:58 +01:00
|
|
|
n.Unlock()
|
2019-08-20 21:12:21 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-09-17 15:40:00 +01:00
|
|
|
// set our internal node address
|
2019-09-18 19:04:22 +01:00
|
|
|
// if advertise address is not set
|
|
|
|
if len(n.options.Advertise) == 0 {
|
2019-09-25 12:56:52 +01:00
|
|
|
n.server.Init(server.Advertise(n.tunnel.Address()))
|
2019-09-18 19:04:22 +01:00
|
|
|
}
|
2019-09-17 15:40:00 +01:00
|
|
|
|
2019-08-20 21:12:21 +01:00
|
|
|
// dial into ControlChannel to send route adverts
|
2019-09-25 12:56:52 +01:00
|
|
|
ctrlClient, err := n.tunnel.Dial(ControlChannel, tunnel.DialMulticast())
|
2019-08-20 21:12:21 +01:00
|
|
|
if err != nil {
|
2019-09-13 03:31:58 +01:00
|
|
|
n.Unlock()
|
2019-08-22 13:17:32 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-08-28 23:11:26 +01:00
|
|
|
n.tunClient[ControlChannel] = ctrlClient
|
2019-08-28 20:11:19 +01:00
|
|
|
|
2019-08-22 13:17:32 +01:00
|
|
|
// listen on ControlChannel
|
2019-09-25 12:56:52 +01:00
|
|
|
ctrlListener, err := n.tunnel.Listen(ControlChannel)
|
2019-08-28 20:11:19 +01:00
|
|
|
if err != nil {
|
2019-09-13 03:31:58 +01:00
|
|
|
n.Unlock()
|
2019-08-28 20:11:19 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// dial into NetworkChannel to send network messages
|
2019-09-25 12:56:52 +01:00
|
|
|
netClient, err := n.tunnel.Dial(NetworkChannel, tunnel.DialMulticast())
|
2019-08-28 20:11:19 +01:00
|
|
|
if err != nil {
|
2019-09-13 03:31:58 +01:00
|
|
|
n.Unlock()
|
2019-08-28 20:11:19 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-08-28 23:11:26 +01:00
|
|
|
n.tunClient[NetworkChannel] = netClient
|
2019-08-28 20:11:19 +01:00
|
|
|
|
|
|
|
// listen on NetworkChannel
|
2019-09-25 12:56:52 +01:00
|
|
|
netListener, err := n.tunnel.Listen(NetworkChannel)
|
2019-08-22 13:17:32 +01:00
|
|
|
if err != nil {
|
2019-09-13 03:31:58 +01:00
|
|
|
n.Unlock()
|
2019-08-20 21:12:21 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// create closed channel
|
|
|
|
n.closed = make(chan bool)
|
|
|
|
|
2019-08-21 19:16:18 +01:00
|
|
|
// start the router
|
|
|
|
if err := n.options.Router.Start(); err != nil {
|
2019-09-13 03:31:58 +01:00
|
|
|
n.Unlock()
|
2019-08-21 19:16:18 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-08-20 21:12:21 +01:00
|
|
|
// start advertising routes
|
|
|
|
advertChan, err := n.options.Router.Advertise()
|
|
|
|
if err != nil {
|
2019-09-13 03:31:58 +01:00
|
|
|
n.Unlock()
|
2019-08-20 21:12:21 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-08-21 19:16:18 +01:00
|
|
|
// start the server
|
2019-08-27 23:08:35 +01:00
|
|
|
if err := n.server.Start(); err != nil {
|
2019-09-13 03:31:58 +01:00
|
|
|
n.Unlock()
|
2019-08-21 19:16:18 +01:00
|
|
|
return err
|
|
|
|
}
|
2019-09-05 17:18:16 +01:00
|
|
|
n.Unlock()
|
2019-08-21 19:16:18 +01:00
|
|
|
|
2019-08-28 20:11:19 +01:00
|
|
|
// send connect message to NetworkChannel
|
2019-09-03 19:51:52 +01:00
|
|
|
// NOTE: in theory we could do this as soon as
|
|
|
|
// Dial to NetworkChannel succeeds, but instead
|
|
|
|
// we initialize all other node resources first
|
2019-09-11 15:55:32 +01:00
|
|
|
msg := &pbNet.Connect{
|
|
|
|
Node: &pbNet.Node{
|
2019-09-17 15:40:00 +01:00
|
|
|
Id: n.node.id,
|
|
|
|
Address: n.node.address,
|
2019-09-11 15:55:32 +01:00
|
|
|
},
|
|
|
|
}
|
|
|
|
if err := n.sendMsg("connect", msg, NetworkChannel); err != nil {
|
2019-09-05 17:18:16 +01:00
|
|
|
log.Debugf("Network failed to send connect message: %s", err)
|
2019-08-28 20:11:19 +01:00
|
|
|
}
|
|
|
|
|
2019-09-03 19:51:52 +01:00
|
|
|
// go resolving network nodes
|
|
|
|
go n.resolve()
|
2019-09-10 01:14:23 +01:00
|
|
|
// broadcast peers
|
2019-09-03 19:51:52 +01:00
|
|
|
go n.announce(netClient)
|
|
|
|
// prune stale nodes
|
|
|
|
go n.prune()
|
|
|
|
// listen to network messages
|
2019-09-05 16:04:44 +01:00
|
|
|
go n.processNetChan(netClient, netListener)
|
2019-09-03 19:51:52 +01:00
|
|
|
// advertise service routes
|
|
|
|
go n.advertise(ctrlClient, advertChan)
|
|
|
|
// accept and process routes
|
2019-09-05 16:04:44 +01:00
|
|
|
go n.processCtrlChan(ctrlClient, ctrlListener)
|
2019-09-03 19:51:52 +01:00
|
|
|
|
2019-09-05 17:18:16 +01:00
|
|
|
n.Lock()
|
2019-09-03 19:51:52 +01:00
|
|
|
n.connected = true
|
2019-09-05 17:18:16 +01:00
|
|
|
n.Unlock()
|
2019-09-03 19:51:52 +01:00
|
|
|
|
2019-08-20 21:12:21 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *network) close() error {
|
2019-08-21 19:16:18 +01:00
|
|
|
// stop the server
|
2019-08-27 23:08:35 +01:00
|
|
|
if err := n.server.Stop(); err != nil {
|
2019-08-21 19:16:18 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-08-20 21:12:21 +01:00
|
|
|
// stop the router
|
2019-09-25 12:56:52 +01:00
|
|
|
if err := n.router.Stop(); err != nil {
|
2019-08-20 21:12:21 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// close the tunnel
|
2019-09-25 12:56:52 +01:00
|
|
|
if err := n.tunnel.Close(); err != nil {
|
2019-08-20 21:12:21 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-08-20 12:48:51 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close closes network connection
|
|
|
|
func (n *network) Close() error {
|
2019-08-20 21:12:21 +01:00
|
|
|
n.Lock()
|
|
|
|
|
|
|
|
if !n.connected {
|
2019-09-13 03:02:20 +01:00
|
|
|
n.Unlock()
|
2019-08-20 21:12:21 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-n.closed:
|
2019-09-13 03:02:20 +01:00
|
|
|
n.Unlock()
|
2019-08-20 21:12:21 +01:00
|
|
|
return nil
|
|
|
|
default:
|
2019-09-13 03:02:20 +01:00
|
|
|
// TODO: send close message to the network channel
|
|
|
|
close(n.closed)
|
|
|
|
// set connected to false
|
|
|
|
n.connected = false
|
|
|
|
|
|
|
|
// unlock the lock otherwise we'll deadlock sending the close
|
|
|
|
n.Unlock()
|
|
|
|
|
2019-09-11 15:55:32 +01:00
|
|
|
msg := &pbNet.Close{
|
|
|
|
Node: &pbNet.Node{
|
2019-09-17 15:40:00 +01:00
|
|
|
Id: n.node.id,
|
|
|
|
Address: n.node.address,
|
2019-09-11 15:55:32 +01:00
|
|
|
},
|
|
|
|
}
|
|
|
|
if err := n.sendMsg("close", msg, NetworkChannel); err != nil {
|
2019-09-05 17:18:16 +01:00
|
|
|
log.Debugf("Network failed to send close message: %s", err)
|
2019-08-28 20:11:19 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-20 21:12:21 +01:00
|
|
|
return n.close()
|
2019-08-20 12:48:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Client returns network client
|
|
|
|
func (n *network) Client() client.Client {
|
2019-08-20 21:12:21 +01:00
|
|
|
return n.client
|
2019-08-20 12:48:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Server returns network server
|
|
|
|
func (n *network) Server() server.Server {
|
2019-08-27 23:08:35 +01:00
|
|
|
return n.server
|
2019-08-20 12:48:51 +01:00
|
|
|
}
|