Angel \”Java\” Lopez on Blog

September 8, 2011

AjFabriq on NodeJs (Part 2) A local Simple Application

Filed under: AjFabriq, Distributed Computing, JavaScript, NodeJs, Open Source Projects — ajlopez @ 11:40 am

Previous Post
Next Post

Lets explore how to use AjFabriq on NodeJs. Here is a simple application:

https://github.com/ajlopez/AjFabriqJs/tree/master/examples/numbers

It implements the ultimate killer application: it receives a message with a number, and post a message with the number less one ;-). Let’s see how the application is defined:

/**
 * Module dependencies.
 */
 
var ajf = require('ajfabriq');

I included c:\Git in my NODE_PATH environment variable, and there is a c:\Git\ajfabriq containing my git local repo under development. You can clone the repo in your node_modules folder if you are using NodeJs 5.x (see Playing With NodeJs (1) Running on Windows (and Azure)) .

AjFabriq defines an object that exposes some methods. This is the way to create a message processor that is local, and it is not exposed to other servers (a distributed example in the repo, to review in an upcoming post):

/**
 * Host.
 */
var host = ajf.createProcessor();

Now, a message processor (see previous post) can be a composite. The aprocessor.createProcessor creates a new processor and adds it to the parent processor:

/**
 * Application configuration.
 */
 
var app = host.createProcessor('numbers', 'application');
var node = app.createProcessor('processor', 'node');

The first processor, app, will accept and process messages with property “application” having "numbers” as value. Its child processor node will process message with additional property “node” with value “processor”. In this way, we can define a tree of message processors. The message properties and their values are the routing information, so each message will be send to the appropriate message processor.

But, how to define the leaf processor behavior? To be aligned to NodeJs async processing, each processor inherits from an EventEmitter. Since the previous post, I defined the Processor as a “subclass” of process.EventEmitter in AjFabriq code:

var EventEmitter = process.EventEmitter;
// ...
function Processor(name, kind)
{
	this.name = name;
	this.kind = kind;
	this.processors = [];
}
Processor.prototype.__proto__ = EventEmitter.prototype;

Now, we can define the leaf processor behavior:

node.on('decrement', function (message) {
	console.log("Processing number " + message.number);
	
	if (message.number <= 1) {
		console.log("End Processing");
		return;
		}
		
	var number = message.number-1;
	
	this.post({ action: 'decrement', number: number });
});

The message processing detects an action property, and raise the corresponding event. Note the emit method invocation in AjFabriq code:

Processor.prototype.process = function (message)
{
	if (this.processors == null || this.processors.length == 0) {
		this.emit(message.action, message);
		return;
	}
	
	for (var processor in this.processors)
		if (this.processors[processor].accepts(message)) 
		{
			this.processors[processor].process(message);
		}
}

 

At the end of my “killer” app sample, a message is sent to the top processor:

host.process({ application: 'numbers', node: 'processor', action: 'decrement', number: 10 });

The output:

But wait! Do you check the ‘decrement’ code I commented above? There is a:

this.post({ action: 'decrement', number: number });

No application, node properties!! Yes, it's right. If you post a message from a processor (node in this case), the parent processors fill the missing properties. That is, application property is set to “numbers”, and node property is set to “processor”. If you want to sent a message to other application, you can explicitly set the corresponding property in your new message. Note that the recommended practice is: don't change the original message. Maybe it could be processed by other processors.

For upcoming posts: new apps, a distributed example, more fun! ;-)

Keep tuned!

Angel “Java” Lopez

http://www.ajlopez.com

http://twitter.com/ajlopez

2 Comments »

  1. [...] Next Post [...]

    Pingback by AjFabriq on NodeJs (Part 1) Introduction « Angel “Java” Lopez on Blog — September 8, 2011 @ 11:48 am


RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

The Shocking Blue Green Theme. Blog at WordPress.com.

Follow

Get every new post delivered to your Inbox.

Join 66 other followers

%d bloggers like this: