Saturday, April 18, 2009

Bernardo The Smallest HTTPD Ever!

Sometimes I need an HTTP Server that allows me to run C or Python code or other scripts, and I need something that require 0 seconds of configuration. So I've decided to write a small httpd that does this work. (Use only for your test, not on a work machine!).

Bernardo (The name is inspired by the Zorro's servant) is a small HTTPD (Less that 200 lines of code) that allows you to Execute C, Python, Perl, PHP code or just "display" a file.

The Usage is Very simple: bernardo-httpd where www dir will be di root of your server and port is the TCP Port of the Service.

And now a simple code Example. You need to complete the HTTP Response Headers and then you can output your data. http://localhost:8080/test.py will display the "Hello Python" page.


#!/usr/bin/env python

import os

if __name__ == '__main__':
print "Content-Type: text/html"
print

print "<html>"
print "<head><title>Hello Python</title></head>"
print "<body>"
print "<h1>Hello Python</h1>"
print "<p><b>Request Method</b>: ", os.environ['REQUEST_METHOD']
print "<p><b>Query String</b>: ", os.environ['QUERY_STRING']
print "</body>"
print "<html>"



The Source Code is Available Here: Bernardo HTTPD Source Code.

Monday, April 13, 2009

Qt4 Xml Object Mapper

I'm looking at Google App Engine, to realize my Web Services (Obviously with Python). Web Service Response are generally in XML, and in the 90% of the case you'll need to map Xml Response with your Objects.
I've written a simple class that allows you to handle this task in a easy way, and with a quite good flexibility.


class TestObject : public QObject {
Q_OBJECT

Q_PROPERTY(QString surname READ surname WRITE setSurname)
Q_PROPERTY(QUrl website READ website WRITE setWebSite)
Q_PROPERTY(QString name READ name WRITE setName)
Q_PROPERTY(int age READ age WRITE setAge)

...
};


The First step is writing Objects that inherit from QObject and adding your properties. The Mapper uses the introspection to assign the values.

Mapping and Introspection: Maybe you don't like the Xml Node Name as your property's name, in this case you can use the HashMap that allows you to say "Hei, the TagName 'A' is the property 'foo'!". There's also a protected virtual method called convertData that tries to covert Xml Node Text Value to your property's type, take a look at this example.


QString xml = "<personInfo>"
"<age>21</age>"
"<personName>Matteo</personName>"
"<surname>Bertozzi</surname>"
"<personWeb>http://th30z.netsons.org/</personWeb>"
"</personInfo>";

THXmlHashMap dataMapping;
dataMapping.insert("personName", "name");
dataMapping.insert("personWeb", "website");

TestObject obj;
THXmlMapper xmlMapper(&obj);
xmlMapper.setMapping(dataMapping);
xmlMapper.map(xml);
qDebug() << obj.name() << obj.surname() << obj.age() << obj.website();



Source Code is Available Here: Qt4 Xml Object Mapper.

Saturday, April 11, 2009

Qt/Cocoa DataStream (Binary Communication)

In a time of REST protocols... I still love the Binary Communication. I don't have much ideas this period, I need to find a different job (C# doesn't fit my code necessity). The Example Source code, contains two Folder. One is the Qt4 TcpServer the second one is the ugly application that you can see below :)

Note: This example doesn't have anything related to the Qt 4.5 Cocoa Support.

Run the Qt4 Server, and the Cocoa Application (Remember to change the SERVER_HOST in AppController.m) and try to send some binary data. The Qt Server will receive this data it will resend you. Protocol is Very easy 1 byte of Data Type (Int8, Int16, ..) and Data. Remember that Intel arch is Little Endian.

Note: This is just a "Draft" a "non complete" example only to give the idea on how it works.

The Source Code is available here: Qt/Cocoa DataStream Source Code.

Qt4 PageFlow

Just another example that I don't have finished last Sunday. This is a simple widget that tries to be similar at the iPhone Mobile Safari Tabs Page. It's a really nice concept used also by Palm Pre to display the running apps. To switch  item swipe left or right (Press Mouse Button, Move mouse in a specified direction and Release it).

Source code is available here: Qt4 PageFlow Source Code.

Sunday, April 5, 2009

Qt4 CoverFlow and QThreadPool

My OpenMoko cannot stay without CoverFlow, so here some code. But I need a lightweight class that allows me to load all files in a directory or a list of files. Scanning directory with QDir entryList can take long time, and loading all the images can take much more, So what I need is a class that solve this problem internally.

I've choose the way of QRunnable and QThreadPool to solve this problem, so I've a class that execute QDir EntryList and another one that load an Image. When file is Ready (FileName, Path) a gray square is displayed and will be replaced by the Image once it is fully loaded and scaled. Using this method you can flow between files even if CPU and Disk are still loading to load images, this means No UI Freeze when CoverFlow starts.

Note 1: You can flow between images using Left and Right Key or Clicking with mouse at the Left or the Right of the Central one.
Note 2: If you want a CoverFlow with a great transition effect take a look at Ariya's PictureFlow.

The Source code Is Available Here: Qt4 CoverFlow Source Code.

Saturday, April 4, 2009

Qt4 Photos Preview

After a great start of the year, working on B*Tree and other Indexing Data Structure, it's about two month that I'm experimenting with User Interface... My Love still remains Networking and File-Systems but it's fun drawing images, shadows and UI Components.

Ok, Networking and Bytes can wait till next week. Today I've watched the PALM Pre Preview, No Back buttons, No Save buttons touch capability outside the glass... Data Abstraction... It's really nice.

But, There are so much things... so I've extracted a tiny nice one. A Nice way to display a "Photo Folders". You can customize the output a bit, Shadow, Photo Size, Second and Third Photo Offsets, but it's an example not a reusable class...

You can find the Source Code Here: Qt4 Photos Preview Source Code.

QtCentre Contest 2008 a Nice Surprise...

Backing home yesterday, I've found a postal package from QtCentre, there's also a QtCentre T-Shirt but this raining day doesn't permit to have great photos.

I would like to thanks again, all the QtCentre, Contest Sponsors (ICS, KDAB, BitRock, Thorsen, ...) and the "Trolltech" Team, for this, for the OpenMoko, and the incredible jobs they do!