Hello all,
The current situation is as following:
class structure:
AbstractPlayer > Player > NetworkPlayer, HumanPlayer, AIPlayer
- Has non-important member vars like int, boolean and such. Also a Robot
wich owns an array of Register objects
a Register object keeps track of 1) a boolean and 2) a Card object.
Game > NetworkGame > NetworkGameClient, NetworkGameServer
Now, everytime the Player.placeYourMove() function has been called, the
representing player's getRobot().Register(x).setCard() gets called and
updates the card the player chose. I'm leaving out all non-important
details, you'll notice soon enough what's troubling me. The most important
part is there's a robot object in player and a register array in robot an a
card in each register.
The client-server structure is roughly a general socketed network
architecture: the server loops trough a player Vector, checks if the player
is a networkplayer instance, and then waits for their move with the
following command:
move = (AbstractPlayer) ((NetworkPlayer)
players.elementAt(i)).getInputStream().readObject()
Where getInputStream returns of course the ObjectInputStream object where
the client and server are connected (created during the server loop while
waiting for clients. This connection should not be broken and is not
generally).
The client has basically the same loop: if player is networkplayer (that
means it's a human or ai player on the server side, OR another networkplayer
but not ourselves) wait for server input, otherwise call the placeYourMove()
locally and send it to the server via writeObject().
Now, everything is fine thanks to the two words 'implements Serializible'.
Except for this:
When the client sends their own AbstractPlayer data, everything is received
successfully and the Robot and other objects are OK. When the server sends
something to the clients (with exact the same call namely
readObject(AbstractPlayer, the changed one)), the move data is received too
but the Robot().Register(x).Card() objects are all null! (in Register
constructor card is null).
I tought hm, an array is not good for patching trough and I made the
Register[] array an Vector or some other data structure. No go. By the way
why does the server receive everything correctly but it gets messed up
otherwise? I tried
--- server sends to clients ---
System.out.println(player.data()); >> prints the RIGHT, changed data info
getOutputStream().writeObject(data);
---
--- client receives from server ---
System.out.println(oldPlayer.data()); >> prints the old data
move = (AbstractPlayer) getInputStream().readObject();
System.out.println(move.data()); >> prints all member vars of the move
correctly except the Register card and boolean.
This basically means move STILL contains the initial registers with the card
object set null and boolean set false in the constructor. This is wrong
because the server is also a player and called locally placeYourMove() to
update his player registers and THEN sent it trough. The println() yust
above the writeObject() method above prints data right so something went
wrong with the streams.
When I alter a testvar, like "boolean test" in AbstractPlayer, in
placeYourMove() and then patch it trough, it is received correctly. Is the
Robot->Register->Stuff tree too messy/wide/deep for object streams?
---
-- server receives from client ---
System.out.println(oldPlayer.data()); >> prints the old data
move = (AbstractPlayer) getInputStream().readObject();
System.out.println(move.data()); >> prints the RIGHT changed data!!!!
---
Woah :-(
What am I doing wrong? Casting wrong? Serializing wrong? Using my streams
wrong? Nope because the server receives everything OK.
Any help?
Thanks a lot in advance!
Greetings