Room Extension accessing Zone Extension functions

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

Moderators: Lapo, Bax

albymack
Posts: 17
Joined: 09 May 2022, 21:17

Room Extension accessing Zone Extension functions

Postby albymack » 02 Aug 2022, 21:59

So I was following along this page.
http://docs2x.smartfoxserver.com/Extens ... assLoading

I understand that each extension is loaded under their own class loader. I see in your documentation that one way to deal with this is to move shared model files into the __libs__ folder so that different extension would have access to the same model classes.

In your example diagram, you expose a public function in the zone extension class for getting the model, and the model classes are defined in __libs__.

What I don't understand is how does the room extension classes get access to this public function on the zone extension class, since the zone extension class itself doesn't exist in the room extension's class loader?

So if I have CustomZoneExtension which inherits from SFSExtension with a public function getModel that returns a ModelClass defined in a jar in __libs__, in room extension class CustomRoomExtension, how do I gain access to the getModel function in the CustomZoneExtension since they're in different class loaders? From the CustomRoomExtension, I only can get the SFSExtension for the zone and call functions defined for SFSExtension. But since the getModel function is not part of SFSExtension, but part of the CustomZoneExtension class, I cannot call it.
User avatar
Lapo
Site Admin
Posts: 23008
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Room Extension accessing Zone Extension functions

Postby Lapo » 03 Aug 2022, 08:18

Hi,
What I don't understand is how does the room extension classes get access to this public function on the zone extension class, since the zone extension class itself doesn't exist in the room extension's class loader?


This assumption is not entirely correct. Java uses a so called "parent-delegation model" to load classes, whereby a Class Loader will first ask its parent if it has already loaded a certain Class.

Because the Zone Extension is loaded by the top class loader (which is the parent of all Extension Class Loaders), the Room Extension will be able to reference the Zone Ext, while the opposite is not possible. At least in the use case you propose.

Makes sense?
Lapo
--
gotoAndPlay()
...addicted to flash games
albymack
Posts: 17
Joined: 09 May 2022, 21:17

Re: Room Extension accessing Zone Extension functions

Postby albymack » 03 Aug 2022, 17:34

So I have an event handler in my room extension. Here's the code for it.

Code: Select all

public class JoinRoomHandler extends BaseServerEventHandler
{
    @Override
    public void handleServerEvent(ISFSEvent pEvent) throws SFSException
    {
        CustomRoomExtension pExtension =  (CustomRoomExtension)getParentExtension ();
        CustomZoneExtension pZoneExtension = (CustomZoneExtension) pExtension.getParentZone ().getExtension();
    }       
}


I'm getting an error trying to get the zone extension and cast it to CustomZoneExtension

Exception: java.lang.NoClassDefFoundError
Message: CustomZoneExtension/CustomZoneExtension

The zone extension (CustomZoneExtension) does work and it auto joins the user into the room (CustomRoomExtension), which I have an event handler for USER_JOIN_ROOM. The JoinRoomHandler triggers but I get that error.

Any idea what I'm doing wrong?
Last edited by albymack on 03 Aug 2022, 17:40, edited 1 time in total.
albymack
Posts: 17
Joined: 09 May 2022, 21:17

Re: Room Extension accessing Zone Extension functions

Postby albymack » 03 Aug 2022, 17:40

To elaborate, the CustomZoneExtension.jar is in the extensions/CustomZoneExtension folder, and the CustomRoomExtension.jar in the extensions/CustomRoomExtension folder.

The CustomZoneExtension is assigned to the zone and does work. The Room is dynamically created on the server in a handler for USER_JOIN_ZONE which creates the room (with CustomRoomExtension assigned) and then auto joins the user who joined the zone into the room.
albymack
Posts: 17
Joined: 09 May 2022, 21:17

Re: Room Extension accessing Zone Extension functions

Postby albymack » 03 Aug 2022, 20:47

I did manage to get something working as a work around by creating an interface class in a shared __libs__ jar file with ICustomZoneExtension that exposes the public functions in the CustomZoneExtension.

Is this a good workaround, other than moving the extension code itself into a jar in __libs__.

So then when I implement the zone extension class it's

public class CustomZoneExtension extends SFSExtension implements ICustomZoneExtension

Would this still allow for dynamic reloading of the CustomZoneExtension as long as the interface doesn't change?

It just seems like your recommended way of moving shared model classes to __libs__ but leaving extension classes in extensions doesn't actually work. At least I couldn't figure out how the get the CustomZoneExtension class to be usable in the CustomRoomExtension, short of moving the classes in __libs__. Your docs say to move the model files into __libs__, not the extension classes themselves.
User avatar
Lapo
Site Admin
Posts: 23008
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Room Extension accessing Zone Extension functions

Postby Lapo » 04 Aug 2022, 07:52

Is this a good workaround, other than moving the extension code itself into a jar in __libs__.

Yes

Would this still allow for dynamic reloading of the CustomZoneExtension as long as the interface doesn't change?

Yes

To further clarify, especially for other readers, the example you have proposed after my last reply is one where two Extension are completely separate from one another, since both are loaded by different class loaders. The two will not be able to interoperate.

By creating a base interface for the Zone Extension and deploying it in the parent class loader (i.e. extensions/__lib__/ folder), Room Extensions will be able to reference the relative Zone Extension and cast it to the interface type.

For more details it's best to reference our docs which also contains diagrams and helps with visualization.
http://docs2x.smartfoxserver.com/Extens ... assLoading

Cheers
Lapo

--

gotoAndPlay()

...addicted to flash games

Return to “SFS2X Questions”

Who is online

Users browsing this forum: No registered users and 46 guests