Skip to content

Commit

Permalink
Reconnector fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Calle Lejdbrandt committed Jun 5, 2012
1 parent 4776a3f commit 5579739
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 73 deletions.
9 changes: 5 additions & 4 deletions src/cnt/network/ConnectionNetworking.java
Expand Up @@ -549,9 +549,7 @@ public void send(final Packet packet, final ObjectOutputStream output)
} catch (Exception err) {
System.err.println("\033[1;33miConnectionNetworking: Couldn't close socket on faulty connection\033[0m");
}
this.sockets.remove(sendToID[i]);
this.outputs.remove(sendToID[i]);
Blackboard.broadcastMessage(new EmergencyPause(true));
this.reconnect(sendToID[i]);
}
}
Expand All @@ -563,9 +561,12 @@ public void send(final Packet packet, final ObjectOutputStream output)
*
* @param id the player ID that lost connection
*/
public synchronized void reconnect(int id)
public void reconnect(int id)
{
System.err.println("\033[1;33mConnectionNetworking: Adding ID to list\033[m");
Reconnector.getInstance(this).addID(id);
Reconnector.getInstance(this).deadIDs.notifyAll();
System.err.println("\033[1;33mConectionNetworking: Done adding ID to list\033[m");
}


Expand All @@ -574,7 +575,7 @@ public synchronized void reconnect(int id)
*
* @param id the player ID the connected
*/
public synchronized void connected(int id)
public void connected(int id)
{
Reconnector.getInstance(this).removeID(id);
}
Expand Down
175 changes: 107 additions & 68 deletions src/cnt/network/Reconnector.java
Expand Up @@ -28,7 +28,6 @@ public class Reconnector implements BlackboardObserver
private Reconnector(ConnectionNetworking connectionNetworking)
{
this.connectionNetworking = connectionNetworking;
this.reconnect();
}


Expand All @@ -51,7 +50,7 @@ private Reconnector(ConnectionNetworking connectionNetworking)
/**
* Set of IDs for the connections that are dead and we need to reconnect,
*/
private HashSet<Integer> deadIDs = new HashSet<Integer>();
public HashSet<Integer> deadIDs = new HashSet<Integer>();

/**
* Checks if a player has joined in a specefic timeframe
Expand Down Expand Up @@ -82,7 +81,6 @@ public static Reconnector getInstance(ConnectionNetworking connectionNetworking)
*/
{
Blackboard.registerObserver(this);
this.reconnect();
}

/**
Expand All @@ -94,8 +92,9 @@ public void addID(int id)
{
synchronized (this.deadIDs)
{
this.reconnect();
this.deadIDs.add(id);
this.deadIDs.notify();

}
}

Expand All @@ -113,7 +112,7 @@ public void removeID(int id)
/**
* Start trying to reconnect to IDs in the list.
*/
protected synchronized void reconnect()
protected void reconnect()
{
while (true)
{
Expand All @@ -126,14 +125,15 @@ protected synchronized void reconnect()
{ this.deadIDs.wait();
}
catch (final InterruptedException err)
{ return;
{
System.err.println("\033[1;31mReconnector: EXCEPTION ENCOUNTERED\033[0m");
}
}
System.err.println("\033[1;33mReconnector: DeadIDs is NOT empty\033[0m");
}


Blackboard.broadcastMessage(new EmergencyPause(true));
System.err.println("\033[1;33mReconnector: EmergencyPause in effect\033[0m");

/* Check if selfdead */
try
Expand All @@ -145,16 +145,24 @@ protected synchronized void reconnect()
Blackboard.broadcastMessage(new PlayerDropped(Player.getInstance(this.connectionNetworking.localID)));
return;
}
System.err.println("\033[1;33mReconnector:Not selfdead \033[0m");

if (this.deadIDs.contains(this.connectionNetworking.foreignID))
{
int id = this.connectionNetworking.foreignID; // less to type
Socket connection = null;

Socket dead = this.connectionNetworking.sockets.get(id);
Socket connection = this.connectionNetworking.connect((Inet4Address)(dead.getInetAddress()), dead.getPort(), true);
if (connection != null && handleConnection(connection, id))
continue;

System.err.println("\033[1;33mDead socket is " + (dead == null ? "\033[1;31mNull\033m" : "\033[1;32mOK\033[0m"));
if (dead != null)
{
connection = this.connectionNetworking.connect((Inet4Address)(dead.getInetAddress()), dead.getPort(), true);

if (connection != null && handleConnection(connection, id))
continue;
}

System.err.println("\033[1;33mReconnector: Couldn't connect back to our server\033[0m");

ArrayList<Player> players = new ArrayList<Player>();
for (int playerID : this.connectionNetworking.connectedIDs)
Expand All @@ -175,36 +183,44 @@ protected synchronized void reconnect()
continue;
}
}


System.err.println("\033[1;33mReconnector: Trying own network\033[0m");
System.err.println("\033[1;33mReconnector: Players empty: " + (players.isEmpty() ? "\033[1,31mEmpty\033[m" : "\033[1;32OK\033[0m"));
if (players.isEmpty() == false)
{

Collections.sort(players);
Player player = players.get(0);
try
{
connection = this.connectionNetworking.connect((Inet4Address)(InetAddress.getByName(player.getLocalIP())), player.getPort(), false);
} catch (Exception e)
{
connection = null;
}
if (connection != null && handleConnection(connection, player.getID()))
continue;
else

for (Player player : players)
{
try
{
connection = this.connectionNetworking.connect((Inet4Address)(InetAddress.getByName(player.getPublicIP())), player.getPort(), false);
} catch (Exception e) {
connection = this.connectionNetworking.connect((Inet4Address)(InetAddress.getByName(player.getLocalIP())), player.getPort(), false);
} catch (Exception e)
{
connection = null;
}
if (connection != null)
{
handleConnection(connection, player.getID());
if (connection != null && handleConnection(connection, player.getID()))
continue;
else
{
try
{
connection = this.connectionNetworking.connect((Inet4Address)(InetAddress.getByName(player.getPublicIP())), player.getPort(), false);
} catch (Exception e) {
connection = null;
}
if (connection != null)
{
handleConnection(connection, player.getID());
continue;
}
}
}
}


System.err.println("\033[1;33mReconnector: Couldn't connect on my network\033[0m");

for (int playerID : this.connectionNetworking.connectedIDs)
{
Player player = Player.getInstance(playerID);
Expand All @@ -223,24 +239,30 @@ protected synchronized void reconnect()
continue;
}
}


System.err.println("\033[1;33mReconnector: Trying other networks\033[0m");
System.err.println("\033[1;33mReconnector: Players empty: " + (players.isEmpty() ? "\033[1,31mEmpty\033[m" : "\033[1;32OK\033[0m"));
if (players.isEmpty() == false)
{
Collections.sort(players);
Player player = players.get(0);
try
{
connection = this.connectionNetworking.connect((Inet4Address)(InetAddress.getByName(player.getLocalIP())), player.getPort(), false);
} catch (Exception e)
{
connection = null;
}
if (connection != null)
{
handleConnection(connection, player.getID());
continue;
for (Player player : players)
{
try
{
connection = this.connectionNetworking.connect((Inet4Address)(InetAddress.getByName(player.getLocalIP())), player.getPort(), false);
} catch (Exception e)
{
connection = null;
}
if (connection != null)
{
handleConnection(connection, player.getID());
continue;
}
}
}

System.err.println("\033[1;33mReconnector: Couldnt connect to neighbour on other network\033[0m");

for (int playerID : this.connectionNetworking.connectedIDs)
{
Expand All @@ -260,36 +282,43 @@ protected synchronized void reconnect()
continue;
}
}

System.err.println("\033[1;33mReconnector: Trying strangers on my network\033[0m");
System.err.println("\033[1;33mReconnector: Players empty: " + (players.isEmpty() ? "\033[1,31mEmpty\033[m" : "\033[1;32OK\033[0m"));

if (players.isEmpty() == false)
{
Collections.sort(players);
Player player = players.get(0);
try
{
connection = this.connectionNetworking.connect((Inet4Address)(InetAddress.getByName(player.getLocalIP())), player.getPort(), false);
} catch (Exception e)
{
connection = null;
}
if (connection != null && handleConnection(connection, player.getID()))
continue;
else
for (Player player : players)
{
try
{
connection = this.connectionNetworking.connect((Inet4Address)(InetAddress.getByName(player.getPublicIP())), player.getPort(), false);
connection = this.connectionNetworking.connect((Inet4Address)(InetAddress.getByName(player.getLocalIP())), player.getPort(), false);
} catch (Exception e)
{
connection = null;
}
if (connection != null)
{
handleConnection(connection, player.getID());
if (connection != null && handleConnection(connection, player.getID()))
continue;
else
{
try
{
connection = this.connectionNetworking.connect((Inet4Address)(InetAddress.getByName(player.getPublicIP())), player.getPort(), false);
} catch (Exception e)
{
connection = null;
}
if (connection != null)
{
handleConnection(connection, player.getID());
continue;
}
}
}
}

System.err.println("\033[1;33mReconnector: Couldn't connect to NON neighbour on other network\033[0m");

for (int playerID : this.connectionNetworking.connectedIDs)
{
Expand All @@ -309,24 +338,32 @@ protected synchronized void reconnect()
continue;
}
}

System.err.println("\033[1;33mReconnector: Trying total strangers\033[0m");
System.err.println("\033[1;33mReconnector: Players empty: " + (players.isEmpty() ? "\033[1,31mEmpty\033[m" : "\033[1;32OK\033[0m"));

if (players.isEmpty() == false)
{
Collections.sort(players);
Player player = players.get(0);
try
{
connection = this.connectionNetworking.connect((Inet4Address)(InetAddress.getByName(player.getLocalIP())), player.getPort(), false);
} catch (Exception e)
{
connection = null;
}
if (connection != null)
for (Player player : players)
{
handleConnection(connection, player.getID());
continue;
try
{
connection = this.connectionNetworking.connect((Inet4Address)(InetAddress.getByName(player.getLocalIP())), player.getPort(), false);
} catch (Exception e)
{
connection = null;
}
if (connection != null)
{
handleConnection(connection, player.getID());
continue;
}
}
}

System.err.println("\033[1;33mReconnector: All is lost\033[0m");

}

for(;;)
Expand All @@ -336,6 +373,7 @@ protected synchronized void reconnect()
this.playerJoined = false;
try
{
System.err.println("\033[1;33mReconnector: timeout 2 sec in effect\033[0m");
joined.wait(2000);
} catch (InterruptedException ie)
{
Expand All @@ -356,6 +394,7 @@ protected synchronized void reconnect()
}
}
}
System.err.println("\033[1;33mReconnector: We are done\033[0m");

}

Expand Down
1 change: 0 additions & 1 deletion src/cnt/network/TCPReceiver.java
Expand Up @@ -205,7 +205,6 @@ else if (packet.getMessage().getMessage() instanceof ConnectionMessage)

} catch (IOException ioe)
{
if (this.foreignID < this.connectionNetworking.localID)
this.connectionNetworking.reconnect(this.foreignID);
} catch (Exception err)
{
Expand Down

0 comments on commit 5579739

Please sign in to comment.