Friday, May 29, 2009

Tool: IP Packet Sniffer

Today I've implemented a simple IP Packet Sniffer.

Sometimes is useful to know what're the IP packages that transit on your network, for Analyze network problems, Monitor network usage, Debug client/server communications or Debug network protocol implementations.

The tool is very simple, but I think that can be useful to learn something more about IP and TCP packets and Protocols implementation.

Maybe you can extend this Tool to support other Transport Layer Protocol.

Note: You need to run it as Root.

You can find the Source Code Here: IP Packet Sniffer Source Code.

Saturday, May 16, 2009

Qt4 Jump Away Animation

When I've started working on Mac OS X with Core Animation, I was impressed by an Example (I don't remember the link, if someone knows it, post it in the comments! thanks)... Very few lines of code for a great effect.

After a year, or probably more.. this example was came back to my mind, so I've decided to clone using Qt. And below you can see a short Demonstration Video. Your desktop jump away.. full screen is fantastic :)



The Source Code is Available Here: Qt4 Jump Away Animation Source Code.

Friday, May 15, 2009

PyQt4: PyCon3 Photos on Flickr

PyCon3 was finished, Only one week is passed and I already miss it. Guido van Rossum, Alex Martelli, Ariya Hidayat, Raymond Hettinger, David Boddie, Fredrik Lundh... C/C++ and Python Developer not .NET "coders" (I hate even more my boring .NET job).

Following the PyCon week, here a simple example of PyQt4 that allows you to see up to 500 photos tagged PyCon3 on Flickr.

As always the source code is available here: PyQt4 PyCon3 Flickr Source Code.

Monday, May 11, 2009

[TIP] Non Blocking C Read

This is Just a simple code that maybe all the Linux C Developer knows, but if someone is still learning, This is a function that wrap the standard read function adding the non blocking feature. It's Really useful in relation with Sockets.


int nonblock_read (int fd, char *buffer, size_t bufsize, int timeout) {
if (timeout > 0) {
struct timeval tv;
fd_set rfds;

FD_ZERO(&rfds);
FD_SET(fd, &rfds);

tv.tv_sec = 0;
tv.tv_usec = timeout * 1000;

if (select(1, &rfds, NULL, NULL, &tv) <= 0)
return(-ETIME);
}

return(read(fd, buffer, bufsize));
}

Thursday, May 7, 2009

PyCon3 Italia, 8-9-10 May 2009


I will spend the next few days in Florence for PyCon. Probably on Monday, I'll publish some photos and why not, code examples from PyCon.

See you there!

Wednesday, May 6, 2009

Qt4 Touch and Rotate

Alan has asked my How I've implemented the Touch and Rotate feature of the Moko. The answer is very simple, It's all Magic! There're very few lines of code to do it, because all the magic is made by atan2.

Atan2 is a variation of the arctangent function. For any real arguments x and y not both equal to zero, atan2(y, x) is the angle in radians between the positive x-axis of a plane and the point given by the coordinates (x, y) on it.

The Source Code is available here: Qt4 Touch and Rotate Source Code.
OT: PyCon3 (Italia) starts Tomorrow!

Saturday, May 2, 2009

Qt4 JSON Stream Reader

JSON, short for JavaScript Object Notation, is a lightweight computer data interchange format. It is a text-based, human-readable format for representing simple data structures and associative arrays (called objects).

One way to use JSON with Qt4 is using the QScriptEngine (engine.Evaluate(jsonSource)), But I like Reinventing the wheel, so I've written a simple JSON Stream Reader that works "almost" like the QXmlStreamReader.

You've a readNext() method that read the next token and returns its type, and two properties name() and values() that Returns the name of the Property, Object or Array and the Value of the Property in a QVariant that can be Bool, Int, Double or String.

Below there's an Example of How to use the JSON Stream Reader. Maybe you can use it to create an automatic Object Mapper like the Xml Object Mapper that I've posted a couple of weeks ago.


QFile file("test.json");
if (!file.open(QIODevice::ReadOnly)) {
qDebug() << file.errorString();
return(1);
}

THJsonStreamReader reader(&file);
while (!reader.atEnd()) {
switch (reader.readNext()) {
case THJsonStreamReader::PropertyNumerical:
qDebug() << " - Property Numerical" << reader.name() << reader.value();
break;
case THJsonStreamReader::PropertyString:
qDebug() << " - Property String" << reader.name() << reader.value();
break;
case THJsonStreamReader::PropertyFalse:
qDebug() << " - Property False" << reader.name() << reader.value();
break;
case THJsonStreamReader::PropertyTrue:
qDebug() << " - Property True" << reader.name() << reader.value();
break;
case THJsonStreamReader::PropertyNull:
qDebug() << " - Property Null" << reader.name();
break;
case THJsonStreamReader::Object:
qDebug() << "Object" << reader.name();
break;
case THJsonStreamReader::ObjectEnd:
qDebug() << "Object End";
break;
case THJsonStreamReader::Array:
qDebug() << "Array" << reader.name();
break;
case THJsonStreamReader::ArrayEnd:
qDebug() << "Array End";
break;
}
}

file.close();



The Source Code is Available Here: Qt4 JSON Stream Reader Source Code.

Friday, May 1, 2009

iPhone: Touch, Drag, Hit!

Yesterday, Fredrik has asked me something like "Drag & Drop with Hit Test" on the iPhone. Here's a simple example.  There's a simple UIView class that handles the touch events and a protocol that says that if the "DragMe View" has hit or not the "HitMe View". Below you can see the result and the Source Code Link.

The source code is available here: Touch, Drag, Hit Source Code.