Archive

Archive for the ‘Programming’ Category

OOPS: Delayed Instantiation

March 17th, 2011 No comments

பலன் கருதி பொருள் ஈட்டல்
நலம் பேணும் நன்முறையாம்
பொருள் சேர்க்கும் நன்மக்கள்
காலத்தே ஈட்டி பலம் கொள்வாரே!

First Application using MongoDB & PHP

March 6th, 2011 No comments
I have been eyeing on MongoDB since I had visited IIIT Hyderabad for campus placements in Jan 2011.  The name MongoDB was introduced to me by the candidates whom I had to interview.  Infact all the candidates who took the interview had worked on MongoDB for solving some problem during their graduate studies.   MongoDB, the name comes from HuMongous DataBase, which is a well instituted name and the database indeed is meant for storing tonnes of data organized in JSON format.   Being a C++ coder, I had less experience with JSON and have always avoided that as it was primarily used in the Web application development.  After learning about Mongo in the official wiki, I got thrilled by the simplicity of JSON format and the advantage of it over the conventional XML format and infact decided to use JSON for a project that my team is working on.
MongoDB is written in C++, which increased my love towards the database.  I wanted to use MongoDB for my product development data store needs, but was stuck with the dependencies of Mongo’s C++ driver, where it wanted Boost libraries to be linked.  The pathetic problem is boost does not compile with the debug version of STLport.  My product is built on STLport, so I have to link MongoDB with Boost/STLport, which works well in release mode, but not in DEBUG mode.  How Sad!
My desperation grew as I wanted to have some useful application developed using MongoDB.  PHP came for my rescue. The PHP driver that was available with MongoDB is a cute baby.   I works like moon walk, when copy pasted on the PHP’s extension folder.   The documentation of PHP/MongoDB is well written and has lot of examples.  Also, there is awesome support available in the internet for all the doubts that I got while building my Brower based Photo SlideShow web application.  All, I did was, I pushed all my JPEG files into MongoDB and wrote simple PHP scripts to fetch the files based on the file name.  When the file name was not mentioned, my script would pick a file randomly.  I have presented the code below:
# view.php

# get the file id
$id = "";
if ( array_key_exists( "id", $_REQUEST ) ) {
    $id = $_REQUEST["id"];
}

# open database connectivity
$conn = new Mongo();
$store = $conn->store;
$image = $store->image;

# fetch the file as per the id or just randomly
if ( $id != "" ) {
    $query = array ( 'id' => $id );
    $cursor = $image->find( $query );
}
else {
    # set the html headers for refreshing the page every 3 sec.
    header( 'Refresh: 3;' );

    # Random fetch logic, get the count first.
    $count = $image->find()->count();
    $rand = rand( 1, $count-1 );  # generate a random number.
    # skip several records of the cursor based on the random number
    $cursor = $image->find()->skip( $rand )->limit(1);
}

# write output stream as JPEG content
header( 'Content-Type: image/jpeg' );
foreach ( $cursor as $obj ) {
    # decode the data, and just write on the output stream.
    echo base64_decode($obj["data"]);
}

# import.php

$dir = $_REQUEST["dir"];
if ( $dir == "" ) {
    echo "<h2>Import directory cannot be empty</h2>";
    exit;
}

$conn = new Mongo();
$db = $conn->store;
$image = $db->image;

echo "<ol>";
$dhandle = opendir( $dir );
while ( ($file = readdir( $dhandle )) != false ) {
    $path = $dir."/".$file;
    if ( is_dir( $path ) ) {
        continue;
    }

    $path = str_replace( '\\', '/', $path );
    $handle = fopen( $path, "rb" );
    $data = fread( $handle, filesize( $path ) );
    fclose( $handle );
    if ( $data == false ) {
	continue;
    }

    $key = preg_replace( "/[[:punct:]]/", "_", $file );
    echo "<li>Importing $path (Key: $key)...";
    try {
        $image->insert( array( "id" => $key,
               "data" => base64_encode( $data ) ), true );
        echo "Success";
    }
    catch ( MongoCursorException $e ) {
        echo "Exception: $e";
    }

    echo "</li>";
    ob_flush();
}

echo "</ol>";

Within minutes, I was able to have a browser based slide show application, which protected all my image files in the Database without being exposed out in the folder system.

I am enjoying every bit of Mongo, and hope to develop several application using that in the future.

My First Autobot

January 24th, 2011 No comments

Use Cases

  1. Find power source, remember the location map to optimize search of power sources. (eating). Witricity to be attempted.
  2. Find the sound direction and go to the source of the sound (watching TV, music, etc)
  3. Find light sources and move to that direction (vigilance)
  4. If it is dark everywhere, go to lower power mode (sleep)
  5. Avoid obstacles (walking)
  6. Self test for temperature, current flow, battery strength (fitness check)
  7. Report status to humans only while meeting them (consultation)
  8. Know heights and avoid falling of the floor from steps, tables, sofa, bed, etc. (no jumping)

Sensors needed

  1. Direction sensor
  2. Distance sensor
  3. Temperature sensor
  4. PIR sensor
  5. Odometer
  6. Tachometer
  7. Microphone
  8. IR LEDs and IR sensors
  9. Current sensor
  10. Voltmeters
  11. LDR sensor
  12. RFID

Controller

  1. ATMega 16/32 on the Autobot side
  2. AMD Phenom II 550BE on the PC side
  3. Communication over 415MHz ASK or Xbee

Know the Social Network that’s spun around you

December 20th, 2010 No comments
Social Networks have become a buzz in the contemporary world.  Most of us use them without understanding what they are and what they could do.  Basically, social networks are a platform the enable friends and family to be touch.  A good deal eh? Yes, it started just like that.  It started as a proxy for a pubs, playgrounds, parties where friends hang around.  Instead of going  for meeting a person, these social networks enabled friends to be in touch over instant messaging, blogging, scribbling, sharing pictures and videos by making use of the Internet technology.  This is definitely good, so far.  Do you know what else these social networks could do to you?  Let’s see some possibilities for an arbitrary ‘X’ whom you never know who that is:-
  1. Find your friends.  If you are little dumb, one can easily find your crush, hated ones, and what not.
  2. Find your likes and dislikes based on your messages, scribblings
  3. Know your places of interest based on what you have written about places, pointers on meeting spots that you chose to meet your friends, etc.
  4. Potentially know your neighborhood, by going through your blogs of scribblings about your neighbors or happenings in your vicinity.
  5. Find out what food you eat, and what you are allergic to.
  6. Find out what type of network connection you have, based on your connectivity logs.
  7. Find out paths from friends-of-friends for connecting an arbitrary person to you through your contacts.
  8. Find out which communities you belong to, and hence your social and professional networks
  9. Find out where you work, what your hobbies are.
  10. Find out where you studied, what you studied, what you aspire and where you are heading to; based on your professional community memberships, activity in forums, answers queries, etc.
  11. Find who you would meet, when and where based on your conversations
  12. Find out your mood swings, based on the vocabulary of your conversations
  13. Find out your pictures, and the pictures of the related ones in family and profession.
Remember something, whatever is meant to be private is never private in Social networks or any other electronic 3rd party services for that matter.  I have listed a very brief potential possibilities.  Please use these platform with fullest caution.

One side of the Internet researchers are working on “Internet Anonymization”, an idea to protect one’s identity; on the other hand, Internet giants are pushing “Social Networks” where they build intelligence on the so-called privacy of people.  What a weird world !?!

Powered by ScribeFire.

DevCamp 2010 by ThoughtWorks Inc., Chennai.

July 11th, 2010 No comments

Developer Camp 2010
10th July 2010, Chennai

It was my first attempt to take part in a BarCamp / unconference, which excited me very much after reading about them in Wikipedia.  Through some contacts, I was invited to attend the Developer Camp hosted by ThoughtWorks Inc, at Thiru Vi. Ka. Industrial Estate, Ekkattuthangal, Chennai on 10th July 2010.  I had originally offered to give a couple of talks on Text mining and Design patterns.  Though I had some anxiety about whether topics like Text Mining would sell amongst hard core developers, I was comforted by Balaji Damodaran (organizer) that there should be a lot of people interested in exploring AI.

    I reached ThoughtWorks office at 9:15AM and was surprised to find atleast a couple of dozen developers already come in.  Saturday morning for hard core developers start only after 11AM, but I was happy to be wrong then :) Registered myself as one of the developers and opted to talk about “Text Mining Applications”, “Plagiarism Detection”, “Text Classification using Naive Bayes”, “Design Patterns” for the 9:30AM slot.  The unconference started at around 9:45 with the introduction by Balaji Damodaran.  At that time, atleast 70 developers were there in the hall (cafetaria).  Then I was asked to start the talk by 10AM.  When I went to the hall, it had only 5 people as audience, which kind of killed me as I am always used to having big crowd as my audience (what an EGO I have!?).

   I had asked a couple of the audience boys to go for hunting more audience for the talk.  See I were to advertise and promote my talk, which in fact is critical for everything in the world we live.  One of the volunteers advised to use a microphone and start the talk.  When I started the talk, I was surprised to see that people walked in to fill up the hall.  The talk went on and on with a lot of interesting examples which made everyone introspect about the way we see and assess our neighbourhood.   I am sure my audience have understood now that everything that we see around and solve could be mathematically modeled and be solved using computers.  Hurray, we made it!!

    Followed by that talk, I was asked to talk about Design patterns as a lot of developers had voted for that topic.  Ok, I wanted a coffee break! Went to the cafeteria and made some light south Indian coffee.  I added some pulverized sugar to my coffee and came back to the hall, while I was talking with another developer from LatentView technologies.  To my surprise, the coffee tasted like made with sea water. Then I realized that I had added salt instead of sugar.  I would like to greet the “brahaspathi” who kept the salt bowl near the coffee vending machine. :)

    The talk on Design pattern started in a small room as the number of votes was ~10 (which is still a large number) in unconferences. When we started that talk, one of the volunteer said, he would want to record the talk which is a good idea. The talk started, and we found that lot of people started to come into the room and we had to move to a bigger hall as the number of audience was over 40, which is like “wow”. The talk went on for a while and we interacted about Singleton vs Multiton, Strategy, Factory vs Bridge patterns with lots of examples. Overall, it was a wonderful discussion forum where we learned a lot of insight about software design using design patterns.

    If I were to use one word to describe the audience, I would say “intriguing”.  It was an awesome experience for me to talk about some of my experiences to a wonderful audience that you had brought it.  It is very rare to find a combination of patient, smart, involved, intelligent, experienced audience who crave for knowledge.  Our talks helped us to introspect on to the technology that we have been practicing. The ambiance was very motivating in the sense, lot of natural light and spaciousness.  Overall, I enjoyed every bit of it.  I am little depressed that I could not enjoy the food as I was rushing back to office.  Also, I wanted to take part in the fish bowl about Industry-Academic Co-op, but couldn’t.  I am sure, there is a lot of people who got benefited by this program, in fact I heard that statement from a lot of the audience after the lecture/talk.

Thanks to Shiv Deepak for introducing DevCamp.
Thanks to Balaji Damodaran for inviting me to the DevCamp.
Thanks to Shaswat Nimesh for the photographs.

EFYTimes news article is here.

Powered by ScribeFire.

ACE 5.6.7 does not compile with STLport in Win32 environment

June 14th, 2010 No comments

ACE 5.6.7 does not compile with STLport in Windows environment (I used vc9 on Windows Server 2008) because of the following header in ACE (ACE_wrappers/ace/checked_iterator.h), which wrongly assumes the existence of stdext::checked_array_iterator in the iterator header.  A PRF is already submitted in the ACE mailing list (http://www.archivum.info/comp.soft-sys.ace/2008-07/00026/%5Bace-users%5D-Checked_iterator.h-problem-with-STLport..html)

# if defined (_MSC_VER) && (_MSC_FULL_VER >= 140050000)
// Checked iterators are currently only supported in MSVC++ 8 or better.
#  include <iterator>
# endif  /* _MSC_VER >= 1400 */

# if defined (_MSC_VER) && (_MSC_FULL_VER >= 140050000)
template <typename PTR>
stdext::checked_array_iterator<PTR>
ACE_make_checked_array_iterator (PTR buf, size_t len)
{
return stdext::checked_array_iterator (buf, len);
}
# else
template <typename PTR>
PTR
ACE_make_checked_array_iterator (PTR buf, size_t /* len */)
{
// Checked iterators are unsupported.  Just return the pointer to
// the buffer itself.
return buf;
}
# endif  /* _MSC_VER >= 1400 */

#endif  /* ACE_CHECKED_ITERATOR_H */

I need to develop a solution to it.  The easiest way to find a solution is to use some macro explicitly set by the STLport header, which is not set by any other STL libraries.  I chose to use the _STLP_ITERATOR macro set by “stl/stlport/iterator” header.

#ifndef _STLP_ITERATOR
#define _STLP_ITERATOR

# ifndef _STLP_OUTERMOST_HEADER_ID
#  define _STLP_OUTERMOST_HEADER_ID 0×38
#  include <stl/_prolog.h>
# endif

# ifdef _STLP_PRAGMA_ONCE
#  pragma once
# endif

#if defined (_STLP_IMPORT_VENDOR_STD)
# include _STLP_NATIVE_HEADER(iterator)
#endif /* IMPORT */

# ifndef _STLP_INTERNAL_ITERATOR_H
#  include <stl/_iterator.h>
# endif

# ifndef _STLP_INTERNAL_STREAM_ITERATOR_H
#  include <stl/_stream_iterator.h>
# endif

# if (_STLP_OUTERMOST_HEADER_ID == 0×38)
#  include <stl/_epilog.h>
#  undef _STLP_OUTERMOST_HEADER_ID
# endif

#endif /* _STLP_ITERATOR */

The solution is the following, where I have added the !defined(_STLP_ITERATOR) condition along with the check for Visual Studio compiler version.

# if !defined(_STLP_ITERATOR) && defined (_MSC_VER) && (_MSC_FULL_VER >= 140050000)
// Checked iterators are currently only supported in MSVC++ 8 or better.
#  include <iterator>
# endif  /* _MSC_VER >= 1400 */

# if defined (_MSC_VER) && (_MSC_FULL_VER >= 140050000)
template <typename PTR>
stdext::checked_array_iterator <PTR>
ACE_make_checked_array_iterator (PTR buf, size_t len)
{
return stdext::checked_array_iterator (buf, len);
}
# else
template <typename PTR>
PTR
ACE_make_checked_array_iterator (PTR buf, size_t /* len */)
{
// Checked iterators are unsupported. Just return the pointer to
// the buffer itself.
return buf;
}
#
endif  /* _MSC_VER >= 1400 */

#endif
/* ACE_CHECKED_ITERATOR_H */

Compile 32bit C/C++ Application in 64bit Linux

October 8th, 2009 No comments

To build (cross compilation) 32 bit C/C++ applications on 64 bit linux box, use “-m32″ as a compiler argument.  You would need the following 32bit libraries in place.

  1. Install glibc.i386, glibc-devel.i386
  2. Install libgcc.i386
  3. Install libstdc++.i386

#include <cstdio>
#include <iostream>
main()
{
    std::cout << “C++” << std::endl;
    printf ( “long: %d\n”, sizeof(long) );
    printf ( “long long: %d\n”, sizeof(long long) );
    return 0;
}

To compile in native 64bit:
[sudar@tstsrv12 tmp]$ g++ -m64 -o out64 code.cpp

[sudar@tstsrv12 tmp]$ ./out64
C++
long: 8
long long: 8

[sudar@tstsrv12 tmp]$ ldd out64
        linux-vdso.so.1 =>  (0x00007fffc43fe000)
        libstdc++.so.6 => /usr/lib64/libstdc++.so.6 (0x0000003e7be00000)
        libm.so.6 => /lib64/libm.so.6 (0×0000000000601000)
        libgcc_s.so.1 => /lib64/libgcc_s.so.1 (0x0000003e79e00000)
        libc.so.6 => /lib64/libc.so.6 (0×0000000000886000)
        /lib64/ld-linux-x86-64.so.2 (0×0000000000110000)

To compile in 32bit:
[sudar@tstsrv12 tmp]$ g++ -m32 -o out32 code.cpp

[sudar@tstsrv12 tmp]$ ./out32
C++
long: 4
long long: 8

[sudar@tstsrv12 tmp]$ ldd out32
        linux-gate.so.1 =>  (0×00130000)
        libstdc++.so.6 => /usr/lib/libstdc++.so.6 (0×00133000)
        libm.so.6 => /lib/libm.so.6 (0×00223000)
        libgcc_s.so.1 => /lib/libgcc_s.so.1 (0x0024c000)
        libc.so.6 => /lib/libc.so.6 (0x0025a000)
        /lib/ld-linux.so.2 (0×00110000)

Get MAC address using C++

July 4th, 2009 No comments

There is a Simple way to capture the MAC address of the machine in Win32 environment. In Win32, UUID (version 1) uses MAC address & timestamp as the seeds for generating the UUID.  In the UUID string, the last 6 bytes are the MAC address of the machine.  UuidCreateSequential() is the Win32 API which is used to capture the MAC address via the UUID generated by it.

#include <rpc.h>      // for UUID, UuidCreateSequential
#include <cstring>    // for memcpy
#include <algorithm>  // for std::swap

unsigned __int64
MACAddress( void ) const
{
    UUID u;
    ::UuidCreateSequential( &u );

    u.Data4[0] = u.Data4[1] = 0;

    std::swap( u.Data4[0], u.Data4[7] );
    std::swap( u.Data4[1], u.Data4[6] );
    std::swap( u.Data4[2], u.Data4[5] );
    std::swap( u.Data4[3], u.Data4[4] );

    unsigned __int64 code;
    ::memcpy( &code, &u.Data4, sizeof(u.Data4) );

    return code;
}   // MACAddress

Let me share another hack. If you want to generate strings which do not repeat themselves, you could use UuidCreate() Win32 API, which generates unique UUIDs everytime the function is called.  The UUID thus obtained can be converted to a number or a string depending upon the need, which guarantees non-repeatable ids.

Process Impossible (திருவிளையாடல்)

March 6th, 2009 No comments

Powered by ScribeFire.

/dev/mapper empty after reboot

February 26th, 2009 No comments

I had configured raid STRIPE on 2 500Gb hard disks to create a 1TB stripped disk using Intel Raid controller on Asus D5Q motherboard.  When I loaded CentOS 5.2, it identified the stripped disk correctly and formatted that too.  After the installation is completed, CentOS 5.2 was not able to detect the disk array at all.

dmesg was showing messages like the following:

device-mapper: uevent: version 1.0.3
device-mapper: ioctl: 4.11.5-ioctl (2007-12-12) initialised: dm-devel@redhat.com
attempt to access beyond end of device
sda: rw=0, want=1953535936, limit=976773168
Buffer I/O error on device sda1, logical block 1953535872
attempt to access beyond end of device

When I tried “fdisk -l”, I got the following:

Disk /dev/sda: 500.1 GB, 500107862016 bytes
255 heads, 63 sectors/track, 60801 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1      121602   976768033+  83  Linux

Disk /dev/sdb: 500.1 GB, 500107862016 bytes
255 heads, 63 sectors/track, 60801 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

Disk /dev/sdb doesn’t contain a valid partition table

The above is natural as RAID STRIPE is built using /dev/sda and /dev/sdb.

When I tried “dmraid -r”, I got

[root@tstsrv12 sudar]# dmraid -r
/dev/sda: isw, “isw_djiaeaj”, GROUP, ok, 976773166 sectors, data@ 0
/dev/sdb: isw, “isw_djiaeaj”, GROUP, ok, 976773166 sectors, data@ 0

Till this point, my /dev/mapper directory is:

[root@tstsrv12 sudar]# ll /dev/mapper/
total 0
crw——- 1 root root  10, 63 Feb 26 13:24 control

To enable the RAID multi-device, I had to run “dmraid -v -d -ay”, which returned:

[root@tstsrv12 sudar]# dmraid -v -d -ay
DEBUG: _find_set: searching isw_djiaeaj
DEBUG: _find_set: not found isw_djiaeaj
DEBUG: _find_set: searching isw_djiaeaj_Volume0
DEBUG: _find_set: searching isw_djiaeaj_Volume0
DEBUG: _find_set: not found isw_djiaeaj_Volume0
DEBUG: _find_set: not found isw_djiaeaj_Volume0
DEBUG: _find_set: searching isw_djiaeaj
DEBUG: _find_set: found isw_djiaeaj
DEBUG: _find_set: searching isw_djiaeaj_Volume0
DEBUG: _find_set: searching isw_djiaeaj_Volume0
DEBUG: _find_set: found isw_djiaeaj_Volume0
DEBUG: _find_set: found isw_djiaeaj_Volume0
DEBUG: checking isw device “/dev/sda”
DEBUG: checking isw device “/dev/sdb”
DEBUG: set status of set “isw_djiaeaj_Volume0″ to 16
DEBUG: set status of set “isw_djiaeaj” to 16
RAID set “isw_djiaeaj_Volume0″ already active
INFO: Activating GROUP RAID set “isw_djiaeaj”
DEBUG: _find_set: searching isw_djiaeaj_Volume0(null)1
DEBUG: _find_set: not found isw_djiaeaj_Volume0(null)1
RAID set “isw_djiaeaj_Volume0(null)1″ already active
INFO: Activating partition RAID set “isw_djiaeaj_Volume0(null)1″
DEBUG: freeing devices of RAID set “isw_djiaeaj_Volume0″
DEBUG: freeing device “isw_djiaeaj_Volume0″, path “/dev/sda”
DEBUG: freeing device “isw_djiaeaj_Volume0″, path “/dev/sdb”
DEBUG: freeing devices of RAID set “isw_djiaeaj”
DEBUG: freeing device “isw_djiaeaj”, path “/dev/sda”
DEBUG: freeing device “isw_djiaeaj”, path “/dev/sdb”
DEBUG: freeing devices of RAID set “isw_djiaeaj_Volume0(null)1″
DEBUG: freeing device “isw_djiaeaj_Volume0(null)1″, path “/dev/mapper/isw_djiaeaj_Volume0″

Now, my “/dev/mapper” directory is:

[root@tstsrv12 sudar]# ll /dev/mapper/
total 0
crw——- 1 root root  10, 63 Feb 26 13:24 control
brw-rw—- 1 root disk 253,  0 Feb 26 13:25 isw_djiaeaj_Volume0
brw-rw—- 1 root disk 253,  1 Feb 26 13:25 isw_djiaeaj_Volume0(null)1

Now, I am able to mount the RAID array on to my folder:

mount LABEL=/vendors /vendors

The output of “df” is now:

[root@tstsrv12 sudar]# df
Filesystem           1K-blocks      Used Available Use% Mounted on
/dev/sdc1             29753556   3516168  24701576  13% /
/dev/sdc3            410600744  26227400 363179596   7% /opt
/dev/sdd1            473086160    202800 448464160   1% /backup
tmpfs                  8154532         0   8154532   0% /dev/shm
/dev/mapper/isw_djiaeaj_Volume0(null)1
                     946176360    204572 897133388   1% /vendors

See, the RAID drive is mapped to /vendors folder.The Real Issue behind the problem is identified and a solution is presented at http://www.linuxquestions.org/questions/linux-kernel-70/centos-kernel-upgrade-breaks-dmraid-on-intel-software-raid-638450/. I am summarizing the changes here:

1. The real problem is the way dmraid is initialized at initrd’s init script. There is a bug in ‘mkinitrd’ script that generates ‘initrd**img’.
2. Open ‘/sbin/mkinitrd’ and search for “dmraid -ay -i -p”; It may appear something like “dmraid -ay -i -p “isw_cfgaijfacg_Volume0″.  Look at the suffix “Volume0″, this suffix attachment has become the problem for us.  dmraid does not like the Volume% suffix.  It should have been initialized as “dmraid -ay -i -p “isw_cfgaijfacg”".
3. Let’s change the script to the following from:
emit “dmraid -ay -i -p \”$dmname\”" 

to

dmnamecore=$(echo $dmname | sed -e ‘s/_Volumn[0-9]\+//;’)
emit “dmraid -ay -i -p \”$dmnamecore\”"
4. Generate a new initrd image by running “mkinitrd /boot/initrd-2.4.18-92.el5.img.dmraid `uname -r`”
5. Edit your /etc/grub.conf to use the newly created initrd image instead of the old one by editing the following line from “initrd /boot/initrd-2.4.18-92.el5.img” to “initrd /boot/initrd-2.4.18-92.el5.img.dmraid”
6. Reboot your machine.

Powered by ScribeFire.