Re: Designing a server for Java applet
On Thu, 22 May 2008, petersprc wrote:
On May 22, 2:28 pm, Tom Anderson <t...@urchin.earth.li> wrote:
On Thu, 22 May 2008,petersprcwrote:
I wouldn't say his method is terrible since it works fine for a
small-to-medium sized app. Adjust the polling interval to
something reasonable so you don't eat the CPU.
I'm not saying it won't work, i'm saying it's a terrible design.
You may be prematurely optimizing.
There's premature optimisation and there's just not being silly.
I totally agree with you that synchronization is superior to polling.
My point was, realistically, the performance cost of polling is
negligible in the poster's scenario. Say you had 100 concurrent
instances polling at 2 second intervals. That's 50 lookups per second
with some waking involved. The total overhead might not even reach
0.0001% of a single CPU on commodity hardware. So, there shouldn't be
any concern about doing this on a shared host, which is why I said his
solution is fine.
You have a point there. I hadn't considered the possibility that he was
using a sleep interval between polls, which was silly of me. A sleeping
poll would indeed be entirely practical.
There's no need to rewrite his code and potentially introduce race
conditions or deadlocks.
I don't think he's likely to get either of those. The solution i suggested
is functionally interchangeable with repeated polling, so it only
introduces a deadlock where he already had an infinite loop.
A robust, buffering, message queue that supports multiple listeners who
can possibly drop and reconnect requires slightly more implementation...
And isn't necessary here.
The main bottleneck is the number of simultaneous connections, which you
would run out of way before polling becomes an issue. A plain socket app
would be a bigger win as the overhead of each connection is much lower
than a full HTTP session and you can use select directly on the sockets,
thus avoiding any polling entirely.
The HTTP server could be implemented with select for all we know.
Using sockets and select directly means dealing with inside-out control
flow, where rather than reading and writing from the socket, you have to
return control from your code to the select loop, which is a huge headache
in a language without coroutines.
A typical server may only be handle to a few hundred simultaneous
transactions, so the poster will need a dedicated server at that
point. And yes, I would use a synchronized message queue that
broadcasts all the messages in a game to all the observers who attach
to it.
And of course the plain socket app or RMI servlet make it easy to do
asynchronous messaging with no polling.
JMS - Jave Message Service - would be the next step, as that can be
distributed across machines. Then there's Sun Darkstar which is
designed to handle very large virtual worlds...
If the OP's game gets to that scale, he should certainly get back to us
for fresh advice!
The code is here:
http://safari.oreilly.com/0596000405/jservlet2-CHP-10-SECT-3
Links to pay subscription resources: not helpful.
tom
--
You are in a twisty maze of directories, all alike. In front of you is
a broken pipe...