How to use a RoomExtension

Post here your questions about the Java client / Android API for SFS2X

Moderators: Lapo, Bax

antuan
Posts: 10
Joined: 06 Dec 2010, 11:49

How to use a RoomExtension

Postby antuan » 06 Jul 2012, 15:34

Hello,

I create a lot of rooms when sfs2x started and also create RoomExtension for each room.
Everything works fine. I can see on the admin panel everything is ok. But I have some problems.
When I send an event for a roomExtension, an error occurred. The error is :

Code: Select all

Exception: com.smartfoxserver.v2.exceptions.SFSRuntimeException
Message: Request handler not found: 'GAME EVENT'. Make sure the handler is registered in your extension using addRequestHandler()
Description: Error while handling client request in extension: { Ext: Games, Type: JAVA, Lev: ZONE, { Zone: Ball}, {} }
Extension Cmd: GAME EVENT
+--- --- ---+
Stack Trace:
+--- --- ---+
com.smartfoxserver.v2.extensions.SFSExtension.handleClientRequest(SFSExtension.java:181)
com.smartfoxserver.v2.controllers.ExtensionController.processRequest(ExtensionController.java:137)
com.smartfoxserver.bitswarm.controllers.AbstractController.run(AbstractController.java:96)
java.util.concurrent.ThreadPoolExecutor$Worker.runTask(Unknown Source)
java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
java.lang.Thread.run(Unknown Source)
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::


addRequestHandler is in RoomExtension init function.

Code: Select all

public class RoomExtension extends SFSExtension{
   
    @Override
    public void init() {       
       
        addRequestHandler(CustomEventType.GAME_EVENT, GameController.class);
   
    }
   
}


Normally RoomExtension extends SFSExtension or SFSBaseExtension.
That means I can add the addRequestHandler in a RoomExtension, but it doenst works. Why doesnt works it?

My second problem is about sfs roomExtensions performance. I create 70 rooms, when server started. Each room has an roomExtension. And each extension works with Thread and some runnable functions. I think that is a serious performance loss. I dont know, maybe I think wrong. Which method is best for that scenario?
User avatar
rjgtav
Posts: 2813
Joined: 19 Apr 2009, 11:31
Location: Lisbon, Portugal

Re: How to use a RoomExtension

Postby rjgtav » 07 Jul 2012, 13:26

Hi.

Please make sure that when you are sending an ExtensionRequest from the client, you specify to which room to send the request, otherwise it will be sent to the Zone-Level extension.
My second problem is about sfs roomExtensions performance. I create 70 rooms, when server started. Each room has an roomExtension. And each extension works with Thread and some runnable functions. I think that is a serious performance loss. I dont know, maybe I think wrong. Which method is best for that scenario?

Yes having a RoomExtension for each room will start using a significant amount of memory. And multiple threads will also slowly kill your processor...
In this case, the best case is to only have 1 Zone-Level extension which takes care of all the rooms and then a global Thread which loops between every room, instead of using a Task for each room.
In order to save room-specific data you can use the room.setProperty() and room.getProperty() methods or a hidden RoomVariable.
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.
antuan
Posts: 10
Joined: 06 Dec 2010, 11:49

Re: How to use a RoomExtension

Postby antuan » 07 Jul 2012, 18:14

rjgtav wrote:Hi.

Please make sure that when you are sending an ExtensionRequest from the client, you specify to which room to send the request, otherwise it will be sent to the Zone-Level extension.
My second problem is about sfs roomExtensions performance. I create 70 rooms, when server started. Each room has an roomExtension. And each extension works with Thread and some runnable functions. I think that is a serious performance loss. I dont know, maybe I think wrong. Which method is best for that scenario?

Yes having a RoomExtension for each room will start using a significant amount of memory. And multiple threads will also slowly kill your processor...
In this case, the best case is to only have 1 Zone-Level extension which takes care of all the rooms and then a global Thread which loops between every room, instead of using a Task for each room.
In order to save room-specific data you can use the room.setProperty() and room.getProperty() methods or a hidden RoomVariable.



Thank you so much,

I sent to the Zone-Level Extension and works clearly.
How can I create a global Thread for every room? Is there any sample for multiple rooms?
User avatar
rjgtav
Posts: 2813
Joined: 19 Apr 2009, 11:31
Location: Lisbon, Portugal

Re: How to use a RoomExtension

Postby rjgtav » 08 Jul 2012, 12:10

You can schedule a Task to run at a fixed race and store a reference to it on the Zone-Level extension so you can then interact with it.
Then, you can have a simple List of Rooms which that task will iterate through and update manually each of the game. When a game starts you simply add that room to the list and when a game ends you remove it from there.

You can find more information regarding the TaskScheduler here.
Last edited by rjgtav on 10 Jul 2012, 19:52, edited 1 time in total.
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.
antuan
Posts: 10
Joined: 06 Dec 2010, 11:49

Re: How to use a RoomExtension

Postby antuan » 09 Jul 2012, 10:15

rjgtav, I tried doing what you told me and everything worked fine!

My RoomExtension is like following.

Code: Select all

public class RoomExtension extends BaseSFSExtension{
       
    SmartFoxServer sfs;
    ScheduledFuture<?> taskHandle;
       
   
    @Override
    public void init() {
       
        sfs = SmartFoxServer.getInstance();

    }

    @Override
    public void handleClientRequest(String cmd, User user, ISFSObject params) throws SFSException {
       

       taskHandle = sfs.getTaskScheduler().scheduleAtFixedRate(new TaskRunner(), 0, 1, TimeUnit.MILLISECONDS);           
       
 
    }
   
    private class TaskRunner implements Runnable
    {
        private int runningCycles = 0;
         
        public void run()
        {
            runningCycles++;
           
        }
    }
   
}


I think this RoomExtension logic is best for the multiple rooms which has the own RoomExtension.

Thank you again,
User avatar
rjgtav
Posts: 2813
Joined: 19 Apr 2009, 11:31
Location: Lisbon, Portugal

Re: How to use a RoomExtension

Postby rjgtav » 10 Jul 2012, 19:54

It is better if you remove completely the need of RoomExtensions. You can schedule the task at the Zone-Level and store in the Zone-Level extension a reference to it.
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.

Return to “SFS2X Java / Android API”

Who is online

Users browsing this forum: No registered users and 2 guests