...

Package mesh

import "github.com/Diarkis/diarkis/mesh"
Overview
Index
Subdirectories

Overview ▾

Package mesh ▷

Mesh - Server to server internal communications

Mesh manages and controls all internal server to server communications.

Configurations

Mesh configurations are explained below.

{
  "address": "127.0.0.1",
  "nic": "eth0",
  "port": "8000",
  "marsAddress": "127.0.0.1",
  "marsPort": "6789",
  "retryTimeout": 500,
  "strictCheckForExternal": false
}

▶︎ Configuration Values

address - Default is "127.0.0.1"

Address for mesh network server to bind with.

nic - Default is "eth0"

Name of a network interface to retrieve address from.

port - Default is "8000"

Port for mesh network server to bind with. Starts from the given port and scans to find an open poet to bind.

marsAddress - Default is "127.0.0.1"

MARS server address. Can be overwritten by env DIARKIS_MARS_ADDR.

marsPort - Default is "6789"

MARS server port. Can be overwritten by env DIARKIS_MARS_PORT.

NOTE - You may use either address or nic in your configurations. If none is used, the default is localhost.

retryTimeout - Default is 500ms

RUDP retry timeout.

strictCheckForExternal - Default is false

When this configuration is set to true, all in coming mesh messages will be validated to make sure the sender of each message comes from a node that is part of the cluster.

Index ▾

func Command(commandID uint16, callback func(map[string]interface{}) (map[string]interface{}, error))
func CreateReturnBytes(data map[string]interface{}) ([]byte, error)
func GetBool(data map[string]interface{}, name string) bool
func GetBytes(data map[string]interface{}, name string) []byte
func GetFloat64(data map[string]interface{}, name string) float64
func GetInt(data map[string]interface{}, name string) int
func GetInt16(data map[string]interface{}, name string) int16
func GetInt32(data map[string]interface{}, name string) int32
func GetInt64(data map[string]interface{}, name string) int64
func GetInt8(data map[string]interface{}, name string) int8
func GetMyEndPoint() string
func GetMyNodeEndPoint() string
func GetMyNodeType() string
func GetNodeAddresses(ignoreList []string) []string
func GetNodeAddressesByRole(nRole string) []string
func GetNodeAddressesByType(nType string) []string
func GetNodeEndPoint(nodeAddr string) string
func GetNodeRoleByType(nType string) string
func GetNodeType() string
func GetNodeTypeByAddress(addr string) string
func GetNodeTypes() []string
func GetNodeValue(nodeAddr string, name string) interface{}
func GetNodeValues(nodeAddr string) map[string]interface{}
func GetString(data map[string]interface{}, name string) string
func GetUint16(data map[string]interface{}, name string) uint16
func GetUint32(data map[string]interface{}, name string) uint32
func GetUint64(data map[string]interface{}, name string) uint64
func GetUint8(data map[string]interface{}, name string) uint8
func Guest(confpath string)
func HandleCommand(cmd uint16, cb func(map[string]interface{}) ([]byte, error))
func IsCommandDuplicate(commandID uint16) bool
func IsMyNodeOnline() bool
func IsMyNodeTaken() bool
func IsNodeOnline(nodeAddr string) bool
func IsNodeTaken(nodeAddr string) bool
func OnUpdate(handler func())
func OnUpdated(handler func())
func Send(commandID uint16, addr string, data map[string]interface{})
func SendMany(commandID uint16, addrs []string, data map[string]interface{}, limit int)
func SendRequest(commandID uint16, addr string, data map[string]interface{}, callback func(error, map[string]interface{}))
func SetAppName(name string)
func SetEndPoint(endPoint string)
func SetNodeRole(role string)
func SetNodeType(_nodeType string)
func SetNodeValue(name string, val interface{})
func SetStatus(status int)
func Setup(confpath string)
func StartAsMARS(confpath string)
func USend(commandID uint16, addr string, data map[string]interface{})
func USendMany(command uint16, addrs []string, data map[string]interface{}, limit int)
type Node
    func GetNode(nodeAddr string) *Node
    func GetNodeByEndPoint(endPoint string) *Node

func Command

func Command(commandID uint16, callback func(map[string]interface{}) (map[string]interface{}, error))

Command [DEPRECATED] defines a handling function for a mesh network messsage as a command Command is replaced by HandleCommand - Command messages are sent by SendRequest, SendMany, USend, USendMany

func CreateReturnBytes

func CreateReturnBytes(data map[string]interface{}) ([]byte, error)

CreateReturnBytes converts a map into a byte array for HandleCommand handler's return value

func GetBool

func GetBool(data map[string]interface{}, name string) bool

GetBool returns a bool from the mesh network request/response data

func GetBytes

func GetBytes(data map[string]interface{}, name string) []byte

GetBytes returns a byte array from the mesh network request/response data

func GetFloat64

func GetFloat64(data map[string]interface{}, name string) float64

GetFloat64 returns a float64 from the mesh network request/response data

func GetInt

func GetInt(data map[string]interface{}, name string) int

GetInt returns an int from the mesh network request/response data

func GetInt16

func GetInt16(data map[string]interface{}, name string) int16

GetInt16 returns an int16 from the mesh network request/response data

func GetInt32

func GetInt32(data map[string]interface{}, name string) int32

GetInt32 returns an int32 from the mesh network request/response data

func GetInt64

func GetInt64(data map[string]interface{}, name string) int64

GetInt64 returns an int64 from the mesh network request/response data

func GetInt8

func GetInt8(data map[string]interface{}, name string) int8

GetInt8 returns an int8 from the mesh network request/response data

func GetMyEndPoint

func GetMyEndPoint() string

GetMyEndPoint Returns its own mesh network end point

[NOTE] Uses mutex lock internally.

func GetMyNodeEndPoint

func GetMyNodeEndPoint() string

GetMyNodeEndPoint returns the server endpoint (internet) its own node

[NOTE] Uses mutex lock internally.

func GetMyNodeType

func GetMyNodeType() string

GetMyNodeType Returns the node type of itself

func GetNodeAddresses

func GetNodeAddresses(ignoreList []string) []string

GetNodeAddresses Returns all node addresses

[NOTE] Uses mutex lock internally.

func GetNodeAddressesByRole

func GetNodeAddressesByRole(nRole string) []string

GetNodeAddressesByRole Return the addresses of nodes by type

[NOTE] Uses mutex lock internally.

func GetNodeAddressesByType

func GetNodeAddressesByType(nType string) []string

GetNodeAddressesByType Return the addresses of nodes by type

[NOTE] Uses mutex lock internally.

func GetNodeEndPoint

func GetNodeEndPoint(nodeAddr string) string

GetNodeEndPoint returns the server endpoint (internet) of a node by its mesh node address - only TCP, UDP, and HTTP

[NOTE] Uses mutex lock internally.

func GetNodeRoleByType

func GetNodeRoleByType(nType string) string

GetNodeRoleByType Returns node role by node type. Possible values are: HTTP, TCP, UDP, WS, and empty if node type is invalid

[NOTE] Uses mutex lock internally.

func GetNodeType

func GetNodeType() string

GetNodeType returns the server type of the server

func GetNodeTypeByAddress

func GetNodeTypeByAddress(addr string) string

GetNodeTypeByAddress returns the server type of the given node by mesh address

[NOTE] Uses mutex lock internally.

func GetNodeTypes

func GetNodeTypes() []string

GetNodeTypes returns all node types currently in the Diarkis cluster

[NOTE] Uses mutex lock internally.

func GetNodeValue

func GetNodeValue(nodeAddr string, name string) interface{}

GetNodeValue Returns a metadata value of a node by its address

[NOTE] Uses mutex lock internally.

func GetNodeValues

func GetNodeValues(nodeAddr string) map[string]interface{}

GetNodeValues Returns all metadata values of a node by its address

[NOTE] Uses mutex lock internally.

func GetString

func GetString(data map[string]interface{}, name string) string

GetString returns a string from the mesh network request/response data

func GetUint16

func GetUint16(data map[string]interface{}, name string) uint16

GetUint16 returns a uint16 from the mesh network request/response data

func GetUint32

func GetUint32(data map[string]interface{}, name string) uint32

GetUint32 returns a uint32 from the mesh network request/response data

func GetUint64

func GetUint64(data map[string]interface{}, name string) uint64

GetUint64 returns a uint64 from the mesh network request/response data

func GetUint8

func GetUint8(data map[string]interface{}, name string) uint8

GetUint8 returns a uint8 from the mesh network request/response data

func Guest

func Guest(confpath string)

Guest starts mesh network as a guest meaning that the process can only send and receive messages but does not become part of the mesh node

func HandleCommand

func HandleCommand(cmd uint16, cb func(map[string]interface{}) ([]byte, error))

HandleCommand defines a handling function for a mesh network messsage as a command HandleCommand replaces Command - Command messages are sent by SendRequest, SendMany, USend, USendMany

func IsCommandDuplicate

func IsCommandDuplicate(commandID uint16) bool

IsCommandDuplicate returns true if commandID is used elsewhere

func IsMyNodeOnline

func IsMyNodeOnline() bool

IsMyNodeOnline returns false if the node is offline (received SIGTERM)

[NOTE] Uses mutex lock internally.

When the node is offline, the connected clients will be raising OnOffline event

func IsMyNodeTaken

func IsMyNodeTaken() bool

IsMyNodeTaken returns true if the node is taken ((mesh.SetStatus(util.MeshStatusTaken))

[NOTE] Uses mutex lock internally.

When a node is thaken, the taken node will not be returned by HTTP server as available server.

func IsNodeOnline

func IsNodeOnline(nodeAddr string) bool

IsNodeOnline Returns true if the given node is online

[NOTE] Uses mutex lock internally.

When the node is offline, the connected clients will be raising OnOffline event

func IsNodeTaken

func IsNodeTaken(nodeAddr string) bool

IsNodeTaken returns false if the node is taken (mesh.SetStatus(util.MeshStatusTaken))

[NOTE] Uses mutex lock internally.

When a node is thaken, the taken node will not be returned by HTTP server as available server.

func OnUpdate

func OnUpdate(handler func())

OnUpdate Registers a callback on announcer update event

[NOTE] Uses mutex lock internally.

func OnUpdated

func OnUpdated(handler func())

OnUpdated Registers a callback on announcer updated event

[NOTE] Uses mutex lock internally.

func Send

func Send(commandID uint16, addr string, data map[string]interface{})

Send Sends a mesh network message to a given node - addr = <address>:<port>

func SendMany

func SendMany(commandID uint16, addrs []string, data map[string]interface{}, limit int)

SendMany Sends a mesh network message to multiple nodes

commandID - Mesh message command ID
addrs     - A list of mesh node addreses to send the message to
data      - Data map to be sent
limit     - Maximum number of node to send the message at a time

func SendRequest

func SendRequest(commandID uint16, addr string, data map[string]interface{}, callback func(error, map[string]interface{}))

SendRequest Sends a mesh network requesst to another node - addr = <address>:<port> and expects a response back from it

commandID - Pre-defined command ID that coresponds with user defined custom handler function(s) You may define multiple handler functions for a command ID.

addr - Target node address (inernal) to send the request to.

data - Request data to be sent to the handler function(s).

callback - The callback function to be invoked when the response comes back.

func SetAppName

func SetAppName(name string)

SetAppName creates a name space within the mars-server in order to have multiple diarkis clusters

func SetEndPoint

func SetEndPoint(endPoint string)

SetEndPoint Sets its own end point

[NOTE] Uses mutex lock internally.

func SetNodeRole

func SetNodeRole(role string)

SetNodeRole [INTERNAL USE ONLY]

func SetNodeType

func SetNodeType(_nodeType string)

SetNodeType Sets a type to a name

func SetNodeValue

func SetNodeValue(name string, val interface{})

SetNodeValue Sets a metadata value to a node

[NOTE] Uses mutex lock internally.

func SetStatus

func SetStatus(status int)

SetStatus Sets its status

func Setup

func Setup(confpath string)

Setup Loads a configuration file into memory - pass an empty string to load nothing

func StartAsMARS

func StartAsMARS(confpath string)

StartAsMARS Starts the server as MARS server - For now this is meant to be used for mars-server ONLY

func USend

func USend(commandID uint16, addr string, data map[string]interface{})

USend Sends an unreliable message to a given node - addr = <address>:<port>

func USendMany

func USendMany(command uint16, addrs []string, data map[string]interface{}, limit int)

USendMany Sends an unreliable mesh network message to multiple nodes

commandID - Mesh message command ID
addrs     - A list of mesh node addreses to send the message to
data      - Data map to be sent
limit     - Maximum number of node to send the message at a time

type Node

Node a mesh network node data structure

type Node struct {
    Addr   string
    Type   string
    Role   string
    Values map[string]interface{}
    Self   bool
}

func GetNode

func GetNode(nodeAddr string) *Node

GetNode Returns a node by its address

[NOTE] Uses mutex lock internally.

func GetNodeByEndPoint

func GetNodeByEndPoint(endPoint string) *Node

GetNodeByEndPoint Returns a node by its server end point - only WebSocket, TCP, UDP, and HTTP

[NOTE] Uses mutex lock internally.

Subdirectories