Source

Description

This implementation is inspired in Akka actor implementation.

This model encourage the writing of application consisting in actors that collaborates using message passing.
In this module, method invocation is like a message passing operation, that not returns value. The application
that uses actor model doesn't need continuation callbacks: each agent receives messages as method invocation,
and produces new messages calling other agent methods.

Installation

Via npm on Node:

npm install simpleactors

Reference in your program:

var simpleactors = require('simpleactors');

Usage

Create a system application:

var system = simpleactors.create('webcrawler');

Create an object of class Downloader, wrap it as an actor, and returns an actor reference:

var ref = system.actorOf(Downloader, 'downloader');

Alternatively, you can use an already create object as first parameter:

var downloader = new Downloader();
var ref = system.actorOf(downloader, 'downloader');

The second parameter is optional. A name is automatically assigned if the second parameter is missing.

Sends a message to an actor reference:

ref.tell(msg);

An object wrapped as an actor has:
- context: Actor context, with information about the actor environment.
- self: This actor reference.

It should implement the function:
- receive(msg): Process an incoming message

Example:

function Logger() {
	this.receive = function (msg) {
		console.log(msg);
	}
}

var system = simpleactors.create('mysys');
var ref = system.actorOf(Logger);
ref.tell('Hello, world');

How to create an actor in our actor object:

this.context.actorOf(MyActor, name);

How to get an actor reference in our actor object:

var ref = this.context.actorFor(path);

Examples:

var refchild = this.context.actorFor('mychild'); // child of current actor
var refactor = this.context.actorFor('/myroot/myactor'); // actor in current system

How to get an actor reference in a system:

var ref = system.actorFor(path);

Example:

var ref = system.actorFor('/myroot/myactor'); // actor in current system
ref.tell('Hello');

An actor system can run in many nodes (running process).

var node = simpleactors.createNode(port [, host]);

Example creating a node, a system, and two actors:

var node = simpleactors.createNode(3000);
var system = node.create('mysys');
var actor1 = system.actorOf(MyActor, 'actor1');
var actor2 = actor1.context.actorOf(MyActor, 'actor2');
// actor1.self.path === 'sactors://mysys@localhost:3000/actor1'
// actor2.self.path === 'sactors://mysys@localhost:3000/actor1/actor2'

A node can refer to another running node. In node A:

var node = simpleactors.createNode(3000);
var system = node.create('mysys');
var actor1 = system.actorOf(MyActor, 'actor1');
node.start();

In node B:

var node = simpleactors.createNode(3001);
var remotenode = node.createRemoteNode(3000);
var remotesystem = remotenode.create('mysys');
var remoteref = remotesystem.actorFor('/actor1');
remotenode.start(function () {
	// after connection, you can tell messages to remote actors
	remoteref.tell('hello'); 
});

Development

git clone git://github.com/ajlopez/SimpleActors.git
cd SimpleActors
npm install
npm test

Samples

Web Crawler sample shows
how you can create an application that don't depend on calling a method and receiving a response: each agent
does its works, receiving calls and sending calls to other agents.

Distributed Web Crawler sample has a
server that launch the web crawling process, and many clients can be launched that collaborates with the download and
harvest of new links to process. It uses a custom router.

References


Tags: distribution   actors   nodejs  

Last modified 06 April 2022