Custom BuddyStorage and DBManager?

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

Moderators: Lapo, Bax

Luke64
Posts: 21
Joined: 08 Nov 2020, 23:15

Custom BuddyStorage and DBManager?

Postby Luke64 » 07 Jul 2021, 15:21

Hi Lapo,

I've implemented a BuddyStorage that uses PostgreSQL as backend. I want to access the Zone's DBManager but it seems I cannot access it in my init() method as the Zone isn't initialized yet:

In my own BuddyStorage.java:

Code: Select all

    @Override
    public void init() {
        var zone = SmartFoxServer.getInstance().getZoneManager().getZoneByName(ZONE_NAME);
        if (zone == null) {
            throw new RuntimeException(String.format("Zone '%s' is missing", ZONE_NAME));
        }
        dbManager = zone.getDBManager();

        try {
            checkDatabaseStructure();
            inited = true;
        } catch (SQLException sqlErr) {
            Logger.getGlobal().log(Level.SEVERE, "Was not able to initialize BuddyStorage", sqlErr);
        }
    }


It gives me this error:
smartfoxserver-6dbd488df9-2vj54 — 14:25:11,706 ERROR [main] v2.SmartFoxServer - java.lang.RuntimeException:
smartfoxserver-6dbd488df9-2vj54 — ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
smartfoxserver-6dbd488df9-2vj54 — Exception: java.lang.RuntimeException
smartfoxserver-6dbd488df9-2vj54 — Message: Zone 'MyZone' is missing
smartfoxserver-6dbd488df9-2vj54 — Description: Unexpected error during Server boot. The server cannot start.
smartfoxserver-6dbd488df9-2vj54 — Solution: Please email us the content of this error message, including the stack trace to support[at]smartfoxserver.com
smartfoxserver-6dbd488df9-2vj54 — +--- --- ---+
smartfoxserver-6dbd488df9-2vj54 — Stack Trace:
smartfoxserver-6dbd488df9-2vj54 — +--- --- ---+
smartfoxserver-6dbd488df9-2vj54 — com.rrg.sfs2x.storage.BuddyStorage.init(BuddyStorage.java:47)
smartfoxserver-6dbd488df9-2vj54 — com.smartfoxserver.v2.buddylist.SFSBuddyListManager.init(SFSBuddyListManager.java:119)
smartfoxserver-6dbd488df9-2vj54 — com.smartfoxserver.v2.entities.managers.SFSZoneManager.configureBuddyListManager(SFSZoneManager.java:611)
smartfoxserver-6dbd488df9-2vj54 — com.smartfoxserver.v2.entities.managers.SFSZoneManager.createZone(SFSZoneManager.java:404)
smartfoxserver-6dbd488df9-2vj54 — com.smartfoxserver.v2.entities.managers.SFSZoneManager.initializeZones(SFSZoneManager.java:239)
smartfoxserver-6dbd488df9-2vj54 — com.smartfoxserver.v2.SmartFoxServer.start(SmartFoxServer.java:292)
smartfoxserver-6dbd488df9-2vj54 — com.smartfoxserver.v2.Main.main(Main.java:13)
smartfoxserver-6dbd488df9-2vj54 — ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::


How should I access the DBManager in my Storage class? And, based on that, when should I check (and possibly init) the table structure if not in the init() method?

Thanks a lot!

Cheers,
Niklas
User avatar
Lapo
Site Admin
Posts: 22999
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Custom BuddyStorage and DBManager?

Postby Lapo » 07 Jul 2021, 15:53

Hi,
trying to get a reference to the Zone via this:

Code: Select all

SmartFoxServer.getInstance().getZoneManager().getZoneByName(ZONE_NAME);

is likely the problem. Besides it's a bit convoluted, I suspect the Zone is still in its setup phase and not yet added to the ZoneManager.

A simpler way is to use this (in your custom BuddyStorage class):

Code: Select all

@Override
public void init()
{
   Zone zone = getBuddyListManager().getZone();
   dbManager = zone.getDBManager();   
   
   // etc...
}


Hope it helps
Lapo
--
gotoAndPlay()
...addicted to flash games
Luke64
Posts: 21
Joined: 08 Nov 2020, 23:15

Re: Custom BuddyStorage and DBManager?

Postby Luke64 » 07 Jul 2021, 16:34

Hi Lapo,

it did! :D
So the lifecycle of the BuddyStorage is: instantiate, setBuddyListManager(), init().

Thank you very much!

Cheers,
Niklas

Return to “SFS2X Questions”

Who is online

Users browsing this forum: No registered users and 38 guests