pit: introduction

In this series I will present pit, a of Swiss Army knife kind of software package. In short, it is a scripting environment supporting an assortment of optional dynamic loadable modules that provide various functionalities, targeting both Linux and Windows. Lua is the first supported scripting language, but others can be added in the future. The software is licensed under GPL v3.0 and is available for download on github. This first article shows how to download, compile and run a first test program on a Linux environment. Assuming you already have git installed, open a terminal shell and clone the repository (the ‘$’ symbol here represents the shell prompt):

$ git clone https://github.com/migueletto/pit.git
$ cd pit

Now you must compile pit. There is not an automated build process yet, so for now you must run make on a few directories:

$ cd src/libpit
$ make
$ cd ../main
$ make
$ cd ../lua-5.3.5
$ make
$ cd ../libtimer
$ make
$ cd ../..

After each make you will some output as each source file is compiled. If everything went well, you will find a ‘pit’ executable inside the bin directory and some ‘.so’ libraries inside the lib directory. A simple shell script called ‘run’ lets you try it out. It accepts the script name as argument. There is a sample script timer.lua inside the script directory. Let’s try it out:

$ ./run script/timer.lua
2021-03-03 00:03:20.424 I 01872 MAIN MAIN: pit starting on Linux (little endian)
2021-03-03 00:03:20.434 I 01872 MAIN SYS: library libscriptlua.so loaded
2021-03-03 00:03:20.435 I 01872 MAIN LUA: Lua 5.3.5
2021-03-03 00:03:20.435 I 01872 MAIN LUA: integer type is 4 bytes
2021-03-03 00:03:20.436 I 01872 MAIN LUA: floating point type is 8 bytes
2021-03-03 00:03:20.437 I 01872 MAIN SYS: library libtimer.so loaded
2021-03-03 00:03:20.437 I 01872 MAIN SCRIPT: libtimer library initialized
2021-03-03 00:03:20.438 I 01872 MAIN THREAD: thread sock 2 bound to port 63065
2021-03-03 00:03:20.439 I 01872 MAIN MAIN: idle loop begin
2021-03-03 00:03:20.439 I 01660 TIMER THREAD: thread port 63065 begin
2021-03-03 00:03:21.454 I 01660 TIMER RUN: timer 0
2021-03-03 00:03:22.457 I 01660 TIMER RUN: timer 1
2021-03-03 00:03:23.459 I 01660 TIMER RUN: timer 2
2021-03-03 00:03:24.440 I 01660 TIMER RUN: timer 3

If you see something like this, congratulations. Press Ctrl-C to stop the script (you will see some more debug outputs on the terminal, but don’t worry, it is normal). Now let’s take a look at timer.lua to understand what is going on:

timer = pit.loadlib("libtimer")
n = 0

function timer_callback()
pit.debug(1, "timer " .. n)
n = n + 1
end

timer.create(timer_callback, 1000)

The first line loads the external timer library we compiled earlier. A handle to the library is assigned to variable ‘timer’. Next an integer ‘n’ is declared and assigned to 0. Next is a function definition. This function will be called as a callback from within the timer library. The last line calls the ‘create’ function from the timer library, specifying out callback function and the timer period in milliseconds. The main script ends execution here, but the timer we created runs on its own thread. If you let it run, timer_callback will be called every second. This function uses the ‘debug’ function to print a line on the output. The line is concatenation of string ‘timer’ with a numeric value that is incremented afterwards.

If you plan to follow this series and are not familiar with the Lua language, I recommend you take a look at official Lua documentation. It is a nice, little, yet powerful scripting language (one that does not require hundreds of megabytes just to print hello 😉

You can read more articles on this subject by selecting category ‘pit’ on the right column.

 

Leave a comment