Event based actor model framework for game
The implementation is inspired by drama
It is an implementation of event-based actor model for node.js. It is designed for game backend service and may work with socket.io for sequential process of game events.
Of course it can be used for non-game service.
var nactor = require"nactor";var actor = nactoractor// Declare the context of your actor by an object// Actor method - "hello"console.logmessage;return "Done";;// Intialize the actoractorinit;// Ask to execute the hello() method. It will be called in next tickactorask"hello""Node.js!";
The nactor.actor() constructs an actor model according to the declaration passed through argument. The return is a proxy of the actor which provides interface same as the declaration but the method will not be executed immediately. Instead, it is scheduled to run by the main event loop. The call is async.
The ask() is the standard method to invoke actor's method from proxy. Alternative method is "automated interface binding".
Instead of calling the ask() , you may execute the declared method by its name directly.
Remarks: You must call "init()" before execute any actor method. The interface will not be binded without "init()"
actorhello"Node.js!"console.logreply; // "Done";
In the previous example shows that the return from actor method will be passed to sender's callback. It is simple but not suitable for calls that depend on I/O resource. In this case , it should enable the async reply mechanism.
var nactor = require"nactor";var actor = nactoractor// Alternative method of actor declaration// It is the constructor and will be executed by// init() immediately// Remarks: It is not suggested to put async method here.thisseq = 0; // Variables that can be shared for all methods.thistimeout = optionstimeout;return// Declare the methodasyncenable; // Enable async interfacesetTimeoutasyncreply"Done!";thistimeout; // Using "this" to access the variable declared;;// Intialize the actoractorinittimeout : 200;actorpingconsole.logmessage; // Done!;
Beside ask() and reply(), actor may send information to any observer through event emission.
var nactor = require"nactor";var actor = nactoractorvar self = this;thishandle = setIntervalselfemit"pong""Pong!";300;returnclearIntervalthishandle;;actorinit;actoron"pong"console.logmsg;;
The emit() method is added to the context automatically. It will not invoke observer's callback immediately just like the ask() method. It is scheduled on tick.
NActor implements a message queue and process one message at a time. It can be used to avoid concurrent access to a single resource. May simplify the complexity of your code and prevent race condition.
As actor is not only an answer machine , it may have its own logic like time out checking. (e.g A player do not response within a time period, he/she will be considered as pass). Once the time out reached, the action taken may be working together with other message from sender.
If you are not happy with this situation , you may post your action to the message queue and let's NActor to handle the concurrecnt issues.
var nactor = require"nactor";var actor = nactoractorvar self = this;thispressed = false;returnsetTimeoutif !selfpressedselfpost"giveup"name// In case you want to process the reply.// This callback is invoked like an actor method.// If async.enable() is called, it will hold the message queue until async.reply();1000;thispressed = true;console.log"Player[" + name + "] give up";;actorinit;actorstart"Player A";
Remarks: An alternative method to post() is next() , the arguments same as post() but the message will be injected to the beginning of the message queue.
As the actor method is not called directly, you can not catch the exception from actor in sender. Instead, you may call onUncaughtException() to add a listener for uncaught exception.
If an exception is uncaught , NActor will skip the processing message and handle the next. If you don't like the behaviour. You may stop the message execuation by calling ''action.stop()''
Remarks : The actor will no longer be usable after called ''action.stop()''