bert

package module
v0.0.0-...-3a65ad6 Latest Latest
Warning

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

Go to latest
Published: Apr 5, 2017 License: MIT Imports: 13 Imported by: 1

README

About

The gobert project is a Go library implementing the BERT serialization and RPC protocol.

Status

I, Seth Klein, don't use this library. I updated it to Go 1 for someone in #go-nuts and the original maintainer declared it dead so I figured I'd do the basics and handle bug reports and pull requests. As of February, 2013, the last post to the BERT mailing list was nearly two years ago so unless this gets serious interest, I'm not going to sink oodles of effort into figuring out what full BERT compliance is or whether this library implements it. (The original author implies that it doesn't.) Feel free to report bugs and make pull requests, though.

Reporting Bugs

Bugs may be reported at https://github.com/sethwklein/gobert/issues or via email or Twitter to the maintainer contact below.

Maintainer Contact

Seth W. Klein sk@sethwklein.net @sethwklein

History

The gobert project was started by Joshua Peek in 2009. In 2013 he confirmed that he was no longer maintaining it, and Seth W. Klein assumed maintenance. As the last update on this was in 2013, Landon Wainwright has added in support for more tag specs.. I aim to create pull requests to merge this back into the original source when I am happy it is all working.

Documentation

Overview

Package bert implements the BERT serialization and RPC protocol. See http://bert-rpc.org/

Index

Examples

Constants

View Source
const (
	VersionTag            = 131
	DistributionHeaderTag = 68
	CompressedTag         = 80
	SmallIntTag           = 97
	IntTag                = 98
	SmallBignumTag        = 110
	LargeBignumTag        = 111
	FloatTag              = 99
	NewFloatTag           = 70
	AtomCacheRefTag       = 82
	AtomTag               = 100
	SmallAtomTag          = 115
	AtomUtf8Tag           = 118
	SmallAtomUtf8Tag      = 119
	SmallTupleTag         = 104
	LargeTupleTag         = 105
	NilTag                = 106
	StringTag             = 107
	ListTag               = 108
	BinTag                = 109
	MapTag                = 116
	PidTag                = 103
	PortTag               = 102
	FunTag                = 117
	ReferenceTag          = 101
	NewReferenceTag       = 114
	NewFunTag             = 112
	ExportTag             = 113
)
View Source
const (
	BertAtom  = Atom("bert")
	NilAtom   = Atom("nil")
	TrueAtom  = Atom("true")
	FalseAtom = Atom("false")
)
View Source
const (
	MinorVersion0 = 0
	MinorVersion1 = 1
)
View Source
const (
	// NewCacheEntry is used to determine if an entry is a new cache entry
	NewCacheEntry byte = 8

	// SegmentIndex can be used to extract the segment index
	SegmentIndex byte = 7

	// LongAtoms is used to determine if 2 byte atoms are used
	LongAtoms byte = 1
)

Variables

View Source
var (
	ErrBadMagic    error = errors.New("bad magic")
	ErrUnknownType error = errors.New("unknown type")
	ErrMissingAtom error = errors.New("missing Atom")
	ErrEOF         error = errors.New("Unexpected EOF")
)

Functions

func Encode

func Encode(val interface{}) ([]byte, error)

Encode encodes val and returns it or an error.

func EncodeAndCompress

func EncodeAndCompress(val interface{}, compress bool) ([]byte, error)

EncodeAndCompress encodes val and returns it or an error. If compress is true the body will be compressed

func EncodeAndCompressUsingMinorVersion

func EncodeAndCompressUsingMinorVersion(val interface{}, compress bool, minorVersion int) ([]byte, error)

EncodeAndCompressUsingMinorVersion encodes val and returns it or an error. If compress is true the body will be compressed It will use the minor version for the encoding

func EncodeTo

func EncodeTo(w io.Writer, val interface{}) (err error)

EncodeTo encodes val and writes it to w, returning any error.

func EncodeToAndCompress

func EncodeToAndCompress(w io.Writer, val interface{}, compress bool) (err error)

EncodeToAndCompress encodes val and writes it to w, returning any error. If compress is true the body will be compressed

func EncodeToAndCompressUsingMinorVersion

func EncodeToAndCompressUsingMinorVersion(w io.Writer, val interface{}, compress bool, minorVersion int) (err error)

EncodeToAndCompress encodes val and writes it to w, returning any error. If compress is true the body will be compressed

func EncodeToUsingMinorVersion

func EncodeToUsingMinorVersion(w io.Writer, val interface{}, minorVersion int) (err error)

EncodeTo encodes val and writes it to w, returning any error.

func EncodeUsingMinorVersion

func EncodeUsingMinorVersion(val interface{}, minorVersion int) ([]byte, error)

Encode encodes val and returns it or an error.

func Marshal

func Marshal(w io.Writer, val interface{}) error

Marshal is an alias for EncodeTo.

func MarshalAndCompress

func MarshalAndCompress(w io.Writer, val interface{}, compress bool) error

MarshalAndCompress is an alias for EncodeTo. If compress is true the body will be compressed

func MarshalAndCompressUsingMinorVersion

func MarshalAndCompressUsingMinorVersion(w io.Writer, val interface{}, compress bool, minorVersion int) error

MarshalAndCompress is an alias for EncodeTo. If compress is true the body will be compressed It will use the minor version for the encoding

func MarshalResponse

func MarshalResponse(w io.Writer, val interface{}) (err error)

MarshalResponse encodes val into a BURP Response struct and writes it to w, returning any error.

func MarshalResponseAndCompress

func MarshalResponseAndCompress(w io.Writer, val interface{}, compress bool) (err error)

MarshalResponseAndCompress encodes val into a BURP Response struct and writes it to w, returning any error. If compress is true the body will be compressed

func MarshalResponseAndCompressUsingMinorVersion

func MarshalResponseAndCompressUsingMinorVersion(w io.Writer, val interface{}, compress bool, minorVersion int) (err error)

MarshalResponseAndCompress encodes val into a BURP Response struct and writes it to w, returning any error. If compress is true the body will be compressed It will use the minor version for the encoding

func MarshalResponseUsingMinorVersion

func MarshalResponseUsingMinorVersion(w io.Writer, val interface{}, minorVersion int) (err error)

MarshalResponse encodes val into a BURP Response struct and writes it to w, returning any error.

func MarshalUsingMinorVersion

func MarshalUsingMinorVersion(w io.Writer, val interface{}, minorVersion int) error

Marshal is an alias for EncodeTo.

func Unmarshal

func Unmarshal(data []byte, val interface{}) (err error)

Unmarshal decodes a value from data, stores it in val, and returns any error encountered.

func UnmarshalFrom

func UnmarshalFrom(r io.Reader, val interface{}) (err error)

UnmarshalFrom decodes a value from r, stores it in val, and returns any error encountered.

Types

type Atom

type Atom string

type DistributionHeader

type DistributionHeader struct {
	// contains filtered or unexported fields
}

As of erts version 5.7.2 the old atom cache protocol was dropped and a new one was introduced. This atom cache protocol introduced the distribution header. Nodes with erts versions earlier than 5.7.2 can still communicate with new nodes, but no distribution header and no atom cache will be used.

The distribution header currently only contains an atom cache reference section, but could in the future contain more information. The distribution header precedes one or more Erlang terms on the external format. For more information see the documentation of the protocol between connected nodes in the distribution protocol documentation.

ATOM_CACHE_REF entries with corresponding AtomCacheReferenceIndex in terms encoded on the external format following a distribution header refers to the atom cache references made in the distribution header. The range is 0 <= AtomCacheReferenceIndex < 255, i.e., at most 255 different atom cache references from the following terms can be made.

func (DistributionHeader) GetAtom

func (dh DistributionHeader) GetAtom(atomCacheReferenceIndex uint8) (*Atom, error)

GetAtom will return the atom that exists for the atomCacheReferenceIndex

func (DistributionHeader) Update

func (dh DistributionHeader) Update(r io.Reader) error

UpdateFlags will update the flags for the cache

type Export

type Export struct {
	Module   Atom
	Function Atom
	Arity    uint8
}

Export wraps the EXPORT_EXT tag type (113) This term is the encoding for external funs: fun M:F/A.

Module and Function are atoms (encoded using ATOM_EXT, SMALL_ATOM_EXT or ATOM_CACHE_REF).

Arity is an integer encoded using SMALL_INTEGER_EXT.

type Func

type Func struct {
	Pid      Pid
	Module   Atom
	Index    uint32
	Uniq     uint32
	FreeVars []Term
}

Func wraps the FUN_EXT tag type (117)

Pid is a process identifier as in PID_EXT. It represents the process in which the fun was created.

Module is an encoded as an atom, using ATOM_EXT, SMALL_ATOM_EXT or ATOM_CACHE_REF. This is the module that the fun is implemented in.

Index is an integer encoded using SMALL_INTEGER_EXT or INTEGER_EXT. It is typically a small index into the module's fun table.

Uniq is an integer encoded using SMALL_INTEGER_EXT or INTEGER_EXT. Uniq is the hash value of the parse for the fun.

Free vars is NumFree number of terms, each one encoded according to its type.

type NewFunc

type NewFunc struct {
	Arity     uint8
	Uniq      []byte
	Index     uint32
	Module    Atom
	OldIndex  uint32
	OldUnique uint32
	Pid       Pid
	FreeVars  []Term
}

NewFunc wraps the NEW_FUN_EXT tag type (112) This is the new encoding of internal funs: fun F/A and fun(Arg1,..) -> ... end.

Size is the total number of bytes, including the Size field.

Arity is the arity of the function implementing the fun.

Uniq is the 16 bytes MD5 of the significant parts of the Beam file.

Index is an index number. Each fun within a module has an unique index. Index is stored in big-endian byte order.

NumFree is the number of free variables.

Module is an encoded as an atom, using ATOM_EXT, SMALL_ATOM_EXT or ATOM_CACHE_REF. This is the module that the fun is implemented in.

OldIndex is an integer encoded using SMALL_INTEGER_EXT or INTEGER_EXT. It is typically a small index into the module's fun table.

OldUniq is an integer encoded using SMALL_INTEGER_EXT or INTEGER_EXT. Uniq is the hash value of the parse tree for the fun.

Pid is a process identifier as in PID_EXT. It represents the process in which the fun was created.

Free vars is NumFree number of terms, each one encoded according to its type.

type NewReference

type NewReference struct {
	Node     Atom
	Creation uint8
	ID       []uint32
}

NewReference wraps the NEW_REFERENCE_EXT tag type (114)

Node and Creation are as in REFERENCE_EXT.

ID contains a sequence of big-endian unsigned integers (4 bytes each, so N' is a multiple of 4), but should be regarded as uninterpreted data.

N' = 4 * Len.

In the first word (four bytes) of ID, only 18 bits are significant, the rest should be 0.

In Creation, only 2 bits are significant, the rest should be 0.

NEW_REFERENCE_EXT was introduced with distribution version 4. In version 4, N' should be at most 12

type Pid

type Pid struct {
	Node     Atom
	ID       uint32
	Serial   uint32
	Creation uint8
}

Pid wraps the PID_EXT tag type (103)

Encode a process identifier object (obtained from spawn/3 or friends). The ID and Creation fields works just like in REFERENCE_EXT, while the Serial field is used to improve safety. In ID, only 15 bits are significant; the rest should be 0.

type Port

type Port struct {
	Node     Atom
	ID       uint32
	Creation uint8
}

Port wraps the PORT_EXT tag type (102)

Encode a port object (obtained form open_port/2). The ID is a node specific identifier for a local port. Port operations are not allowed across node boundaries. The Creation works just like in REFERENCE_EXT.

type Reference

type Reference struct {
	Node     Atom
	ID       uint32
	Creation uint8
}

Reference wraps the REFERENCE_EXT tag type (101)

Encode a reference object (an object generated with make_ref/0).

The Node term is an encoded atom, i.e. ATOM_EXT, SMALL_ATOM_EXT or ATOM_CACHE_REF.

The ID field contains a big-endian unsigned integer, but should be regarded as uninterpreted data since this field is node specific. Creation is a byte containing a node serial number that makes it possible to separate old (crashed) nodes from a new one.

In ID, only 18 bits are significant; the rest should be 0. In Creation, only 2 bits are significant; the rest should be 0. See NEW_REFERENCE_EXT.

type Request

type Request struct {
	Kind      Atom
	Module    Atom
	Function  Atom
	Arguments []Term
}

func UnmarshalRequest

func UnmarshalRequest(r io.Reader) (Request, error)

UnmarshalRequest decodes a BURP from r and returns it as a Request.

type Term

type Term interface{}

func Decode

func Decode(data []byte) (Term, error)

Decode decodes a Term from data and returns it or an error.

Example
i, err := Decode([]byte{131, 97, 42})
if err != nil {
	fmt.Println(err)
}
fmt.Printf("%#v\n", i)
s, err := Decode([]byte{131, 107, 0, 3, 102, 111, 111})
if err != nil {
	fmt.Println(err)
}
fmt.Printf("%#v\n", s)
a, err := Decode([]byte{131, 104, 1, 100, 0, 3, 102, 111, 111})
if err != nil {
	fmt.Println(err)
}
fmt.Printf("%#v\n", a)
Output:

42
"foo"
[]bert.Term{"foo"}

func DecodeFrom

func DecodeFrom(r io.Reader) (Term, error)

DecodeFrom decodes a Term from r and returns it or an error.

Notes

Bugs

  • Full BERT specification compliance is still in progress.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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