The server: There's a server that holds an array of data that can be marked by a certain client (which is given a color by the server). So, it's supposed to take an event by the client, process it, and update all the clients with the new state of the array. The array is a minesweeper stage all the clients share.
The client: It has several buttons, each referring to a position in the array. Whenever a button is pressed, it first synchronizes with the server (using Cristian's algorithm), then an event is sent to the server. It should also wait to receive an update anytime, and when that happens, the buttons should be updated too.
The problem: What should I do if I want the client to be permanently waiting for an update and not mix itself with any other receiving data? Say if the client receives a Long
for synchronizing the clock, whatever is waiting to receive an update client-wise shouldn't receive the Long
.
Thank you beforehand!
EDIT: I'm using TCP as my protocol. Also, I added some pseudocode on how the server and the client manage the events.
With some pseudocode I should explain this better:
Game main()
Create an event handler, and make it run as a separate thread
while amount of players < 2
Receive a connection
Create an instance that represents the player
Send the player some data it will need
Register the player as an observer of the game
end while
for each player
Tell them the game is ready to start
Make the player run in a separate thread
end for each
Mark the game as started
Notify each of the observers about the game
Player run()
while true
Wait for a time request
Send the player the current time
Wait for a button click
Insert the event into a global list of events
end while
Event handler run()
while true
Wait 50 ms
Order the event list by the time marks
Get and remove the first element in the list
Tell the game the button was pressed, also tell it who pressed it
Ask the game to notify the observers
end while
Currently, my client does this:
Client main()
Connect to the server
Receive the basic needed data
Wait for a confirmation to start the game
Receive the stage (an array with the positions in the server)
Display one button for each element in the received array
Append an action listener to the button
if the button is pressed
Request the current time to the server
Adjust the time
Send a button click event to the server
Wait to receive the stage again
Update the stage
end if
And here comes the problem. How do I separate the moment where the server receives the time and the stage so neither can look to each other? I mean, the server can send the time or the stage in any order and the client should know what it's receiving. I'm lost and I don't know what to do here...