Skip to content

Commit

Permalink
small fix on scripting stop func, added quick start guide
Browse files Browse the repository at this point in the history
  • Loading branch information
diogok committed Apr 10, 2017
1 parent b0c9dcc commit 034f742
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 6 deletions.
20 changes: 18 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Evented Scripting Cluster (name subject to change)

This project is an study and attempt to create an replicated cluster of scripts to fire events and commands on and off.
This project is an study and attempt to create a replicated cluster of scripts to fire events and commands on and off.

## Features

Expand All @@ -21,7 +21,23 @@ Soon:
- Docker swarm compatible
- Join mode / non-discovery mode

## Usage
## Quick start

Download the proper binary from the [releases page](https://github.com/diogok/esc/releases):

$ wget https://github.com/diogok/esc/releases/download/0.0.1/esc-amd64 -O esc

Create scripts directory:

$ mkdir scripts

Start the program on proper network interface:

$ ./esc -iface wlan0

Repeat on all nodes, edit the scripts on any one of them.

## Complete usage

Download the binary from the [releases page](https://github.com/diogok/esc/releases).

Expand Down
17 changes: 13 additions & 4 deletions scripting.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,14 @@ type Script struct {
Lua *lua.LState
Listeners []*Listener

Active bool

File string
Hash string
}

func stopScript(script *Script) {
script.Active = false
for _, listener := range script.Listeners {
Off(listener)
}
Expand Down Expand Up @@ -126,11 +129,14 @@ func luaSelf(vm *lua.LState) int {
return 1
}

func luaTicker(vm *lua.LState) int {
func luaTicker(script *Script, vm *lua.LState) int {
sec := vm.ToInt(1)
fun := vm.ToFunction(2)
go func() {
for {
if !script.Active {
return
}
err := vm.CallByParam(lua.P{
Fn: fun,
NRet: 1,
Expand All @@ -156,11 +162,11 @@ func startScript(file string) *Script {
vm := lua.NewState()

script := Script{
Lua: vm,
File: file,
Lua: vm,
File: file,
Active: true,
}

vm.SetGlobal("tick", vm.NewFunction(luaTicker))
vm.SetGlobal("self", vm.NewFunction(luaSelf))
vm.SetGlobal("log", vm.NewFunction(luaLog))
vm.SetGlobal("set", vm.NewFunction(luaSet))
Expand All @@ -171,6 +177,9 @@ func startScript(file string) *Script {
vm.SetGlobal("onCommand", vm.NewFunction(func(vm *lua.LState) int {
return luaOnCmd(&script, vm)
}))
vm.SetGlobal("tick", vm.NewFunction(func(vm *lua.LState) int {
return tick(&script, vm)
}))
vm.SetGlobal("sendEvent", vm.NewFunction(luaSendEvt))
vm.SetGlobal("sendCommand", vm.NewFunction(luaSendCmd))
vm.SetGlobal("sendCommandC", vm.NewFunction(luaSendCmdC))
Expand Down

0 comments on commit 034f742

Please sign in to comment.