node

package module
v0.0.0-...-39fb571 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 16, 2015 License: MIT Imports: 10 Imported by: 4

README

Go-node

Implementation of Erlang/OTP node in Go

Supported features:

  • Publish listen port via EPMD
  • Handle incoming connection from other node using Erlang Distribution Protocol
  • Spawn Erlang-like processes
  • Register and unregister processes with simple atom
  • Send messages to registered (using atom) or not registered (using Pid) processes at Go-node or remote Erlang-node
  • Create own process with GenServer behaviour (like gen_server in Erlang/OTP)

Not supported (but should be):

  • Initiate connection to other node
  • Supervisors tree
  • Create own behaviours
  • RPC callbacks
  • Atom cache references to increase throughput

Examples

See examples/ to see example of go-node and GenServer process

Another project which uses this library: Eclus https://github.com/goerlang/eclus

Documentation

Overview

Package node provides interface for creation and publishing node using Erlang distribution protocol: http://www.erlang.org/doc/apps/erts/erl_dist_protocol.html

The Publish function allows incoming connection to the current node on 5858 port. EPMD will reply with this port on name request for this node name

enode := node.NewNode(name, cookie)
err := enode.Publish(5858)
if err != nil {
	log.Printf("Cannot publish: %s", err)
	enode = nil
}

Function Spawn creates new process from struct which implements Process interface:

eSrv := new(eclusSrv)
pid := enode.Spawn(eSrv)

Now you can call Register function to store this pid with arbitrary name:

enode.Register(etf.Atom("eclus"), pid)

Index

Constants

View Source
const (
	LINK           = 1
	SEND           = 2
	EXIT           = 3
	UNLINK         = 4
	NODE_LINK      = 5
	REG_SEND       = 6
	GROUP_LEADER   = 7
	EXIT2          = 8
	SEND_TT        = 12
	EXIT_TT        = 13
	REG_SEND_TT    = 16
	EXIT2_TT       = 18
	MONITOR_P      = 19
	DEMONITOR_P    = 20
	MONITOR_P_EXIT = 21
)

Distributed operations codes (http://www.erlang.org/doc/apps/erts/erl_dist_protocol.html)

Variables

This section is empty.

Functions

This section is empty.

Types

type Behaviour

type Behaviour interface {
	ProcessLoop(pcs procChannels, pd Process, args ...interface{}) // method which implements control flow of process
}

Behaviour interface contains methods you should implement to make own process behaviour

type GenServer

type GenServer interface {
	Init(args ...interface{})
	HandleCast(message *etf.Term)
	HandleCall(message *etf.Term, from *etf.Tuple) (reply *etf.Term)
	HandleInfo(message *etf.Term)
	Terminate(reason interface{})
}

GenServer interface

type GenServerImpl

type GenServerImpl struct {
	Node *Node   // current node of process
	Self etf.Pid // Pid of process
}

GenServerImpl is implementation of GenServer interface

func (*GenServerImpl) Options

func (gs *GenServerImpl) Options() map[string]interface{}

Options returns map of default process-related options

func (*GenServerImpl) ProcessLoop

func (gs *GenServerImpl) ProcessLoop(pcs procChannels, pd Process, args ...interface{})

ProcessLoop executes during whole time of process life. It receives incoming messages from channels and handle it using methods of behaviour implementation

func (*GenServerImpl) Reply

func (gs *GenServerImpl) Reply(fromTuple *etf.Tuple, reply *etf.Term)

Reply sends delayed reply at incoming `gen_server:call/2`

type Node

type Node struct {
	epmd.NodeInfo
	Cookie string
	// contains filtered or unexported fields
}

func NewNode

func NewNode(name string, cookie string) (node *Node)

NewNode create new node context with specified name and cookie string

func (*Node) Publish

func (n *Node) Publish(port int) (err error)

Publish allow node be visible to other Erlang nodes via publishing port in EPMD

func (*Node) RegSend

func (currNode *Node) RegSend(from, to etf.Term, message etf.Term)

RegSend sends message from one process to registered

func (*Node) Register

func (n *Node) Register(name etf.Atom, pid etf.Pid)

Register associates the name with pid

func (*Node) Registered

func (n *Node) Registered() (pids []etf.Atom)

Registered returns a list of names which have been registered using Register

func (*Node) RpcProvide

func (currNode *Node) RpcProvide(modName string, funName string, fun rpcFunction) (err error)

func (*Node) RpcRevoke

func (currNode *Node) RpcRevoke(modName, funName string)

func (*Node) Send

func (currNode *Node) Send(to etf.Pid, message etf.Term)

Send sends message to destination process withoud source

func (*Node) SendFrom

func (currNode *Node) SendFrom(from etf.Term, to etf.Pid, message etf.Term)

SendFrom sends message from source to destination

func (*Node) Spawn

func (n *Node) Spawn(pd Process, args ...interface{}) (pid etf.Pid)

Spawn create new process and store its identificator in table at current node

func (*Node) Unregister

func (n *Node) Unregister(name etf.Atom)

Unregister removes the registered name

func (*Node) Whereis

func (currNode *Node) Whereis(who etf.Atom) (pid etf.Pid)

Whereis returns pid of registered process

type Process

type Process interface {
	Options() (options map[string]interface{}) // method returns process-related options
	// contains filtered or unexported methods
}

Process interface contains methods which should be implemented in each process

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL