messageStream

package module
v0.0.0-...-60d8fa7 Latest Latest
Warning

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

Go to latest
Published: Feb 9, 2021 License: MIT Imports: 11 Imported by: 2

README

messageStream

Message passing over TCP written in Go

Documentation

Index

Constants

View Source
const CausalityAttributeName = "Causality"
View Source
const LogEndPoint = "EndPoint"
View Source
const LogMessages = "Messages"
View Source
const LogService = "Service"
View Source
const RequestIdAttributeName = "_RequestId"

Variables

View Source
var ErrEndPointDisconnected = fmt.Errorf("EndPoint disconnnected")

Functions

This section is empty.

Types

type Attributes

type Attributes map[string]interface{}

type BroadcastPacket

type BroadcastPacket struct {
	Element *Element
	Service *Service
}

func (*BroadcastPacket) Broadcast

func (broadcastPacket *BroadcastPacket) Broadcast(sendPredicate func(endPoint *EndPoint, packet *BroadcastPacket) bool) error

func (BroadcastPacket) String

func (packet BroadcastPacket) String() string

type Element

type Element struct {
	Name       string
	Attributes Attributes
	Children   []interface{}
}

func NewElement

func NewElement(name string, content ...interface{}) *Element

Create new element with a given name You can add any number of attributes or child elements

For example:

element := ms.NewElement("Message", &Attributes{"Type" : "Warning", "Context" : "Unknwon", "Id": 1}, NewElement("Child"))

func (*Element) Add

func (element *Element) Add(content ...interface{}) *Element

Add content (element or attributes) to an element

func (*Element) GetAttribute

func (element *Element) GetAttribute(name string, defaultValue string) string

func (*Element) GetBoolAttribute

func (element *Element) GetBoolAttribute(name string, defaultValue bool) bool

func (*Element) GetChild

func (element *Element) GetChild(name string) *Element

func (*Element) GetChildren

func (element *Element) GetChildren(name string) []*Element

func (*Element) GetIntAttribute

func (element *Element) GetIntAttribute(name string, defaultValue int) int

func (*Element) GetValue

func (element *Element) GetValue() string

func (*Element) RemoveAttribute

func (element *Element) RemoveAttribute(name string)

func (*Element) SetAttribute

func (element *Element) SetAttribute(name string, value interface{})

func (*Element) SetAttributes

func (element *Element) SetAttributes(setMap map[string]interface{})

func (Element) String

func (element Element) String() string

type EndPoint

type EndPoint struct {
	Connection net.Conn

	Service *Service

	Id   int
	Info interface{}

	UseCausality bool // Use Causality attribute in addition to _RequestID for backward compatability
	// contains filtered or unexported fields
}

EndPoint - a message stream end point is used to send/receive messages to another end point

func NewEndPoint

func NewEndPoint(name string, connection net.Conn) *EndPoint

Create a new end point and associate it with a connection

name - the end point's name. When calling NewEndPoint from a service createEndPoint call back,

you can use an empty string to assign default name (serivce name with end point id suffix)

Usually you will assign packet recieved callback also do not forget to start the end point if not created in the context of a service createEndPoint callback

Example:

	conn, err := net.Dial("tcp", "someserver.com")
 endPoint := ms.NewEndPoint("MyConnection", conn).OnPacketReceived(func (packet *msPacket) {
		fmt.Println("Received:", packet)
	}).Start()

func (*EndPoint) AddLoggingHandler

func (endPoint *EndPoint) AddLoggingHandler(defaultLoggers []string) *EndPoint

Add an handler for _Log messages

The message has the following format

_Log [Name={list of to log}] [Scope="EndPoint"|"Service"] [Disable=True|False]

Default scope is Service (logging of this type will be applied to all EndPoint created by this service) If no specific logger Names are given, MessageStream related logs along with a supplied list of logger names will be enabled (or disabled)

func (*EndPoint) Close

func (endPoint *EndPoint) Close() error

Close the end point and disconnect it

func (*EndPoint) DisableLogging

func (endPoint *EndPoint) DisableLogging(names string)

Disable a logger with a given name. If logger is disable then things of this type will not be logged

For example:

endPoint.DisableLogger("Messages") will stop logging of all messages received/sent by the end point

func (*EndPoint) EnableLogging

func (endPoint *EndPoint) EnableLogging(names string) *EndPoint

Enable a logger with a given name. If logger is enabled then things of this type will be logged

For example:

endPoint.EnableLogger("Messages") will cause all messages received/sent by the end point to be logged

func (*EndPoint) GetActiveLoggerNames

func (endPoint *EndPoint) GetActiveLoggerNames() []string

Get a list of all the logger which are enabled, both loggers which are enabled just for this end point and those enabled for the service

func (*EndPoint) GetLoggerNames

func (endPoint *EndPoint) GetLoggerNames() []string

Get a list of all the loggers which are enabled for this end point (this does not include loggers which are enabled for the service)

func (*EndPoint) IsConnected

func (endPoint *EndPoint) IsConnected() error

Is EndPoint connected

func (*EndPoint) IsStarted

func (endPoint *EndPoint) IsStarted() bool

Is EndPoint started

func (*EndPoint) Log

func (endPoint *EndPoint) Log(name string) OptionalLogger

Get a logger with a specific name.

If the logger is enabled, a logger that does something is returned If the logger is not enabled, a logger which does nothing is returned

A typical usage:

endPoint.Log("LogTemperatures").Println("It is hot today")

A logger can be enabled either in end point scope (only for this specific endpoint) or in a service scope (for all endpoint created by the service)

func (*EndPoint) Message

func (endPoint *EndPoint) Message(content ...interface{}) *Packet

Create a message that can be sent

endPoint.Message(Attributes{"Type" : "SundayArrived"}).Send()

func (*EndPoint) Name

func (endPoint *EndPoint) Name() string

Get the Endpoint's name

func (*EndPoint) OnClose

func (endPoint *EndPoint) OnClose(f func(endPoint *EndPoint)) *EndPoint

Add end point close handler (function called when end point is closed)

end point is either explicitly closed by calling endPoint.Close() or when the connection is terminated

func (*EndPoint) OnPacketReceived

func (endPoint *EndPoint) OnPacketReceived(f func(packet *Packet) bool) *EndPoint

Add packet receive handler (function called when a new packet is received)

func (*EndPoint) Request

func (endPoint *EndPoint) Request(content ...interface{}) *Packet

Create a request packet that can submitted

reply := <- endPoint.Request(Attributes{"Type" : "FindMyWife"}).Submit()

func (*EndPoint) SetName

func (endPoint *EndPoint) SetName(name string) *EndPoint

Set the EndPoint name

func (*EndPoint) Start

func (endPoint *EndPoint) Start() *EndPoint

Start (activate the end point)

The following code is typical for creating a client side end point

	conn, err := net.Dial("tcp", "someserver.com")
 endPoint := ms.NewEndPoint("MyConnection", conn).OnPacketReceived(func (packet *msPacket) {
		fmt.Println("Received:", packet)
	}).Start()

func (*EndPoint) String

func (endPoint *EndPoint) String() string

type NullLogger

type NullLogger struct{}

func (NullLogger) Fatal

func (l NullLogger) Fatal(v ...interface{})

func (NullLogger) Fatalf

func (l NullLogger) Fatalf(f string, v ...interface{})

func (NullLogger) Fatalln

func (l NullLogger) Fatalln(v ...interface{})

func (NullLogger) Flags

func (l NullLogger) Flags() int

func (NullLogger) Output

func (l NullLogger) Output(depth int, s string) error

func (NullLogger) Panic

func (l NullLogger) Panic(v ...interface{})

func (NullLogger) Panicf

func (l NullLogger) Panicf(f string, v ...interface{})

func (NullLogger) Panicln

func (l NullLogger) Panicln(v ...interface{})

func (NullLogger) Prefix

func (l NullLogger) Prefix() string

func (NullLogger) Print

func (l NullLogger) Print(v ...interface{})

func (NullLogger) Printf

func (l NullLogger) Printf(f string, v ...interface{})

func (NullLogger) Println

func (l NullLogger) Println(v ...interface{})

func (NullLogger) SetFlags

func (l NullLogger) SetFlags(flag int)

func (NullLogger) SetOutput

func (l NullLogger) SetOutput(w io.Writer)

func (NullLogger) SetPrefix

func (l NullLogger) SetPrefix(prefix string)

func (NullLogger) Writer

func (l NullLogger) Writer() io.Writer

type OptionalLogger

type OptionalLogger interface {
	Fatal(v ...interface{})
	Fatalf(f string, v ...interface{})
	Fatalln(v ...interface{})
	Flags() int
	Output(depth int, s string) error
	Panic(v ...interface{})
	Panicf(f string, v ...interface{})
	Panicln(v ...interface{})
	Prefix() string
	Print(v ...interface{})
	Printf(f string, v ...interface{})
	Println(v ...interface{})
	SetFlags(flag int)
	SetOutput(w io.Writer)
	SetPrefix(prefix string)
	Writer() io.Writer
}

Interface for something that act as a logger, this can be either the logger returned by log.NewLogger or null logger which log nothing

type Packet

type Packet struct {
	Element  *Element
	EndPoint *EndPoint
}

func (*Packet) Exception

func (requestPacket *Packet) Exception(exception error, content ...interface{}) *Packet

Create exception packet

func (*Packet) GetRequestId

func (requestPacket *Packet) GetRequestId() (int, error)

Return requestID (if one exists)

func (*Packet) GetType

func (packet *Packet) GetType() string

Get message/request type

func (*Packet) IsRequest

func (packet *Packet) IsRequest() bool

func (*Packet) OptionalReply

func (maybeRequestPacket *Packet) OptionalReply(content ...interface{}) *Packet

OptionalReply - create reply packet if the sending packet is request. Otherwise, return null

func (*Packet) Reply

func (requestPacket *Packet) Reply(content ...interface{}) *Packet

Create a reply to a request

didSundayArrivedRequest.Reply(Attributes{"Answer" : true}).Send()

func (*Packet) Send

func (packet *Packet) Send() error

Actions

Send either a message or reply packet.

Sending a message:
 endPoint.Message(Attributes{"Type", "SundayArrived"}).Send()

Reply to a request
 pingRequest.Reply(Attributes{"Type", "PingResult"}).Send()

func (Packet) String

func (pakcet Packet) String() string

func (*Packet) Submit

func (packet *Packet) Submit(ctx ctx.Context) chan SubmitResult

Submit request and get a channel on which reply is received

For example: pingResult := <- endPoint.Request(Attributes{"Type" : "Ping"}).Submit()

type Service

type Service struct {
	Name string
	Info interface{}
	// contains filtered or unexported fields
}

Service - wait for connection request, associate each incoming connection request with an end point which then processes received messages

func NewService

func NewService(name string, network string, address string, info interface{}, createEndPoint func(service *Service, conn net.Conn) *EndPoint) *Service
 Create a new service (server side):

 name - service name
 metwork, address - where sevice will listen
 createEndPoint - function that associats end point with accepted connection

 Example:

 myService := ms.NewService("My service", "tcp", ":1000", func (service *ms.Service, conn ney.Conn) *ms.EndPoint) {
		return ms.NewEndPoint("", conn).OnPacketReceived(func (packet *ms.Packet) {
			// Do something with the packet...
		})
	})

func (*Service) Broadcast

func (service *Service) Broadcast(packet *BroadcastPacket, sendPredicate func(endPoint *EndPoint, packet *BroadcastPacket) bool) error

Broadcast a packet to all streams connected to a service

func (*Service) DisableLogging

func (service *Service) DisableLogging(names string)

Disable a logger with a given name. If logger is disable then things of this type will not be logged

For example:

servicet.DisableLogger("Service") will stop logging of service state related messages

func (*Service) EnableLogging

func (service *Service) EnableLogging(names string) *Service

Enable a logger with a given name. If logger is enabled then things of this type will be logged

For example:

service.EnableLogger("Service") will cause all messages assocaited with service state to be logged

func (*Service) GetLoggerNames

func (service *Service) GetLoggerNames() []string

Get a list of all the loggers which are enabled for this service

func (*Service) Log

func (service *Service) Log(name string) OptionalLogger

Get a logger with a specific name.

If the logger is enabled, a logger that does something is returned If the logger is not enabled, a logger which does nothing is returned

A typical usage:

service.Log("LogTemperatures").Println("It is hot today")

func (*Service) Message

func (service *Service) Message(content ...interface{}) *BroadcastPacket

Create message that is suitable for broadcasting

service.Bradcast(Message(Attributes{"Type": "SundayArrived"}))

func (*Service) Terminate

func (service *Service) Terminate()

Terminate a service

type SubmitResult

type SubmitResult struct {
	Reply *Packet
	Error error
}

Jump to

Keyboard shortcuts

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