Java Session Implementation
Asked Answered
A

3

5

I am developing a multiplayer online game. I have the following issue:

When the user breaks his/her connection with the server, he needs to reconnect. At the first connection, during registration, the registration module produces a special ResponseDispatcher which holds the reference to the connection Channel. But if the user logs out, this Channel becomes invalid. Even though I can detect the problem and clean up resources, I have to store the reference to the registration module and Connection module to the game module, in order to renew the Channel when the user authorises and reconnects again. This creates a lot of interdependencies among modules and it gets really hard to maintain.

What I need is something like an HttpSession in Servlet Container, so that I can get the references to my channel and session resources from all modules of my server.

How is HttpSession implemented in Servlet? Is it a global hashmap storing all JSESSIONID, from which the container determines which attribute map to return? If it is a global sysmbol table, will it hit the performance (even though the time is O(1) for hashMap, there might be session modifications so it probably has to be synchronized)?

PS. Maybe some recommendations of design patterns for this case would also do.

Aspirate answered 23/10, 2013 at 12:14 Comment(4)
What I need is something like an HttpSession in Servlet Container... I'm not sure if you need this. Did I read this correctly: You obtain user information, and when the user logs in to a later time, you need to get this information again? If so, why not storing the data on server-side (in da database)?Gilding
Yes, but he/she re-logs in (connection breakdown). It means that I dont want to store his data in the database - the player is still online. His client just needs to establish the connection again, perform the authentication procedure with the corresposing module, and somehow to hook up into the running JVM process (game module), where his data is already present. The problem is that I need to hook up all modules together again. Storing everything in the database happens when the player leaves the game intentionally. In that case I can afford to use extra resource-loading + database handling.Aspirate
Is your application running in a Servlet Container?Gilding
No. Its my own game server - Java SE7 + Netty.Aspirate
G
3

I would recommend trying Shiro

Shiro can handle Session Management outside of a servlet container.

You may want to back Shiro with EhCache to provide proper caching and, if required, session persistence (and load balancing, etc...)

Gauger answered 24/10, 2013 at 8:26 Comment(0)
I
2

Have a look at the Facade Pattern

enter image description here

Inconvenient answered 24/10, 2013 at 7:36 Comment(2)
While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes.Scrimp
Agreed for unknown pages, but a wikipedia link can be trusted. Nevertheless, have made changes for fellow SOF users.Inconvenient
W
2

I'm not exactly sure what the question is. Why do you "have to store the reference to the registration module and connection module"?

Anyway there are two immediately sensible solutions to your problem.

1) Make the Registration module and Connection module Singletons. Whether this is useful depends entirely on what functionality these modules provide.

2) Make the Registration module and Connection module Persistent Entities, save them to a DataStore, complete with necessary references, and retrieve and rebuild them on reconnect.

I'm not quite sure why rolling your own session implementation would be something you want to do, what happens when the session times out?

Your design seems somewhat flawed. The player should not "still be online" if his connection goes down (this is a contradiction in terms, you cannot by definition be online if you are not connected to a network), regardless of whether this was intentional or not (you cannot know whether it is intentional or not), you have no idea whether the player is able to reconnect within a timely fashion or not, so you should assume the worst. More importantly, from a purely design aspect, getting killed by the game, because your internet connection is rubbish, is probably not something you want to deal with. If persisting data is such a costly affair you should reexamine your datastore options. Also, what happens in the scenario where the server crashes while he's offline?

Wobble answered 24/10, 2013 at 8:45 Comment(1)
There are a few states that a player can be in - in case he is ONLINE and not doing anything particular - then yes, I can generally persist his data. But if he is in the Fight, I dont want to save his data immediately - because his hp might change rapidly. + he should be seen for other players even if he is disconnected for the short duration of fight. In case he returns, he must be fetched in the fight immediately. Some storing operations take 80ms to complete (fight logs). I am using JPA. Nevertheless, with plain JDBC the time is 2 times less but still its millisecond.Aspirate

© 2022 - 2024 — McMap. All rights reserved.