Assigning a Room Extension programatically

Post here your questions about SFS2X. Here we discuss all server-side matters. For client API questions see the dedicated forums.

Moderators: Lapo, Bax

valr2
Posts: 3
Joined: 29 Jun 2022, 09:23

Assigning a Room Extension programatically

Postby valr2 » 29 Jun 2022, 09:40

Hello!

I have a JAVA extension at the Zone level, called "CasinoExtension" and loaded from /extensions/CasinoExtension/CasinoExtension.jar with main class com.casinocity.zone.CasinoExtension. And this Zone-level extension is working fine.

I'm creating some Rooms from code, from the server-side, at init time, using this Zone extension. They are set as non-dynamic, NEVER_REMOVE. But I also want to attach another custom extension to these rooms. I tried the following code:

CreateRoomSettings settings = new CreateRoomSettings();
settings.setName("Poker1");
...
settings.setExtension( new CreateRoomSettings.RoomExtensionSettings("CasinoExtension", "com.casinocity.poker.PokerExtension"));
api.createRoom(zone, settings, null);

But nothing happens. I see my rooms in the Zone Monitor (in the Admin Tool), but their Room Extension tab is grayed out. Also, my traces from the PokerExtension class init() method are not reached, so it seems that the Room-level extension is never fired up. Yet there is no error in the logs.

PokerExtension is an existing class under the com.casinocity.poker package and I can see this PokerExtension.class file inside the exported CasinoExtension.jar.

The reason I want to have both a main extension at Zone level and other extensions at Room level is that I plan to support multiple types of games, so depending on game type, each Room might get a specialized extension to handle that game type logic, instead of stuffing the main Zone extension with request handlers for all types of games.

Any idea what I'm doing wrong?
Thanks!
User avatar
Lapo
Site Admin
Posts: 22476
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Assigning a Room Extension programatically

Postby Lapo » 29 Jun 2022, 13:12

Hi,
it seems rather strange that there're no messages whatsoever regarding the Extension in the logs.
If Extension settings are provided the server will definitely attempt to load the provided class(es) and if that fails an error will appear in the log files.

What is the version of SFS2X in use?
Also what JDK are you using to compile the Room Extension in question?

Thanks
Lapo
--
gotoAndPlay()
...addicted to flash games
valr2
Posts: 3
Joined: 29 Jun 2022, 09:23

Re: Assigning a Room Extension programatically

Postby valr2 » 04 Jul 2022, 16:45

Hello!

Sorry for the late reply...

I managed to sort it out by myself in the meanwhile. I think it was a combination of multiple issues that tripped me. Now the room loads that extension dynamically, as instructed by CreateRoomSettings.

However, I have a new, but related problem. I have this "com.casinocity.poker.entities.PokerTable" class which is compiled and exported to my CasinoExtension.jar, which is the only JAR file I have under /extensions/CasinoExtension. Some part of my code that runs from the Zone-level CasinoExtension will create such a PokerTable object and assign it as Room Property to a Room. Afterward, another Request Handler registered by the PokerExtension at Room-level will try to retrieve and type-cast this same PokerTable object. But apparently, because the two occurrences start at different levels/extensions (zone vs room), they end up using different URLClassLoaders. So I get an error like:

Exception: java.lang.ClassCastException
Message: class com.casinocity.poker.entities.PokerTable cannot be cast to class com.casinocity.poker.entities.PokerTable (com.casinocity.poker.entities.PokerTable is in unnamed module of loader java.net.URLClassLoader @73a4f32c; com.casinocity.poker.entities.PokerTable is in unnamed module of loader java.net.URLClassLoader @759b447a)

@73a4f32c and @759b447a are not the same, so different URLClassLoaders, hence typecasting won't work.

But I don't know how to make sure that all classes load inside the same URLClassLoader. They are all part of a single JAR file.

Any suggestions? Thanks!
User avatar
Lapo
Site Admin
Posts: 22476
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Assigning a Room Extension programatically

Postby Lapo » 04 Jul 2022, 17:05

Hi,
I am glad you found out what the issue was.
As regards the class loading problem we outline this in the the documentation here: http://docs2x.smartfoxserver.com/Extens ... assLoading
which I highly recommend if you want to understand how class loading works in SFS2X.

All Extensions run in separate class loaders to be able to reload at runtime. If you need it, you can change this behavior by deploying the Extension code in the extensions/__lib__/ folder (more details in the link above).

By doing so you'll avoid the exceptions you have mentioned, although runtime reloading won't be possible.

Cheers
Lapo

--

gotoAndPlay()

...addicted to flash games
valr2
Posts: 3
Joined: 29 Jun 2022, 09:23

Re: Assigning a Room Extension programatically

Postby valr2 » 04 Jul 2022, 17:25

Edit: Oh, you already answered my previous question while I was writing this reply... thank you! I'll try putting the whole .jar directly under __lib__ and maintain two extensions, and see how it goes! I didn't realize that I can place it entirely there, without extracting the model classes into a separate JAR.

According to http://docs2x.smartfoxserver.com/Extens ... assLoading this is normal behavior.

A Room Extension will always have a different ClassLoader than a Zone Extension, no matter if they come from the same .jar or not. So in order for them to share some common classes, those classes need to be packaged in a separate .jar and placed under __lib__. I suppose there is no way to work around thins.

Since this complicates matters (I would have preferred to have a single exported JAR for easier maintenance and keeping track of versioning etc.) I might look into options to give up the Room-level extension after all, and cram all game-specific handlers at Zone-level, in order to have a single extension, even if it will handle multiple types of games.

Any suggestions you might have are still welcomed of course...
Thanks!
User avatar
Lapo
Site Admin
Posts: 22476
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Assigning a Room Extension programatically

Postby Lapo » 05 Jul 2022, 08:27

Yes, the deployment under extensions/__lib__/ will avoid the class loader "segregation" issue.
Cheers
Lapo

--

gotoAndPlay()

...addicted to flash games

Return to “SFS2X Questions”

Who is online

Users browsing this forum: Bing [Bot] and 56 guests