Changed: Now Disconnection request can be sent , (TUI,RMI,TCP)
This commit is contained in:
@@ -70,4 +70,7 @@ public interface IClient {
|
||||
* @param totem the name of the selected totem.
|
||||
*/
|
||||
void totemChoice(String playerUsername, String totem);
|
||||
|
||||
//TODO
|
||||
void notifyDisconnection();
|
||||
}
|
||||
|
||||
@@ -124,7 +124,7 @@ public class RMIClient implements IClient {
|
||||
if (!running) return;
|
||||
running = false;
|
||||
if (pingSender != null) pingSender.shutdownNow();
|
||||
controller.view.showError("Connessione al server persa");
|
||||
controller.view.showError("Connection with server closed");
|
||||
}
|
||||
|
||||
|
||||
@@ -235,5 +235,15 @@ public class RMIClient implements IClient {
|
||||
}
|
||||
}
|
||||
|
||||
public void notifyDisconnection() {
|
||||
pingSender.shutdownNow();
|
||||
try{
|
||||
stub.disconnectPlayer(username);
|
||||
}
|
||||
catch (RemoteException e){
|
||||
System.out.println("Error during remote disconnection");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -93,4 +93,6 @@ public interface IGameServer extends Remote {
|
||||
* @throws RemoteException if an RMI communication error occurs.
|
||||
*/
|
||||
void ping(String username) throws RemoteException;
|
||||
|
||||
void disconnectPlayer(String username) throws RemoteException;
|
||||
}
|
||||
@@ -89,7 +89,7 @@ public class RMIHeartbeat {
|
||||
* the associated RMI callback is removed, and a disconnection event is
|
||||
* added to the action queue for server-side processing.
|
||||
*/
|
||||
private void disconnect() {
|
||||
public void disconnect() {
|
||||
if (!running) return;
|
||||
running = false;
|
||||
watchdog.shutdownNow();
|
||||
|
||||
@@ -332,6 +332,10 @@ public class RMIServer extends UnicastRemoteObject implements IGameServer {
|
||||
watchdogs.put(username, wd);
|
||||
wd.start();
|
||||
}
|
||||
public void disconnectPlayer(String username) {
|
||||
RMIHeartbeat wd = watchdogs.get(username);
|
||||
wd.disconnect();
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -107,8 +107,12 @@ public class TCPClient implements IClient {
|
||||
ScheduledExecutorService sender = Executors.newSingleThreadScheduledExecutor();
|
||||
sender.scheduleAtFixedRate(() -> {
|
||||
try {
|
||||
heartbeatOut.write(PING);
|
||||
heartbeatOut.flush();
|
||||
synchronized (heartbeatOut)
|
||||
{
|
||||
heartbeatOut.write(PING);
|
||||
heartbeatOut.flush();
|
||||
}
|
||||
|
||||
} catch (IOException e) {
|
||||
sender.shutdownNow();
|
||||
disconnect();
|
||||
@@ -263,4 +267,17 @@ public class TCPClient implements IClient {
|
||||
public void totemChoice(String playerUsername,String totems) {
|
||||
doEvent(new TotemChoice(playerUsername,totems));
|
||||
}
|
||||
|
||||
public void notifyDisconnection()
|
||||
{
|
||||
synchronized (heartbeatOut)
|
||||
{
|
||||
try {
|
||||
heartbeatOut.write(-1);
|
||||
} catch (IOException e) {
|
||||
disconnect();
|
||||
}
|
||||
}
|
||||
disconnect();
|
||||
}
|
||||
}
|
||||
@@ -94,10 +94,9 @@ public class ClientHandler implements Runnable {
|
||||
}
|
||||
}
|
||||
} catch (IOException e) {
|
||||
clientHandlers.remove(this);
|
||||
e.printStackTrace();
|
||||
System.err.println("Error in incoming connection/disconnection");
|
||||
} catch (ClassNotFoundException e) {
|
||||
throw new RuntimeException(e);
|
||||
System.err.println("Class not found");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -94,7 +94,13 @@ public class HeartbeatHandler implements Runnable {
|
||||
try {
|
||||
while (running) {
|
||||
int b = in.read(); // blocca finché non arriva un byte
|
||||
if (b == -1) { disconnect(); break; } // stream chiusa
|
||||
if (b == -1) {
|
||||
synchronized (this)
|
||||
{
|
||||
out.write(-1);
|
||||
}
|
||||
disconnect(); break;
|
||||
} // stream chiusa
|
||||
if (b == PING) {
|
||||
lastReceivedTime = System.currentTimeMillis();
|
||||
out.write(PONG);
|
||||
@@ -127,7 +133,7 @@ public class HeartbeatHandler implements Runnable {
|
||||
* <p>The handler is stopped, the watchdog task is terminated, the main
|
||||
* {@link ClientHandler} is disconnected, and the heartbeat socket is closed.
|
||||
*/
|
||||
private void disconnect() {
|
||||
private synchronized void disconnect() {
|
||||
running = false;
|
||||
watchdog.shutdownNow();
|
||||
mainHandler.disconnect(); // disconnette anche il socket principale
|
||||
|
||||
Reference in New Issue
Block a user