Download a Scheduler for AS Extension

Post here your questions about Actionscript and Java server side extensions development.

Moderators: Lapo, Bax

Post Reply
Pippoflash
Posts: 135
Joined: 30 Jan 2006, 17:16
Contact:

Download a Scheduler for AS Extension

Post by Pippoflash »

Hi,
Here is a useful stand-alone zone extension to manage intervals and timed tasks. Keeping a centralized single Scheduler will create and use only 1 thread in the java vm, preventing performance problems.

Just put it in the sfsExtensions folder (together with the "uty" folder, which contains System.as, used by ScheduleMan).

Download ScheduleMan

Usage:

Create the zone level class by adding to your ZONE in config.xml:

Code: Select all

<extension name="ScheduleMan"  className="ScheduleMan.as" type="script" />
Then, you can register any task you want very simply. There are 2 ways of calling tasks, one is made for safe room destruction, in order not to have tasks running again after room is bound to be destroyed, and the other one is simple, without any check.

The task will call timed a function in any extension. The onlyargument in the function will be an instance of "MyTask", from which all sent properties can be accessible, and also a property "task" with the real instance of the java Task.

The MyTask instance, also has a method stop(), that will stop the task from running, and remove all references cleaning up for garbage collection.

You have to make an extension call.

For simple tasks do the following:

Code: Select all

_server.getCurrentZone().getExtension("ScheduleMan").handleInternalRequest({cmd:"createTask", par:params});
the "params" object holds the following properties:

Code: Select all

var params = {
   id:"My Task", // String with any ID of your choice"
   interval:3, // Number of seconds to wait to perform the task
   recurrent:true, // If task has to be recurrent. If false, it happens only once
   executions:10, // number of times the task has to be executed if recurrent
   func:myFunction, // Direct reference to a method that the task will call on any execution 
   par:{} // This can be anything. Any extra parameter to be stored
}

For room tasks do the following:
Room tasks are designed to happen in rooms that can be dynamically created and destroyed. If room is bound to be destroyed, task will kill itself automatically.

Code: Select all

_server.getCurrentZone().getExtension("ScheduleMan").handleInternalRequest({cmd:"createGameRoomTask", par:params});
It works like for simple tasks, but to the par you have to add these 2 parameters:

Code: Select all

room:Room // Direct reference to the Room instance
roomExt:this // (optional) direct reference to the room extension
Now, this differs from the simple task in one thing, before execution, the task checks to see wether the room is still active, and if its not, it will cleanup the task for garbage collection, and try again to destroy the room.

In order to inhibit task action and cleanup, task can notice if room is inactive using the property "roomState". If roomState == "DESTROYED", task knows room doesn't need it anymore.

you can add you custom code to detect if room is bound to be destroyed in line 129 of ScheduleMas.as, in the function gameRoomIsInactive(room)

I wish I had found this myself long time ago :) I hope it helps...
namaste
Filippo

PS
You can also take advantage of the System.as class, very very very useful in any extension. It provides a lot of useful methods and speeds up SFS server side development a lot...

I.E.
_system.sendMessage(recipient, cmd, par);
recipient can be:
"ROOM" - Will send to all users presents in the actual room
"ZONE" - Will send to all usersin the zone
User - A single user object
[User, User, User] - An array of users
channel - A single user channel (useful for custom login)...

Etc... explore it yourself :)
I will make another post about it...
-----------------------
www.pippoflash.com
-----------------------
Post Reply