I am writing Battleships game in Java for Event-Driven Programming on my studies. The game is supposed to be a network game and i am going to make client and server in one application.
This is an image representing structure of my application: class diagram http://dl.dropbox.com/u/41993645/mvc.jpeg
To get to the point - i want to write Server class that will run as separate thread and are responsible for remote View - Controller communication. So, the Server class will be responsible for:
- Reading objects from socket in infinite loop and if any arrives, putting them in the BlockingQueue for the Controller.
- Providing method like 'sendActionEventToView()' which will allow Controller to pass object the other way - from Controller to remote View.
Unfortunately, when running the Server thread, whole application stops responding. If anybody would tell me what am i doing wrong, i would be very grateful. This is in my opinion the problematic part of Server code:
/** Main Server method - responsible for reading objects
* and putting them in the queue if any arrived */
public void run() {
GameEvent event;
while(true) {
try {
event = (GameEvent)objectStream.readObject();
if(event != null) eventQueue.put(event);
} catch(ClassNotFoundException e) {
e.printStackTrace();
} catch(IOException e1) {
e1.printStackTrace();
} catch(InterruptedException e2) {
e2.printStackTrace();
}
}
}
I think thread hangs on 'readingObject()' - how can i force it to give processor time to another threads if there are not any objects in the stream?
Rest of Server: https://github.com/mc-suchecki/Battleships/blob/master/controller/Server.java Rest of the app: https://github.com/mc-suchecki/Battleships
Thank you very much in advance, if anything is unclear, please comment. And sorry for my English.