iobroker

package
v0.0.1-beta.7 Latest Latest
Warning

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

Go to latest
Published: Oct 22, 2024 License: BSD-3-Clause Imports: 14 Imported by: 0

README

I/O Broker

Adapter between opshell's channels and good old-fashioned io.Reader/io.Writers.

We all do silly things sometimes.

Documentation

Overview

Package iobroker converts io.Read/Writers into opshell channels.

Index

Constants

View Source
const (
	// ShellReadyMessage is what we print when both sides of the shell are
	// connected.
	ShellReadyMessage = "Shell is ready to go!"

	// ShellDisconnectedMessage is what we print when both sides of the
	// shell are gone.
	ShellDisconnectedMessage = "Shell is gone :("
)
View Source
const (
	LMAlreadyConnected = "Connection already established"
	LMDisconnected     = "Disconnected"
	LMDisconnecting    = "Previous shell disconnecting"
	LMIncorrectKey     = "Incorrect key"
	LMKeyMissing       = "Key missing"
	LMNewConnection    = "New connection"
	LMShellIO          = "Shell I/O"
	LMShuttingDown     = "Shutting down"

	LKData         = "data"
	LKDirection    = "direction"
	LKError        = "error"
	LKIncorrectKey = "incorrect_key"
	LKKey          = "key"

	LVInput  sDirection = "input"
	LVOutput sDirection = "output"
)

Log messages, keys, and values.

View Source
const (
	// EVChanLen is number of unsent events we'll buffer before blocking
	// happens.  It may be used for other event channels' buffers as well.
	EVChanLen = 1024
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Broker

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

Broker handles I/O From shells. It ensures only one shell is connected at once, but also makes sure it disconnects properly.

func New

func New(ich <-chan string, och chan<- opshell.CLine) (*Broker, error)

New returns a new Broker, ready for use. New's methods are safe for concurrent usage.

func (*Broker) AddEventListener

func (b *Broker) AddEventListener(ch chan<- Event)

AddEventListener starts events being sent to ch. ch should be buffered.

func (*Broker) ConnectIn

func (b *Broker) ConnectIn(
	ctx context.Context,
	sl *slog.Logger,
	addr string,
	w io.Writer,
	key string,
)

ConnectIn connects w to a shell with the given key, which should match a corresponding call to ConnectOut. Addr is used for logging.

func (*Broker) ConnectInOut

func (b *Broker) ConnectInOut(
	ctx context.Context,
	sl *slog.Logger,
	addr string,
	w io.Writer,
	r io.Reader,
)

ConnectInOut connects a bidirectional connection to a shell. w and r may be the same io.ReadWriter.

func (*Broker) ConnectOut

func (b *Broker) ConnectOut(
	ctx context.Context,
	sl *slog.Logger,
	addr string,
	r io.Reader,
	key string,
)

ConnectOut connects r to a shell with the given key, which should match a corresponding call to ConnectOut.

func (*Broker) Do

func (b *Broker) Do(ctx context.Context) error

Do starts the broker going. Specifically, it starts events processing.

func (*Broker) Errorf

func (b *Broker) Errorf(addr, format string, a ...any)

Errorf sends the message to the shell, in red.

func (*Broker) Logf

func (b *Broker) Logf(addr, format string, a ...any)

Logf sends the message to the shell, in green.

func (*Broker) RemoveEventListener

func (b *Broker) RemoveEventListener(ch chan<- Event)

RemoveEventListener removes ch from the set of channels to which events will be sent. ch will not be closed, but no events will be sent to ch after RemoveEventListener returns.

type Event

type Event struct {
	Type EventType
}

Event is something which happens in this library.

type EventType

type EventType string

EventType speciifes a type of Event.

const (
	EventTypeConnected    EventType = "connected"
	EventTypeDisconnected EventType = "disconnected"
)

Jump to

Keyboard shortcuts

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