Logo Anfibia Reactor ™ - User Guide
Copyright © 2012 Anfibia Software. All Rights Reserved.
www.anfibia-soft.com
cpu Lua in Reactor

Starting with Reactor 4, you have the possibility to create your own monitors by writing plugins in Lua. In other words, write your own plugins to fit your own needs.

In order to create plugins, you'd need to write them in Lua, which is a well-known scripting language. If programming is not your cup of tea, feel free to contact us with your requirements, or check the included plugins, a list that will keep growing.

Lua is a powerful and lightweight programming language. It supports general procedural programming as well as object-oriented and functional paradigms. It has automatic memory management (garbage collection) so you do not have to worry about allocating and deallocating memory.

-- Lua code example
print("Hello World!")

function factorial(n)
  if n == 0 then
    return 1
  else
    return n * factorial(n - 1)
  end
end

Lua Plugins in Reactor

A Reactor plugin consists of a Lua script that implements a function named plugin. This function will be called every time the monitor is executed, and it will receive the parameters specified in the monitor URL, if any. The function must return -at least- a boolean value indicating if the monitoring was successful. Optionally, it can also return a responseText (string) and a responseCode (number) with extra details as a result of the execution.

function plugin(options)
  -- do some stuff
  
  return success, responseText, responseCode
end

Brief Introduction to Lua

Lua is dynamically typed, meaning that values have types, but variables do not.
There are eight basic types in Lua: nil, boolean, number, string, function, userdata, thread, and table. Although only the following 6 are normally used in Reactor plugins:

Local variables are defined with the local keyword:
local message = "Hello world!"

Some basic notions:

Lua provides you with the most commonly used control structures. The if instruction for conditionals and while, repeat, and for for iteration. All control structures have an explicit terminator: end terminates the if, for and while structures; and until terminates the repeat structure.

-- if condition then
if a < 0 then 
  a = 0 
end
    
-- if condition then else
if a < b then 
  return a 
else 
  return b 
end

-- while
local i = 1
while not isProcessFinished() or (i > maxTries) do
  tryAgain()
  i = i + 1
end

-- repeat until
repeat
  line = readData()
until line ~= ""

-- numeric for: for var=init, limit, step do
for i=100, 1 , -1 do 
  print(i) 
end

-- generic for: for namelist in explist do
days = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"}
revDays = {}
for i,v in ipairs(days) do
  revDays[v] = i
end

Finally, Lua supports multiple assignments in one statement. The syntax for assignment defines a list of variables on the left side and a list of expressions on the right side, separated by commas.

x, name, result = 42 , "Reactor" , false
There is a specific concatenation operator, indicated by 2 dots ('..'), that is used for concatenating strings. The '+' operator is only for addition in Lua and using it with strings will cause a runtime error.
local searchWord = "Lua"
local baseUrl = "http://en.wikipedia.org/w/index.php"
local url = baseUrl .. "?title=" .. searchWord

References

Lua official website
Lua 5.1 Reference Manual
Programming in Lua (online version)
Lua Unofficial FAQ