A Day in a Pile of Work

My personal Web development blog

Fifth week update 18/05/15 to 29/05/15 in Valum

These past two weeks, I have bee working on a new release 0.2.0-alpha and fixed a couple of bugs in the last release.

To make thing simple, this report will cover what have been introduced in 0.1.1-alpha and 0.2.0-alpha releases separately.

The 0.2.0-alpha should be released by the fifth of june (05/05/15) and will introduce the so awaited asynchronous processing model.

As of right now

I have fixed a couple of bugs, backported minor changes from the 0.2.0-alpha and introduced minor features for the 0.1.1-alpha release.

Here’s the changelog:

e66277c Route must own a reference to the handler.
ec89bef Merge pull request #85 from valum-framework/0.1/methods
b867548 Documents all and methods for the Router.
42ecd2f Binding a callback to multiple HTTP methods.
e81d4d3 Documents how errors are handled with the next continuation.
5dd296e Example using the next continuation.
ec16ea7 Throws a ClientError.NOT_FOUND in process_routing.
fb04688 Introduces Next in Router.
79d6ef5 Support HTTPS URI scheme for FastCGI.
29ce894 Uses a synchronous request processing model for now.
e105d00 Configure option to enable threading.
5f8bf4f Request provide HTTPVersion information.
0b78178 Exposes more GObject properties for VSGI.
33e0864 Renames View splice function for stream.
3c0599c Fixes a potential async bug with Soup implementation.
91bba60 Server documentation.

It is not possible to access the HTTP version in the Request, providing useful information about available features.

I fixed the --threading option and submitted a patch to waf development that got merged in their trunk.

FastCGI implementation honors the URI scheme if it sets the HTTPS environment variable. This way, it is possible to determine if the request was secured with SSL.

I enforced a synchronous processing model for the 0.1.* branch since it’s not ready yet.

It is now possible to keep routing if we decide that a handler does not complete the user request processing. The next continuation is crafted to continue routing from any point in the route queue. It will also propagate Redirection, ClientError and ServerError up the stack.

app.get ("", (req, res, next) => {
    next ();
});

app.get ("", (req, res) => {
    res.write ("Hello world!".data);
});

It is now possible to connect a handler to multiple HTTP methods at once using all and methods functions in the router.

The Route is safer and keep a strong reference to the handler callback. This avoid a potentially undesired deallocation.

Changes for the next release

The next release 0.2.0-alpha will focus on the asynchronous processing model and VSGI specification.

In short,

  1. the server receives a user request
  2. the request is transmitted to the application with a continuation that release the request resources
  3. the application handles the pair of request and response:
    • it may invoke asynchronous processings
    • it returns as fast as possible the control to the server and avoid any synchronous blocking on I/O
    • it must invoke the end continuation when all processing have completed so that the server can release the resources
  4. the server is ready to receive a new request

The handler is purely synchronous, this is why it is not recommended to perform blocking operations in it.

app.get ("", (req, res, end) => {
    res.write ("Hello world!".data);
    res.close ();
    end ();
});

This code should be rewritten with write_async and close_async to return the control to the server as soon as possible.

app.get ("", (req, res, end) => {
    res.write_async ("Hello world".data, Priority.DEFAULT, null,
                 () => {
        res.close_async (Priority.DEFAULT, null, () => {
            end ();
        })
    });
});

Processing asynchronously has a cost, because it delegates the work in an event loop that awaits events from I/O.

The synchronous version will execute faster, but it will not scale well with multiple requests and significant blocking. The asynchronous model will outperform this easily due to a pipeline effect.

VSGI improvments

Request and Response now have a base_stream and expose a body that may filter what’s being written in the base_stream. The libsoup-2.4 implementation uses that capability to perform chunked transfer encoding.

There is no more inheritence from InputStream or OutputStream, but this can be reimplemented using FilterInputStream and FilterOutputStream.

I have implemented a ChunkedConverter to convert data into chunks of data according to RFC2126 section 3.6.1.

It can also be used to do transparent gzip compression using the ZlibCompressor.

Soup reimplementation

The initial implementation was pretty much a prototype wrapping a MessageBody with an OutputStream interface. It is however possible to steal the connection and obtain an IOStream that can be exposed.

MessageBody would usually worry about transfer encoding, but since we are working with the raw streams, some work will have to be done in order to provide that encoding capability.

In HTTP, transfer encoding determines how the message body will be transmitted to its recipient. It provides information to the client about what amount of data will be transfeered.

Two possible transfeer encoding exist:

  • use the Content-Length header, the recipient expects to receive that number of bytes
  • use chunked in Transfer-Encoding header, the recipient expects to receive a chunk size followed by the content of the chunk

TCP guarantees the order of packets and thus, the order of the received chunks.

I implemented an OutputStream capable of encoding written data according to the header of the response. It can be composed with other streams from the GIO api, which is more flexible than a MessageBody.

The response exposes two streams:

  • output_stream, the raw and protected stream
  • body, the public and safe for transporting the message body

Some implementations (CGI, FastCGI, etc…) delegate the transfer encoding responsbiility to the HTTP server.

Status handling

The setup and teardown approach have been deprecated in favour of next continuation and status handling.

Handler can be connected to status thrown during the processing of a request.

If a status handler throws a status, it will be captured by the Router. This can be used to cancel the effect of a redirection for instance.

Likewise, status handling can invoke end to end the request processing and next to delegate the work to the next status handler in the queue.

app.status (404, (req, res, end) => {
    end ();
});

app.status (302, (req, res, next) => {
:
});

Roadmap (long-term stuff)

More to come, but I have already a few ideas

  • handling of multipart/* messages issue #81
  • polling for FastCGI issue #77
  • implementation for SCGI issue #60
  • middlewares issue #51
  • reverse rule-based routes issue #45
  • get CTPL Vala bindings right with GI (GObject Introspection)
  • more converters for more common web encoding (base64, urlencoded, etc…)

FastCGI streams can benefit from polling and reimplementing them is planned. APIs would remain the same as all would happen under the hood.

Reversing rules and possibly regular expression would make URLs in web application much easier to maintain.

CTPL has a hand-written binding and it would be great to just generate them with GI.

Posted on .

Third week update! 11/05/15 to 22/05/15 in Valum

In the past two weeks, I’ve been working on the roadmap for the 0.1.0-alpha release.

gcov

gcov has been fully integrated to measure code coverage with cpp-coveralls. gcov works by injecting code during the compilation with gcc.

You can see the coverage on coveralls.io, it’s updated automatically during the CI build.

Current master branch coverage: Coverage Status

The inconvenient is that since coveralls measures coverage from C sources using valac generated C code, it is not possible to identify which regions are covered in Vala. However, it is still possible to identify these regions in the generated code.

Asynchronous handling of requests

I changed the request handling model to be fully asynchronous. VSGI.Application handler have become an async function, which means that every user request will be processed concurrently as the server can immediatly accept a new request.

Merged glib-application-integration in the trunk

The branch was sufficiently mature to be merged in the trunk. I will only work on coverage and minor improvements until I reach the second alpha release.

It brings many improvements:

  • VSGI.Server inherit from GLib.Application, providing enhancements described in the Roadmap for 0.1.0-alpha
  • setup and teardown in the Router for pre and post processing of requests
  • user documentation improvments (Sphinx + general rewrites)
  • optional features based on gio-2.0 and libsoup-2.4 versions

0.1.0-alpha released!

I have released a 0.1.0-alpha version. For more information, you can read the release notes on GitHub, download it and try it out!

Posted on and tagged with gcc.

Roadmap for 0.1.0-alpha in Valum

0.0.1 is far behind what will be introduced in 0.1.0-alpha. This release will bring new features and API improvements.

We are releasing a new alpha since the first version was a working but incomplete prototype.

Along with the changes already introduced, the release will be ready as soon as the following will be done:

  • merge complete FastCGI integration in the trunk, which include integration of GLib.Application in the server design
  • api documentation (improvments and merge of valadoc branch)
  • improve user documentation
  • more tests and a measured coverage with gcov

Integration of GLib.Application is really cool. It basically provide any written application with a GLib.MainLoop to process asynchronous tasks and signals to handle startup and shutdown events right from the Server.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
using Valum;
using VSGI.Soup;

var app    = new Router ();
var server = new Server (app);

// unique identifier for your application
app.set_application_id ("your.unique.application.id");

app.get("", (req, res) => {
    res.write ("Hello world!".data);
});

server.startup.connect (() => {
    // no request have been processed yet
    // initialize services here (eg. database, memcached, ...)
});

server.shutdown.connect (() => {
    // called after the mainloop finished
    // all requests have been processed
});

server.run ();

Moreover, application can access a DBusConnection and obtain environment data or request external services.

This sample uses the org.freedesktop.hostname DBus service to obtain information about the hosting environment. Note that you can use DBus to perform IPC between workers fairly easily in Vala.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
var connection = server.get_dbus_connection ();

app.get ("hostname", (req, res) => {
    // asynchronous dbus call
    connection.call.begin (
        "org.freedesktop.hostname",  // bus name
        "/org/freedesktop/hostname", // object path
        "org.freedesktop.hostname",  // interface
        "Hostname",
        null, // no arguments
        VariantType.STRING, // return type
        DBusCallFlags.NONE,
        -1, // timeout
        null,
        (obj, r) => {
            var hostname = connection.call.end (r);
            res.write (hostname.get_string ().data);
        });
});

GLib.Application are designed to be held and released so that it can quit automatically whenever it’s idle (with a possible timout). Gtk uses it to count the number of opened windows, we use it to measure the number of processing requests.

Past a certain timeout after the last release, the worker will terminate.

If you have a long-running operation to process asynchronously that does not involve writting the response (in which case, you are better blocking), you have to hold the application to keep it alive while it’s processing.

What next?

The next release will be more substantial:

  • middlewares
  • components (if relevant)
  • improve VSGI specification
    • more signals to handle external events
    • better documentation to guide implementations
  • new VSGI implementations (SCGI & CGI)
  • extract VSGI (if ready)

I decided to go ahead for a Mustache implementation that targets GLib and GObject. I’m still surprised that it hasn’t been done yet. It is clearly essential to bring Vala in general purpose web development. The development will be in a separate project here on GitHub and it will not block the release of the framework.

GResource API is really great and it would be truly amazing to bundle Mustache templates like we already do with CTPL.

Posted on .

First week update! 04/05/15 to 08/05/15 in Valum

As part of the first week, I have to produce an initial document describing what I will be working on during the semester. Once it’s written down, I will post it on this blog.

I have already a good idea of what I would like to work on:

  • finish VSGI specification
  • second alpha release & feedback from communities
  • SCGI implementation
  • mustache implementation for GLib
  • more tests and awesomeness

The first alpha release is already 4 years old and this one bring such radical changes that we’re almost starting over. Therefore, a second alpha release will permit us to tease the targeted audience and obtain recommendations to build the very best framework.

SCGI is a very simple protocol to communicate HTTP messages over streams. It will take a real advantage of the GIO stream API and I am sure this could become an efficient way to serve web application in production.

Mustache (or any templating engine) is essential if we want to bring Valum outside the web service development. I plan to provide a GLib implementation so that it can be used anywhere. CTPL will remain the default templating engine for its simplicity and convenience as it covers quite well simple UI requirements.

Testing is part of any sane software development process. I will focus on providing quality software that does not break easily.

Subsequent weeks will contain more sustained posts that will describe what have been done, so stay put!

Posted on and tagged with Vala.

Project accepted! in Valum

The project was accepted!

I will officially work on Valum this summer under the direction of François Major at the IRIC lab.

As part of the evaluation, I will be have the opportunity to demonstrate the framework potential with an assignment. Also, I am required to keep track of the project advancements, so keep in touch with the Valum category of my blog!

Posted on and tagged with valum.

Presentation of Valum at the IRIC lab team in Valum

I have presented the framework to the IRIC team. They do research in bio-informatics.

I covered the following points:

  • history of Vala
  • presentation of Valum
  • code examples
  • middleware architecture
  • framework architecture
  • justification
  • objectives
  • application

You can access the presentation PDF and the source in Markdown. It is generated by pandoc, beamer and LaTeX.

Build the template with:

pandoc -t beamer -V theme:Rochester --latex-engine xelatex -o presentation.pdf presentation.md

Posted on and tagged with vala and web.

Just discovered.. zsh!

I never thought I would find zsh actually that great. I feel like I’ve been missing a nice prompt since ages.

I would like to cover my first experience a little and show you how you can turn your default shell into a powerful development tool. In order to do that, you have to:

  1. install zsh
  2. get a real plugin manager (antigen here!)
  3. get a really nice and powerful prompt
  4. enjoy all the above!

zsh is quite easy to install using your distribution package manager:

yum install zsh

antigen can be cloned from GitHub

git clone https://github.com/zsh-users/antigen.git .antigen

Now, edit your first .zshrc initialization file!

source ~/.antigen/antigen.zsh

antigen use oh-my-zsh

antigen bundle git

antigen bundle zsh-users/zsh-completions
antigen bundle zsh-users/zsh-syntax-highlighting
antigen bundle nojhan/liquidprompt

antigen apply

Run zsh from your current shell and antigen should clone and install all the declared bundles.

liquidprompt will be installed, which you shall enjoy quite greatly.

Posted on and tagged with Linux and zsh.

Using ghdl instead of Quartus II

ghdl is a great tool to prototype hardware quickly. It can be combined with gtkwave to analyze signals.

I did hardware design last semester and this is a bit tough for my mind right now, but I think it could help others out having a hard time with Quartus II. This post explain how to replace Quartus in the process of developing the device …

First of all, you need ghdl and gtkwave installed on your workstation.

yum install ghdl gtkwave

Then you can create a sample project or clone one I did last semester.

git clone https://github.com/arteymix/ghdl-lmc.git

A project usually consist of entities and testbeds on these entities. A testbed applies entries on an entity and make assertions on outputs.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
library ieee;

use ieee.std_logic_1164.all;
use ieee.numeric_std.all;

library work;

entity testbed is
end entity;

architecture testbed of testbed is

    type inputs_t is array(0 to 255) of signed(11 downto 0);

    constant inputs: inputs_t := (
        0 => x"000",
        1 => x"123",
        2 => x"456",
        3 => x"ABC",
        others => x"000"
    );

    signal cycle: natural             := 0;
    signal clk:   std_logic           := '1';
    signal input: signed(11 downto 0) := inputs(cycle);

begin
    input <= inputs(cycle mod 256);
    lmc: entity work.lmc port map(clk, '1', input);
    process is
    begin

        while TRUE loop
            clk <= '1';
            wait for 10 ns;
            clk <= '0';
            wait for 10 ns;
            cycle <= cycle + 1;
        end loop;

    end process;
end architecture;

ghdl can generate a Makefile for a specified unit

ghdl --gen-makefile testbed

ghdl can analyze, elaborate or run a simulation. The analyze part is essential as it will generate object files for each entities. Then you can link all those into a single executable. This is automated by the make command.

make

Once you have a correct result, you may run it and capture signals

./testbed --vcd=testbed.vcd

gtkwave is a tool designed to analyze signals, specifically the generated vcd file.

gtkwave testbed.vcd

In gtkwave, you have to select the device in SST section and append the signals on your workarea. You may then zoom it and out to see the actual waves.

Example of gtkwave usage.

I really hope this will help you out! I did enjoy VHDL and I really liked learning Ada-like syntax.

Posted on and tagged with GHDL.

Doing WearHacks!

This week-end, I’ll be participating to WearHacks which occurs in my hometown Montreal. You can find out more here.

So far, I am very confident. We have 2 excellent programmers and a UI/UX guys which will be working on Unity. If everything goes as planned, we will push a web app backed with Python offering a very interesting user experience.

I have two concerns right now. I do not know much about the device, which will be the Nod ring and I am worried about scaling the computation we will have to be done. Roughly, we have to do some linear algebra and approximate value comparison.

Thing is, I want to keep stuff in Python as it will allow us to code lightning quick, which is essential when you have a 36 hours deadline.

Communicating using Bluetooth

That’s the tricky part: we don’t know the device since it’s not on market. We will have to reverse-engineer the data we need. I know it follows these OpenSpacial next-to-be standard and it seems to work in 6D (x, y, z) for acceleration and gyro, so if I can extract that data, I’m fine. I need to do this quick, if I can do it, then the rest will be a piece of cake.

I will also have the possibility to communicate with people who designed the hardware, so I guess I will have more information on Bluetooth protocol implied and general data encoding.

Scaling the computation

We will receive 3-directional data from the device which is pretty much an accelerometer. We have to figure out the trajectory, smooth the transitions and compare it with another trajectory. This is a lot of data to treat, especially since we need to have the result in real time (otherwise, we will have to redesign the product). I plan to rely on numpy to make any of these calculation possible.

I will have to normalize the acceleration based on gyroscope data. I do not want to deal with rotational acceleration.

The trajectory will be approximated using a polynom per dimension since the acceleration data is 3-dimensional. I will generate a polynom going through every points of acceleration using the polyfit function.

To compare two trajectories, we will have to calculate the integral difference between each polynoms

Communicating with the frontend

Frontend communication will be done using WebSocket through socket.io library. It creates a full-duplex communication system, which will allow us to communicate in both directions. The device will update the frontend and the frontend will send messages. The frontend uses Unity, so this library will do the trick UnitySocketIO.

Producing the UI

UI will be done using Unity 3D. I do not know much about it and I don’t need to!

I really hope we will do good at this Hackaton.

Posted on and tagged with Bluetooth and Hackaton.

Kohana Makefile

I have just released my sample Kohana Makefile. It has useful recipes for minification, testing, setting permissions and SELinux contexts.

Clone it

git clone https://gist.github.com/4a162dd185ac0e4f781e.git

Or wget it

wget https://gist.githubusercontent.com/arteymix/4a162dd185ac0e4f781e/raw/5f744b0e157a03e330e2512af852005c8c51d594/Makefile

It has a recipe for installing Kohana files like index.php and application/bootstrap.php

make install

It runs PHPUnit

make test

Or minify your resources

make minify

It is fully configurable, so if you use a different css minifier, you may edit the file like you need it.

My goal is to provide every Kohana developer with a good Makefile to automate frequent tasks when using the framework. I will eventually propose it in the sample Kohana application.

Posted on and tagged with Kohana.