sdlspec

package module
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Apr 14, 2021 License: MIT Imports: 3 Imported by: 0

README

SDL specification and simulation in GO

GoDoc

The purpose of this package is to allow easy definition and simulation of SDL (Specification and Description Language http://sdl-forum.org/index.htm) processes in GO.

This is an experimental project and is not fully tested yet.

Hello world example

package main

import (
	"fmt"
	"time"

	"github.com/dranidis/sdlspec"
)

type HI struct{}

func helloStates(p *sdlspec.Process) {
	start := sdlspec.State(p, "start", func(s sdlspec.Signal) {
		switch s.(type) {
		case HI:
			fmt.Println("Hello SDL")
		default:
		}
	})
	go start()
}

func main() {
	die := make(chan sdlspec.Signal)
	helloProcess := sdlspec.MakeProcess(helloStates, "hello", die)
	helloProcess <- HI{}

	time.Sleep(1000 * time.Millisecond)
	close(die)
}

The output is:

PROCESS hello AT STATE start: main.HI, {}
Hello SDL

Signals

Each SDL signal is declared as a new go struct type:

type HI struct {}

Processes

A process is created using the sdlspec.MakeProcess function:

	helloProcess := sdlspec.MakeProcess(helloStates, "hello", die)

that takes as a parameter a function like the following:

func helloStates(p *sdlspec.Process) {
	start := sdlspec.State(p, "start", func(s sdlspec.Signal) {
		switch s.(type) {
		case HI:
			fmt.Println("Hello SDL")
		default:
		}
	})
	go start()
}

The function defines a state start using the construction:

	start := sdlspec.State(p, "start", func(s sdlspec.Signal) { ... })

The callback function defines the behaviour at that state. The important part is within the switch statement:

		switch s.(type) {
		case HI:
			fmt.Println("Hello SDL")
		default:
		}

If the received signal is of type HI, print the Hello SDL message. Else ignore the signal. Note that the signal is consumed anyway.

The process is spawned at the initial state with:

	go start()

In the main:

    helloProcess <- HI{}
    time.Sleep(2000 * time.Millisecond)
    close(die)

we send the signal HI{} to the process, sleep for 2 secs and terminate all SDL processes by closing the die channel.

Documentation

Overview

Package sdlspec is an attempt to simulate SDL specifications in GO.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ChannelConsumer

func ChannelConsumer(die chan Signal, n string, p chan Signal)

ChannelConsumer reads all signals at channel p and logs them at std out together with the name of the consumer

func DefaultMessage

func DefaultMessage(p *Process, s Signal)

DefaultMessage is a helper function for printing a message that it is consumed as a default action at a switch signal.

func DieChannel

func DieChannel(p *Process) chan Signal

DieChannel returns the channel for the termination of the process.

func DisableLogging

func DisableLogging()

DisableLogging turns logging off.

func EnableLogging

func EnableLogging()

EnableLogging turns logging on.

func Execute

func Execute(ts ...Transmission)

Execute exetutes a number of Transmissions.

func Ignored

func Ignored(p *Process, s Signal)

Ignored is a helper function to print a message for ignored (consumed) messages. It is placed within the default section of a switch of a state. It prints only when Logging is enabled.

func MakeBuffer

func MakeBuffer() chan Signal

MakeBuffer creates and returns a buffer for asynchronous communication Buffersize is defined by SetBufferSize

func MakeProcess

func MakeProcess(states func(*Process), name string, die chan Signal) chan<- Signal

MakeProcess accepts a process definition and a name. It also receives a signal channel used for termination. All processes sharing the same die channel will terminate when close(die) is called. It initializes the buffer where the process is reading from. It returns the buffer of the process so that other processes can write to it.

func SendSignalsWithDelay

func SendSignalsWithDelay(c chan<- Signal, ss []Signal, ms time.Duration)

SendSignalsWithDelay sends all the signals in the signal list to channel c with a delay between each transmission equal to ms milliseconds

func SetBufferSize

func SetBufferSize(s int)

SetBufferSize sets the size of process buffers. Default is 100

func State

func State(p *Process, name string, f func(s Signal)) func()

State function receives the process and a callback function on the signal to be consumed from the buffer. It returns a function that will be called by the process when entering the state. State returns when the channel Done is closed.

Types

type Process

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

Process is a type encapsulating the buffer of a process and the name of a process.

type Signal

type Signal interface{}

Signal is the main structure communicated on channels. It can be any type.

type Transmission

type Transmission struct {
	MsDelay  int
	Receiver chan<- Signal
	Signal   Signal
}

Transmission is used for simulations. Defines a delay in ms, after which the signal is sent to the receiver channel. Executed with the Execute method for one Trasmission or with the Execute function for a variant number of Transmissions.

func (Transmission) Execute

func (t Transmission) Execute()

Execute executes a single Transmission.

Jump to

Keyboard shortcuts

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