Ejemplo n.º 1
0
//handleMessageCB brokers messages back to the lua script that asked for them
func handleMessageCB(index int, message LocalPatternMatch) {
	l := CBTable[index].Script.State
	lmsg := luar.New(l, message)

	if err := l.CallByParam(lua.P{
		Fn:      CBTable[index].Func,
		NRet:    0,
		Protect: false,
	}, lmsg); err != nil {
		panic(err)
	}
}
Ejemplo n.º 2
0
//luaMain creates a new lua state for each file in ./lua
//and hands them the globals they need to interact with lazlo
func luaMain(b *lazlo.Broker) {
	broker = b
	var luaDir *os.File
	luaDirName := "lua"
	if luaDirInfo, err := os.Stat(luaDirName); err == nil && luaDirInfo.IsDir() {
		luaDir, _ = os.Open(luaDirName)
	} else {
		lazlo.Logger.Error("Couldn't open the Lua Plugin dir: ", err)
	}
	luaFiles, _ := luaDir.Readdir(0)
	for _, f := range luaFiles {
		if f.IsDir() {
			continue
		}

		file := fmt.Sprintf("%s/%s", luaDirName, f.Name())

		//make a new script entry
		script := LuaScript{
			Robot: &Robot{
				ID: len(LuaScripts),
			},
			State: lua.NewState(),
		}
		defer script.State.Close()

		// register hear and respond inside this lua state
		script.State.SetGlobal("robot", luar.New(script.State, script.Robot))
		//script.State.SetGlobal("respond", luar.New(script.State, Respond))
		//script.State.SetGlobal("hear", luar.New(script.State, Hear))
		LuaScripts = append(LuaScripts, script)

		// the lua script will register callbacks to the Cases
		if err := script.State.DoFile(file); err != nil {
			panic(err)
		}
	}
	//block waiting on events from the broker
	for {
		index, value, _ := reflect.Select(Cases)
		handle(index, value.Interface())
	}
}