Desi Founder @ Work

24 Jul, 2008

Django Subdomains

Posted by: sharjeel In: Programming

We have added a new feature at See’n'Report. It now provides each user with a personal sub-domain URL. The URL maintains user’s profile which lists all the photo reports submitted by the user. For example my See’n'Report profile URL is http://sharjeel.seenreport.com/.

Adding support for sub-domains in Django is simple but has a few catches. It took me quite some time to get everything working.

The following links provide a quick way to make sub-domains:

rossp.org - Using Subdomains with Django
Django Ticket #5022 - Proposed middleware: SubdomainURLsMiddleware

The summary of above links is that you have to put a wildcard entry in your domain DNS so that all your subdomains resolve to the IP on which your site is hosted. Then make sure apache handles all the requests to sub-domains; again using a wildcard in apache’s configuration. Then write a middleware which checks the subdomain in request’s HTTP Host header and processes it accordingly by either loading a customized URLs pattern or by handling the request with a view with appropriate logic.

This worked well for me and I was quickly able to make the changes so that http://www.seenreport.com/user/sharjeel was available at http://sharjeel.seenreport.com/.

There were a few little problems:

Firstly, sessions did not work across the subdomains. If I were logged in www.seenreport.com, I wouldn’t be logged in at sharjeel.seenreport.com.  This was solved by using setting the SESSION_COOKIE_DOMAIN variable in settings.py:

SESSION_COOKIE_DOMAIN = ‘.seenreport.com’ if not DEBUG else ‘.localhost’

Secondly, all the main navigation links (login, register etc.) which were written as relative links were now pointing to sharjeel.seenreport.com instead of www.seenreport.com. For instance the login on my profile page became sharjeel.seenreport.com/login instead of www.seenreport.com/login.

I could have hardcoded “www.seenreport.com” with these links to make them absolute URLs but that would have been bad in terms of maintainability. It requires an if-else logic for each link so that it renders accordingly on dev/test machines and the production machine.

I used <base> tag with href=”http://www.seenreport.com/” (for production, and localhost for dev) to make the navigation links relative to main domain.  This also changed the profile links. However, in our case, profile links are very few so adding hardcoded absolute URLs alongwith some {{ if }}  {{ else }} is viable.

Using base-href everything became well except one. Ajax calls didn’t work now. When using <base> tag, the URLs of your ajax calls become relative to href. In this case when an ajax call is made, a domain other than your current one is contacted, which is disallowed by the browser and following exception is raised:

“Access to restricted URI denied” code: “1012″

To overcome this, I appended following with each ajax call’s URL:

location.protocol + ‘//’ + location.host + original_url …

This made all the ajax calls relative to the current subdomain and made everything work perfectly. Took me quite some time to figure out :)

14 Jul, 2008

My first carputer - v0.1

Posted by: sharjeel In: carputer

Have you ever thought about putting a PC in your car? I did!

LCD ScreenMini KeyboardMini Keyboard
I’ve put my first carputer (car computer) into use in my Honda Civic EXI 96. The whole process which includes lots of Googling, forums readings, hectic trips to Hafeez Center and writing some code, too me two years. This time span is not a metric of complexity of the task but just a reflection of my great quality of laziness. The carputer has bare minimum functionality and it requires a lot of improvements but I feel that the time spent was worth it.

Specs

  • Dell Optiplex SX270, 512 MB RAM, 40 GB HDD – Rs. 9,600 ~ $150
  • Windows XP (came with the machine)
  • Lilliput 12” Touchscreen – $200 ~ Rs. 12,800
  • Garmin USB GPS - $100 ~ Rs. 6000
  • Havit Mini-Keyboard – Rs. 800 ~ $13
  • Sony Ericsson T290i – Rs. 1,200 ~ $20
  • And some other small accessories

The whole system cost me about Rs. 30,000 i.e. about US$ 500.

Some of its usages

  • Play music
  • Play videos. [I can watch my favourite Google TechTalks in my car ;) ]
  • Navigate my route using Google Earth
  • Control cell phone using voice commands
  • Car Tracking
  • Parking Assistance using sensors and cameras.
  • Night vision cameras
  • Accident Recording
  • Monitor Car Health using OBD-II sensors
  • War driving
  • Use Emacs, Eclipse & PyScripter on the go (soon a new law will be passed to forbid the use of IDEs while driving)

Right now only the first three of the purposes are being served.

Bao G asked me to add one more thing in my wish list: A button, when pressed, activates the weapon system and brings up machine gun in front and two missiles on each side of the car. The way people drive around on the roads these days, I am seriously thinking about it.

Components & Installation Details

Machine

Dell OptiplexUnder the seat

The Dell Optiplex SX270 is extremely compact. I found it ideal for three reasons:

Small Size
Low Cost
Runs on DC 12v (Most Important)

The machine is so compact (8.5cm x 24.7cm x 24.2cm) that it can be easily placed at most of the places in your car. Also, another great thing about SX-270 is that it has parallel and serial ports. Which means you can easily attach it to other devices and program them. New machines usually don’t have Serial and especially Parallel ports and put a lot of restrictions on interfacing with other devices.

For the time being I’ve placed it under the passenger seat. However I plan to move it in the trunk in future.

I bought it back in October last year and at that time its cost was only Rs. 9,600 ($160). The chap selling these machines wasn’t finding customers because it runs on 12v and has a separate AC-DC adapter. And that is precisely the reason I bought it.

It is no more available in Hafeez Center. I wish I had bought another one for my home router.

Power

Power drawn directly from batteryJoin

Dell Optiplex SX-270 runs on 12v 12.5A. That means it can be powered directly from cigarette lighter. However it can easily fry up the wiring. So I ran wires from machines directly to the car battery with a 15A fuse and a connector in between. 15A fuse prevents it from getting cooked up during spikes etc and the connector is there so I may conveniently unplug the machine for car wash etc.

The Optiplex SX-270 adapter is proprietary. Initially I tried to reverse engineer it but later found everything about at this link.

There are two major problems with my approach of directly attaching to battery. Firstly, when the car is not on ignition the machine is on battery which can be drained out easily. Secondly, when the car is given self-start, there is a spike of power on the wiring which restarts the machine. MP3Car Forums suggest that this spike can also cook the machine but I think 15A fuse will take care of it.

To make sure that the battery doesn’t drain, I have turned on XP’s power saving and have added the rule to hibernate the machine after 1 hour of inactivity. The day XP gets hanged, the day my car’s battery shall drain out.

Addressing the second problem is quite difficult. It appears that I’ll have to buy some extra stuff to avoid that spike on self start. A UPS will do but that’ll be another headache. For the time being I’ve enabled “Auto Power On” in BIOS which makes the machine turn on automatically after restarting from a power spike.

Every time I get out of my car, I hibernate the machine. Even if I forget, it gets hibernated after 1 hour anyway. When I start the car next time, the machine gets turned on automatically and Windows starts from where I left. This is certainly not a very good way and I am looking for other better ways.

Touchscreen LCD

LilliputLilliput

Touchscreens come in different forms. In-dash, on-dash. In-dash screens are usually expensive so I bought Lilliput’s on-dash touchscreen LCD. Lilliput’s Touchscreen LCD is specifically designed for car usage. It comes with all the wires, the drivers, the configuration software and runs well out of the box. It also has a nice stand to stick the screen at a nice place on the dashboard.

I’ve placed it under the console, in front of the lever gear. Though at this place it is not as much accessible as it would be somewhere on the console or on dashboard, it is prevent by prying eyes of the other car drivers ;) Also, it just fitted well into the lower area of Civic and did not require any drilling or interior modding.

Garmin External USB GPS

Garmin external GPS

Garmin External GPS module uses NMEA format. It has a Serial Port version as well USB. I didn’t do much R&D over this and bought the one with USB thinking it’ll be better. But it turned out that it wasn’t. The only application I found supporting it was Google Earth Pro. Every other application demanded a COM port.

However I found this nice utility called Spanner by Garmin which takes your USB based GPS device and maps it onto several virtual COM ports. This means that different applications can access your GPS data simultaneously through different COM ports.

The problem with Spanner is that many times when the system wakes up from hibernation it does not detect the GPS device and the system has to be restarted. Otherwise it is great.

Havit Mini Keyboard

Havit’s Mini Keyboard is extremely thin. I’ve placed it in the slit between the passenger seat and the handbrake cover.

It can also be placed under the sun visor but then you’ll have to run the wires all over.

UserInteface: RoadRunner

Road Runner is an application specifically designed for car computers. It has a skinnable interface which provides a touch friendly environment with big buttons and support for gestures. Its functionality can be enhanced by its plug-in support. This might not be the best software for user interface of carputer but it surely is the most popular.

Google Earth Navigation

This may seem useless in Pakistan as we don’t have digital maps available here. However tracking your location on just satellite imagery is still quite useful and fun. Most of the times you remember can recognize the roads and it becomes easy to figure out rest of the map.

Google Earth works best when connected to internet but even when it isn’t it works well using its cache. To make it work in my city, I had to increase the cache size, hover over all of the areas and then copy the cache from %HOME% folder to the carputer machine.

This manual process can be tiresome but can be overcome using a great utility called Google Earth Voyager. GEV makes Google Earth visit certain areas and cache them. Just give it starting and ending coordinates and it will cache the rectangular region in between.

Once you get satellite imagery cached, you have to map your GPS location on Google Earth. For this you can buy Google Earth Pro or use some third party software. As I have experience with GPS programming as well as Google Earth interfacing, I started writing my own. It is still under development.

For the time being I am using free version of GooPS. It not only maps your car on Google Earth, it also shows your speed and direction as well as the track you just followed. There are also other options like logging your data etc. which I haven’t explored yet.

However I feel that GooPS is not up to the mark. I’d still prefer writing my own program in which I can add other features I want such as trips, bookmarked locations and different routes I followed.

Take a look at this video we shot while driving around LUMS.

Audio output to car’s stereo

To avoid any modding, I simply bought this cassette based adapter which has a wire with jack which you plug into your machine’s sound output and insert the cassette into cassette player to get computer’s sound on your car’s speakers.

Car Tracking

Car tracking is simple. I haven’t done it yet but all the required stuff and techniques are with me. All I need is to attach the Sony Ericsson T290i phone with a connection and make it respond to SMS queries from my phone. If I want to find out my car’s location, all I’ll do is SMS my car with a query and it will return me its coordinates. Surely it won’t work

That’s all folks! I’ll keep my blog updated with the updates on my carputer.

PS: A lot of people have asked me for a test ride. FYI ticket is Rs. 50 / head :P

02 Jul, 2008

Great agile development workspace arrangement

Posted by: sharjeel In: Uncategorized

Yesterday when I came to my lab I noticed a great change. Mansoor had re-arranged the semi-hexagonal table modules in a great way. Its practical effectiveness was obvious at the first glance.

Take a look:

Great Agile Development Workspace Arrangement

It can easily accomodate atleast five persons. With each of them having enough room to for an LCD, a notebook and a writing pad. Moreover most of the team members are close enough to interact with each other without having to shout. This is great particularly for Agile Software Development.

The arrangement has lots of merits. The core reason in my opinion behind its effectiveness remains the use of hexagon. Hexagons have a unique property in polygons that if you tile them on a plane, median of each hexagon will be equidistant from the medians of neighboring hexagons. That is more or less the reason why they are used in cellular networks. I believe same is the reason why this workspace works so well.

Hexagon’s inspiration of use in art, design and engineering comes from nature. Take a look at this link.

27 Jun, 2008

Wardriving Lahore

Posted by: sharjeel In: Security

Ever wondered how many access points are there in your city? Last week I did an interesting thing: Wardriving in my city Lahore.

Wardriving is act of searching for WiFi wirless networks on the go using laptop or other mobile devices. It is legal in most parts of the world but ethically it comes under gray area. I personally do not find anything unethical with it as far as the discovered networks are not misused. Hence I did it :)

I used my HTC P3300 equipped with builtin WiFi and GPS to scan the networks using an interesting PocketPC application called WiFiFoFum. It promisciously looks for wireless networks and records each access point’s information (MAC, SSID etc) alongwith geo-coordinates and allows you to later map it on Google Earth for better visualization. It is free as long as you use it for non-commercial purposes.

I war-drove my way from home to office which is 15 kilometers. The areas I passed roughly include Garhi Shahu, Canal,  Gulberg Main Boulevard, Cavalry Ground, Walton Road, DHA Main Boulevard and LUMS.

What I discovered was quite surprising. My office is 15 kilometers from my home and in that span I found more than 90 access points. I wasn’t expecting Lahore to be that hi-tech! More interesting was the fact that most of these access points were insecure with their default SSIDs such as ‘linksys’, ‘netgear’ etc. I did not try connecting to any of these but I’m sure many of them would be using default passwords. Those with named SSIDs suggested that many of the insecure networks were of offices.

Here is a screen shot of Google Earth’s mapping at high level. The green ones are insecure while the red ones are those having some level of authentication or encryption.

Wardriving Lahore

With insecure wireless networks it is very easy for even a script kiddie to steal your bandwidth as well as invade your privacy. Make sure you keep your network secure, or alternatively you can have some fun

24 Jun, 2008

Why we discontinued MapMyPakistan

Posted by: sharjeel In: mapping

Today is the day when I can better explain why we discontinued MapMyPakistan.

Three years ago, when I was about to graduate from FAST-NU, I started working on an idea for a startup: Maps for Pakistan. At that time there were no street level maps available and their demand was obvious: Car Navigation, Route Optimization, Courier Services, Supply Chain … you name it! The use was so obvious that I could think of hundreds of ways of monetization.

The real challenge was to get data since there were no accurate street level maps already available, not in hard copy, not even with Survey of Pakistan. As Web 2.0 was getting hot, the most appealing way to get it done was through collaboration.

Over the the next two years I did my day job and at night I read research papers, explored different technologies and worked on some prototypes until I was convinced that I was ready work on it fulltime. I gave up my day job, met Dr. Umar Saif who lent in space in his lab for startup, convinced Adil to join me and started working on it. By this time I was familiar with PostGIS, Python, a little bit Django, Google Maps API and some other desktop GIS tools such as Quantum GIS, uDig etc. I had with me one of the best minds I’ve ever met. The only thing we needed to just do it and do it well.

However as we started working on it, we started getting the feeling that probably the big players will be more interested in doing the collaboration on their own and probably acquisition won’t be a good exit strategy. In May 2007 Google released My Maps, a service that allows you to create your own simple maps and share them with others. Although it was not truly collaborative, it was heading in the same direction. We also got some news that Google is working on digitizing street level maps of Pakistan.

Today it is confirmed that we were thinking in right direction. Today Google has opened Map Maker, a collaborative tool for digital cartography of countries where street maps are unavailable. See the following posts for more info.

Google Opens Mapmaker for Pakistan
Google LatLong: Making your mark on the world

I tried using the system, and as always, Google has done it well! I can’t wait to see detailed street level maps of Pakistan so I may use my GPS enabled phone for car navigation :)

I am happy that we changed direction of 2’s Complement. Afterall, we still have some great products on our portfolio including iSMS.pk and SeeNReport.com

PS: If anyone needs any help related to collaborative mapping, feel free to ask. We’d love to share what we learnt.

13 Jun, 2008

Lesson of the day

Posted by: sharjeel In: Uncategorized

Lesson of the day: Get your site up before you make a sales call

It not just a good idea but a must-have that your site should be up before you ask someone to open it … especially when you are making a sales call to a potential customer :D

Me: XYZ TV Channel?
Rep: Yes
Me: [Describes  See n Report]
Rep: Ahan … that seems quite interesting, whats the URL?
Me: www.seenreport.com
Rep: Ok, lemme open it.
(Adil Shouting in the background: “Oye! Site is not working, apache giving rights access problems!”)
Rep: Site seems down
Me (Shamelessly): Yeah, my engineer just told me it is down. I’ll get back to you in a few minutes

19 May, 2008

We are open, please find your way

Posted by: sharjeel In: Eye-Witness

01 May, 2008

Security by obscurity works best sometimes

Posted by: sharjeel In: Security

When I was graduating from FAST-NU, I designed a web based system in PHP/MySQL for collecting data for Yearbook of our batch. Each student of our batch was given a username and password using which s/he could enter the personal data, make comments about friends, give nicknames and other such features.

At that time there was no server in our university available on which it could be deployed and the only place to host it was a Windows based machine in the labs on which all files in wwwroot folder were readable to anyone logged into the machine. But it did not mater as the MySQL database was secure and I did not mind anyone getting my PHP code. The only problem was that the password to MySQL had to be stored in a PHP config file and it could be read by anyone. I knew one naughty junior was already looking forward to the deployment of the application and I did not want to take any risk. On the other hand I wanted to get it deployed ASAP as well.

I thought about encrypting the password but the key had to be stored at a world readable place. I thought about many other possibilities but found loopholes in each of them. Finally I came up with a clever scheme. After applying it my config file looked like this:

$db_host = “localhost”;
$db_user = “yearbook”;
$db_pass = “********”;

Two days later the kid showed up looking a bit frustrated. He said “How the hell did you mask your password in a PHP file? I have looked all over web and I couldn’t find any info about password masking in PHP or any other text file”.

With a smug look I replied “I rewrote the NTFS to do that”. He went away looking a bit angry as well as disappointed.

I wish the poor chap had thought about the possibility of using eights asterisks as my password.

Tags:

19 Apr, 2008

What Lisp & Assembly instill

Posted by: sharjeel In: Programming

My Assembly Programming Language teacher, Belal Hashmi Sahib said in the first lecture of the course, “Assembly is extremely simple. It is so simple that students don’t expect such simplicity and hence it starts appearing complex to them”. Then he wrote MOV AX, BX on the board, explained it and asked if everyone understood it. Everybody nodded. He asked again to make sure there was not even a slightest bit of confusion. It seemed very simple and trivial to everyone. He proceeded by saying “This is the end of the assembly language programming course. The rest of the course is just the revision of MOV AX,BX”. Everybody thought it was a joke but later it turned out that assembly indeed ended at MOV AX, BX. The rest of the course was about learning other concepts to fully utilize the potential of MOV AX, BX.

Last year I learned Lisp on my own and then TAed a course on Lisp at LUMS with Dr. Umar Saif. In the first lecture the first thing Dr. Umar taught was (lambda (x) (+ x x)). At this moment I realized that just like Assembly, Lisp had finished! Because if students had understood it, there was nothing much about the language left. All they needed now was to understand some important concepts such as recursion, closures etc. to use Lisp.

What is common between Assembly and Lisp? Both of these two languages are very simple to learn as they have little syntax or fancy language features. However the process of learning them compels one to grasp some important concepts. For instance by learning assembly you are bound to understand some flavor of computer architecture, interrupts, timers and other hardware related stuff. Similarly by learning Lisp you automatically get to know functional programming, interpretation, recursion, closures etc.

A programmer familiar with assembly can easily understand the use of unions, options such as __decl and other such features of C because he knows the underlying magic. Similarly if one wants to understand the philosophy of the features of Javascript, Python & Ruby I strongly recommend learning Lisp.

Tags: ,

03 Apr, 2008

Secret Weapons for Java Programmers

Posted by: sharjeel In: Programming

Many great hackers and programmers do not consider Java programming language a good choice. The safety features of Java such as pure OO, strong typing etc. force its users to laboriously write lots of classes, declare and typecast variables, call extraneous methods to fetch simple data and puts under a lot of restrictions. Such techniques might be good to prevent a mediocre coder from making blunders and protecting him from shooting himself in the foot. However for great hackers these things only take away the freedom to freely convert their thought process into code and make effective use of their time. The extra safeties are considered distractions which force the users to focus on implementation rather than the logic.

However it is a fact that many good programmers do use Java as their main tool. Some of them use it because they’ve never been exposed to great stuff such as Lisp, Ruby or Python. Others are those who have been forced to use it by their Pointy-Haired-Bosses or unavailability of a good library.

I want to help out both of these types of good programmers. Recently I went through a similar situation where I could not find a library except in Java. I thought being forced to Java is going to be a nightmare for me but I found a fantastic work around which lets you use power of great languages while staying in the Java platform.

I wanted to use a good library for interfacing with my GSM Modem. The only reasonable library I could find was SMSLib. Bad day: it was written in Java. I tried to play around with it for a while by the standard hit & trial methods of writing small code snippets with many printf’s and compiling and running to see the result. But I was soon lost in memorizing dozens of classes, pointless getters and setters and other irrelevant stuff I did not need at all to implement my logic.

Before banging my head against the wall and rewriting the whole library in C with interfaces to Python and Lisp, I decided to give a try to one of the scripting languages for Java platform. I’d heard about Jython, Rhino, Grooby and JRuby. Since my favorite language is Python, I chose Jython for the experiment.

I downloaded Jython from Jython.org. Its installation was a charm. Double clicking the downloaded JAR did everything for me including writing a batch file for setting up environment variables. Running the batch files gives Jython’s shell. The shell is exactly like Python’s shell except that it runs in Java and can run Python code as well as uses Java’s libraries and classes.

C:/> Jython
Jython 2.2.1 on java1.6.0_01
Type “copyright”, “credits” or “license” for more information.
>>>

First thing to do for any programming experiment: Print Hello World. How do I do that?

>>> print ‘Hello World’
Hello World

What? Don’t I have to make a separate class with a static void type main method which instantiates printer factory, inherited by abstract factory to take a message as argument and forward it to system.out.println and use that factory to print “Hello World”? No! Because you are still using Java’s platform, but on top of it instead of Java language you are using one of the most powerful languages.

Before moving on I just wanted to make sure if I still had the power of Python. So I tried writing a MD5 reverser in three lines which takes a file containing list of commonly used passwords.

>>> from md5 import md5
>>> f = open(’cpasses.txt’)
>>> p = dict([ (md5(s.strip()).hexdigest(), s.strip()) for s in f.readlines()])
>>> p[’f30aa7a662c728b7407c54ae6bfd27d1′] # Test
‘hello123’

Beautiful! Lets try importing Java classes.

>>> from java.util import *
>>> dir()
[’AbstractCollection’, ‘AbstractList’, …. ‘regex’, ’spi’, ‘zip’]

Cool! I can now use all classes of Java with Python’s syntax. Let’s try SMSLib with Jython.

>>> from org.smslib import *
Traceback (innermost last):
File “<console>”, line 1, in ?
ImportError: no module named smslib

All I had to do was put SMSLib’s Jar in CLASSPATH.

>>> from org.smslib import *
>>> dir()
[’AGateway’, ‘AsyncEvents’, ‘DeliveryStatuses’ … ’smsserver’, ’smssvr’]
>>> c = Service()
>>> dir ( c )
[’S', ‘__init__’, ‘addGateway’, ‘class’, ‘deleteMessage’ … ‘toString’, ‘wait’]

You can introspect methods while you are experimenting giving commands. This effectively means that you can debug, code and execute all at the same time. In the interactive mode the state of execution is maintained as you give command so you can try out small things without regenerating the whole scenario!

By trying out small things I was able to easily figure out how to use different stuff of SmsLib. Afterwards I copy pasted all the relevant commands I executed in a separate .jy file and made small changes to carve out my required code. Jython made my day!

Not only just Python, but you can also use Ruby and Javascript over Java platform using JRuby and Rhino. There is also Groovy, another dynamic language specifically designed for Java. These languages make programming with Java platform much more powerful. By power, I mean the ability to code conveniently and timely without getting frustrated.

So the next time your Pointy-Haired-Boss tells you that the next project is to be done in Java, don’t argue; just include an extra JAR of your favorite powerful language in your Java project.

PS: Python and Ruby are also available for .net but I haven’t tried them out.


  • Babar Bhatti: Very interesting post. You should publish it at other blogs as well.
  • party balloons: This article gets a big thank you from me. I appreciate this kind of information.
  • Bergamot: It's interesting how different people solve the same problem different ways. I'd probably have written a mod-rewrite rule to change xxxx.yyyy.com/z

About

This is an example of a WordPress page, you could edit this to put information about yourself or your site so readers know where you are coming from.