Page 1 of 1

How to use a RoomExtension

Posted: 06 Jul 2012, 15:34
by antuan
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?

Re: How to use a RoomExtension

Posted: 07 Jul 2012, 13:26
by rjgtav
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.

Re: How to use a RoomExtension

Posted: 07 Jul 2012, 18:14
by antuan
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?

Re: How to use a RoomExtension

Posted: 08 Jul 2012, 12:10
by rjgtav
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.

Re: How to use a RoomExtension

Posted: 09 Jul 2012, 10:15
by antuan
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,

Re: How to use a RoomExtension

Posted: 10 Jul 2012, 19:54
by rjgtav
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.