patch crash serveur
This commit is contained in:
@@ -97,7 +97,7 @@ import java.util.List;
|
|||||||
public class CardRegistery {
|
public class CardRegistery {
|
||||||
public static List<Class<? extends Card>> registry;
|
public static List<Class<? extends Card>> registry;
|
||||||
|
|
||||||
public CardRegistery()
|
private static void initCardRegistery()
|
||||||
{
|
{
|
||||||
registry = new ArrayList<>();
|
registry = new ArrayList<>();
|
||||||
|
|
||||||
@@ -205,6 +205,7 @@ public class CardRegistery {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static int indexOf(Class<? extends Card> class1) {
|
public static int indexOf(Class<? extends Card> class1) {
|
||||||
|
if(registry == null)initCardRegistery();
|
||||||
int a = registry.indexOf(class1);
|
int a = registry.indexOf(class1);
|
||||||
|
|
||||||
if(a == -1){
|
if(a == -1){
|
||||||
@@ -216,12 +217,14 @@ public class CardRegistery {
|
|||||||
|
|
||||||
|
|
||||||
public static Class<? extends Card> get(int index) {
|
public static Class<? extends Card> get(int index) {
|
||||||
|
if(registry == null)initCardRegistery();
|
||||||
if(registry.size() <= index || index < 0) return null;
|
if(registry.size() <= index || index < 0) return null;
|
||||||
return registry.get(index);
|
return registry.get(index);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public static int indexOf(Card card) {
|
public static int indexOf(Card card) {
|
||||||
|
if(registry == null)initCardRegistery();
|
||||||
return indexOf(card.getClass());
|
return indexOf(card.getClass());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ package com.iutlaval.myapplication.Game;
|
|||||||
public final class Command {
|
public final class Command {
|
||||||
|
|
||||||
public static final String UPDATE = "update";
|
public static final String UPDATE = "update";
|
||||||
|
public static final String PONG = "pong";
|
||||||
|
|
||||||
private Command(){} // pas de contructor public on ne veut pas instancier cette classe
|
private Command(){} // pas de contructor public on ne veut pas instancier cette classe
|
||||||
public static final String YOURTURN="yourturn";
|
public static final String YOURTURN="yourturn";
|
||||||
|
|||||||
@@ -0,0 +1,95 @@
|
|||||||
|
package com.iutlaval.myapplication.Game;
|
||||||
|
|
||||||
|
import android.util.Log;
|
||||||
|
|
||||||
|
import com.iutlaval.myapplication.exception.NotAnIntegerException;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.ObjectInputStream;
|
||||||
|
import java.io.ObjectOutputStream;
|
||||||
|
import java.net.Socket;
|
||||||
|
import java.net.SocketException;
|
||||||
|
|
||||||
|
public class Communication {
|
||||||
|
private static final short MAX_ACCEPTABLE_PING = 500;
|
||||||
|
public static final String TIMEOUT = "timeout";
|
||||||
|
public static final String ERROR = "ERROR";
|
||||||
|
|
||||||
|
private ObjectInputStream clientIn;
|
||||||
|
private ObjectOutputStream clientOut;
|
||||||
|
private Socket client;
|
||||||
|
|
||||||
|
public Communication(Socket client) throws IOException {
|
||||||
|
clientIn = new ObjectInputStream(client.getInputStream());
|
||||||
|
clientOut = new ObjectOutputStream(client.getOutputStream());
|
||||||
|
this.client=client;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* on met un delais infini d'attente de réponse
|
||||||
|
* @throws SocketException
|
||||||
|
*/
|
||||||
|
public void setUnlimitedTimeOut() throws SocketException {
|
||||||
|
//ce n'est pas réelement infini juste trés grand
|
||||||
|
client.setSoTimeout(Integer.MAX_VALUE);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* on met le delais défini par la constante MAX_ACCEPTABLE_PING en tant que delais de reponse
|
||||||
|
* @throws SocketException
|
||||||
|
*/
|
||||||
|
public void setLimitedTimeOut() throws SocketException {
|
||||||
|
client.setSoTimeout(MAX_ACCEPTABLE_PING);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* on envoie que des chaines de caractére car on na pas euh de cours sur le reseau et que l'envoie d'entier a déja causé de nombreux probléme
|
||||||
|
* @param message
|
||||||
|
*/
|
||||||
|
public void send(String message) throws IOException {
|
||||||
|
clientOut.writeObject(message);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* redéfinitoin de send() qui convertie l'entier recu en chaine de caractére
|
||||||
|
* @param message
|
||||||
|
* @throws IOException
|
||||||
|
*/
|
||||||
|
public void send(int message) throws IOException {
|
||||||
|
send(""+message);
|
||||||
|
}
|
||||||
|
|
||||||
|
public String recieve(){
|
||||||
|
try {
|
||||||
|
return (String) clientIn.readObject();
|
||||||
|
} catch (ClassNotFoundException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
//on crash
|
||||||
|
return ERROR;
|
||||||
|
} catch (IOException e) {
|
||||||
|
return TIMEOUT;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public int recieveInt()
|
||||||
|
{
|
||||||
|
String message = recieve();
|
||||||
|
|
||||||
|
if(message.matches("-?\\d+"))
|
||||||
|
{
|
||||||
|
return Integer.parseInt(message);
|
||||||
|
}else{
|
||||||
|
if(!(message.equals(TIMEOUT)||message.equals(ERROR)))
|
||||||
|
{
|
||||||
|
throw new NotAnIntegerException();
|
||||||
|
}
|
||||||
|
return Integer.MIN_VALUE;//retourne la valeur la plus petite en guise de message d'erreur
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void close() throws IOException {
|
||||||
|
if(clientOut != null)clientOut.close();
|
||||||
|
if(clientIn != null)clientIn.close();
|
||||||
|
if(client != null)client.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -39,17 +39,16 @@ public class GameLogicThread extends Thread{
|
|||||||
private boolean ready;
|
private boolean ready;
|
||||||
private TouchHandler touch;
|
private TouchHandler touch;
|
||||||
private GameActivity gameActivity;
|
private GameActivity gameActivity;
|
||||||
private ObjectInputStream clientIn=null;
|
|
||||||
private ObjectOutputStream clientOut=null;
|
|
||||||
|
|
||||||
private NetworkDeck deck;
|
private NetworkDeck deck;
|
||||||
private Hand hand;
|
private Hand hand;
|
||||||
private Board board;
|
private Board board;
|
||||||
private boolean isYourTurn;
|
private boolean isYourTurn;
|
||||||
private boolean cancelled;
|
private boolean cancelled;
|
||||||
private Socket client;
|
|
||||||
private String deckName;
|
private String deckName;
|
||||||
private int mana=0;
|
private int mana=0;
|
||||||
|
private Communication coms;
|
||||||
|
|
||||||
|
|
||||||
//ces variable permette la comunication avec le thread android du tactile et le reseau en passant par ce thread
|
//ces variable permette la comunication avec le thread android du tactile et le reseau en passant par ce thread
|
||||||
@@ -61,7 +60,6 @@ public class GameLogicThread extends Thread{
|
|||||||
|
|
||||||
public GameLogicThread(GameActivity gameActivity, String deckName, Renderer renderer)
|
public GameLogicThread(GameActivity gameActivity, String deckName, Renderer renderer)
|
||||||
{
|
{
|
||||||
new CardRegistery();
|
|
||||||
this.deckName=deckName;
|
this.deckName=deckName;
|
||||||
Log.e("RESOLUTION:",""+GameActivity.screenWidth+"x"+GameActivity.screenHeight);
|
Log.e("RESOLUTION:",""+GameActivity.screenWidth+"x"+GameActivity.screenHeight);
|
||||||
ready=false;
|
ready=false;
|
||||||
@@ -92,13 +90,12 @@ public class GameLogicThread extends Thread{
|
|||||||
//Rectangle pos = new Rectangle(0F,0F,100F,100F);
|
//Rectangle pos = new Rectangle(0F,0F,100F,100F);
|
||||||
ready=true;
|
ready=true;
|
||||||
|
|
||||||
final String host = "4.tcp.ngrok.io";//192.168.43.251tcp://2.tcp.ngrok.io:
|
final String host = "0.tcp.ngrok.io";//192.168.43.251tcp://2.tcp.ngrok.io:
|
||||||
final int port = 19165;
|
final int port = 13562;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
client = new Socket(host, port);
|
Socket client = new Socket(host, port);
|
||||||
clientIn = new ObjectInputStream(client.getInputStream());
|
coms= new Communication(client);
|
||||||
clientOut = new ObjectOutputStream(client.getOutputStream());
|
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
Log.e("ERROR SERVER DEAD","srv");
|
Log.e("ERROR SERVER DEAD","srv");
|
||||||
//TODO display error and don't crash the game;
|
//TODO display error and don't crash the game;
|
||||||
@@ -113,7 +110,7 @@ public class GameLogicThread extends Thread{
|
|||||||
if(!requestDone && requestZone != -1 && requestCard != null)
|
if(!requestDone && requestZone != -1 && requestCard != null)
|
||||||
{
|
{
|
||||||
try {//on allonge le delais car sinon on skip l'autorisation serveur
|
try {//on allonge le delais car sinon on skip l'autorisation serveur
|
||||||
client.setSoTimeout(Integer.MAX_VALUE);
|
coms.setUnlimitedTimeOut();
|
||||||
} catch (SocketException e) {}
|
} catch (SocketException e) {}
|
||||||
requestResult = onCardPlayed(requestCard,requestZone);
|
requestResult = onCardPlayed(requestCard,requestZone);
|
||||||
requestZone=-1;
|
requestZone=-1;
|
||||||
@@ -125,28 +122,20 @@ public class GameLogicThread extends Thread{
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
//on limite le temps du readobject pour eviter les blockaeg
|
//on limite le temps du readobject pour eviter les blockaeg
|
||||||
client.setSoTimeout(1000);
|
coms.setLimitedTimeOut();
|
||||||
String serveurCmd = (String)clientIn.readObject();
|
String serveurCmd = coms.recieve();
|
||||||
client.setSoTimeout(Integer.MAX_VALUE);
|
coms.setUnlimitedTimeOut();
|
||||||
Log.e("command",serveurCmd);
|
Log.e("command",serveurCmd);
|
||||||
switch (serveurCmd)
|
switch (serveurCmd)
|
||||||
{
|
{
|
||||||
case Command.GET_DECK:
|
case Command.GET_DECK:
|
||||||
clientOut.writeObject(deckName);
|
coms.send(deckName);
|
||||||
String deckstr = (String)clientIn.readObject();
|
String deckstr = coms.recieve();
|
||||||
deck = new NetworkDeck(deckstr,gameActivity.getBaseContext());
|
deck = new NetworkDeck(deckstr,gameActivity.getBaseContext());
|
||||||
Log.e("got deck",deckstr);
|
Log.e("got deck",deckstr);
|
||||||
break;
|
break;
|
||||||
case Command.DRAW:
|
case Command.DRAW:
|
||||||
Object o = clientIn.readObject();
|
int nbcard = coms.recieveInt();
|
||||||
int nbcard;
|
|
||||||
if(o instanceof String)
|
|
||||||
{
|
|
||||||
nbcard = Integer.parseInt((String) o);
|
|
||||||
}else{
|
|
||||||
nbcard = (Integer)o;
|
|
||||||
}
|
|
||||||
|
|
||||||
hand.pickCardFromDeck(deck,nbcard);
|
hand.pickCardFromDeck(deck,nbcard);
|
||||||
Log.e("picked",nbcard+"card");
|
Log.e("picked",nbcard+"card");
|
||||||
drawHandPreview();
|
drawHandPreview();
|
||||||
@@ -160,7 +149,7 @@ public class GameLogicThread extends Thread{
|
|||||||
|
|
||||||
case Command.SETMANA:
|
case Command.SETMANA:
|
||||||
renderer.removeToDraw("mana");
|
renderer.removeToDraw("mana");
|
||||||
mana = (int)clientIn.readObject();
|
mana = coms.recieveInt();
|
||||||
renderer.addToDraw(new DrawableText("Mana : "+mana,90F,90F,"mana",10F,10F,100,800,200));
|
renderer.addToDraw(new DrawableText("Mana : "+mana,90F,90F,"mana",10F,10F,100,800,200));
|
||||||
break;
|
break;
|
||||||
|
|
||||||
@@ -169,7 +158,7 @@ public class GameLogicThread extends Thread{
|
|||||||
break;
|
break;
|
||||||
|
|
||||||
case Command.POPUP:
|
case Command.POPUP:
|
||||||
String recivedMessage = (String)clientIn.readObject();
|
String recivedMessage = coms.recieve();
|
||||||
Toast popupToast = new Toast(gameActivity);
|
Toast popupToast = new Toast(gameActivity);
|
||||||
popupToast.setText(recivedMessage);
|
popupToast.setText(recivedMessage);
|
||||||
popupToast.setDuration(Toast.LENGTH_LONG);
|
popupToast.setDuration(Toast.LENGTH_LONG);
|
||||||
@@ -195,15 +184,15 @@ public class GameLogicThread extends Thread{
|
|||||||
loseToast.show();
|
loseToast.show();
|
||||||
break;
|
break;
|
||||||
case Command.PING:
|
case Command.PING:
|
||||||
clientOut.writeObject("pong");
|
coms.send(Command.PONG);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case Command.UPDATE:
|
case Command.UPDATE:
|
||||||
renderer.updateFrame();
|
renderer.updateFrame();
|
||||||
|
|
||||||
case Command.PUT_ENEMY_CARD:
|
case Command.PUT_ENEMY_CARD:
|
||||||
int cardId = (int)clientIn.readObject();
|
int cardId = coms.recieveInt();
|
||||||
int zone = (int)clientIn.readObject();
|
int zone = coms.recieveInt();
|
||||||
|
|
||||||
//on instancie la carte recu
|
//on instancie la carte recu
|
||||||
Class<? extends Card> c = CardRegistery.get(cardId);
|
Class<? extends Card> c = CardRegistery.get(cardId);
|
||||||
@@ -272,21 +261,20 @@ public class GameLogicThread extends Thread{
|
|||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
//on veut jouer une carte
|
//on veut jouer une carte
|
||||||
clientOut.writeObject(Command.PUT_CARD);
|
coms.send(Command.PUT_CARD);
|
||||||
|
|
||||||
|
|
||||||
//on lui dit quelle caret on veut
|
//on lui dit quelle caret on veut
|
||||||
int cardId = CardRegistery.indexOf(card.getCard());
|
int cardId = CardRegistery.indexOf(card.getCard());
|
||||||
if (cardId == -1) {
|
if (cardId == -1) {
|
||||||
clientOut.writeObject("CLIENT REGISTRY ERROR");
|
coms.send("CLIENT REGISTRY ERROR");
|
||||||
throw new UnrecognizedCard();
|
throw new UnrecognizedCard();
|
||||||
}
|
}
|
||||||
clientOut.writeObject(cardId);
|
coms.send(cardId);
|
||||||
clientOut.writeObject(zone);
|
coms.send(zone);
|
||||||
|
|
||||||
Log.e("waiting","for ok");
|
Log.e("waiting","for ok");
|
||||||
if (clientIn.readObject().equals(Command.OK)) {
|
if (coms.recieve().equals(Command.OK)) {
|
||||||
Log.e("waiting","done");
|
|
||||||
board.setCard(zone, card.getCard());
|
board.setCard(zone, card.getCard());
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@@ -315,7 +303,6 @@ public class GameLogicThread extends Thread{
|
|||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Log.e("card play","sending to render");
|
|
||||||
return requestResult;
|
return requestResult;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -366,9 +353,7 @@ public class GameLogicThread extends Thread{
|
|||||||
public void terminate() {
|
public void terminate() {
|
||||||
ready=false;
|
ready=false;
|
||||||
try {
|
try {
|
||||||
if(clientOut != null)clientOut.close();
|
if(coms != null)coms.close();
|
||||||
if(clientIn != null)clientIn.close();
|
|
||||||
if(client != null)client.close();
|
|
||||||
} catch (IOException e) {}
|
} catch (IOException e) {}
|
||||||
cancelled=true;
|
cancelled=true;
|
||||||
|
|
||||||
@@ -379,7 +364,7 @@ public class GameLogicThread extends Thread{
|
|||||||
if(isYourTurn)
|
if(isYourTurn)
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
clientOut.writeObject(Command.PASS_TURN);
|
coms.send(Command.PASS_TURN);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
@@ -390,12 +375,4 @@ public class GameLogicThread extends Thread{
|
|||||||
t.show();
|
t.show();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public ObjectOutputStream getClientOut() {
|
|
||||||
return clientOut;
|
|
||||||
}
|
|
||||||
|
|
||||||
public ObjectInputStream getClientIn() {
|
|
||||||
return clientIn;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,4 @@
|
|||||||
|
package com.iutlaval.myapplication.exception;
|
||||||
|
|
||||||
|
public class NotAnIntegerException extends RuntimeException{
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user