Monkey on a Keyboard

my online scrapbook of scrambled thoughts

The infinite monkey theorem states that a monkey hitting keys at random on a typewriter keyboard for an infinite amount of time will almost surely type any given text, such as the complete works of William Shakespeare. In fact, the monkey would almost surely type every possible finite text an infinite number of times. However, the probability that monkeys filling the observable universe would type a complete work such as Shakespeare's Hamlet is so tiny that the chance of it occurring during a period of time hundreds of thousands of orders of magnitude longer than the age of the universe is extremely low (but technically not zero).

Source: https://en.wikipedia.org/wiki/Infinite_monkey_theorem

Basically I was just looking for a name for this blog that isn't tied to the domain name. Why? Because of this: https://yohanes.dev. That is a *.dev domain name which has HSTS preloading on the TLD level. I'll still keep the old domain name. I just want to experiment with this one for a bit. For now, it's just another way to load my blog.

   Posted in Blog        Yohanes Mario Chandra        0 Comments

JavaScript is a confusing language. You can do OOP with it, you can do functional programming with it, and so on. A typical and modern way of encapsulating things in javascript is by creating an object. Objects in javascript are defined by a JSON notation (a single object throughout the runtime) or by creating what's known as a constructor function:

function Creature(legs) {
    this.legs = legs; // This is a public attribute

    this.speak = function(){ // This is a public method
        console.log("Hi!");
    };
}

var person = new Creature(2);
person.speak(); // console: Hi!

However, you don't have private or public modifiers in javascript. Everything added to this will result as a public member of that object. So, how do we create private members? The answer: closure. Closure is a concept in javascript which states that anything defined inside a closure (those curly braces) will stay inside it. Nothing from outside of the closure can access anything that's defined inside of the closure. So, we can make:

function Creature(legs) {
    this.legs = legs; // This is a public attribute
    var sound = "Hi!"; // This is a private attribute
    
    var makeSound = function(){ //This is a private method
        console.log(sound);
    };

    this.speak = function(){ // This is a public method
        makeSound();
    };
}

var person = new Creature(2);
person.speak(); // console: Hi!

I exploited this behavior to make sure that no one can mess with the internal states of my web applications by doing this:

$(document).ready(function(){
    // The rest of your code goes here
});

That way, no external javascript can mess with my internal states. The lesson is, never define your application states as globals, always use closure, be it in an object, or as simple as inside of an initial function. As always, happy blogging!

   Posted in Web        Yohanes Mario Chandra        0 Comments

While trying to create a simple web-based desk clock, I notice that not all computers are made equal in regards to the way they sync time. Since then, I've been trying to create a simple HTTP and JSON based ntp server to sync all of my clocks. The result is as follows:

var start = (new Date()).getTime();
$.ajax({
    url: 'http://time.yohanesmario.com/ntp',
    type: "GET",
    cache: false,
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function(data) {
        var end = (new Date()).getTime();
        var responseTime = end-start;
        if (data!=null && data.milliseconds_since_epoch!=null) {
            var time = parseInt(data.milliseconds_since_epoch);
            
            // Calibrate ntp time
            time = time + (responseTime/2);
            
            // Calculate offset
            offset = end-time;
        }
    }
});

Notice that I add half of the response time to the time obtained from the NTP. This is because HTTP as a protocol consist of 2 part, request and response, each took half of the total time. And then, there is TLS.

TLS (more widely known as SSL or HTTPS) works very differently. It, under normal circumstances (TLS 1.2), took 2 round trip handshake before the actual HTTP protocol begin. So, in total, it took 3 round trip to complete an HTTPS request. That's why, the actual time it took for the HTTP response to actually reach the client is responseTime/6. We change our original code accordingly:

var start = (new Date()).getTime();
$.ajax({
    url: 'https://time.yohanesmario.com/ntp',
    type: "GET",
    cache: false,
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function(data) {
        var end = (new Date()).getTime();
        var responseTime = end-start;
        if (data!=null && data.milliseconds_since_epoch!=null) {
            var time = parseInt(data.milliseconds_since_epoch);
            
            // Calibrate ntp time
            time = time + (responseTime/6);
            
            // Calculate offset
            offset = end-time;
        }
    }
});

This may help you, or may not. Either way, happy blogging!

   Posted in Web        Yohanes Mario Chandra        0 Comments