Commit 1d60e562 authored by Bogdan's avatar Bogdan

Network exceptions - big number of modifications in rest of the code.

parent 286a005a
......@@ -11,6 +11,8 @@ import core.zone.Direction;
import core.zone.Translation;
import core.zone.Zone;
import gameloop.DummyLocalGameLoop;
import gameloop.GameStarter;
import network.helpers.NetworkConnectionLost;
import java.io.Serializable;
import java.util.ArrayList;
......@@ -232,7 +234,11 @@ public class EffectBuilder implements Serializable{
}
if(hit){
Event event = new AbilityEvent(casterCharacterID, 0);
DummyLocalGameLoop.getInstance().getNetworkConnection().sendEvent(event);
try {
DummyLocalGameLoop.getInstance().getNetworkConnection().sendEvent(event);
} catch (NetworkConnectionLost networkConnectionLost) {
GameStarter.initiateBrokenConnectionMeasures();
}
}
}
};
......
......@@ -8,6 +8,8 @@ import core.gamestate.GameContent;
import core.zone.Point;
import core.zone.Translation;
import gameloop.DummyLocalGameLoop;
import gameloop.GameStarter;
import network.helpers.NetworkConnectionLost;
/**
* This event is executed when an entity wants to move.
......@@ -37,7 +39,11 @@ public class MoveEvent implements ClientEvent,ServerEvent {
}
else if(target.getSpeciesName().equals("FrostBolt") || target.getSpeciesName().equals("FireBoltLP")){
Event event = new AbilityEvent(targetID, 0);
DummyLocalGameLoop.getInstance().getNetworkConnection().sendEvent(event);
try {
DummyLocalGameLoop.getInstance().getNetworkConnection().sendEvent(event);
} catch (NetworkConnectionLost networkConnectionLost) {
GameStarter.initiateBrokenConnectionMeasures();
}
}
return true;
} catch (EntityNotFoundExeption entityNotFoundExeption) {
......
......@@ -10,9 +10,11 @@ import core.event.Event;
import core.event.ToServerDeathEvent;
import core.zone.Zone;
import gameloop.DummyLocalGameLoop;
import gameloop.GameStarter;
import graphics.guiSkeleton.entityDisplayer.EntityDisplayerType;
import logging.Logging;
import map_generation.tiles.TilePropertyVector;
import network.helpers.NetworkConnectionLost;
import network.inner_shell.NetworkConnection;
/**
......@@ -152,7 +154,11 @@ public class Being extends Entity {
LOGGER.severe("NetworkConnection null, Being"+this.getID()+" can't die");
} else {
LOGGER.info("Death of "+this.getID()+" is send");
network.sendEvent(event);
try {
network.sendEvent(event);
} catch (NetworkConnectionLost networkConnectionLost) {
GameStarter.initiateBrokenConnectionMeasures();
}
}
}
......
......@@ -13,11 +13,13 @@ import core.zone.Direction;
import core.zone.Point;
import core.zone.Translation;
import core.zone.Zone;
import gameloop.GameStarter;
import graphics.guiSkeleton.entityDisplayer.EntityDisplayer;
import graphics.guiSkeleton.entityDisplayer.EntityDisplayerProvider;
import graphics.guiSkeleton.entityDisplayer.EntityDisplayerType;
import map_generation.map.Map;
import map_generation.tiles.TilePropertyVector;
import network.helpers.NetworkConnectionLost;
import network.inner_shell.NetworkConnection;
/**
......@@ -452,7 +454,11 @@ public class Entity implements Serializable{
targets.add(c.getID());
}
list.clear();
networkConnection.sendEvent(new TriggerEvent(trigger,ID));
try {
networkConnection.sendEvent(new TriggerEvent(trigger,ID));
} catch (NetworkConnectionLost networkConnectionLost) {
GameStarter.initiateBrokenConnectionMeasures();
}
for(Effect effect:trigger.getEffectList()){
effect.effect(targets, gameContent, ID);
}
......
package core.gamestate;
import core.zone.Point;
import gameloop.GameStarter;
import logging.Logging;
import map_generation.map.Map;
import network.helpers.NetworkConnectionLost;
import network.inner_shell.NetworkConnection;
import core.event.MapChangeEvent;
import core.relayer.*;
......@@ -131,7 +133,11 @@ public class GameContent implements Serializable{
for(Character player:players){
if(player.isOnStairs(map)){
recreate(map,true);
networkConnection.sendEvent(new MapChangeEvent(newMap));
try {
networkConnection.sendEvent(new MapChangeEvent(newMap));
} catch (NetworkConnectionLost networkConnectionLost) {
GameStarter.initiateBrokenConnectionMeasures();
}
return true;
}
}
......
......@@ -9,7 +9,9 @@ import core.gamestate.Entity;
import core.zone.Direction;
import core.zone.Translation;
import gameloop.DummyLocalGameLoop;
import gameloop.GameStarter;
import gameloop.ServerLoop;
import network.helpers.NetworkConnectionLost;
import java.util.Objects;
......@@ -74,7 +76,11 @@ public class Relayer extends Thread{
return;
}
Event event = new MoveEvent(character.getID(), Translation.construct(direction,character.getSpeed()));
DummyLocalGameLoop.getInstance().getNetworkConnection().sendEvent(event);
try {
DummyLocalGameLoop.getInstance().getNetworkConnection().sendEvent(event);
} catch (NetworkConnectionLost networkConnectionLost) {
GameStarter.initiateBrokenConnectionMeasures();
}
}
private void stopMove(){
......@@ -95,7 +101,11 @@ public class Relayer extends Thread{
*/
private void applyAbility() {
Event event = new AbilityEvent(character.getID(), currentAbility);
DummyLocalGameLoop.getInstance().getNetworkConnection().sendEvent(event);
try {
DummyLocalGameLoop.getInstance().getNetworkConnection().sendEvent(event);
} catch (NetworkConnectionLost networkConnectionLost) {
GameStarter.initiateBrokenConnectionMeasures();
}
hasTriedToCastAbility = false;
}
......
......@@ -8,7 +8,9 @@ import core.gamestate.GameContent;
import core.zone.Direction;
import core.zone.Translation;
import gameloop.DummyLocalGameLoop;
import gameloop.GameStarter;
import gameloop.ServerLoop;
import network.helpers.NetworkConnectionLost;
import java.util.Objects;
......@@ -40,7 +42,11 @@ public class RelayerEntity extends Thread{
return;
}
Event event = new MoveEvent(character.getID(), Translation.construct(direction,character.getSpeed()));
DummyLocalGameLoop.getInstance().getNetworkConnection().sendEvent(event);
try {
DummyLocalGameLoop.getInstance().getNetworkConnection().sendEvent(event);
} catch (NetworkConnectionLost networkConnectionLost) {
GameStarter.initiateBrokenConnectionMeasures();
}
}
private void stopMove(){
......
......@@ -9,6 +9,7 @@ import core.relayer.Relayers;
import graphics.graphical_abstraction.GraphicsMasterAbstraction;
import logging.Logging;
import network.helpers.EndEvent;
import network.helpers.NetworkConnectionLost;
import network.inner_shell.NetworkConnection;
......@@ -136,23 +137,27 @@ public class DummyLocalGameLoop extends Thread{
LOGGER.info("DummyLocalGameLoopThread,"+currentThread().getId()+ "game is "+isPlaying);
Event eventToReceive = null;
LOGGER.info("The game is on!");
while (!((eventToReceive = (Event) getNetworkConnection().receiveEvent(true)) instanceof EndEvent)) {
//System.out.println("DummyLocalGameLoop Thread: We have received an event!applying...+"+eventToReceive);
try {
eventToReceive.apply(gameContent);
} catch (InterruptedException e) {
e.printStackTrace();
}
if (eventToReceive instanceof MapInit) {
GameContent gc = DummyLocalGameLoop.getInstance().getContent();
Relayer firstEntityRelayer = Relayers.addFollowedRelayer(((MapInit) eventToReceive).getFollowedDummyEntity());
setFollowedRelayer(firstEntityRelayer);
relayerSemaphore.release();
}
if (eventToReceive instanceof ToClientDeathEvent && ((ToClientDeathEvent) eventToReceive).getEntityID()==followedRelayer.getCharacter().getID()) {
GraphicsMasterAbstraction.getInstance().changeGUIStateTo(GraphicsMasterAbstraction.GUIStates.GAME_OVER);
try {
while (!((eventToReceive = (Event) getNetworkConnection().receiveEvent(true)) instanceof EndEvent)) {
//System.out.println("DummyLocalGameLoop Thread: We have received an event!applying...+"+eventToReceive);
try {
eventToReceive.apply(gameContent);
} catch (InterruptedException e) {
e.printStackTrace();
}
if (eventToReceive instanceof MapInit) {
GameContent gc = DummyLocalGameLoop.getInstance().getContent();
Relayer firstEntityRelayer = Relayers.addFollowedRelayer(((MapInit) eventToReceive).getFollowedDummyEntity());
setFollowedRelayer(firstEntityRelayer);
relayerSemaphore.release();
}
if (eventToReceive instanceof ToClientDeathEvent && ((ToClientDeathEvent) eventToReceive).getEntityID()==followedRelayer.getCharacter().getID()) {
GraphicsMasterAbstraction.getInstance().changeGUIStateTo(GraphicsMasterAbstraction.GUIStates.GAME_OVER);
}
}
} catch (NetworkConnectionLost networkConnectionLost) {
GameStarter.initiateBrokenConnectionMeasures();
}
if (eventToReceive instanceof EndEvent)
LOGGER.info("End Event Received!");
......
......@@ -3,6 +3,8 @@ package gameloop;
import artificial_intelligence.AIControler;
import core.relayer.Relayer;
import core.relayer.Relayers;
import graphics.graphical_abstraction.GraphicsMasterAbstraction;
import graphics.guiSkeleton.GraphicsMaster;
import logging.Logging;
import network.inner_shell.ClientConnection;
import network.inner_shell.LocalConnection;
......@@ -13,9 +15,11 @@ import network.inner_shell.NetworkConnection;
* Some changes made by Guerquin on 17/12/15
*
* This class's purpose is to start the gameLoop with the initialized network connection.
* and then to stop the game if a connection problem appears
*
* @author Dupriez Thomas
* @author Guerquin Arnaud
* @author bogdanbear
*
*/
public class GameStarter {
......@@ -63,6 +67,11 @@ public class GameStarter {
DummyLocalGameLoop.getInstance().getNetworkConnection().initiateConnectionEnd();
DummyLocalGameLoop.getInstance().pauseGame();
}
static public void initiateBrokenConnectionMeasures()
{
leaveGame();
GraphicsMaster.getInstance().changeGUIStateTo(GraphicsMasterAbstraction.GUIStates.LOST_CONNECTION);
}
/**
* To be called by the gui when the player wants to launch a game
......
......@@ -10,6 +10,7 @@ import map_generation.map.Map;
import map_generation.map.MapGeneration;
import map_generation.map.MapSize;
import network.helpers.EndEvent;
import network.helpers.NetworkConnectionLost;
import network.inner_shell.NetworkConnection;
import network.inner_shell.ServerConnection;
import network.outer_shell.Lounge;
......@@ -67,8 +68,12 @@ public class ServerLoop extends Thread {
else {
//we are dealing with a LocalConnection
Event initEvent = new MapInit(gameContent, gameContent.getPlayer(0));
networkConnection.sendEvent(initEvent);
}
try {
networkConnection.sendEvent(initEvent);
} catch (NetworkConnectionLost networkConnectionLost) {
networkConnectionLost.printStackTrace();
}
}
return gameContent;
}
......@@ -84,16 +89,20 @@ public class ServerLoop extends Thread {
while (cont)
{
/*Add all event received to the queue*/
for(Event event=(Event)networkConnection.receiveEvent(false);event!=null;event=(Event)networkConnection.receiveEvent(false)){
queue.addEvent(event, cframe);
if (event instanceof EndEvent) {
Logging.getInstance().getLogger().info("Just received an EndEvent");
//this is so bad because if this thread sleeps for too long it's a disaster
cont=false;
networkConnection.initiateConnectionEnd();
}
}
if(cont == false)
try {
for(Event event=(Event)networkConnection.receiveEvent(false);event!=null;event=(Event)networkConnection.receiveEvent(false)){
queue.addEvent(event, cframe);
if (event instanceof EndEvent) {
Logging.getInstance().getLogger().info("Just received an EndEvent");
//this is so bad because if this thread sleeps for too long it's a disaster
cont=false;
networkConnection.initiateConnectionEnd();
}
}
} catch (NetworkConnectionLost networkConnectionLost) {
networkConnectionLost.printStackTrace();
}
if(cont == false)
break;
ctime = System.currentTimeMillis();
if(ctime - ltime > frame)
......@@ -122,8 +131,12 @@ public class ServerLoop extends Thread {
//System.out.println("Coucou, local execution");
Event event = queue.poll().getEvent();
Event toBeSentToClientsEvent = event.apply(gameContent);
networkConnection.sendEvent(toBeSentToClientsEvent);
}
try {
networkConnection.sendEvent(toBeSentToClientsEvent);
} catch (NetworkConnectionLost networkConnectionLost) {
networkConnectionLost.printStackTrace();
}
}
gameContent.applyTrigger(networkConnection);
if(gameContent.checkStrairs(nextMap,networkConnection))
nextMap=MapGeneration.mapGeneration(MapSize.SMALL);
......
package network.helpers;
/**
* Created by bogdanbear on 06/01/2016.
*/
public class NetworkConnectionLost extends Exception {
}
......@@ -2,6 +2,7 @@ package network.inner_shell;
import logging.Logging;
import network.helpers.NetworkConnectionLost;
import network.helpers.NetworkObject;
import network.helpers.TestEvent;
......@@ -49,7 +50,7 @@ public abstract class NetworkConnection {
* Can be from the server to the client or from the client to the server
* @param event Event to send
*/
public abstract void sendEvent(NetworkObject event);
public abstract void sendEvent(NetworkObject event) throws NetworkConnectionLost;
/**
* Add incoming event to the queue.
......@@ -77,7 +78,9 @@ public abstract class NetworkConnection {
*/
//making this method synchronized would be a grave error, since incoming events might induce a block inside this method
@SuppressWarnings("unchecked")
public NetworkObject receiveEvent(boolean blocking) {
public NetworkObject receiveEvent(boolean blocking) throws NetworkConnectionLost {
if (isBroken())
throw new NetworkConnectionLost();
if (!blocking) {
try {
return incomingEvents.removeFirst();
......@@ -98,10 +101,18 @@ public abstract class NetworkConnection {
public boolean isBroken() {
//attempt to send a test event first
//must not be sunchronized because broken might need to be updated when we attempt to send a test event
NetworkObject testEvent = new TestEvent("this is just to test if no exceptions are being thrown");
sendEvent(testEvent);
//broken might have been updated after this.
return broken;
if (broken==false)
return broken;
else{
NetworkObject testEvent = new TestEvent("this is just to test if no exceptions are being thrown");
try {
sendEvent(testEvent);
} catch (NetworkConnectionLost networkConnectionLost) {
//we do a dummy send event to test connection, broken will be updated by the corresponding threads
return false;
}
}
return true;
}
//package visibility
......
package network.junit_tests;
import network.helpers.NetworkConnectionLost;
import network.inner_shell.ClientConnection;
import org.junit.*;
import org.junit.runners.MethodSorters;
......@@ -68,7 +69,12 @@ public class ClientConnectionTest{
public void test1ReceiveEvent(){
try {
outputStream.writeObject(event);
ObjectForTest receivedEvent = (ObjectForTest)client.receiveEvent(true);
ObjectForTest receivedEvent = null;
try {
receivedEvent = (ObjectForTest)client.receiveEvent(true);
} catch (NetworkConnectionLost networkConnectionLost) {
networkConnectionLost.printStackTrace();
}
assertEquals("Network ServerConnection.receiveEvent failed",event.str,receivedEvent.str);
} catch (IOException e) {
e.printStackTrace();
......
package network.junit_tests;
import network.helpers.NetworkConnectionLost;
import network.inner_shell.LocalConnection;
import network.inner_shell.NetworkConnection;
import network.helpers.NetworkObject;
......@@ -24,14 +25,14 @@ public class LocalConnectionTest{
private NetworkObject event = new ObjectForTest();
@Test
public void testSendEventForServer(){
public void testSendEventForServer() throws NetworkConnectionLost {
serverConnection.sendEvent(event);
NetworkObject receivedEvent = clientConnection.receiveEvent(false);
assertEquals("Network : the object send by the server side is not received by the client side.",event,receivedEvent);
}
@Test
public void testSendEventForClient(){
public void testSendEventForClient() throws NetworkConnectionLost {
clientConnection.sendEvent(event);
NetworkObject receivedEvent = serverConnection.receiveEvent(false);
assertEquals("Network : the object send by the client side is not received by the server side.",event,receivedEvent);
......
package network.junit_tests;
import network.helpers.NetworkConnectionLost;
import network.inner_shell.ServerConnection;
import org.junit.AfterClass;
import org.junit.BeforeClass;
......@@ -163,7 +164,12 @@ public class ServerConnectionTest{
public void testReceiveEvent(){
try {
client1.getOutputStream().writeObject(e);
ObjectForTest event = (ObjectForTest)server.receiveEvent(true);
ObjectForTest event = null;
try {
event = (ObjectForTest)server.receiveEvent(true);
} catch (NetworkConnectionLost networkConnectionLost) {
networkConnectionLost.printStackTrace();
}
assertEquals("Network ServerConnection.receiveEvent failed",e.str,event.str);
} catch (IOException e1) {
e1.printStackTrace();
......
package network.legacy_tests;
import network.helpers.NetworkConnectionLost;
import network.helpers.TestEvent;
import network.inner_shell.ClientConnection;
......@@ -32,8 +33,12 @@ public class ConnectionEndTestClient {
TestEvent event = null;
while ((event = (TestEvent) clientConnection.receiveEvent(false)) != null) {
System.out.println(event.getTestMessage());
try {
while ((event = (TestEvent) clientConnection.receiveEvent(false)) != null) {
System.out.println(event.getTestMessage());
}
} catch (NetworkConnectionLost networkConnectionLost) {
networkConnectionLost.printStackTrace();
}
......
package network.legacy_tests;
import network.helpers.NetworkConnectionLost;
import network.helpers.TestEvent;
import network.inner_shell.ClientConnection;
......@@ -33,9 +34,13 @@ public class SimpleTestClient {
TestEvent event=null;
while ((event=(TestEvent)clientConnection.receiveEvent(false))!=null)
{
System.out.println(event.getTestMessage());
try {
while ((event=(TestEvent)clientConnection.receiveEvent(false))!=null)
{
System.out.println(event.getTestMessage());
}
} catch (NetworkConnectionLost networkConnectionLost) {
networkConnectionLost.printStackTrace();
}
}
......
package network.legacy_tests;
import logging.Logging;
import network.helpers.NetworkConnectionLost;
import network.helpers.NetworkObject;
import network.inner_shell.ServerConnection;
......@@ -22,7 +23,12 @@ public class SimpleTestServer {
{
LOGGER.info("Server started second phase");
//just read what was sent by clients and send it back
NetworkObject event=serverConnection.receiveEvent(true);
NetworkObject event= null;
try {
event = serverConnection.receiveEvent(true);
} catch (NetworkConnectionLost networkConnectionLost) {
networkConnectionLost.printStackTrace();
}
LOGGER.info("I have received the event: "/*+event.toString()*/);
serverConnection.sendEvent(event);
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment