arbor

package module
v0.2.1 Latest Latest
Warning

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

Go to latest
Published: May 18, 2019 License: BSD-3-Clause Imports: 9 Imported by: 7

README

Arbor-Go

Build Status GoDoc Go Report Card

This is a Golang library implementation of the Arbor chat protocol. It allows you to send and recieve messages over any io.ReadCloser and io.WriteCloser.

Contribute

PRs and suggestions welcome!

Documentation

Overview

Package arbor is an implementation of the Arbor chat protocol.

This package provides tools for reading and writing Arbor protocol messages from io.Readers and io.Writers, as well as a simple concurrent storage data structure that might be useful to clients or servers.

Index

Constants

View Source
const (
	// WelcomeType should be used as the `Type` field of a WELCOME ProtocolMessage
	WelcomeType = 0
	// QueryType should be used as the `Type` field of a QUERY ProtocolMessage
	QueryType = 1
	// NewMessageType should be used as the `Type` field of a NEW_MESSAGE ProtocolMessage
	NewMessageType = 2
	// NewType is the `Type` of a NEW Message
	NewType = 2
	// MetaType is the `Type` of a META Message
	MetaType = 3
)

Variables

This section is empty.

Functions

func MakeMessageReader

func MakeMessageReader(conn io.ReadCloser) <-chan *ProtocolMessage

MakeMessageReader wraps the io.ReadCloser and returns a channel of ProtocolMessage pointers. Any JSON received over the io.ReadCloser will be unmarshalled into an ProtocolMessage struct and sent over the returned channel. If invalid JSON is received, the ReadCloser will close the io.ReadCloser and the returned channel.

func MakeMessageWriter

func MakeMessageWriter(conn io.Writer) chan<- *ProtocolMessage

MakeMessageWriter wraps the io.Writer and returns a channel of ProtocolMessage pointers. Any ProtocolMessage sent over that channel will be written onto the io.Writer as JSON. This function handles all marshalling. If a message fails to marshal for any reason, or if a write error occurs, the returned channel will be closed and no further messages will be written to the io.Writer.

func NoopRWCloser added in v0.1.4

func NoopRWCloser(in io.ReadWriter) io.ReadWriteCloser

NoopRWCloser wraps an io.ReadWriter with an implementation of io.Closer's Close() method that does nothing. It's like ioutil.NoopCloser but for io.ReadWriters instead of just io.Readers.

Types

type ChatMessage

type ChatMessage struct {
	UUID      string
	Parent    string
	Content   string
	Username  string
	Timestamp int64
}

ChatMessage represents a single chat message sent between users.

func NewChatMessage

func NewChatMessage(content string) (*ChatMessage, error)

NewChatMessage constructs a ChatMessage with the provided content. It's not necessary to create messages with this function, but it sets the timestamp for you.

func (*ChatMessage) AssignID

func (m *ChatMessage) AssignID() error

AssignID generates a new UUID and sets it as the ID for the message.

func (*ChatMessage) Equals added in v0.1.3

func (m *ChatMessage) Equals(other *ChatMessage) bool

Equals compares all message fields to determine whether two messages are the same.

func (*ChatMessage) Reply

func (m *ChatMessage) Reply(content string) (*ChatMessage, error)

Reply returns a new message with the given content that has its parent, content, and timestamp already configured.

type Message added in v0.2.0

type Message ProtocolMessage

Message is a protocol-layer message in Arbor

type Post added in v0.2.0

type Post ChatMessage

Post is a single chat message within Arbor

type ProtocolMessage

type ProtocolMessage struct {
	// Root is only used in WELCOME messages and identifies the root of this server's message tree
	Root string
	// Recent is only used in WELCOME messages and provides a list of recently-sent message ids
	Recent []string
	// The type of the message, should be one of the constants defined in this
	// package.
	Type uint8
	// Major is only used in WELCOME messages and identifies the major version number of the protocol version in use
	Major uint8
	// Minor is only used in WELCOME messages and identifies the minor version number of the protocol version in use
	Minor uint8
	// Message is the actual chat message content, if any. This is currently only
	// used in NEW_MESSAGE messages
	*ChatMessage
	// Meta is the `Meta` field in META type arbor messages.
	Meta map[string]string
}

ProtocolMessage represents a message in the Arbor chat protocol. This may or may not contain a chat message sent between users.

func (*ProtocolMessage) Equals added in v0.1.4

func (m *ProtocolMessage) Equals(other *ProtocolMessage) bool

Equals returns true if other is equivalent to the message (has the same data or is the same message)

func (*ProtocolMessage) IsValid added in v0.1.7

func (m *ProtocolMessage) IsValid() bool

IsValid returns whether the message has the minimum correct fields for its message type.

func (*ProtocolMessage) IsValidMeta added in v0.2.0

func (m *ProtocolMessage) IsValidMeta() bool

IsValidMeta returns whether the message is valid as a META-type protocol message.

func (*ProtocolMessage) IsValidNew added in v0.1.7

func (m *ProtocolMessage) IsValidNew() bool

IsValidNew checks that the message is a valid New message.

func (*ProtocolMessage) IsValidQuery added in v0.1.7

func (m *ProtocolMessage) IsValidQuery() bool

IsValidQuery checks that the message is a valid Query message.

func (*ProtocolMessage) IsValidWelcome added in v0.1.7

func (m *ProtocolMessage) IsValidWelcome() bool

IsValidWelcome checks that the message is a valid Welcome message.

func (*ProtocolMessage) MarshalJSON

func (m *ProtocolMessage) MarshalJSON() ([]byte, error)

MarshalJSON transforms a ProtocolMessage into JSON

func (*ProtocolMessage) String

func (m *ProtocolMessage) String() string

String returns a JSON representation of the message as a string.

type ProtocolReadWriter added in v0.1.4

type ProtocolReadWriter struct {
	*ProtocolReader
	*ProtocolWriter
	// contains filtered or unexported fields
}

ProtocolReadWriter can read and write arbor protocol messages (as JSON) from an io.ReadWriter

func NewProtocolReadWriter added in v0.1.4

func NewProtocolReadWriter(wrap io.ReadWriteCloser) (*ProtocolReadWriter, error)

NewProtocolReadWriter wraps the given io.ReadWriter so that it is possible to both read and write arbor protocol messages to it.

func (*ProtocolReadWriter) Close added in v0.1.4

func (c *ProtocolReadWriter) Close() (err error)

Close both closes the io.ReadWriteCloser wrapped by this ProtocolReadWriter and tears down all protocol-related internal structure. Once you close a ProtocolReadWriter, you must create a new one in order to use it again.

type ProtocolReader added in v0.1.4

type ProtocolReader struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

ProtocolReader reads arbor protocol messages (as JSON) from an io.Reader

func NewProtocolReader added in v0.1.4

func NewProtocolReader(source io.Reader) (*ProtocolReader, error)

NewProtocolReader wraps the source to make serializing *ProtocolMessages easy.

func (*ProtocolReader) Read added in v0.1.4

func (r *ProtocolReader) Read(into *ProtocolMessage) error

Read attempts to read a JSON-serialized ProtocolMessage from the Reader's source into the provided ProtocolMessage. If the provided message is nil, it will error. This method will block until a ProtocolMessage becomes available.

type ProtocolWriter added in v0.1.4

type ProtocolWriter struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

ProtocolWriter writes arbor protocol messages (as JSON) to an io.Reader

func NewProtocolWriter added in v0.1.4

func NewProtocolWriter(destination io.Writer) (*ProtocolWriter, error)

NewProtocolWriter creates a ProtocolWriter by wrapping a destination io.Writer

func (*ProtocolWriter) Write added in v0.1.4

func (w *ProtocolWriter) Write(target *ProtocolMessage) error

Write persists the given arbor protocol message into the ProtocolWriter's backing io.Writer

type ReadWriteCloser added in v0.1.4

type ReadWriteCloser interface {
	ReadWriter
	io.Closer
}

ReadWriteCloser defines the behavior of types that can both emit and consume arbor protocol messages that have a logical "Close" operation (file/socket wrappers, for instance)

type ReadWriter added in v0.1.4

type ReadWriter interface {
	Reader
	Writer
}

ReadWriter defines the behavior of types that can both emit and consume arbor protocol messages

type Reader added in v0.1.4

type Reader interface {
	// Populates the provided ProtocolMessage pointer with the contents of a newly read message
	Read(*ProtocolMessage) error
}

Reader defines the behavior of types that can emit arbor protocol messages

type Store

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

Store is a data structure that holds ChatMessages and allows them to be easily looked up by their identifiers. It is safe for concurrent use.

func NewStore

func NewStore() *Store

NewStore creates a Store that is ready to be used.

func (*Store) Add

func (s *Store) Add(msg *ChatMessage)

Add inserts the given message into the store.

func (*Store) Get

func (s *Store) Get(uuid string) *ChatMessage

Get retrieves the message with a UUID from the store.

type Writer added in v0.1.4

type Writer interface {
	// Consumes the provided ProtocolMessage without modifying it
	Write(*ProtocolMessage) error
}

Writer defines the behavior of types that can consume arbor protocol messages

Jump to

Keyboard shortcuts

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