Archive for the ‘Programming’ Category

Getting Jiggy with it, na-na-naaa-na-naaa

Friday, May 30th, 2008

After a very long period of ignoring Jiggy I finally decided to give it a try, the only problem it was that i didn’t have any idea to experiment with and i had no useful need of an iPhone application, so after thinking at it for a couple f hours i finally found something. Anyway the idea itself its not important first I wanted to get started with some tutorials just to get the hang of it.

Let’s get to the actual story because I’m feeling that I’m giving too many details (like I usually do), the thing is that I find the browser based IDE too primitive, it’s missing a lot of features but the most important one that doesn’t exist is the “autocomplete” feature. So of-course that i wanted to develop with TextMate, my favorite editor. The editing part presents no problem you can mount the iPhone file system very easy, via SSH (using sshfs) or via AFP, the real problem comes in when you want to debug or test the application. Actual that’s the only powerful feature of the web-based editor is the test/debug feature … hmmmm, that got me started I was sure I could replicate that in TextMate, said and done. The question was how, the answer was … very easy.

So, on the iPhone I installed Lighttpd with CGI support and Perl. Luckily I’m a Perl programmer and I had no problem writing a set of services for interacting with the iPhone via http trough a browser … something to start and stop an specific application. Well actually I couldn’t resist so I implemented an entire system, sort of a mini framework. With all of this all I had to do was to interact with these services through TextMate, said and done, now that’s why I really like this editor, it’s so flexible and adding features to it it’s like taking a walk in the park (that’s if you are a developer and you are on the right PATH). So I wrote a set of TextMate commands to control the application execution on the phone. Basically it’s just a class that sends and receives data using the LWP library, at the moment I’m still experiencing with it a little bit but sometime next week I’m sure I’ll have TextMate bundle ready.

I saw the light

Monday, December 24th, 2007

Upgrading my operating system to Leopard didn’t help my development environment at all because Leopard ships with Apache 2.0 without mod_perl and that’s a problem because mod_perl doesn’t compile properly. With all this I had to switch to lighttpd and run HTML::Mason with FCGI, for that I had to install the new web server and create a FCGI handler and configure lighttpd to run the pages trough this handler. This is how the handler looks like:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#!/usr/bin/perl
 
use strict;
use warnings;
 
use CGI::Fast;
use HTML::Mason::CGIHandler;
use BSD::Resource;
use HTML::Entities;
 
my $handler;
while (my $cgi = CGI::Fast->new()) {
	$handler = HTML::Mason::CGIHandler->new(
	data_dir      => $ENV{MASON_DATA_ROOT}, 
	error_mode    => 'output', 
	allow_globals => [],
	request_class => 'MasonX::Request::WithApacheSession',
 
	session_cookie_name => 'sid',
	session_cookie_domain => '127.0.0.1',
	session_class => 'Apache::Session::File',
	session_directory => '/usr/mason/Tracker/sessions/data',
	session_lock_directory => '/usr/mason/Tracker/sessions/locks', 
	session_use_cookie => 1,
	session_cookie_expires => '+1d'
	) if(!$handler);
 
    my($url) = ($ENV{'REQUEST_URI'}=~/(.*)/);   
    $url =~ s{\?.*}{};
    eval {
	$url .= "/index.html" if (
		(!$handler->interp->comp_exists($url)) and 
		($handler->interp->comp_exists("${url}/index.html")));
 
        $handler->_handler({comp => $url, cgi => $cgi,});
    };
 
    if(my $raw_error = $@) {
	print $cgi->header();
	print "A really fatal error occured while processing your request:";
	print encode_entities( $raw_error );
    }
}
 
exit;

This example also contains the basic session configuration for the MasonX::Request::WithApacheSession and of course you need to change the paths. Also in order to work properly you need to read the previous article.

The next step is to configure lighttpd to use this handler, the configuration kinda looks something like this:

1
2
3
4
5
6
7
8
9
10
11
fastcgi.server = (
"/Tracker/app" => (
	(
	"socket" => "/tmp/fastcgi.socket",
	"bin-path" => "/Library/WebServer/CGI-Executables/mason.fcgi",
	"check-local" => "disable",
	"bin-environment" => (
	"MASON_COMP_ROOT" => "/Library/WebServer/Documents/Tracker/app",
	"MASON_DATA_ROOT" => "/usr/mason/Tracker"
	))
))

DON’T FORGET TO ENABLE mod_fastcgi in lighttpd. I used the same paths as Apache because I didn’t want to move the files and the links again plus I also have a lot of local scripts that are based on those paths. But you might still encounter some problems when you will try to restart the web server. On MacOS X you can configure launchd to take care of the automatic start/stop functions, but I’m on old fashion guy so .. I created a simple script for that:

1
2
3
4
5
#!/bin/bash
 
killall -9 lighttpd
rm -rf /tmp/fastcgi.socket-*
/usr/local/sbin/lighttpd -f /private/etc/lighttpd.conf

That should be sufficient to run lighttpd with Mason with MasonX::Request::WithApacheSession … so far I haven’t encountered any problems so please send me an email if you do. Merry Christmas everyone!

The court is now in SESSION! (uncensored version)

Friday, December 7th, 2007

Of course I got bored the past few days and I started working on some silly project just to have something to do. So I created my deploy environment on my hosting provider which is DreamHost, using FCGI and HTML::Mason … everything good so far until I had to use sessions in my application.

Said and done I installed from CPAN MasonX::Request::WithApacheSession from this point all my work really started to get ugly. MasonX::Request::WithApacheSession DOES NOT, AND I MEAN NOT WORK WITHOUT SOME PATCHING WITH FCGI (wtf?!?) it seems that there are several bugs (one in MasonX::Request::WithApacheSession and another two in Apache::Session::Wrapper).

So after several hours of googling the problem I managed to find a link that helped me solve some of the problems. Basically there were two patches one for MasonX::Request::WithApacheSession and one for the Apache::Session::Wrapper, after applying the patches the server stopped displaying “Internal Server Error” message which lead me to think that I was on the right track with the problem.

BUT when I tried to test it the browser was not receiving the cookie, that’s when I started to investigate the problem. I opened Apache::Session::Wrapper and looked in the “_bake_cookie” subroutine which is setting the cookie in browser and discovered that the $header_object variable is in fact an instance of HTML::Mason::FakeApache and the $meth variable called from that instance was called “err_headers_out”, this is how the cookie is set in browser, this is how my file looks (after applying the previous patches):

my $header_object = $self->{header_object};
 
for my $meth (@HeaderMethods)
{   
	if ( $header_object->can($meth) )
	{
		$header_object->$meth( 'Set-Cookie' => $cookie );
		last;
	}
}

The @HeaderMethods array contains to names err_headers_out and headers_out, HTML::Mason::FakeApache implements both those methods so obviously it was running the first one in the list which is err_headers_out the problem is that err_headers_out DOES NOT TAKE ANY MOTHER FUCKING PARAMETERS!!! but err_header_out (yes .. it’ without the “s”) does. The fix is pretty easy just remove the “s” and it should work.

Piss out !

The yellow brick road is coming to an end …

Thursday, September 6th, 2007

I’m happy to announce that in a few days I will put a screen-cast for the Fuel project. Basically Fuel is a RoR-like framework for Perl and it runs on Apache, mod_perl it uses Mason as a templating system and Class::DBI for accessing the database, it’s still in a begging phase but it’s working and applications can be developed with it. Right now I’m still working on the files generator script in order to finalize the bits and pieces for this application, hopefully everything will be done by this week-end. Until then then you can get a sneak peek at the files on my Subversion server http://svn.stuffo.info/FuelRails. Also in the next couple of days I will try to explain the mechanism of this mini-bundled-framework.

Never, never I tells ya’

Thursday, July 5th, 2007

I just realized today that if I don’t explicitly promote the new components at work to my colleagues these new components will NEVER be used. Well actually it’s like this for a while now everybody at work started to see the advantages brought by AJAX and they started using it in various forms for delegating information and making the static content more “dynamic”. Of course everybody started implementing their own AJAX library … well more or less, most of the cases it’s the same JavaScript code that you find on the first link when you search on Google for “XMLHttpRequest”, and actually it’s not really a library more of a function customized for everyone’s needs. Also many of these implementations don’t even check to see whether the operation ended successfully because many of them don’t even send data back returning the status of the custom operation.

It was the same case even on my project which had a custom AJAX implementation though a very complicated mechanism which involved too many steps. To be honest I didn’t even bother to try to understand what was going on there but after some benchmarking tests I decided that we should re-write the whole Data-to-AJAX-to-Data problem, so this way we came up with something similar to a web service which has several levels of customization (serialization, content-type, security) and MOST important it sets a standard for working with AJAX and also it clearly separates the presentation layer from the domain logic. Also together with the JavaScript guy we wrote a class for the client-side which has error management and it also implements a customization level trough delegates. All being said and done we set a standard in our application for us for an easy implementation but I was surprised to find that most of the programmers don’t care about this … they still use the old crappy methods of getting the data and justify by saying that it takes too much to implement these new service model, and now I try a question: “Does it really take more time to implement this model, more than patching the current implementations?”, there were also people interested in this people for which I explicitly created a small example so this way I managed to get some feedback and even some implementation requests.

And probably the conclusion is that inside a company it’s probably necessary once a while to promote your work, even more when the company has many projects on the role that use the same technology. But the implementation for these components still remains an issue. So how to deal with these problems?

On a more personal thing but the title applies perfectly in this case, yesterday evening I went to a book-signing followed by a concert, well the concert was quite nice actually (I also know some guys from the band) but when the author started reading from the book I realized he’s a complete idiot and I was absolutely sure that I would NEVER buy the book.

Peace out