Tuesday, August 4, 2009

A Bit of Distributed Computation...

In the last months I've worked on various 2D rendering projects, that requires lots of row power to be executed in smallest time as possible.

The rendering result is an aggregation of components (or better, Group of Components) that can be rendered independently of each other in a  process because each components has its own input data and until the aggregation process starts there're no dependencies between components. In a few words, foreach input data I've to call a Render method that returns the "computed" data, that at the end will be aggregated with all the computed values.

So, how speed-up the rendering? The standard answer is using Threads, to take advantage of multi-core system. But I need lots row power and lots of core to be fast enought. Another solution that doesn't require to spent a lot of money for a faster computer it to distribute the computation across different machines. And this is my attempt...

The Master receive an array of elements as input and splits up it according to the number of the available Nodes (Machines), and foreach node assigns its sub-array and sends an "informative message" to the node.

The Node waits for an "informative message" and when it has received it, starts its computation. Foreach Data Item that fetch runs the Computation and sends back the result.

The Master decompose data into equal-size partitions, so each node has  an equal-size queue to process, but if someone finishes its job and there're more data to process (in someone else queue) The Master dequeue couple of items from the slower node queue and add to the queue of the one that has finished its job. In this way you've done your computation faster then ever, and if one machine crashes (slow one case) its job will be taken by someone else that has finished its own.

And this is just a bit of General Theory, but the implementation is really simple. Maybe I'll try to reimplement something more generic in the near future (October, November) when I'll have a bit more of free time. If you've any question send me a mail!

Friday, July 31, 2009

Qt4 Imap API

Pushed in my GitHub repo the first draft of the Qt4 Imap (RFC 3501 Interne Message Access Protocol) Library.
I've implemented a sync communication because it's much easier to use, so if you want to use it in async way you need to implement a thread class that wrap the Imap class and throws the events that you need.


Imap imap;
if (!imap.connectToHost(IMAP_HOST, IMAP_PORT, IMAP_USE_SSL))
IMAP_MAIN_ABORT("connectToHost()", imap.errorString());

if (!imap.login(IMAP_USERNAME, IMAP_PASSWORD, IMAP_LOGIN_TYPE))
IMAP_MAIN_ABORT("login()", imap.errorString());

ImapMailbox *mailbox = imap.select("INBOX");
qDebug() << "INBOX";
qDebug() << " - Exists:" << mailbox->exists();
qDebug() << " - Unseen:" << mailbox->unseen();

QList<int> messageList = imap.searchRecentUnseen();
imap.fetch(mailbox, messageList);
foreach (int msgId, messageList) {
ImapMessage *message = mailbox->findById(msgId);
if (message == NULL) continue;

imap.fetchBodyStructure(message);

qDebug() << "FROM" << message->fromAddress().toString();
foreach (ImapAddress address, message->toAddresses())
qDebug() << " - TO" << address.toString();
qDebug() << "SUBJECT" << message->subject();

for (int i = 0; i < message->bodyPartCount(); ++i) {
ImapMessageBodyPart *bodyPart = message->bodyPartAt(i);
imap.fetchBodyPart(message, i);

qDebug() << bodyPart->isAttachment() << bodyPart->contentType();
qDebug() << bodyPart->data();
}
}

delete mailbox;

imap.logout();
imap.disconnectFromHost();

Thursday, July 23, 2009

Qt4 Google Contacts APIs

Just pushed at GitHub the first draft of the Qt4 Google Contacts Service...

Contacts APIs allows client applications to view and update Contacts content in the form of Google Data API feeds. Your client application can request a list of a user's contacts, edit or delete content in an existing contact.


// Setup Contact
THGoogleContact *contact = new THGoogleContact;
contact->setFullName("John Doe");

THGoogleIm *im = new THGoogleIm;
im->setRel(THGoogleIm::relWork());
im->setAddress("john.doe@aim.com");
im->setProtocol(THGoogleIm::protocolAim());
contact->addImAddress(im);

THGoogleEmail *email = new THGoogleEmail;
email->setAddress("john.doe@mail.com");
email->setRel(THGoogleEmail::relWork());
contact->addEmailAddress(email);

// Insert Contact
THGoogleContactsService *gContacts = new THGoogleContactsService;
gContacts->setAuthToken(...);
gContacts->insertContact(contact);


Take a look at contacttest.cpp for a more complete usage example.

Tuesday, July 14, 2009

OpenGL: 3D Cube, Rotation, Translation and Texts

Second hour with OpenGL, last time I've written a simple 2D example to learn what Vertex are, and now a little step forward using 3D, Rotation, Translation and Texts.

Using GLUT writing 2D texts is really simple, and here is how to do it. Setup Text Color, X and Y Location, pick up a font and set your string, and it's done.


glColor3f(1.0, 0.0, 0.0);
glRasterPos2f(-4.6, 2.3);
glutBitmapString(GLUT_BITMAP_TIMES_ROMAN_24, 'Hello Text');


To handle left, right, up, down keys to move the cube use the glutSpecialFunc() that allows you to handle GLUT_KEY_LEFT, GLUT_KEY_RIGHT, GLUT_KEY_UP, GLUT_KEY_DOWN keys and others. Translation and rotation are made using glTranslatef() and glRotatef().

The source code is available here: Python OpenGL 3D Cube Source Code.

Sunday, July 12, 2009

OpenGL: First Test - 2D Example

I'm not a Computer Graphics fan, but I've spent my the last year working on Report Engine, and User Interfaces... drawLine, drawRect.. setLocation... (I don't know.. I'm a malloc/memset boy).

But I need to learn more about Computer Graphics for the future, and today I've started playing with OpenGL... No more 2D points!!! :D
Below you can see a couple of lines of code that draws the example above...



static void drawTriangle (void) {
glColor3f(0.0f, 0.0f, 1.0f);
glBegin(GL_TRIANGLES);

glVertex3f( 0.0f,  0.6f, 0.0f);
glVertex3f(-0.2f, -0.3f, 0.0f);
glVertex3f( 0.2f, -0.3f, 0.0f);

glEnd();
}

static void drawSquare (void) {
glColor3f(1.0f, 0.0f, 0.0f);
glBegin(GL_TRIANGLE_FAN);

glVertex3f(0.0f, 0.0f, 0.0f);
glVertex3f(0.5f, 0.0f, 0.0f);
glVertex3f(0.5f, 0.5f, 0.0f);
glVertex3f(0.0f, 0.5f, 0.0f);

glEnd();
}

static void drawSquareMode2 (void) {
const GLfloat squareVertices[] = {
-0.2,  0.2, 0.2, 
-0.2, -0.2, 0.2,
0.2, -0.2, 0.2, 
0.2,  0.2, 0.2 
};

glColor3f(0.0f, 1.0f, 0.0f);
glVertexPointer(3, GL_FLOAT, 0, squareVertices);
glEnableClientState(GL_VERTEX_ARRAY);
glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
}


I've used NSOpenGLView to display the GL, so the drawing code is inside a drawRect method.
- (void)drawRect:(NSRect)bounds {
glClearColor(0, 0, 0, 0);
glClear(GL_COLOR_BUFFER_BIT);

glTranslatef(-0.2f, 0.0f, 0.0f);
drawTriangle();

glTranslatef(0.2f, 0.0f, 0.0f);
drawSquare();
drawSquareMode2();

glFlush();
}

Sunday, June 28, 2009

Reliable Protocol on Unreliable Channel

When you work with Unreliable communication channel (like UDP), you need to implement your own "Reliable Mechanism" to be sure that packet that you've send was received correctly. Here there's my attempt.

Reliable Transmitter:

The schema below shows the operations that reliable transmitter has to do. "Red" are "error or unexpected operations", Green are "expected operations", Blue are TX Operations.

Transmitter send a packet and waits for the ACK packet (sended by receiver). If ACK doesn't arrive before the Tx Timeout packet is resended. When ACK arrive Tx send the CACK (Confirm ACK) packet and waits for a timeout, if another ACK Arrive TX resend the CACK, this because receiver doesn't have the confirm of transmitter. When TX timeout all is done, and the packet is sended correctly.


Reliable Receiver:

The schema below shows the operations that reliable receiver has to do. "Red" are "error or unexpected operations", Green are "expected operations", Blue are Rx Operations.

When RX receive a Packet sends an ACK, and waits for the CACK, if CACK doesn't arrive before timeout RX resend the ACK. When CACK arrive all is done. We've received the packet and the transmitter knows that we've received it.

Qt4 Canvas Plugins

Today, during my Sunday morning bike trip I've had an Idea on how to simply implement a Canvas Plugins in Qt Applications, to give you an idea of How browser external plugins like flash or others works. The Code is really simple there's a class CanvasPlugin with a couple of Abstract Methods Init, UnInit, Resize, MouseEvent and a signal that says "Hey, Paint this Image!". To give you an Idea of the result below you can see a Window with two Widgets, a CanvasPluginViewer and a Label. Canvas Plugin Viewer load an external Library (.so) passed to the command line as first argument.


You can interact with the canvas like a normal widget (the red square moves along the window and you can stop and resume its animation). But you can extend the sample sending and receiving information from a server, or you can do other crazy external things. (See flash usage for more ideas).

That's all folks, it's lunch time. If you've any question send me a mail or write a comment, and remember that this is just a couple of minutes coded example, so don't use in your highly stable app. :)

The Source Code is Available Here: Qt4 Canvas Plugins Source Code.