Add: JavaDOC in ServerLauncher
Fix: Action error handling in the entire network stack Modified: TCP's broadcast methods name
This commit is contained in:
@@ -1,7 +1,6 @@
|
||||
package it.polimi.ingsw.gc14.Network;
|
||||
|
||||
import it.polimi.ingsw.gc14.Controller.GameController;
|
||||
import javafx.event.Event;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
@@ -12,9 +11,23 @@ public abstract class NetworkEvent implements Serializable {
|
||||
}
|
||||
protected EventType eventType;
|
||||
public EventType getEventType() {return eventType;}
|
||||
protected NetworkEvent(String username, EventType eventType) {
|
||||
protected boolean isError;
|
||||
public boolean getIsError() {return isError;}
|
||||
public void setIsError(boolean isError) {this.isError = isError;}
|
||||
|
||||
protected NetworkEvent(String username, EventType eventType, boolean isError) {
|
||||
this.username = username;
|
||||
this.eventType = eventType;
|
||||
this.isError = isError;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
if(isError) {
|
||||
return ("ERROR: action " + eventType.toString());
|
||||
} else {
|
||||
return ("ACTION: action " + eventType.toString());
|
||||
}
|
||||
}
|
||||
|
||||
public abstract boolean apply(GameController gameController);
|
||||
|
||||
@@ -23,13 +23,11 @@ public class ClientCallbackImpl extends UnicastRemoteObject implements IClientCa
|
||||
|
||||
@Override
|
||||
public void onAction(NetworkEvent event) throws RemoteException {
|
||||
event.apply(clientController.localController); // delega tutto al controller
|
||||
//clientController.view.update(); TODO
|
||||
if(event.getIsError()) {
|
||||
System.out.println(event.toString());
|
||||
} else {
|
||||
event.apply(clientController.localController); // delega tutto al controller
|
||||
//clientController.view.update(); TODO
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onError(String message) throws RemoteException {
|
||||
clientController.onError(message);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -8,5 +8,4 @@ import java.rmi.*;
|
||||
public interface IClientCallback extends Remote {
|
||||
void onGameInit(Game model) throws RemoteException;
|
||||
void onAction(NetworkEvent action) throws RemoteException;
|
||||
void onError(String message) throws RemoteException;
|
||||
}
|
||||
@@ -83,10 +83,6 @@ public class RMIServer implements IGameServer {
|
||||
cb.onGameInit(model);
|
||||
}
|
||||
}
|
||||
public void notifyError(String username, String message) throws RemoteException {
|
||||
IClientCallback cb = clients.get(username);
|
||||
if (cb != null) cb.onError(message);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package it.polimi.ingsw.gc14.Network.TCP.Client;
|
||||
|
||||
import it.polimi.ingsw.gc14.Controller.GameController;
|
||||
import it.polimi.ingsw.gc14.Model.Game;
|
||||
import it.polimi.ingsw.gc14.Network.NetworkEvent;
|
||||
import it.polimi.ingsw.gc14.Network.NetworkEvents.AddPlayer;
|
||||
|
||||
@@ -46,7 +47,21 @@ public class TCPClient implements Serializable{
|
||||
private void ReceiveMessage(){
|
||||
while(true){
|
||||
try{
|
||||
((NetworkEvent)(socketReceive.readObject())).apply(controller);
|
||||
Object read = socketReceive.readObject();
|
||||
|
||||
if (read instanceof NetworkEvent) { //TODO non fare con instanceof
|
||||
NetworkEvent event = (NetworkEvent) read;
|
||||
if(event.getIsError()) {
|
||||
System.out.println(event.toString());
|
||||
} else {
|
||||
event.apply(controller);
|
||||
//clientController.view.update(); TODO
|
||||
}
|
||||
}
|
||||
else if (read instanceof Game) {
|
||||
controller.setModel((Game) read);
|
||||
}
|
||||
|
||||
}
|
||||
catch(IOException e){
|
||||
e.printStackTrace();
|
||||
|
||||
@@ -43,8 +43,8 @@ public class ClientHandler implements Runnable {
|
||||
while(true){
|
||||
try{
|
||||
input = (NetworkEvent) (in.readObject());
|
||||
if(actionQueue.add(input)){
|
||||
server.broadcastUpdate(input);
|
||||
if(!actionQueue.add(input)){
|
||||
System.out.println("An error occurred in inserting an action into queue");
|
||||
}
|
||||
}
|
||||
catch(java.io.IOException e){
|
||||
@@ -53,8 +53,6 @@ public class ClientHandler implements Runnable {
|
||||
catch (ClassNotFoundException e){
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
catch (IOException e) {
|
||||
|
||||
@@ -118,11 +118,11 @@ public class TCPServer {
|
||||
this.playerList = players;
|
||||
}
|
||||
|
||||
public void broadcastUpdate(NetworkEvent event){
|
||||
public void notifAll(NetworkEvent event){
|
||||
clientHandlers.forEach((x) -> x.notifyEvent(event));
|
||||
}
|
||||
|
||||
public void broadcastModel(Game model){
|
||||
public void notifyAll(Game model){
|
||||
clientHandlers.forEach((x) -> x.notifyModel(model));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user