Questions about scheduler and it's use on room level

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

Moderators: Lapo, Bax

User avatar
yuvallahav
Posts: 247
Joined: 07 Oct 2008, 13:03
Location: Rome, Italy
Contact:

Questions about scheduler and it's use on room level

Postby yuvallahav » 16 Mar 2012, 10:29

Hello!

So I had a room level extension, and I've switched all of my setInterval in my extension to the scheduler, and now I have a few question, since I'm not sure I've done it in the most officiant way.

In my old extension, I have used the setInterval to time turns, delay phases of the game, and such, and I did make sure not to have more the one setInterval at a time inside the room, but since each setInterval takes a thread, it means that for 500 rooms, I would have 500 threads working only on this issue.

So what I did inside my extension is this:

Code: Select all

var _scheduler = Packages.it.gotoandplay.smartfoxserver.util.scheduling
var scheduler = null
var taskHandler;
var task;

function init(){
   scheduler = new _scheduler.Scheduler();
   scheduler.startService();
   
   var handlerObj = {};
   handlerObj.doTask = function(task){
      handle_tasks(task);
   }

   taskHandler = new _scheduler.ITaskHandler(handlerObj);
}

function handle_tasks(task){
   task.active = false;
   
   var task_name = task.id.name;
   var task_params = task.id.params;
   
   if(task_name == "xxx"){
      do_something(task_params);
   }
}

function destroy(){
   kill_task();
   scheduler.destroy(null);
}

function set_task(name,params,time){
   if(task){
      task.active = false;
   }
   
   task = new _scheduler.Task({name:name, params:params});
   scheduler.addScheduledTask(task, time, false, taskHandler);
}

function kill_task(){
   if(task){
      task.active = false;
   }
}


And to explain, since I need only one task running at a time (no general game timer, plus turn time and such), but one task at a time that will activate one function on it's execution, I didn't use a different task for each action, but a single task I can turn off (with kill_task), and when I'm in need of a new task, if there is already a task defined, I kill that and reset the task.

I do this since the bigger part of my tasking need is to wait for a client response, kill the waiting task if the client responded in the allotted time, or execute a function if he didn't, so when a client response, I can kill the task, switch the turn, and start a new waiting task for the next player, and so on.

Now this all works perfectly, no problem, but my fear is that since I'm instancing ("scheduler = new _scheduler.Scheduler();") a new scheduler in the room level, does it not mean that I will have 500 instances taking 500 threads, if I have 500 rooms with this code in them? or does the scheduler object work that is does not instance a new copy of itself when I instance it like that in the room level, but just create a reference to itself in that object, and in the end it is still just one general scheduler, taking just the one thread??

And if it's like I fear it is, what should I do? should I instance my scheduler just once on the zone level, then direct all calls to it from inside the room extension, and then when it fires, I will redirect the event to the specific room that initiated the task?


Thank.

Yuval Lahav.
User avatar
yuvallahav
Posts: 247
Joined: 07 Oct 2008, 13:03
Location: Rome, Italy
Contact:

Re: Questions about scheduler and it's use on room level

Postby yuvallahav » 20 Mar 2012, 11:54

Please, Lapo, anyone? I need to know if what I've done in my room level extension will not load up the server with scheduler instances before we go to production!!
User avatar
Lapo
Site Admin
Posts: 23027
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Questions about scheduler and it's use on room level

Postby Lapo » 22 Mar 2012, 09:09

Sorry for the delay.
Yes if you create a new Scheduler for each Room you will end up with as many threads as the Rooms are, so no good.
The strategy is to create one scheduler and share it across all Rooms.

In particular I would highly recommend to use the Java ScheduledThreadPoolExecutor which can be backed by a thread pool, so it can scale very easily as your application grows. ( http://docs.oracle.com/javase/1.5.0/doc ... cutor.html )
My suggestion is to create a singleton/static member in a Java class so that it can be accessed from all your Room Extensions.
You find the Java<-->Actctionscript interoperability guide in the SFSPRO docs.

hope it helps
Lapo
--
gotoAndPlay()
...addicted to flash games
User avatar
yuvallahav
Posts: 247
Joined: 07 Oct 2008, 13:03
Location: Rome, Italy
Contact:

Re: Questions about scheduler and it's use on room level

Postby yuvallahav » 22 Mar 2012, 09:15

I hope it helps too as my java experience is null, but I'll read up and test some stuff and post back when I want to make sure what I did was the right idea, thanks!

Yuval.
User avatar
yuvallahav
Posts: 247
Joined: 07 Oct 2008, 13:03
Location: Rome, Italy
Contact:

Re: Questions about scheduler and it's use on room level

Postby yuvallahav » 22 Mar 2012, 11:11

Well... I was able, of course, to set my scheduler interface on my zone level, and now all rooms in that zone can send their tasks to the zone scheduler, and it will fire up the action all right, but... I was simple unable to communicate back to the room any information from the zone level!! I've tried these, and not one of them worked:

Code: Select all

room_obj = zone.getRoom(room_id);
room_obj.getExtension("room_extension_name").handle_tasks(task);


Code: Select all

room_obj = zone.getRoom(room_id);
room_obj.getExtension("room_extension_name").handleInternalRequest("hello");


Code: Select all

room_obj = zone.getRoom(room_id);
response = room_obj.getExtension("room_extension_name").handleInternalRequest("hello");


Code: Select all

response = zone.getExtension("room_extension_name").handleInternalRequest("hello");


and a few others on that line, even directly to the room object:

Code: Select all

room_obj = zone.getRoom(room_id);
room_obj.handle_tasks(task);


Non of these work, of course there is a way to do this, only I'm not finding anything in the documents, and nothing I can think up my self seems to be working, can you give me a tip on whats going wrong and why my rooms won't listen to my zone??

Yuval.
User avatar
yuvallahav
Posts: 247
Joined: 07 Oct 2008, 13:03
Location: Rome, Italy
Contact:

Re: Questions about scheduler and it's use on room level

Postby yuvallahav » 22 Mar 2012, 11:16

Also... the forum search does not work... it gives me the same message every time:
"The following words in your search query were ignored because they are too common words..."
And then it lists all the words I used for my search...
User avatar
rjgtav
Posts: 2813
Joined: 19 Apr 2009, 11:31
Location: Lisbon, Portugal

Re: Questions about scheduler and it's use on room level

Postby rjgtav » 26 Mar 2012, 21:27

Hello.
Well, are you sure that the extension is called "room_extension_name"? Also, please try calling the handleInternalRequest and specify an object as the parameter (something like handleInternalRequest({msg:"hello"})). And is the handleInternalRequest method of that room extension being called at least?
Skills: SFS Pro, SFS2X, AS2.0/AS3.0, Java, HTML5/CSS3/JS, C#
Portfolio: https://rjgtav.wordpress.com/
SFS Tutorials: http://sfs-tutor.blogspot.com/ - Discontinued. Some examples may be bugged.
User avatar
yuvallahav
Posts: 247
Joined: 07 Oct 2008, 13:03
Location: Rome, Italy
Contact:

Re: Questions about scheduler and it's use on room level

Postby yuvallahav » 26 Mar 2012, 22:12

Hello again rjgtav.

So:
Q: are you sure that the extension is called "room_extension_name"?
A: of course it's not called that, but lets assume I'm calling the right name that I chose for that extension when I created the room dynamically on the server side (I never use real names when writing on the forum, what if the commies are reading this!!?).

Q: lease try calling the handleInternalRequest and specify an object as the parameter.
A: I have been calling the "handleInternalRequest" function as you can see in one of the examples, and I have tried sending an object, a string, and a number, just in case.

Q: is the handleInternalRequest method of that room extension being called at least.
A: no, it's not being called, which is what I'm trying to crack down here.

You know, the thing I find most annoying is how badly these features are documents, and also, that the forum search does not work, who knows if these questions have been answered before??!!

Yuval.
(just in case, I'll re run every test I have a second time tomorrow when I get to the office, just to be on the safe side, we wouldn't want us to shame myself when and if we find it was all just because a stupid mistake....).




Well, are you sure that the extension is called "room_extension_name"? Also, please try calling the handleInternalRequest and specify an object as the parameter (something like handleInternalRequest({msg:"hello"})). And is the handleInternalRequest method of that room extension being called at least?
User avatar
Lapo
Site Admin
Posts: 23027
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Questions about scheduler and it's use on room level

Postby Lapo » 27 Mar 2012, 08:18

@yuvallahav:
You seem to be complicating your life in ways I don't understand.
Please remember that any task added to a Scheduler can store any number of parameters. So, why don't you simply put a reference to your RoomExtension inside your Task object?
This way when the task is executed you don't have to run 3 lines of code for targeting the right RoomExtensions, you already have your reference there ready to be used.

Hope it helps
Lapo

--

gotoAndPlay()

...addicted to flash games
User avatar
yuvallahav
Posts: 247
Joined: 07 Oct 2008, 13:03
Location: Rome, Italy
Contact:

Re: Questions about scheduler and it's use on room level

Postby yuvallahav » 27 Mar 2012, 08:30

but I would still need to get the room object from the zone in order to call the function in the room, no?

or could I just send a reference to the extension itself (with a "this" added as a parameter) and just shoot off that one refrence??

I'll try this out, see what comes out, because I'm pretty sure I'm doing something wrong here, since even with the long way around, I still can't seem to be able to activate or run any function in my room level from the zone level.
User avatar
rjgtav
Posts: 2813
Joined: 19 Apr 2009, 11:31
Location: Lisbon, Portugal

Re: Questions about scheduler and it's use on room level

Postby rjgtav » 27 Mar 2012, 09:07

Hello. Yes you just need a reference to the extension and then call its handleInternalRequest() method.
Skills: SFS Pro, SFS2X, AS2.0/AS3.0, Java, HTML5/CSS3/JS, C#
Portfolio: https://rjgtav.wordpress.com/
SFS Tutorials: http://sfs-tutor.blogspot.com/ - Discontinued. Some examples may be bugged.
User avatar
yuvallahav
Posts: 247
Joined: 07 Oct 2008, 13:03
Location: Rome, Italy
Contact:

Re: Questions about scheduler and it's use on room level

Postby yuvallahav » 27 Mar 2012, 09:20

Nothing I do seems to work, except for the scheduler, which works fine, but I can't send any information to my room for some reason, so to clear things up, lets see what I've done and try to guess whats wrong with it, since I can't find it, I look at the code, and it supposed to work. (note I've discarded any code which does not directly connected to the scheduler).

ZONE LEVEL (zone.as, extension called "zone_ext", room extension is called "room_ext"):

Code: Select all

var zonel
var _scheduler = Packages.it.gotoandplay.smartfoxserver.util.scheduling
var scheduler = null;
var taskHandler;

function init(){
   scheduler = new _scheduler.Scheduler();
   scheduler.startService();
   
   var handlerObj = {};
   handlerObj.doTask = function(task){
      handle_tasks(task);
   }

   taskHandler = new _scheduler.ITaskHandler(handlerObj);

   zone = _server.getCurrentZone();
}

function destroy(){
   scheduler.destroy(null);
}

function handle_tasks(task){
   var from_room = task.id.params.room_id;
   task.active = false;
   if(from_room != undefined && from_room != "undefined"){
      //getting, hopfully, the room extension to parse my data
      event = {action:"parse_task",task:task};
      zone.getRoom(from_room).getExtension("room_ext").handleInternalRequest(event);
   }
}

function handleInternalRequest(evt){   
   response = new Object();
   var write_db = evt.write_db;
   
   if(evt.action == "set_task"){
      var name = evt.name;
      var params = evt.params;
      var time = evt.time;
      set_task(name,params,time);
   }
}

function set_task(name,params,time){
   var tmp_task = new _scheduler.Task({name:name, params:params});
   scheduler.addScheduledTask(tmp_task, time, false, taskHandler);
}


ROOM LEVEL (room.as, extension called "room_ext", zone extension is called "zone_ext"):

Code: Select all

var zone;
var room;
var room_id;

function init(){
   zone = _server.getCurrentZone();
   room = _server.getCurrentRoom();
   room_id = room.getId();
}

function handleInternalRequest(evt){   
   trace(evt.action);
}

function set_task(){
   params = {room_id:room_id};
   var myParams = {action:"set_task",name:"play_ball", params:params, time:10};
   task = zone.getExtension("zone_ext").handleInternalRequest(myParams);
}


So when I fire off "set_task()" in my room, it sets up the task in the zone level, no problem there, after 10 seconds, the scheduler fires off the handler with all the correct information, now, trying to send the information to the room it came from simply does nothing. it does not fire off, it's like the room is invisible to the zone although I can trace the room object using the id, and it's there, but the "handleInternalRequest" function is not fired off!!
What is wrong with this logic here??
User avatar
yuvallahav
Posts: 247
Joined: 07 Oct 2008, 13:03
Location: Rome, Italy
Contact:

Re: Questions about scheduler and it's use on room level

Postby yuvallahav » 27 Mar 2012, 10:01

Isn't there some point in my script where someone can point and say, "here, this is not right, and that's why it's not working"??
User avatar
rjgtav
Posts: 2813
Joined: 19 Apr 2009, 11:31
Location: Lisbon, Portugal

Re: Questions about scheduler and it's use on room level

Postby rjgtav » 27 Mar 2012, 10:11

I think I've found where your problem is.
When you want to retrieve an extension from a zone, you simply do:

var extension = zone.getExtension("extensionName");

But when you want to retrieve an extension from a room, you have to get its reference through the ExtensionManager() instance:

var extension = room.getExtManager().getExtension("extensionName");
Skills: SFS Pro, SFS2X, AS2.0/AS3.0, Java, HTML5/CSS3/JS, C#
Portfolio: https://rjgtav.wordpress.com/
SFS Tutorials: http://sfs-tutor.blogspot.com/ - Discontinued. Some examples may be bugged.
User avatar
yuvallahav
Posts: 247
Joined: 07 Oct 2008, 13:03
Location: Rome, Italy
Contact:

Re: Questions about scheduler and it's use on room level

Postby yuvallahav » 27 Mar 2012, 10:19

Still doesn't work :(

I've changed:

zone.getRoom(from_room).getExtension("room_ext").handleInternalRequest(event);
to:

Code: Select all

var room = zone.getRoom(from_room);
room.getExtManager().getExtension("room_ext").handleInternalRequest(event);


but nothing still goes on happening...

Return to “Server Side Extension Development”

Who is online

Users browsing this forum: No registered users and 37 guests