Friday, March 21, 2008

Debian parodies, Iceweasel and Icedove

Few days back while going through an article about new Knoppix 5.3.0 I came upon the two familiar but unheard tools i.e. Iceweasel and Icedove, both followed by the names Firefox & Thunderbird 2.0 in brackets respectively. For a moment I began to muse about when did these two names came into existence for the two most popular open source internet tools. After a little googling and going through wikipedia I got my answer.

Mozilla corporation holds trademark for name Firefox and its logo. Unofficial releases of Firefox can’t use the same unless they use the binaries supplied by Mozilla or have special permission. If none of the two conditions satisfies then one must compile the source with an option that gives the generated binaries code-name of the release version of the Firefox on which it is based, and which doesn’t use the official logo or any other artwork.

Iceweasel and Icedove are the re-branded version of Firefox and Thunderbird maintained by Debian. Debian guidelines, commonly known as Debian Free Software Guidelines, states that all parts of Debian distribution should be modifiable by those who use it. This conflicts with the licensing and strict guidelines of Mozilla Trademark Policy. As a result Debian forked the development of Firefox & Thunderbird projects with new names together with some extra features and a logo which is freely licensed.

Saturday, March 15, 2008

Carriage Return (CR) & Line Feed (LF)

Today while playing with `od` - a command-line tool to dump file data in octal and other formats - and looking at 012 (octal code of \n) it just came to my mind to find out the difference between carriage return (\r) and line feed (\n) characters and how do they date back in history. I got my answer from the very first attempt in Google which directly led me to Yahoo answers.

The two characters emulate the two main actions in typewriters. Carriage-Return which moves the cursor back to the beginning of the line corresponds to the moving of typewriter’s carriage to the initial leftmost position, whereas Line-Feed which move the cursor to the next line corresponds to the rolling up of paper by one line. The combination of both i.e. CR-LF were initially used in Teletypewriters to move the print head back to the beginning of new line.

Even though the two still performs the same action in standard output and printers, not all operating systems use both of them. *nix based systems use \n, Mac uses \r, whereas Windows uses both \r\n. This is a reasons why *nix text documents look weird in windows.

Wednesday, March 5, 2008

Some common abbreviations to remember

GDK GIMP Drawing Kit
GIMP GNU Image Manipulation Program
GNOME GNU Network Object Model Environment
GNU GNU is not Unix
GTK GIMP Tool Kit

Monday, February 4, 2008

FastCGI: incomplete headers (0 bytes) received from server

Today I nearly spent my whole day in resolving a small problem which took hardly 30 seconds to resolve. The issue was quite simple. While upgrading one of the rails application on my local machine I suddenly started getting this error in my apache logs.
[Mon Feb 04 16:48:50 2008] [error] [client 127.0.0.1] FastCGI: comm with (dynamic) server "/home/chetan/www/myrailsproject/public/dispatch.fcgi" aborted: (first read) idle timeout (30 sec) [Mon Feb 04 16:48:50 2008] [error] [client 127.0.0.1] FastCGI: incomplete headers (0 bytes) received from server "/home/chetan/www/myrailsproject/public/dispatch.fcgi"
With this I put my seat belts on and started googling. There were many users who faced the similar problem while upgrading Rails. But each one was telling a different solution in his anecdote. I tried each of them but nothing worked for me. May be bad luck was not good today. I continued my hunt and kept looking for other solutions. Ultimately my prayers were accepted and I got the solution in one of the message boards. The problem was in the shebang line of dispatch.fcgi file. It was set to #!/usr/local/bin/ruby in the updated version, whereas in my machine ruby executable was located at /usr/bin/ruby. I fixed this and everything returned back to normal. I wish I had got this post in the very beginning so that my whole day would save. :)

Friday, January 11, 2008

Tata's 1-Lakh ($2550) car

Ending a long suspense and amidst a lot of media hype Tata motors finally unveiled its 1 Lakh ($2550) car `Nano` in 2008 Auto-Expo at Pragati Maidan, New Delhi. It was eagerly awaited for a long by the people. The launch of this car is supposed to bring another revolution in Indian auto market exactly the way it happened with the launch of Maruti 800 in 1980s. It can already be seen with Bajaj announcing its entry in small car segment in collaboration with Nissan and Renault by 2010. The prototype of this car has already been put on display in Auto-Expo. Similarly Suzuki Motors of Japan, a major share holder in Maruti-Udyog limited, has already tied up it's seat belt with development of a low cost 660cc engine for it's new car in reply to Tata's `Nano`. This car is also about to roll out somewhere in 2008.

As it was being expected the launch news immediately took the whole media like a fire with everyone from company personnels to media reporters to market analysts expressing their views about it. I noticed a mixed public reaction from various news channels about this car. People from lower to middle income groups are very excited as they can easily fulfill their long cherished dream of becoming a proud four-wheeler owner now. Along with being economical its smaller size demands lesser parking space in comparison to other smaller cars. Analysts also say that it will also impact the used car market by not only slashing their prices but also providing a new car option at the same price. On the other hand there is this another group who is worried with the repercussions from the over sale of this car on already deteriorating traffic and climatic conditions in cities. Delhi is at first place in this regard with the highest number of vehicles among all the metropolitan cities. The main target for this car would be two-wheeler aspirants who will now have the option to own a four-wheeler with slightly more investment.

Whatever people would say, but this much envisaged car of Mr. Ratan Tata is already in market now. It is powered by a 624cc 33bhp engine which claims to deliver a mileage of 20kmpl. It has an un-geared (continuous variable transmission) engine at the back powering the rear wheels. Despite its lower cost and small size company claims it to be completely secure from all security point of views. It is available in three variants, one basic and two deluxe. The showroom price of the basic version is Rs. 1-Lakh exclusive of VAT and road tax.

Thursday, January 10, 2008

Firefox loading blank pages

I had been facing this weird firefox problem for quite a few days in my linux box (Ubuntu 7.04 - the Feisty Fawn). After connecting to my ISP and browsing for few minutes suddenly all the URL requests were ending up in blank pages. Earlier I thought this was a problem with my ISP. But when I noticed that messenger and all the command-line tools (wget, lynx etc.) were working fine I understood that actual problem was something else. Considering the presence of some bug I first tried to upgrade the firefox (v2.0.08 to v2.0.0.11). Unfortunately this didn't solve the problem. I tried analyzing the http response headers using wget but couldn't gain much. Ultimately I had to resort to lynx with yahoo search as google was also dying with "400 Bad Request" message. After one or two attempts I got its solution in a forum. The problem was occurring with the pages responding with http 302 status. Some people also mentioned to experience the same problem with pages using script based redirection. Anyhow, the reason for this was attributed to some buggy firefox extension. With this knowledge it was easy to resolve the problem. Trying disabling each firefox extension one by one. I was too desperate to fix the issue ASAP, so I disabled all the extensions in one go. Voila! it worked and everything restored back to normal.

Tuesday, January 8, 2008

Singleton methods in Ruby

I am not a guru in any specific language. I work for a firm where technology keeps changing with projects. This time its again RoR. Before this I have worked on couple of RoR projects. With each project it seemed to me as if I was doing the same repetitive job, store and fetch data from database. So this time I decided to go a bit further and explore the language basics together with the new project.

After having a quick look at the rails core I got the first thing to start with.
class Foo
  class << self
    def bar()
      self.name
    end
  end
end

After googling around a bit I figured out that it's a way to create and bind a class to a specific instance or object. This binding class is called eigenclass, or more commonly a singleton class. Similarly, methods defined in a singleton class are called singleton methods. There is another alternative syntax for defining singleton methods.

irb(main):001:0> duck = Object.new
=> #
irb(main):002:0> 
irb(main):003:0* def duck.speak
irb(main):004:1>   'quack! quack!'
irb(main):005:1> end
=> nil
irb(main):006:0> duck.speak
=> "quack! quack!"
irb(main):007:0> 

Only the specific instance with singleton class has access to its singleton methods. The other instances from the same object's class don't share the added class and hence behave normally.

irb(main):001:0> duck = Object.new
=> #
irb(main):002:0> class << duck
irb(main):003:1>   def speak
irb(main):004:2>     'quack! quack!'
irb(main):005:2>   end
irb(main):006:1> end
=> nil
irb(main):007:0> duck.speak
=> "quack! quack!"
irb(main):008:0> 
irb(main):009:0* cat = Object.new
=> #
irb(main):010:0> cat.speak
NoMethodError: undefined method `speak' for #
This was all about the general idea. Lets move to some more depth beginning with a generic definition.
In Ruby, a singleton class is an instance specific anonymous virtual class that is created on the fly to hold the object specific behaviors (or methods). It is anonymous in the sense that it never appears in a list of objects returned from methods such as Module#ancestors or ObjectSpace::each_object, and virtual since it cannot be sub-classed or instantiated. It's only purpose is to provide a place to mount methods and instance variables that are uniquely associated with the specified object.

All the classes that Ruby creates automatically are marked as singleton (in class flags) internally. Also, like any other ruby class, a singleton class can also include modules.

irb(main):001:0> module BirdCharacteristics
irb(main):002:1> 
irb(main):003:1*   def can_fly?
irb(main):004:2>     true
irb(main):005:2>   end
irb(main):006:1> 
irb(main):007:1* end
=> nil
irb(main):008:0> duck = Object.new
=> #
irb(main):009:0> 
irb(main):010:0* class << duck
irb(main):011:1> 
irb(main):012:1*   include BirdCharacteristics
irb(main):013:1> 
irb(main):014:1*   def speak
irb(main):015:2>     'quack! quack!'
irb(main):016:2>   end
irb(main):017:1> end
=> nil
irb(main):018:0> duck.can_fly?
=> true
irb(main):019:0> duck.speak
=> "quack! quack!"
irb(main):020:0>
Now to grab the strategy behind the singleton classes lets first look at the structure of Ruby objects and classes. Each Ruby object consists of three main components:
  • A set of flags to store the meta-data about the object (like whether the object is frozen or tainted).
  • A hash to store instance variables.
  • A reference pointer (kclass) to object's class.
A class in Ruby is an instance of class Class. So it too contains all the three components described above together with
  • A pointer (super) to the parent or super class.
  • A list of methods.
 --------------------            -------           --------------
| object             |          | class |         | Parent class |
 --------------------            -------           --------------
| flags              |     ---> |  ...  |         |              |
| instance variables |    |     | super |-------> |     super    | ---> nil
| kclass             | ---      |   _   |         |              |
 --------------------            -------           --------------

Interestingly ruby objects itself don't store any methods. All the instance methods are stored in Class objects and shared by all the instances. When a method is invoked on an object ruby interpreter first searches the object's direct class using its kclass pointer. If it is found there then the method body is returned for the evaluation, next parent (super class) in the inheritance hierarchy is looked-up. This process continues until method is found, or no parent is available i.e. Root class (super = nil) is reached. In the latter case an exception is raised.

This was fine, but things get complex when it comes to invocation of class methods. Class methods like instance methods can't be stored in classes, or more specifically Class objects. If it is so, then they would be accessible from all the class instances using normal method calls. To resolve this ruby plays a small trick. Whenever a class method definition is encountered, it internally creates a new anonymous class, adds the class method to it, and binds it with the object manipulating the klass pointer. This new class is our singleton class. To retain the original behavior of object ruby manipulates the value of super pointer in singleton class to point the actual (instantiation) class object establishing an implicit inheritance.

 --------------------            -------
| object             |          | class |
 --------------------            -------
| flags              |     ---> |  ...  |
| instance variables |    |     | super |
| kclass             | ---      |   _   |
 --------------------            -------

                        Before class method definition
                        
 --------------------            ----------------------------           -------
| object             |          |         singleton class    |         | class |
 --------------------            ----------------------------|           -------
| flags              |     ---> |             ...            |         |  ..   |
| instance variables |    |     |            super           |-------> | super |
| kclass             | ---      | [.., new_class_method, ..] |         |   _   |
 --------------------            ----------------------------           -------

                        After class method definition

Now when a method invocation request is received ruby first checks the singleton class. If method is found then it is executed, else search is forwarded to the actual class using the super pointer in singleton class, followed by super class chain in case of failure.

All the methods defined for a singleton class are called singleton methods. The is another alternative method (other than the one described in the beginning) for defining singleton methods.