Fixed: Some exception not managed fixed
This commit is contained in:
@@ -8,26 +8,26 @@ import java.util.Objects;
|
||||
public interface IClient {
|
||||
public boolean connect(String username,int preferredInt);
|
||||
|
||||
public void drawUpperTribeCard(String playerUsername, int pos) throws RemoteException;
|
||||
public void drawUpperTribeCard(String playerUsername, int pos) ;
|
||||
|
||||
public void drawLowerTribeCard(String playerUsername,int pos) throws RemoteException;
|
||||
public void drawLowerTribeCard(String playerUsername,int pos) ;
|
||||
|
||||
public void drawUpperBuildingCard(String playerUsername,int pos) throws RemoteException;
|
||||
public void drawUpperBuildingCard(String playerUsername,int pos) ;
|
||||
|
||||
|
||||
public void drawLowerBuildingCard(String playerUsername,int pos) throws RemoteException;
|
||||
public void drawLowerBuildingCard(String playerUsername,int pos) ;
|
||||
|
||||
public void skipTurn(String playerUsername) throws RemoteException;
|
||||
|
||||
|
||||
|
||||
public void pickOptionalTribeCard(String playerUsername,int pos) throws RemoteException;
|
||||
public void pickOptionalTribeCard(String playerUsername,int pos) ;
|
||||
|
||||
public void pickOptionalBuildingCard(String playerUsername,int pos) throws RemoteException;
|
||||
public void pickOptionalBuildingCard(String playerUsername,int pos) ;
|
||||
|
||||
|
||||
public void noOptionalCard(String playerUsername) throws RemoteException;
|
||||
public void noOptionalCard(String playerUsername) ;
|
||||
|
||||
public void slotChoice(String playerUsername,int pos) throws RemoteException;
|
||||
public void totemChoice(String playerUsername,String totem) throws RemoteException;
|
||||
public void slotChoice(String playerUsername,int pos) ;
|
||||
public void totemChoice(String playerUsername,String totem) ;
|
||||
}
|
||||
|
||||
@@ -134,8 +134,13 @@ public class RMIClient implements IClient {
|
||||
* @param playerUsername the name of the player performing the action
|
||||
* @param pos the index of the card to draw
|
||||
*/
|
||||
public void drawUpperTribeCard(String playerUsername, int pos) throws RemoteException {
|
||||
stub.drawUpperTribeCard(playerUsername,pos);
|
||||
public void drawUpperTribeCard(String playerUsername, int pos) {
|
||||
try{
|
||||
stub.drawUpperTribeCard(playerUsername,pos);
|
||||
}
|
||||
catch (RemoteException e){
|
||||
System.out.println("Error during remote draw upper tribe card");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -145,8 +150,13 @@ public class RMIClient implements IClient {
|
||||
* @param playerUsername the name of the player performing the action
|
||||
* @param pos the index of the card to draw
|
||||
*/
|
||||
public void drawLowerTribeCard(String playerUsername,int pos) throws RemoteException {
|
||||
stub.drawLowerTribeCard(playerUsername,pos);
|
||||
public void drawLowerTribeCard(String playerUsername,int pos) {
|
||||
try{
|
||||
stub.drawLowerTribeCard(playerUsername,pos);
|
||||
}
|
||||
catch (RemoteException e){
|
||||
System.out.println("Error during remote draw lower tribe card");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -156,8 +166,13 @@ public class RMIClient implements IClient {
|
||||
* @param playerUsername the name of the player performing the action
|
||||
* @param pos the index of the card to draw
|
||||
*/
|
||||
public void drawUpperBuildingCard(String playerUsername,int pos) throws RemoteException {
|
||||
stub.drawUpperBuildingCard(playerUsername,pos);
|
||||
public void drawUpperBuildingCard(String playerUsername,int pos) {
|
||||
try{
|
||||
stub.drawUpperBuildingCard(playerUsername,pos);
|
||||
}
|
||||
catch (RemoteException e){
|
||||
System.out.println("Error during remote draw upper building card");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -167,8 +182,13 @@ public class RMIClient implements IClient {
|
||||
* @param playerUsername the name of the player performing the action
|
||||
* @param pos the index of the card to draw
|
||||
*/
|
||||
public void drawLowerBuildingCard(String playerUsername,int pos) throws RemoteException {
|
||||
stub.drawLowerBuildingCard(playerUsername,pos);
|
||||
public void drawLowerBuildingCard(String playerUsername,int pos) {
|
||||
try{
|
||||
stub.drawLowerBuildingCard(playerUsername,pos);
|
||||
}
|
||||
catch (RemoteException e){
|
||||
System.out.println("Error during remote draw lower building card");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -177,8 +197,13 @@ public class RMIClient implements IClient {
|
||||
* This action is available only when the player cannot draw any tribe card, but still can buy some buildings.
|
||||
* @param playerUsername the name of the player performing the action
|
||||
*/
|
||||
public void skipTurn(String playerUsername) throws RemoteException {
|
||||
stub.skipTurn(playerUsername);
|
||||
public void skipTurn(String playerUsername) {
|
||||
try{
|
||||
stub.skipTurn(playerUsername);
|
||||
}
|
||||
catch (RemoteException e){
|
||||
System.out.println("Error during remote skip turn");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -190,8 +215,13 @@ public class RMIClient implements IClient {
|
||||
* @param playerUsername the name of the player performing the action
|
||||
* @param pos the index of the card to draw
|
||||
*/
|
||||
public void pickOptionalTribeCard(String playerUsername,int pos) throws RemoteException {
|
||||
stub.pickOptionalTribeCard(playerUsername,pos);
|
||||
public void pickOptionalTribeCard(String playerUsername,int pos) {
|
||||
try{
|
||||
stub.pickOptionalTribeCard(playerUsername,pos);
|
||||
}
|
||||
catch (RemoteException e){
|
||||
System.out.println("Error during remote pick optional tribe card");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -201,8 +231,13 @@ public class RMIClient implements IClient {
|
||||
* @param playerUsername the name of the player performing the action
|
||||
* @param pos the index of the card to draw
|
||||
*/
|
||||
public void pickOptionalBuildingCard(String playerUsername,int pos) throws RemoteException {
|
||||
stub.pickOptionalBuildingCard(playerUsername,pos);
|
||||
public void pickOptionalBuildingCard(String playerUsername,int pos) {
|
||||
try{
|
||||
stub.pickOptionalBuildingCard(playerUsername,pos);
|
||||
}
|
||||
catch (RemoteException e){
|
||||
System.out.println("Error during remote pick optional building card");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -211,8 +246,13 @@ public class RMIClient implements IClient {
|
||||
* Available only if the player owns the building 12.
|
||||
* @param playerUsername the name of the player performing the action
|
||||
*/
|
||||
public void noOptionalCard(String playerUsername) throws RemoteException {
|
||||
stub.noOptionalCard(playerUsername);
|
||||
public void noOptionalCard(String playerUsername) {
|
||||
try{
|
||||
stub.noOptionalCard(playerUsername);
|
||||
}
|
||||
catch (RemoteException e){
|
||||
System.out.println("Error during remote no pick optional card");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -221,12 +261,22 @@ public class RMIClient implements IClient {
|
||||
* @param playerUsername the name of the player performing the action
|
||||
* @param pos the index of the selected slot
|
||||
*/
|
||||
public void slotChoice(String playerUsername,int pos) throws RemoteException {
|
||||
stub.slotChoice(playerUsername,pos);
|
||||
public void slotChoice(String playerUsername,int pos) {
|
||||
try{
|
||||
stub.slotChoice(playerUsername,pos);
|
||||
}
|
||||
catch (RemoteException e){
|
||||
System.out.println("Error during remote slot choice");
|
||||
}
|
||||
}
|
||||
//TODO
|
||||
public void totemChoice(String playerUsername,String totem) throws RemoteException {
|
||||
stub.totemChoice(playerUsername,totem);
|
||||
public void totemChoice(String playerUsername,String totem) {
|
||||
try{
|
||||
stub.totemChoice(playerUsername,totem);
|
||||
}
|
||||
catch (RemoteException e){
|
||||
System.out.println("Error during remote totem choice");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -153,11 +153,17 @@ public class RMIServer extends UnicastRemoteObject implements IGameServer {
|
||||
* Also updates every watchdog with the latest model so disconnect logic
|
||||
* knows whose turn it is.
|
||||
*/
|
||||
public void notifyAll(NetworkEvent action) throws RemoteException {
|
||||
public void notifyAll(NetworkEvent action){
|
||||
for (Map.Entry<String, IClientCallback> entry : clients.entrySet()) {
|
||||
if (!action.getIsError() ||
|
||||
(action.getIsError() && action.getUsername().equals(entry.getKey()))) {
|
||||
entry.getValue().onAction(action);
|
||||
try{
|
||||
entry.getValue().onAction(action);
|
||||
}
|
||||
catch (RemoteException e)
|
||||
{
|
||||
System.out.println("Remote exception: Exception during action sending attempt " + e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -167,13 +173,19 @@ public class RMIServer extends UnicastRemoteObject implements IGameServer {
|
||||
* Mirrors {@code TCPServer.notifyAll(Game)} + the {@code ClientHandler.notifyModel}
|
||||
* call that stores the model for disconnect-turn checking.
|
||||
*/
|
||||
public void notifyAll(MiniModel model) throws RemoteException {
|
||||
public void notifyAll(MiniModel model) {
|
||||
// Keep every watchdog's game reference up to date
|
||||
synchronized (controller){
|
||||
watchdogs.values().forEach(wd -> wd.setGame(controller.getModel()));
|
||||
}
|
||||
for (IClientCallback cb : clients.values()) {
|
||||
cb.onGameInit(model);
|
||||
try{
|
||||
cb.onGameInit(model);
|
||||
}
|
||||
catch (RemoteException e)
|
||||
{
|
||||
System.out.println("Remote exception: Exception during model sending attempt " + e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -69,12 +69,10 @@ public class TCPClient implements IClient {
|
||||
*/
|
||||
public boolean connect(String user, int proposedNPlayers) {
|
||||
try {
|
||||
// Socket principale
|
||||
communicationSocket = new Socket(hostname, mainPort);
|
||||
socketSend = new ObjectOutputStream(communicationSocket.getOutputStream());
|
||||
socketReceive = new ObjectInputStream(communicationSocket.getInputStream());
|
||||
NetworkEvent event= new AddPlayer(user, proposedNPlayers);
|
||||
System.out.println("Sending event: " + event);
|
||||
socketSend.writeObject(event);
|
||||
int read= communicationSocket.getInputStream().read();
|
||||
if ( read== -1) {
|
||||
@@ -94,8 +92,6 @@ public class TCPClient implements IClient {
|
||||
new Thread(this::heartbeatLoop, "heartbeat").start();
|
||||
|
||||
running = true;
|
||||
|
||||
|
||||
return true;
|
||||
|
||||
} catch (IOException e) {
|
||||
@@ -105,7 +101,6 @@ public class TCPClient implements IClient {
|
||||
}
|
||||
|
||||
private void heartbeatLoop() {
|
||||
// thread che manda ping ogni 3s
|
||||
ScheduledExecutorService sender = Executors.newSingleThreadScheduledExecutor();
|
||||
sender.scheduleAtFixedRate(() -> {
|
||||
try {
|
||||
@@ -117,7 +112,6 @@ public class TCPClient implements IClient {
|
||||
}
|
||||
}, 0, 3, TimeUnit.SECONDS);
|
||||
|
||||
// thread corrente: aspetta pong con timeout
|
||||
try {
|
||||
heartbeatSocket.setSoTimeout(5_000);
|
||||
while (running) {
|
||||
@@ -142,7 +136,7 @@ public class TCPClient implements IClient {
|
||||
running = false;
|
||||
try { communicationSocket.close(); } catch (IOException ignored) {}
|
||||
try { heartbeatSocket.close(); } catch (IOException ignored) {}
|
||||
controller.view.showError("Connessione al server persa");
|
||||
controller.view.showError("Connection with server lost");
|
||||
}
|
||||
|
||||
|
||||
@@ -162,7 +156,7 @@ public class TCPClient implements IClient {
|
||||
e.printStackTrace();
|
||||
break;
|
||||
}
|
||||
if (read instanceof NetworkEvent event) { //TODO: avoid instanceof
|
||||
if (read instanceof NetworkEvent event) {
|
||||
if (event.getIsError()) {
|
||||
controller.view.showError(event.toString());
|
||||
} else {
|
||||
@@ -285,7 +279,6 @@ public class TCPClient implements IClient {
|
||||
private void doEvent(NetworkEvent event) {
|
||||
try {
|
||||
synchronized (socketSend) {
|
||||
System.out.println("Sending event: " + event);
|
||||
socketSend.writeObject(event);
|
||||
}
|
||||
} catch (IOException e) {
|
||||
|
||||
Reference in New Issue
Block a user