Multiple user game

I only programmed in plain Java so far, no network programming, so sorry for my dumb question. I’ve searched the net and forums, but didn’t find the answer. My question is:

I have to write an on-line Poker game that runs in the browser, but I’m in trouble with the session management. I already have the register/login part done. Now, if a user is logged in, he can click on “play game” and enter the game, but the game only starts if at least 2 user is playing. So there is my first problem, how do I count how many user is ready to play ? How should I manage multiple users? I’ve could only manage separate sessions, but that way I can’t write a multiplayer game. My guess would be that I create the Game class, and use static variables, well, at least static from the sessions. But Vaadin treats sessions like a new program is launched in a simple desktop application, so if I declare my variables simply static, than it only has affect on the current session.

Can someone please give me advice what kind of technique I should use to implement the game? A link to a tutorial or a book is fine too, I just don’t really know where I should start, as I only got the project and an advice to use Vaadin :slight_smile: I’ve read the Vaadin book, but I’m kind of stuck here.

And sorry for my bad english, I couldn’t find the write words sometimes so I hope my problem is understandable :slight_smile:

No, this is not right.

The scope of static variables are controlled by the classloader, not Vaadin. Oversimplifying a bit, every reload of a webapplication will give you new static variables, but your static variables
will
be available to all your sessions.

Suggested reading: look up the “Singleton” pattern. Don’t get alarmed by people telling you not to use the most usual implementation using static variables, it will work just fine for your purpose.

Thank you for the advice, I’ve heard about Singleton pattern but never used it before. I’ll get started with the actual programing as soon as I can, until then I’ll do my reading.

To express it perhaps more clearly, static variables are global in the entire JVM in which the application server runs, that is, to all applications and users.

Related Pro Account article:
#111
(a bit short but the point is simple).

Sorry, in our area of concern, i.e. web applications, they are not global to the JVM. They are dependent on the class loader. You may have the same class loaded by different classloaders and you will not share static values. This is in fact what happens when the same class is loaded by different web applications.

Oh… Didn’t know that, I stand corrected. Java sure is odd. :blink:


This
was an interesting read. It’s kind of beginning to make sense to me now. Looks like the behavior depends a bit on the server used. But OK…within the same application, unless it’s reloaded, and within the same servlet, the statics should work just OK.

Thanks.

Within several servlets statics should work just fine if there is no clustering. It is a typical application to use statics to share between multiple JSPs or multiple servlets.

Other useful tidbit: a servlet instance is a singleton according to the J2EE spec. The web application instantiates the servlet once, and then every request calls the service() method. This tends to work much nicer for debugging with code hotswappig…