Skip to content

Commit

Permalink
add the network interface
Browse files Browse the repository at this point in the history
  • Loading branch information
asim committed Oct 22, 2020
1 parent d28a5aa commit 2d5391f
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 0 deletions.
55 changes: 55 additions & 0 deletions network/network.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// Package network is for creating internetworks
package network

import (
"github.com/asim/go-micro/v3/client"
"github.com/asim/go-micro/v3/server"
)

// Network is micro network
type Network interface {
// Node is network node
Node
// Initialise options
Init(...Option) error
// Options returns the network options
Options() Options
// Name of the network
Name() string
// Connect starts the resolver and tunnel server
Connect() error
// Close stops the tunnel and resolving
Close() error
// Client is micro client
Client() client.Client
// Server is micro server
Server() server.Server
}

// Node is network node
type Node interface {
// Id is node id
Id() string
// Address is node bind address
Address() string
// Peers returns node peers
Peers() []Node
// Network is the network node is in
Network() Network
// Status returns node status
Status() Status
}

// Error is network node errors
type Error interface {
// Count is current count of errors
Count() int
// Msg is last error message
Msg() string
}

// Status is node status
type Status interface {
// Error reports error status
Error() Error
}
52 changes: 52 additions & 0 deletions network/options.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package network

type Option func(*Options)

// Options configure network
type Options struct {
// Id of the node
Id string
// Name of the network
Name string
// Address to bind to
Address string
// Advertise sets the address to advertise
Advertise string
// Nodes is a list of nodes to connect to
Nodes []string
}

// Id sets the id of the network node
func Id(id string) Option {
return func(o *Options) {
o.Id = id
}
}

// Name sets the network name
func Name(n string) Option {
return func(o *Options) {
o.Name = n
}
}

// Address sets the network address
func Address(a string) Option {
return func(o *Options) {
o.Address = a
}
}

// Advertise sets the address to advertise
func Advertise(a string) Option {
return func(o *Options) {
o.Advertise = a
}
}

// Nodes is a list of nodes to connect to
func Nodes(n ...string) Option {
return func(o *Options) {
o.Nodes = n
}
}

0 comments on commit 2d5391f

Please sign in to comment.