Commit 299b8aa7 authored by Xuhao Lin's avatar Xuhao Lin
Browse files

Merge branch 'develop' of https://gitlab.utc.fr/qdruault/lo23-project into IHM-Main/secondinteg

parents 4b2f8cdf fdd09fa9
......@@ -18,8 +18,11 @@ import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.Set;
/**
* Facade for the communication module
*
......@@ -32,18 +35,15 @@ public class ComFacade {
*/
protected IDataCom iDataCom;
private final DiscoveryController discoCtrl;
private final KnownIPController kIpCtrl;
private final Receiver receiver;
public ComFacade(IDataCom iDataCom) {
System.out.println(this.getClass() + " Creation de la facade");
this.iDataCom = iDataCom;
discoCtrl = DiscoveryController.getInstance();
kIpCtrl = KnownIPController.getInstance(); // creation of KnownIPController
// TODO: Instanciate receiver
kIpCtrl = KnownIPController.getInstance();
receiver = new Receiver(25000, iDataCom);
new Thread(receiver).start();
Logger.getLogger(ComFacade.class.getName()).log(Level.INFO, null, "Facade created");
}
public void setUsedInterface(InterfaceAddress uif) {
......@@ -59,9 +59,9 @@ public class ComFacade {
* @param recipients is the player who must receive the ships coordinates
*/
public void sendShipsToEnnemy(List<Ship> listShips, List<LightPublicUser> recipients) {
M_PlaceShip m_placeship = new M_PlaceShip(iDataCom.getMyPublicUserProfile(), listShips);
M_PlaceShip mPlaceship = new M_PlaceShip(iDataCom.getMyPublicUserProfile(), listShips);
for (LightPublicUser recipient : recipients) {
Sender os = new Sender(kIpCtrl.getHashMap().get(recipient.getId()).getHostAddress(), kIpCtrl.getPort(), m_placeship);
Sender os = new Sender(kIpCtrl.getHashMap().get(recipient.getId()).getHostAddress(), kIpCtrl.getPort(), mPlaceship);
new Thread(os).start();
}
}
......@@ -71,21 +71,15 @@ public class ComFacade {
*/
public void notifyUserSignedIn() {
kIpCtrl.initIpList(iDataCom);
/*M_Connexion m_connexion = new M_Connexion(user);
for(Inet4Address ip : kIpCtrl.getHashMap().values()){
Sender os = new Sender(ip.getHostAddress(), kIpCtrl.getPort(), m_connexion);
new Thread(os).start();
}*/
}
/**
* Called to send "log out" notification to everybody.
*/
public void notifyUserSignedOut() {
M_Deconnection m_deconnection = new M_Deconnection(iDataCom.getMyPublicUserProfile());
M_Deconnection mDeconnection = new M_Deconnection(iDataCom.getMyPublicUserProfile());
for (Inet4Address ip : kIpCtrl.getHashMap().values()) {
Sender os = new Sender(ip.getHostAddress(), kIpCtrl.getPort(), m_deconnection);
Sender os = new Sender(ip.getHostAddress(), kIpCtrl.getPort(), mDeconnection);
new Thread(os).start();
}
}
......@@ -96,9 +90,9 @@ public class ComFacade {
* @param message is the message content and recipients
*/
public void notifyNewMessage(com.utclo23.data.structure.Message message) {
M_Chat m_chat = new M_Chat(iDataCom.getMyPublicUserProfile(), message, message.getTimestamp());
M_Chat mChat = new M_Chat(iDataCom.getMyPublicUserProfile(), message);
for (LightPublicUser recipient : message.getRecipients()) {
Sender os = new Sender(kIpCtrl.getHashMap().get(recipient.getId()).getHostAddress(), kIpCtrl.getPort(), m_chat);
Sender os = new Sender(kIpCtrl.getHashMap().get(recipient.getId()).getHostAddress(), kIpCtrl.getPort(), mChat);
new Thread(os).start();
}
}
......@@ -110,9 +104,9 @@ public class ComFacade {
* @param recipients are the recipients of the new mine
*/
public void notifyNewCoordinates(Mine mine, List<LightPublicUser> recipients) {
M_PlaceMine m_placemine = new M_PlaceMine(iDataCom.getMyPublicUserProfile(), mine);
M_PlaceMine mPlaceMine = new M_PlaceMine(iDataCom.getMyPublicUserProfile(), mine);
for (LightPublicUser recipient : recipients) {
Sender os = new Sender(kIpCtrl.getHashMap().get(recipient.getId()).getHostAddress(), kIpCtrl.getPort(), m_placemine);
Sender os = new Sender(kIpCtrl.getHashMap().get(recipient.getId()).getHostAddress(), kIpCtrl.getPort(), mPlaceMine);
new Thread(os).start();
}
}
......@@ -124,9 +118,9 @@ public class ComFacade {
* @param game is the new created game
*/
public void notifyNewGame(StatGame game) {
M_CreationGame m_creationgame = new M_CreationGame(iDataCom.getMyPublicUserProfile(), game);
M_CreationGame mCreationGame = new M_CreationGame(iDataCom.getMyPublicUserProfile(), game);
for (Inet4Address ip : kIpCtrl.getHashMap().values()) {
Sender os = new Sender(ip.getHostAddress(), kIpCtrl.getPort(), m_creationgame);
Sender os = new Sender(ip.getHostAddress(), kIpCtrl.getPort(), mCreationGame);
Thread thread = new Thread(os);
thread.start();
}
......@@ -140,9 +134,9 @@ public class ComFacade {
*/
public void connectionToGame(StatGame game, String role) {
M_JoinGame m_joingame = new M_JoinGame(iDataCom.getMyPublicUserProfile(), game, role);
M_JoinGame mJoinGame = new M_JoinGame(iDataCom.getMyPublicUserProfile(), game, role);
Inet4Address adr = KnownIPController.getInstance().getHashMap().get(game.getCreator().getId());
Sender os = new Sender(adr.getHostAddress(), kIpCtrl.getPort(), m_joingame);
Sender os = new Sender(adr.getHostAddress(), kIpCtrl.getPort(), mJoinGame);
new Thread(os).start();
}
......@@ -150,9 +144,9 @@ public class ComFacade {
* Called to send "leave game" notification to everybody.
*/
public void leaveGame() {
M_LeaveGame m_leavegame = new M_LeaveGame(iDataCom.getMyPublicUserProfile());
M_LeaveGame mLeaveGame = new M_LeaveGame(iDataCom.getMyPublicUserProfile());
for (Inet4Address ip : kIpCtrl.getHashMap().values()) {
Sender os = new Sender(ip.getHostAddress(), kIpCtrl.getPort(), m_leavegame);
Sender os = new Sender(ip.getHostAddress(), kIpCtrl.getPort(), mLeaveGame);
new Thread(os).start();
}
}
......@@ -163,8 +157,8 @@ public class ComFacade {
* @param listIpTarget is the list of known ip targets
*/
public void sendDiscovery(List<Inet4Address> listIpTarget) {
HashMap<String, Inet4Address> tmp_hash = new HashMap(kIpCtrl.getHashMap());
tmp_hash.put(iDataCom.getMyPublicUserProfile().getId(), kIpCtrl.getMyInetAddress());
HashMap<String, Inet4Address> tmpHash = new HashMap(kIpCtrl.getHashMap());
tmpHash.put(iDataCom.getMyPublicUserProfile().getId(), kIpCtrl.getMyInetAddress());
List<LightPublicUser> tmp = new ArrayList(iDataCom.getConnectedUsers());
tmp.add(iDataCom.getMyPublicUserProfile().getLightPublicUser());
......@@ -177,20 +171,15 @@ public class ComFacade {
set.remove(kIpCtrl.getMyInetAddress());
List<Inet4Address> newIpTarget = new ArrayList(set);
for (Inet4Address ipDest : newIpTarget) {
/*M_GetIP m_getIp = new M_GetIP(iDataCom.getMyPublicUserProfile());
Sender os = new Sender(listIpTarget.get(i).getHostAddress(), kIpCtrl.getPort(), m_getIp);
new Thread(os).start();
discoCtrl.addIP(listIpTarget.get(i));*/
for (Inet4Address ipDest : newIpTarget) {
// On vérie si l'ip n'est pas déjà dans le hashMap
if(!kIpCtrl.getHashMap().containsValue(ipDest)){
List otherTargets = new ArrayList(newIpTarget);
otherTargets.remove(ipDest);
M_Bleu m_Bleu = new M_Bleu(iDataCom.getMyPublicUserProfile(),
tmp_hash, tmp, iDataCom.getGameList(), otherTargets);
Sender os = new Sender(ipDest.getHostAddress(), kIpCtrl.getPort(), m_Bleu);
M_Bleu mBleu = new M_Bleu(iDataCom.getMyPublicUserProfile(),
tmpHash, tmp, iDataCom.getGameList(), otherTargets);
Sender os = new Sender(ipDest.getHostAddress(), kIpCtrl.getPort(), mBleu);
new Thread(os).start();
}
}
......@@ -202,8 +191,8 @@ public class ComFacade {
* @param id is the UID of the recipient
*/
public void getPublicUserProfile(String id) {
M_GetPlayerInfo m_getplayerinfo = new M_GetPlayerInfo(iDataCom.getMyPublicUserProfile());
Sender os = new Sender(kIpCtrl.getHashMap().get(id).getHostAddress(), kIpCtrl.getPort(), m_getplayerinfo);
M_GetPlayerInfo mGetPlayerInfo = new M_GetPlayerInfo(iDataCom.getMyPublicUserProfile());
Sender os = new Sender(kIpCtrl.getHashMap().get(id).getHostAddress(), kIpCtrl.getPort(), mGetPlayerInfo);
new Thread(os).start();
}
......@@ -217,17 +206,18 @@ public class ComFacade {
* @param game is the game in question
*/
public void joinGameResponse(boolean success, String id, StatGame game) {
M_JoinGameResponse m_joingameresponse = new M_JoinGameResponse(iDataCom.getMyPublicUserProfile(), success, game);
M_JoinGameResponse mJoinGameResponse = new M_JoinGameResponse(iDataCom.getMyPublicUserProfile(), success, game);
if (success) {
for (Inet4Address ip : kIpCtrl.getHashMap().values()) {
Sender os = new Sender(ip.getHostAddress(), kIpCtrl.getPort(), m_joingameresponse);
Sender os = new Sender(ip.getHostAddress(), kIpCtrl.getPort(), mJoinGameResponse);
new Thread(os).start();
System.out.println("send success joinGame");
Logger.getLogger(ComFacade.class.getName()).log(Level.INFO, null, "Send success joinGame");
}
} else {
Sender os = new Sender(kIpCtrl.getHashMap().get(id).getHostAddress(), kIpCtrl.getPort(), m_joingameresponse);
Sender os = new Sender(kIpCtrl.getHashMap().get(id).getHostAddress(), kIpCtrl.getPort(), mJoinGameResponse);
new Thread(os).start();
System.out.println("fail joinGame");
Logger.getLogger(ComFacade.class.getName()).log(Level.INFO, null, "Fail joinGame");
}
}
}
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.utclo23.com;
import java.net.Inet4Address;
import java.util.ArrayList;
import java.util.List;
/**
*
* @author Thomas
*/
public class DiscoveryController {
// TODO: add a lock on this one
private List<Inet4Address> getIpIssuedList;
private DiscoveryController() {
getIpIssuedList = new ArrayList<Inet4Address>();
}
// SINGLETON
// Holder
private static class SingletonHolder {
private final static DiscoveryController instance = new DiscoveryController();
}
// Access point for unique instance of the singleton class
public static DiscoveryController getInstance() {
return SingletonHolder.instance;
}
public void addIP(Inet4Address ip) {
getIpIssuedList.add(ip);
}
public boolean isIn(Inet4Address Ip) {
return getIpIssuedList.contains(Ip);
}
public List<Inet4Address> getGetIpIssuedList() {
return getIpIssuedList;
}
public void removeIpRetrieved(Inet4Address ip){
getIpIssuedList.remove(ip);
}
}
......@@ -10,8 +10,6 @@ import java.net.Inet4Address;
import com.utclo23.data.facade.IDataCom;
import java.net.InterfaceAddress;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
/**
* Class containing a hashmap with UID and corresponding IP address of the known
......@@ -22,15 +20,13 @@ import java.util.Map;
*/
public class KnownIPController {
// All IP known by this node
// TODO: add a lock on this one
private final HashMap<String, Inet4Address> knownIp;
IDataCom iDataCom;
InterfaceAddress usedInterface;
protected IDataCom iDataCom;
private InterfaceAddress usedInterface;
private final int PORT = 25000;
private static final int PORT = 25000;
// private constructor
private KnownIPController() {
knownIp = new HashMap<>();
}
......@@ -40,8 +36,11 @@ public class KnownIPController {
* the application.
*/
private static class SingletonHolder {
private SingletonHolder(){
throw new IllegalStateException("Utility class");
}
private final static KnownIPController INSTANCE = new KnownIPController();
private static final KnownIPController INSTANCE = new KnownIPController();
}
/**
......@@ -76,40 +75,16 @@ public class KnownIPController {
}
public String getKeyFromValue(HashMap<String, Inet4Address> tmphash, Inet4Address value) {
String key = null;
for (Map.Entry<String, Inet4Address> entry : tmphash.entrySet()) {
if (value.equals(entry.getValue())) {
return entry.getKey();
}
}
return null;
}
/**
* Called to return attribute "knownIp" value without our own node.
*
* @return Hashmap value
*/
public HashMap<String, Inet4Address> getHashMap() {
HashMap<String, Inet4Address> tmphash = knownIp;
HashMap<String, Inet4Address> tmpHash = knownIp;
String id = iDataCom.getMyPublicUserProfile().getLightPublicUser().getId();
tmphash.remove(id); // to avoid sending back our own Ip adress (cuz it already got it).
return tmphash;
}
public HashMap<String, Inet4Address> getNewIpHashMap() {
DiscoveryController discoCtrl = DiscoveryController.getInstance();
HashMap<String, Inet4Address> tmphash = knownIp;
tmphash.remove(iDataCom.getMyPublicUserProfile().getLightPublicUser().getId()); // to avoid sending back our own Ip adress (cuz it already got it).
List<Inet4Address> getIpIssuedList = discoCtrl.getGetIpIssuedList();
String key;
for (int i = 0; i < getIpIssuedList.size(); i++) {
key = getKeyFromValue(tmphash, getIpIssuedList.get(i));
tmphash.remove(key);
}
return tmphash;
tmpHash.remove(id); // to avoid sending back our own Ip adress (cuz it already got it).
return tmpHash;
}
......@@ -129,6 +104,7 @@ public class KnownIPController {
*
* @param hashToCheck is the Hashmap containing ids of type String and
* Inet4Address
* @return the new updated HashMap
*/
public HashMap<String, Inet4Address> addNonExistingNodes(HashMap<String, Inet4Address> hashToCheck) {
Iterator it = hashToCheck.entrySet().iterator();
......@@ -144,6 +120,10 @@ public class KnownIPController {
return tmpHash;
}
/**
* Acessor
* @return the network port number
*/
public int getPort(){
return PORT;
}
......
......@@ -11,6 +11,8 @@ import java.io.ObjectInputStream;
import java.net.ServerSocket;
import com.utclo23.com.messages.Message;
import com.utclo23.data.facade.IDataCom;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
* This class implements a serverSocket to receive messages from another
......@@ -38,8 +40,8 @@ public class Receiver implements Runnable {
try {
serverSocket = new ServerSocket(port);
iDataCom = dataCom;
}catch(IOException e){
e.printStackTrace();
}catch(IOException ex){
Logger.getLogger(ComFacade.class.getName()).log(Level.INFO, null, ex);
}
}
......@@ -49,24 +51,15 @@ public class Receiver implements Runnable {
try {
client = serverSocket.accept();
in = new ObjectInputStream(client.getInputStream());
System.out.println(in.available());
request = (Message) in.readObject();
request.callback(iDataCom);
/*while((request = (Message) in.readObject()) != null)
{
request.callback(iDataCom);
break;
}*/
System.out.println("message received" + request.getClass().toString());
Logger.getLogger(Receiver.class.getName()).log(Level.INFO, null, "Message received : " + request.getClass().toString());
client.close();
in.close();
} catch (IOException e) {
} catch (ClassNotFoundException e) {
} catch (IOException|ClassNotFoundException ex) {
Logger.getLogger(ComFacade.class.getName()).log(Level.INFO, null, ex);
}
}
}
}
......@@ -6,34 +6,36 @@
package com.utclo23.com;
import java.net.Socket;
import java.net.UnknownHostException;
import java.io.IOException;
import java.io.ObjectOutputStream;
import com.utclo23.com.messages.Message;
import java.net.InetSocketAddress;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
* This class is used to send an object to a recipient.
* The object is sent by a different thread thanks to a socket. Thread and
* socket are closed after sending. A sender should be instantiate every time
* an object has to be sent.
* This class is used to send an object to a recipient. The object is sent by a
* different thread thanks to a socket. Thread and socket are closed after
* sending. A sender should be instantiate every time an object has to be sent.
*
* @author Grégoire MARTINACHE
*/
public class Sender implements Runnable {
Socket socket;
int port;
String ip;
Message request;
ObjectOutputStream out;
//ObjectInputStream in;
/**
* Constructor of Sender class. Initialize attributes ip, port and request
* message with the specified parameters.
* @param ip IP destination
* @param port Number of the port
* @param request Message instance to send
*/
* Constructor of Sender class. Initialize attributes ip, port and request
* message with the specified parameters.
*
* @param ip IP destination
* @param port Number of the port
* @param request Message instance to send
*/
public Sender(String ip, int port, Message request) {
this.ip = ip;
this.port = port;
......@@ -50,10 +52,11 @@ public class Sender implements Runnable {
out = new ObjectOutputStream(socket.getOutputStream());
request.setIpSender(kIpCtrl.getMyInetAddress());
out.writeObject(request);
System.out.println("message sent" + request.getClass().toString());
Logger.getLogger(ComFacade.class.getName()).log(Level.INFO, null, "Message sent : " + request.getClass().toString());
out.close();
socket.close();
} catch (UnknownHostException e) {}
catch (IOException e) {}
} catch (IOException ex) {
Logger.getLogger(ComFacade.class.getName()).log(Level.INFO, null, ex);
}
}
}
......@@ -15,84 +15,92 @@ import java.net.Inet4Address;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author Thibault Chiche
*/
public class M_Bleu extends Message {
HashMap<String, Inet4Address> hashMap_received;
List<LightPublicUser> users_received;
List<StatGame> listGames_received;
List<Inet4Address> otherTargets;
public M_Bleu(PublicUser user, HashMap<String, Inet4Address> hashMap,
private final HashMap<String, Inet4Address> hashMapReceived;
private final List<LightPublicUser> usersReceived;
private final List<StatGame> listGamesReceived;
private final List<Inet4Address> otherTargets;
public M_Bleu(PublicUser user, HashMap<String, Inet4Address> hashMap,
List<LightPublicUser> users, List<StatGame> listGames, List<Inet4Address> oTargets) {
super(user);
this.hashMap_received = hashMap;
this.users_received = users;
this.listGames_received = listGames;
this.hashMapReceived = hashMap;
this.usersReceived = users;
this.listGamesReceived = listGames;
this.otherTargets = oTargets;
}
@Override
public void callback(IDataCom iDataCom){
System.out.println("Message bleu reçu");
List<String> my_users_id = new ArrayList(KnownIPController.getInstance().getHashMap().keySet());
List<LightPublicUser> my_users_profile = new ArrayList(iDataCom.getConnectedUsers());
HashMap<String, Inet4Address> new_pairs = new HashMap();
List<LightPublicUser> new_users_profile = new ArrayList();
public void callback(IDataCom iDataCom) {
Logger.getLogger(M_Bleu.class.getName()).log(Level.INFO, null, "Blue message received");
List<String> myUsersId = new ArrayList(KnownIPController.getInstance().getHashMap().keySet());
List<LightPublicUser> myUsersProfile = new ArrayList(iDataCom.getConnectedUsers());
HashMap<String, Inet4Address> newPairs = new HashMap();
List<LightPublicUser> newUsersProfile = new ArrayList();
// Envoi message bleu aux autres targets afin que tout le monde ait
// les mêmes joueurs connectés. Les premiers qui recevront ces messages
// seront prioritaires
HashMap<String, Inet4Address> tmp_hash = new HashMap(KnownIPController.getInstance().getHashMap());
tmp_hash.put(iDataCom.getMyPublicUserProfile().getId(), KnownIPController.getInstance().getMyInetAddress());
HashMap<String, Inet4Address> tmpHash = new HashMap(KnownIPController.getInstance().getHashMap());
tmpHash.put(iDataCom.getMyPublicUserProfile().getId(), KnownIPController.getInstance().getMyInetAddress());
List<LightPublicUser> tmp = new ArrayList(iDataCom.getConnectedUsers());
tmp.add(iDataCom.getMyPublicUserProfile().getLightPublicUser());
if(this.otherTargets != null){
for(Inet4Address ipDest : this.otherTargets){
if (this.otherTargets != null) {
for (Inet4Address ipDest : this.otherTargets) {
// On envoie pas si on avait déjà l'IP dans le hashMap
if(!KnownIPController.getInstance().getHashMap().containsValue(ipDest)){
M_Bleu m_Bleu = new M_Bleu(iDataCom.getMyPublicUserProfile(),
tmp_hash, tmp, iDataCom.getGameList(), null);
if (!KnownIPController.getInstance().getHashMap().containsValue(ipDest)) {
M_Bleu m_Bleu = new M_Bleu(iDataCom.getMyPublicUserProfile(),
tmpHash, tmp, iDataCom.getGameList(), null);
Sender os = new Sender(ipDest.getHostAddress(), KnownIPController.getInstance().getPort(), m_Bleu);
new Thread(os).start();
}
}
}
// Envoi message rouge à ceux non déjà présents dans nos joueurs connectés
for(String key : this.hashMap_received.keySet()){
if(!key.equals(iDataCom.getMyPublicUserProfile().getId()) &&
!my_users_id.contains(key)){
M_Rouge m_Rouge = new M_Rouge(iDataCom.getMyPublicUserProfile(), tmp_hash, tmp, iDataCom.getGameList());
Sender os = new Sender(this.hashMap_received.get(key).getHostAddress(), KnownIPController.getInstance().getPort(), m_Rouge);
for (Map.Entry<String, Inet4Address> entry : this.hashMapReceived.entrySet()) {
if (!entry.getKey().equals(iDataCom.getMyPublicUserProfile().getId())
&& !myUsersId.contains(entry.getKey())) {
M_Rouge mRouge = new M_Rouge(iDataCom.getMyPublicUserProfile(), tmpHash, tmp, iDataCom.getGameList());
Sender os = new Sender(entry.getValue().getHostAddress(), KnownIPController.getInstance().getPort(), mRouge);
Thread thread = new Thread(os);
thread.start();
new_pairs.put(key, this.hashMap_received.get(key));
newPairs.put(entry.getKey(), entry.getValue());
}
}
for(LightPublicUser key : this.users_received){
if(!key.equals(iDataCom.getMyPublicUserProfile().getLightPublicUser()) &&
!my_users_profile.contains(key)){
new_users_profile.add(key);
for (LightPublicUser key : this.usersReceived) {
if (!key.equals(iDataCom.getMyPublicUserProfile().getLightPublicUser())
&& !myUsersProfile.contains(key)) {
newUsersProfile.add(key);
}