Code is law
2016-12-01 23:48:17
It all started with the novel "Code and Other Laws of Cyberspace" from Lawrence Lessig
I used to work in developing accounting software. Sometimes, regulations and business rules that comes with it are so complex that the users of the application themselves would go and ask developers the behavior of the software. The code written become the base on which people in the company will base their decision. it become more and more evident that technology and regulations are now tightly bonded.
Then I came to face the "Code is law" concept : what if the code did not comply to regulation but the code itself was the regulation. In the cybernetic era we are living in, computer software is everywhere. What if software ruled our lives.


In France, there's a law that only "government approved" cashier software are allowed, to prevent any falsification. Still nothing can guarantee that the some would run unmodified. Therefore i dont think that closed source program can be used as a base of trustable software because it is impossible to know whether the program contains any illegal backdoor.
How to produce that kind of software and how do ensure that is still meets its requirement ? State or established company could deliver such service, just think at Verisign which, for instance, issue encryption certificates. But that how can we make sure that the state or the company does not break the law, and isn't putting the power of law enforcement to private company a risk?
Open-source software is a good alternative as the source code can be read and verified statically but it is impossible to ensure that the code will not be modified at run time. So in order for the code to be the law, we need a platform where the code is publicly available and also an unmodifiable unstoppable platform where the code will run. These these approved software are law in a way that there business rules enforce the law. One can say that they are the law.
Blockchain technology comes into play because of its ability to for a decentralized trust mechanism that can support this concept that code can be a sort of unmodifiable contract that can be trusted as truthfully as the legal code itself, comes Ethereum. Ethereum is a blockchain platform that can run publicly non stoppable, non alterable software. Ethereum, hence, fill the need for trustable execution of the law code.
It's an innovative idea : one can imagine escrow service that will act as a man in middle without any human action, or billing software that will run on the platform to absolutely non modifiable.

It's only the begining

The cyber law era hasn't even started yet but with the ever growing role of technology on our lives, we've come to realize that software code rules our lives. With great power comes great responsiblity. So is the need for software that we can trust.
Single board computer in 2016
2016-11-09 23:32:10

When I've first heard about the Raspberry Pi in 2012, I honestly didn't saw any use for it outside the "Small is beautiful" effect.

It's small, it's cheap but it's badly underpowered and with as little as 256 Mb RAM it cannot be decently used to launch any desktop class application.
The only proper use that I could see for it was as NAS server for all your pirated movies (yay I don't download movies :( )

Well...time has passed and now IoT is the new hype. IoT(Internet of Things) meant to change our consumer life by making everyday objects connected to the Internet.

A few month ago, I'd bought myself an Arduino (Yes I know I am once again late to the party). I played with it a little bit and it made me realize that it was the doorstep of the IoT and also what IoT meant actually.

Then I finally got it : the Raspberry Pi is by no means a pocket computer or a cheap computer for stingy people. It is a platform to prototype smart and connected object.

The selling point of the Raspberry Pi is it's integrated GPIO pins that allows to send or receive electric signals from any connected component. It also features a camera interface that is directly connected to the onboard GPU for direct compression and streaming.
Raspberry Pi ZeroThe board that started the race to the bottom
Orange Pi ZeroThe chinese copycat, is it ?
C.H.I.Pthe new comer

Networking in C/C++
2016-10-24 22:25:33
In a funny way, I spent most of my working days writing network software.
Client/server, blocking/non blocking, TCP/HTTP/UDP and I'm suprised to feel all but embarrassed coming back at home :
What library should I use to write networking software ?

A stackoverflow topic sum it up :

Given the choice, I'd rather go for SDL_net as I used SDL already a lot in my home projects.

Biggest game ever
2016-01-27 22:33:15

What is the video game that offers the largest open world to explore ? Not Oblivion, not World of Warcraft, I was stunned to learn that it's a rather anonymous game called "Fuel" by the french developers of Asobo Studio.

The game offer 118km by 118km of fully detailed explorable world and all fitting in a single DVD.

That is a seriously impressive fact but why would you accomplish such glorious software engineering for a racing game !?
Returning implementing class from an abstract class method
2016-01-19 16:34:15
This is a common problem :

A is an abstract class that implement a method

B implements A
C implements A

A's method returns "this" so that you can use the chain calls like this a.method.method.method

Problem : a.method retursn an instance of A, so that you can not use a method of B after calling b.method.

I didn't know that this kind of pattern has a name : F-bounded quantification

The solution is pretty weak :
abstract class A<T extends A<T>> {
public T clone(T original);
class B extends A<B> {
public B clone(B original) {

Downloading Java JDK on a headless server
2015-12-10 13:36:09
Because every server deserve a JDK :

wget --no-check-certificate --no-cookies --header "Cookie: oraclelicense=accept-securebackup-cookie" <a href =""></a>
Oculus Rift PC SDK from version 0.5 to 0.7
2015-10-25 23:46:43
Oculus PC SDK API has been stable for about a year but the late august 2015 v0.7 has seen introduction of a major architecture shift.
Always eager to lower latency, the new architecture, used with the latest VR mode drivers from GPU maker AMD and NVIDIA should bring performance to another level.
In daily use I've already seen a huge improvement : FINALLY we are able to get the VR directly into the headset, and at the same time, have the rendering mirrored on the main screen.
Before it was almost mandatory to have the DK2 device set as the primary monitor which was awful when trying to launch something from the desktop as everything is barely readable split in half for each eye and with only the center zone being sharp.
Alas ! The DK2 is no longer seen as a monitor but light's up immediately when an Oculus ready application is started.

From the programming side the shift is also substantial while the motion tracking interface remain unchanged, the graphical rendering interface has seen big changes
ovrHmd_BeginFrame(hmd, 0);

at the beginning of a new frame and
ovrHmd_EndFrame(hmd, headPose, &fb_ovr_tex[0].Texture);

to indicate the end of a frame and the buffer swap, are no longer relevant.

It all starts with a new initialisation method :
ovrResult result2 = ovr_CreateSwapTextureSetGL(hmd,GL_RGBA, this->screenWidth, this->screenHeight, &textureSet);
    puts("ovr_CreateSwapTextureSetGL ERROR");

This method return a "TextureSet" : an array of OpenGL allocated textures.
One has to bind a framebuffer to the texture, I have managed to reuse my FrameBuffer class by allowing it to receive an already allocated texture as backing texture for a newly created framebuffer.

At the end of all frame draw calls, one has to call the following method :
ovrResult result = ovr_SubmitFrame(hmd, 0, NULL,&layers, 1);
if (!OVR_SUCCESS(result)) {
    puts("ovr_SubmitFrame ERROR");

The main paramater is an ovrLayerHeader which is composed of :

ld.ColorTexture[eye] = textureSet;
ld.Viewport[eye] = viewPort;
ld.Fov[eye] = hmdDesc.DefaultEyeFov[eye];
ld.RenderPose[eye] = headPose[eye];

So for each eye, one has to bind our textureSet to "ld.ColorTexture[eye]".
Personnally I didn't saw the point of having a different texture for each eye, so I only use one textureSet that contains half of the surface for each eye.

Then we blit mirror texture to back buffer, I guess this is buffer that support the final rendering.
glBindFramebuffer(GL_READ_FRAMEBUFFER, mirrorFBO);
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
GLint w = ((ovrGLTexture*)mirrorTexture)->OGL.Header.TextureSize.w;
GLint h = ((ovrGLTexture*)mirrorTexture)->OGL.Header.TextureSize.h;
glBlitFramebuffer(0, h, w, 0,
0, 0, w, h,
glBindFramebuffer(GL_READ_FRAMEBUFFER, 0);

Note that SDL_GL_SwapWindow is back and that we have to call it manually.

And this this is were the magic happens :
textureSet->CurrentIndex = (textureSet->CurrentIndex + 1) % textureSet->TextureCount;
swapFBdrawing(&this->fbDrawing, &this->fbDrawing2);

First we have to manully increase the index on our textureset so that the next texture will be used for the rendering on the next ovr_SubmitFrame.
We have to do so because the texture used in ovr_SubmitFrame CANNOT be used in the next call to ovr_SubmitFrame. I know it because binding the backed framebuffer immediately after having called ovr_SubmitFrame will cause an error.
This is why, I am swapping between 2 Frambuffers, each one is bound to one texture of my textureset structure. I might just have been lucky but ovr_CreateSwapTextureSetGL for my part only returns a set of 2 textures.
Source translation with Emscripten
2015-10-12 22:10:00
Emscripten is a software that allows to transform C/C++ code to Javascript, achieving near native speed.

Since I still do a lot of my personal work in C/C++, the idea of being able to push my work directly to the web sounded so sweet that I had to give it a try.

1. Download
I'm running Windows 7 and the latest web installer seems broken for me (emsdk-1.34.1-web-64bit.exe)
I had better results with the offline installer as it worked flawlessly.

2. Usage
For a single source file its pretty straightforward :
emcc source.c

The default output is a a.out.js file containing the translated source code.

First impression : this is huge.
Even converting a one liner, with the -o2 option (that "minify" and optimize the JS), the generated source is 300kb !
Then one can understand what really is Emscripten : Emscripten produces a javascript implementation of the source code with a javascript virtual machine on which the generated source will run.
No I won't translate my superfast sorting algorithm into a directly pluggable javascript equivalent.

A function can be exported to javascript using the proper export directive.
#include <math.h>
extern "C" {
int int_sqrt(int x) {
return sqrt(x);

After having copy-pasted the 300kb source in your html source, calling the generated code is easy from Javascript :
int_sqrt = Module.cwrap('int_sqrt', 'number', ['number'])

int_sqrt : method name
'number' : return type (or null if void)
['number'] : list of parameters
Easy ? Because you didn't use pointer.
For Emscripten, pointers are number referencing memory location inside the javascript virtual machine.
Which means that memory has to be allocated and data copied there before calling the target method.

Here is the method I was willing to port to Javascript:
extern "C" {
    int computeMove(int playerValue, int x, int y,char * tab,int p_x,int p_y, int * opponentPos, int opponentCount,int *newx, int *newy) {

Feeding the good values to the function was painful !
c_computeMove = Module.cwrap('computeMove', 'number',['number' , 'number' , 'number', 'number', 'number' , 'number' , 'number' , 'number' , 'number' , 'number'])
//int playerValue;
var playerValue = -1;
//int x;
var x = 0;
//int y;
var y = 0;
//char * tab;
var tab = new Array();
for (var i = 1;i < 700;i++) { tab.push(0);}
var tab_data = new Int8Array(tab);
var tab_nDataBytes = tab_data.length * tab_data.BYTES_PER_ELEMENT;
var tab_dataPtr = Module._malloc(tab_nDataBytes);
var tab_dataHeap = new Uint8Array(Module.HEAPU8.buffer, tab_dataPtr, tab_nDataBytes);
tab_dataHeap.set(new Uint8Array(tab_data.buffer));
//int p_x;
var p_x = 0;
//int p_y;
var p_y = 0;
//int * opponentPos;
var data = new Int32Array([player_x, player_y]);
var nDataBytes = data.length * data.BYTES_PER_ELEMENT;
var dataPtr = Module._malloc(nDataBytes);
var dataHeap = new Uint8Array(Module.HEAPU8.buffer, dataPtr, nDataBytes);
dataHeap.set(new Uint8Array(data.buffer));
//int opponentCount;
var opponentCount = 1;
//int *newx;
var newx = new Array();
for (var i = 0;i < 1;i++) { newx.push(-1);}
var newx_data = new Int32Array(newx);
var newx_nDataBytes = newx_data.length * newx_data.BYTES_PER_ELEMENT;
var newx_dataPtr = Module._malloc(newx_nDataBytes);
var newx_dataHeap = new Uint8Array(Module.HEAPU8.buffer, newx_dataPtr, newx_nDataBytes);
newx_dataHeap.set(new Uint8Array(newx_data.buffer));
//int *newy;
var newy = new Array();
for (var i = 0;i < 1;i++) { newy.push(-1);}
var newy_data = new Int32Array(newy);
var newy_nDataBytes = newy_data.length * newy_data.BYTES_PER_ELEMENT;
var newy_dataPtr = Module._malloc(newy_nDataBytes);
var newy_dataHeap = new Uint8Array(Module.HEAPU8.buffer, newy_dataPtr, newy_nDataBytes);
newy_dataHeap.set(new Uint8Array(newy_data.buffer));

3. Conclusion
Porting C/C++ the code has proven a viable solution, especially knowing that commercial games were ported to web-browser using this technique.3D draw calls are even map to their WebGL equivalent.

But, it's not a lightweight solution, a 300kb minimal runtime is huge.
The biggest put-off is probably the difficulty to input/ouput non-primitive types. Data exchange between Javascript and C/C++ is not very smooth a require hand-crafted and error-prone operations.
Functional thinking paradigm over syntax
2015-09-17 15:51:07
As newcomer in the world of functional programming, I have been through this interesting book to start my journey :

I like the fact that it tries to sum up the concepts of functional programming and map them to an imperative programming language, Java for that matters. For sure programmers from another background may not find this approach as meaningful.
This book is also language agnostic with functional examples in Groovy, Scala, Closure which give a good overiew of the different syntaxes and implementations.

Functional programming has been around for a long time now with even in the Java world, for instance, the Scala language making functional programming its selling point.
Now even Java 8 introduce some bits of functional programming with the so called "Lambda blocks".

In short, functional programming is a programming style where functions are considered as variables.
In imperative programming, a succession of instructions executed in sequential order modify a context.
Functional programming the same result would be achieved through successive function calls.
Moreover, functional programming allows for new design patterns and new software paradigms.

Closures : function passed as argument of a method which have access to their declaration context.

Laziness : Delaying evaluation of an expression until needed, one defining aspect of functional programming.

Dataset stream : Finite or infinite list of objects where one can apply high order functions. High order function are daisy-chained and thanks to laziness, evalution is done only on a terminal function, a function requiring an immediate result.
Thanks to generalized immuatability and higher level of abstraction, functional programming favours parallel optmization at runtime level.
Operators on streams :
- Map() apply an operation to each element of a stream creating a new stream.
- Filter() filters a stream with a predicate function returning the filtered stream.
- Reduce() apply an operation to a stream and returning a result.

Curry and partials : Design pattern that creates functions that returns the same function with less arity.

Memoization : Design pattern where caching is done implicitly by creating a function that wraps the memoized function and a caching mecanism.

Why is there a tight bond between immutability and functional programming ?

The paradigm shift of functional programming means that the objects no longer carry the application logic. "Moving parts" now are functions.
In functional programming, the logic should be wrap around "pure functions" which means functions that return the same result for a given input, this can only be enforced by using immutable objects as arguments. For instance : passing a stateful "context" as argument of a function breaks the functional programmiong paradigm.
The side effect of immutability is that it greatly eases parallel execution as it clearly separates independant execution units.
In the same idea, laziness can only be achieved knowning that an input parameter will not be changed during execution, hence immutability.
1) You have thread T1, T2 and T3, how will you ensure that thread T2 run after T1 and thread T3 run after T2?
2015-08-28 10:12:36
Let's remember the basics of Java threading :

The code that has to be run in a different thread than the main execution can be written in a class that implements Runnable :
public class MyRunnable implements Runnable {
    public void run() {

Then a thread object will be declared with the "Runnable" object as a parameter.
public static void main(String[] argv) {
    Thread t = new Thread(new MyRunnable());

Another way of implementing threads is to extend the "Thread" class:
public static class MyThread extends Thread {
    public void run() {

Then instanciate the extended class and call the start method:
public static void main(String[] argv) {
    MyThread mt = new MyThread();

I use to consider both technique as being equivalent, implementing Runnable is less restrictive as it's only possible to extend one class but multiple interfaces can be implemented.
Starting from Java 1.5, a new framework that eases implementation of worker thread pool is available : the java.util.concurrent.

public static void main(String[] argv) {        
    java.util.concurrent.ExecutorService executor = java.util.concurrent.Executors.newFixedThreadPool(10);
    for (int i = 0; i < 500; i++) {
        Runnable worker = new MyRunnable();

Here I have set a number of thread to 10, then my runnable object will automatically be dispatched on any available thread.
Using thread pool saves thread creation overhead, and allows to control the number of thread running concurrently.
Obviously, Thread class cannot be extended anymore when using the Executor framework.

Bringing back to the main question : You have thread T1, T2 and T3, how will you ensure that thread T2 run after T1 and thread T3 run after T2?
If you are allowed to synchronize start call within the main thread, you can call the Thread.join() method after each Thread.start() to wait for thread execution.

public static class MyRunnable implements Runnable {
    int id;
    public MyRunnable(int id) { = id;
    public void run() {
public static void main(String[] argv) throws InterruptedException {    
    Thread t1 = new Thread(new MyRunnable(1));
    Thread t2 = new Thread(new MyRunnable(2));
    Thread t3 = new Thread(new MyRunnable(3));        

IF your threads extends Thread, you can pass previously create Thread object and call the Thread.join() method on the thread that has to wait.
public static class MyThread extends Thread {
    private Thread threadToWait = null;    
    public MyThread(Thread ttw, int id) {
        threadToWait = ttw;
    public void run() {
        if (threadToWait != null) {
            try {
            } catch (InterruptedException e) {
        System.out.println(this.getName() + "Run");
public static void main(String[] argv) {    
    MyThread mt1 = new MyThread(null,1);
    MyThread mt2 = new MyThread(mt1,2);
    MyThread mt3 = new MyThread(mt2,3);

No matter in which order mt1.start(), mt2.start() and mt3.start() are called, inevitably the execution result will always be :
Now let say that you want to use runnable implementation and you want to synchronize runnable within run code.
You do not know in advance on which thread it will run. Well you could pass the thread running the Runnable object as a parameter, but you cannot do that while using thread pool.

One has to use the wait() and notifiy() :
when calling wait() on any java Object, the running object will stop its execution until a thread calls notify() on the same object.
Thus by making Runnable r2 wait() on r1, and r1 notifiy on itself when it has ended, we can make r2 wait for r1 execution.
NOTE that the wait() releases the lock that is acquired by the previously called synchronized(), this might be counterintuitive.
BUT if r1 calls notify on itself before r2 calls r1.wait(), r2 will never resume its execution.
To Counter this problem, I added a "ended" flag, so that whenever r1 finishies, it set this flag to "true" so that r2 won't call wait() if r1 has already finished its execution.
    public static class MyRunnable implements Runnable {
        int id;
        MyRunnable runnableToWait = null;
        public volatile boolean ended = false;
        public MyRunnable(int id,MyRunnable runnableToWait) {
   = id;
            this.runnableToWait = runnableToWait;
        public void run() {
            if (runnableToWait != null) {
                synchronized (runnableToWait) {
                    try {
                        if (!runnableToWait.ended) {
                    } catch (InterruptedException e) {
                synchronized (this) {
            } else {
                synchronized (this) {
                    this.ended = true;
    public static void main(String argv[]) throws InterruptedException {
        MyRunnable r1 = new MyRunnable(1,null);
        MyRunnable r2 = new MyRunnable(2,r1);
        MyRunnable r3 = new MyRunnable(3,r2);
        Thread t1 = new Thread(r1);
        Thread t2 = new Thread(r2);
        Thread t3 = new Thread(r3);
See 10 older news | Back to the top