Change network id to name

This commit is contained in:
Asim Aslam 2019-07-07 10:10:38 +01:00
parent d1fc3c361e
commit eafc930f84
3 changed files with 20 additions and 20 deletions

View File

@ -30,8 +30,8 @@ type network struct {
// proxy used to route through the network // proxy used to route through the network
proxy proxy.Proxy proxy proxy.Proxy
// id of this network // name of this network
id string name string
// links maintained for this network // links maintained for this network
// based on peers not nodes. maybe maintain // based on peers not nodes. maybe maintain
@ -65,7 +65,7 @@ func (n *network) lease(muid string) *pb.Lease {
Muid: muid, Muid: muid,
Id: id, Id: id,
Address: address, Address: address,
Network: n.id, Network: n.name,
}, },
} }
} }
@ -76,18 +76,18 @@ func (n *network) lookup(r registry.Registry) []*resolver.Record {
rr := nreg.Resolver{Registry: r} rr := nreg.Resolver{Registry: r}
// get all the nodes for the network that are local // get all the nodes for the network that are local
localRecords, err := rr.Resolve("network:" + n.Id()) localRecords, err := rr.Resolve(n.Name())
if err != nil { if err != nil {
// we're not in a good place here // we're not in a good place here
} }
// if its a local network we never try lookup anything else // if its a local network we never try lookup anything else
if n.Id() == "local" { if n.Name() == "local" {
return localRecords return localRecords
} }
// now resolve incrementally based on resolvers specified // now resolve incrementally based on resolvers specified
networkRecords, err := n.resolver.Resolve(n.Id()) networkRecords, err := n.resolver.Resolve(n.Name())
if err != nil { if err != nil {
// still not in a good place // still not in a good place
} }
@ -96,8 +96,8 @@ func (n *network) lookup(r registry.Registry) []*resolver.Record {
return append(localRecords, networkRecords...) return append(localRecords, networkRecords...)
} }
func (n *network) Id() string { func (n *network) Name() string {
return n.id return n.name
} }
// Connect connects to the network and returns a new node. // Connect connects to the network and returns a new node.
@ -135,16 +135,16 @@ func newNetwork(opts ...options.Option) *network {
// new network instance with defaults // new network instance with defaults
net := &network{ net := &network{
Options: options, Options: options,
id: DefaultId, name: DefaultName,
router: router.DefaultRouter, router: router.DefaultRouter,
proxy: new(mucp.Proxy), proxy: new(mucp.Proxy),
resolver: new(nreg.Resolver), resolver: new(nreg.Resolver),
} }
// get network id // get network name
id, ok := options.Values().Get("network.id") name, ok := options.Values().Get("network.name")
if ok { if ok {
net.id = id.(string) net.name = name.(string)
} }
// get router // get router

View File

@ -10,8 +10,8 @@ import (
// is responsible for routing messages to the correct services. // is responsible for routing messages to the correct services.
type Network interface { type Network interface {
options.Options options.Options
// Id of the network // Name of the network
Id() string Name() string
// Connect to the network // Connect to the network
Connect() (Node, error) Connect() (Node, error)
// Peer with a neighboring network // Peer with a neighboring network
@ -53,8 +53,8 @@ type Message struct {
} }
var ( var (
// The default network ID is local // The default network name is local
DefaultId = "local" DefaultName = "local"
// just the standard network element // just the standard network element
DefaultNetwork = NewNetwork() DefaultNetwork = NewNetwork()

View File

@ -69,7 +69,7 @@ func newNode(n *network) (*node, error) {
// create a new node // create a new node
node := &node{ node := &node{
// this nodes unique micro assigned mac address // this nodes unique micro assigned mac address
muid: fmt.Sprintf("%s-%s", n.id, uuid.New().String()), muid: fmt.Sprintf("%s-%s", n.name, uuid.New().String()),
// map of connected records // map of connected records
connected: make(map[string]bool), connected: make(map[string]bool),
// the links // the links
@ -156,7 +156,7 @@ func newNode(n *network) (*node, error) {
// a registrar or tld or whatever // a registrar or tld or whatever
if err := node.registry.Register(&registry.Service{ if err := node.registry.Register(&registry.Service{
// register with the network id // register with the network id
Name: "network:" + n.Id(), Name: n.Name(),
Nodes: []*registry.Node{ Nodes: []*registry.Node{
{Id: node.id, Address: addr, Port: port}, {Id: node.id, Address: addr, Port: port},
}, },
@ -473,7 +473,7 @@ func (n *node) Close() error {
// deregister self // deregister self
n.registry.Deregister(&registry.Service{ n.registry.Deregister(&registry.Service{
Name: "network:" + n.network.Id(), Name: n.network.Name(),
Nodes: []*registry.Node{ Nodes: []*registry.Node{
{Id: n.id, Address: n.address}, {Id: n.id, Address: n.address},
}, },
@ -514,7 +514,7 @@ func (n *node) Id() string {
} }
func (n *node) Network() string { func (n *node) Network() string {
return n.network.id return n.network.Name()
} }
// Send propagates a message over all links. This should probably use its proxy. // Send propagates a message over all links. This should probably use its proxy.