Commit c8aa6a57 authored by dkonam's avatar dkonam

Merge branch 'develop' into Data/develop

parents 410e1cb1 f60c09c6
<?xml version="1.0" encoding="UTF-8"?>
<actions>
<action>
<actionName>run</actionName>
<packagings>
<packaging>jar</packaging>
</packagings>
<goals>
<goal>process-classes</goal>
<goal>org.codehaus.mojo:exec-maven-plugin:1.2.1:exec</goal>
</goals>
<properties>
<exec.args>-classpath %classpath ${packageClassName}</exec.args>
<exec.executable>java</exec.executable>
</properties>
</action>
<action>
<actionName>debug</actionName>
<packagings>
<packaging>jar</packaging>
</packagings>
<goals>
<goal>process-classes</goal>
<goal>org.codehaus.mojo:exec-maven-plugin:1.2.1:exec</goal>
</goals>
<properties>
<exec.args>-Xdebug -Xrunjdwp:transport=dt_socket,server=n,address=${jpda.address} -classpath %classpath ${packageClassName}</exec.args>
<exec.executable>java</exec.executable>
<jpda.listen>true</jpda.listen>
</properties>
</action>
<action>
<actionName>profile</actionName>
<packagings>
<packaging>jar</packaging>
</packagings>
<goals>
<goal>process-classes</goal>
<goal>org.codehaus.mojo:exec-maven-plugin:1.2.1:exec</goal>
</goals>
<properties>
<exec.args>-classpath %classpath ${packageClassName}</exec.args>
<exec.executable>java</exec.executable>
</properties>
</action>
</actions>
......@@ -13,6 +13,7 @@ import com.utclo23.com.messages.*;
import com.utclo23.data.facade.IDataCom;
import com.utclo23.data.structure.LightPublicUser;
import java.net.Inet4Address;
import java.net.InterfaceAddress;
import java.util.List;
/**
......@@ -41,9 +42,13 @@ public class ComFacade {
new Thread(receiver).start();
}
public void setUsedInterface(InterfaceAddress uif){
kIpCtrl.setUsedInterface(uif);
}
// envoi au dest
public void sendShipsToEnnemy(List<Ship> listShips, PublicUser dest){
M_PlaceShip m_placeship = new M_PlaceShip(listShips);
M_PlaceShip m_placeship = new M_PlaceShip(iDataCom.getMyPublicUserProfile(), listShips);
Sender os = new Sender(kIpCtrl.getHashMap().get(dest.getId()).getHostAddress(), 80, m_placeship);
new Thread(os).start();
}
......@@ -72,7 +77,7 @@ public class ComFacade {
// envoi à tout ceux présents dans le game
public void notifyNewMessage(com.utclo23.data.structure.Message message) {
M_Chat m_chat = new M_Chat(message, message.getTimestamp());
M_Chat m_chat = new M_Chat(iDataCom.getMyPublicUserProfile(), message, message.getTimestamp());
for (LightPublicUser recipient : message.getRecipients()) {
Sender os = new Sender(kIpCtrl.getHashMap().get(recipient.getId()).getHostAddress(), 80, m_chat);
new Thread(os).start();
......@@ -81,7 +86,7 @@ public class ComFacade {
// envoi à tout ceux dans le game
public void notifyNewCoordinates(Mine mine, List<LightPublicUser> recipients){
M_PlaceMine m_placemine = new M_PlaceMine(mine);
M_PlaceMine m_placemine = new M_PlaceMine(iDataCom.getMyPublicUserProfile(), mine);
for(LightPublicUser recipient : recipients){
Sender os = new Sender(kIpCtrl.getHashMap().get(recipient.getId()).getHostAddress(), 80, m_placemine);
new Thread(os).start();
......@@ -90,7 +95,7 @@ public class ComFacade {
// à tout le monde
public void notifyNewGame(StatGame game){
M_CreationGame m_creationgame = new M_CreationGame(game);
M_CreationGame m_creationgame = new M_CreationGame(iDataCom.getMyPublicUserProfile(), game);
for(Inet4Address ip : kIpCtrl.getHashMap().values()){
Sender os = new Sender(ip.getHostAddress(), 80, m_creationgame);
new Thread(os).start();
......@@ -99,7 +104,7 @@ public class ComFacade {
// envoi à la machine qui a crée la game
public void connectionToGame(StatGame game){
M_JoinGame m_joingame = new M_JoinGame(game);
M_JoinGame m_joingame = new M_JoinGame(iDataCom.getMyPublicUserProfile(), game);
Inet4Address adr = KnownIPController.getInstance().getHashMap().get(game.getCreator().getId());
Sender os = new Sender(adr.getHostAddress(), 80, m_joingame);
new Thread(os).start();
......@@ -107,7 +112,7 @@ public class ComFacade {
// envoi à tout ceux qui sont dans la game logiquement, paramètre à revoir
public void leaveGame(PublicUser user){
M_LeaveGame m_leavegame = new M_LeaveGame();
M_LeaveGame m_leavegame = new M_LeaveGame(iDataCom.getMyPublicUserProfile());
for(Inet4Address ip : kIpCtrl.getHashMap().values()){
Sender os = new Sender(ip.getHostAddress(), 80, m_leavegame);
new Thread(os).start();
......@@ -118,7 +123,7 @@ public class ComFacade {
public void sendDiscovery(PublicUser user, List<Inet4Address> listIpTarget) {
for (int i = 0; i < listIpTarget.size(); i++) {
M_GetIP m_getIp = new M_GetIP();
M_GetIP m_getIp = new M_GetIP(iDataCom.getMyPublicUserProfile());
Sender os = new Sender(listIpTarget.get(i).getHostAddress(), 80, m_getIp);
new Thread(os).start();
discoCtrl.addIP(listIpTarget.get(i));
......@@ -128,18 +133,23 @@ public class ComFacade {
// envoi à l'id
public void getPublicUserProfile(String id){
M_GetPlayerInfo m_getplayerinfo = new M_GetPlayerInfo();
M_GetPlayerInfo m_getplayerinfo = new M_GetPlayerInfo(iDataCom.getMyPublicUserProfile());
Sender os = new Sender(kIpCtrl.getHashMap().get(id).getHostAddress(), 80, m_getplayerinfo);
new Thread(os).start();
}
// envoi à tout le monde
// envoi à tout le monde si success
public void joinGameResponse (boolean success, String id, StatGame game){
M_JoinGameResponse m_joingameresponse = new M_JoinGameResponse(success);
for(Inet4Address ip : kIpCtrl.getHashMap().values()){
Sender os = new Sender(ip.getHostAddress(), 80, m_joingameresponse);
/*M_JoinGameResponse m_joingameresponse = new M_JoinGameResponse(success);
if (success){
for(Inet4Address ip : kIpCtrl.getHashMap().values()){
Sender os = new Sender(ip.getHostAddress(), 80, m_joingameresponse);
new Thread(os).start();
}
} else {
Sender os = new Sender(kIpCtrl.getHashMap().get(id).getHostAddress(), 80, m_joingameresponse);
new Thread(os).start();
}
}*/
}
}
......@@ -9,6 +9,8 @@ import java.util.HashMap;
import java.net.Inet4Address;
import java.net.UnknownHostException;
import com.utclo23.data.facade.IDataCom;
import java.net.InterfaceAddress;
import java.net.NetworkInterface;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
......@@ -23,7 +25,7 @@ public class KnownIPController {
// TODO: add a lock on this one
private final HashMap<String, Inet4Address> knownIp;
IDataCom iDataCom;
InterfaceAddress usedInterface;
// private constructor
private KnownIPController() {
knownIp = new HashMap<>();
......@@ -40,15 +42,23 @@ public class KnownIPController {
public static KnownIPController getInstance() {
return SingletonHolder.INSTANCE;
}
public Inet4Address getMyInetAddress(){
return (Inet4Address)this.usedInterface.getAddress();
}
public void setUsedInterface(InterfaceAddress uif){
this.usedInterface = uif;
}
// used to put our own IP in the hashmap of IP
public void initIpList(IDataCom iDataCom) {
try {
this.iDataCom = iDataCom;
knownIp.put(
iDataCom.getMyPublicUserProfile().getId(),
(Inet4Address) Inet4Address.getLocalHost());
} catch (UnknownHostException e) {}
this.iDataCom = iDataCom;
knownIp.put(
iDataCom.getMyPublicUserProfile().getId(),
getMyInetAddress());
}
public String getKeyFromValue(
......@@ -65,34 +75,24 @@ public class KnownIPController {
}
public HashMap<String, Inet4Address> getHashMap() {
try {
Inet4Address ourIpAddress = (Inet4Address) Inet4Address.getLocalHost();
HashMap<String, Inet4Address> tmphash = knownIp;
tmphash.remove(iDataCom.getMyPublicUserProfile().getLightPublicUser().getId()); // to avoid sending back our own Ip adress (cuz it already got it).
return tmphash;
} catch (UnknownHostException e) {
// No good
return null;
}
HashMap<String, Inet4Address> tmphash = knownIp;
tmphash.remove(iDataCom.getMyPublicUserProfile().getLightPublicUser().getId()); // to avoid sending back our own Ip adress (cuz it already got it).
return tmphash;
}
public HashMap<String, Inet4Address> getNewIpHashMap() {
DiscoveryController discoCtrl = DiscoveryController.getInstance();
try {
Inet4Address ourIpAddress = (Inet4Address) Inet4Address.getLocalHost();
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;
} catch (UnknownHostException e) {
// No good
return null;
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;
}
public void addNode(String id, Inet4Address ip) {
......
......@@ -40,12 +40,14 @@ public class Receiver implements Runnable {
try {
client = serverSocket.accept();
in = new ObjectInputStream(client.getInputStream());
while((request = (Message) in.readObject()) != null)
System.out.println(in.available());
request = (Message) in.readObject();
request.callback(iDataCom);
/*while((request = (Message) in.readObject()) != null)
{
request.callback(iDataCom);
break;
}
}*/
client.close();
in.close();
......
......@@ -5,12 +5,15 @@
*/
package com.utclo23.com;
import com.utclo23.com.messages.M_Connexion;
import java.net.Socket;
import java.net.UnknownHostException;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import com.utclo23.com.messages.Message;
import com.utclo23.data.structure.PublicUser;
import java.net.Inet4Address;
import java.net.InetSocketAddress;
/**
......@@ -24,7 +27,7 @@ public class Sender implements Runnable {
String ip;
Message request;
ObjectOutputStream out;
ObjectInputStream in;
//ObjectInputStream in;
public Sender(String ip, int port, Message request) {
this.ip = ip;
......@@ -35,11 +38,13 @@ public class Sender implements Runnable {
@Override
public void run() {
try {
KnownIPController kIpCtrl = KnownIPController.getInstance();
socket = new Socket();
socket.setSoTimeout(2000);
socket.connect(new InetSocketAddress(ip, port), 2000);
out = new ObjectOutputStream(socket.getOutputStream());
//in = new ObjectInputStream(socket.getInputStream());
request.setIpSender(kIpCtrl.getMyInetAddress());
out.writeObject(request);
//in.close();
out.close();
......
......@@ -5,6 +5,7 @@
*/
package com.utclo23.com.messages;
import com.utclo23.data.facade.IDataCom;
import com.utclo23.data.structure.PublicUser;
import java.util.Date;
/**
*
......@@ -14,7 +15,8 @@ public class M_Chat extends Message {
private com.utclo23.data.structure.Message message;
private Date timeStamp;
public M_Chat(com.utclo23.data.structure.Message m, Date t){
public M_Chat(PublicUser user, com.utclo23.data.structure.Message m, Date t){
super(user);
message = m;
timeStamp = t;
}
......
......@@ -11,10 +11,9 @@ import com.utclo23.data.structure.PublicUser;
* @author thibault
*/
public class M_Connexion extends Message{
public PublicUser user;
public M_Connexion(PublicUser u){
user = u;
public M_Connexion(PublicUser user){
super(user);
}
@Override
......
......@@ -6,12 +6,17 @@
package com.utclo23.com.messages;
import com.utclo23.data.facade.IDataCom;
import com.utclo23.data.structure.PublicUser;
/**
*
* @author thibault
*/
public class M_ConnexionLost extends Message{
public M_ConnexionLost(PublicUser user){
super(user);
}
@Override
public void callback(IDataCom iDataCom){
iDataCom.connectionLostWithOpponent();
......
......@@ -5,6 +5,7 @@
*/
package com.utclo23.com.messages;
import com.utclo23.data.facade.IDataCom;
import com.utclo23.data.structure.PublicUser;
import com.utclo23.data.structure.StatGame;
/**
*
......@@ -13,7 +14,8 @@ import com.utclo23.data.structure.StatGame;
public class M_CreationGame extends Message{
public StatGame game;
public M_CreationGame(StatGame g){
public M_CreationGame(PublicUser user, StatGame g){
super(user);
game = g;
}
......
......@@ -11,10 +11,9 @@ import com.utclo23.data.structure.PublicUser;
* @author thibault
*/
public class M_Deconnexion extends Message {
public PublicUser user;
public M_Deconnexion(PublicUser u){
user = u;
public M_Deconnexion(PublicUser user){
super(user);
}
@Override
......
......@@ -10,6 +10,7 @@ import java.net.Inet4Address;
import java.util.HashMap;
import com.utclo23.data.facade.IDataCom;
import com.utclo23.data.structure.LightPublicUser;
import com.utclo23.data.structure.PublicUser;
import com.utclo23.data.structure.StatGame;
import java.util.List;
......@@ -23,7 +24,8 @@ public class M_GetIP extends Message{
String name;
public M_GetIP(){
public M_GetIP(PublicUser user){
super(user);
}
......@@ -34,8 +36,8 @@ public class M_GetIP extends Message{
iDataCom.addConnectedUser(user.getLightPublicUser());
// TODO: add fonction to get the data from DATA
List<LightPublicUser> listUsers = null; // = iDataCom.getConnectedUsers();
List<StatGame>listGames = null; // = iDataCom.getGameList();
List<LightPublicUser> listUsers = iDataCom.getConnectedUsers();
List<StatGame>listGames = iDataCom.getGameList();
kic = KnownIPController.getInstance();
// add new user to own knownIP hashmap.
......@@ -47,9 +49,9 @@ public class M_GetIP extends Message{
HashMap<String,Inet4Address> IdToIp = kic.getHashMap();
// send back the data this node has about its known network.
M_ReturnIP returnIp = new M_ReturnIP(listGames, listUsers, IdToIp);
M_ReturnIP returnIp = new M_ReturnIP(user, listGames, listUsers, IdToIp);
Sender os = new Sender(IP_sender.toString(), 80, returnIp);
Sender os = new Sender(IP_sender.getHostAddress(), 80, returnIp);
Thread thread = new Thread(os);
thread.start();
......
......@@ -13,11 +13,16 @@ import com.utclo23.data.structure.PublicUser;
* @author thibault
*/
public class M_GetPlayerInfo extends Message {
public M_GetPlayerInfo(PublicUser user){
super(user);
}
@Override
public void callback(IDataCom iDataCom){
PublicUser user = iDataCom.getMyPublicUserProfile();
M_PlayerInfo m_PlayerInfo = new M_PlayerInfo(user);
Sender os = new Sender(this.IP_sender.getHostAddress(), 80, m_PlayerInfo);
Sender os = new Sender(IP_sender.getHostAddress(), 80, m_PlayerInfo);
new Thread(os).start();
}
}
......@@ -5,6 +5,7 @@
*/
package com.utclo23.com.messages;
import com.utclo23.data.facade.IDataCom;
import com.utclo23.data.structure.PublicUser;
import com.utclo23.data.structure.StatGame;
/**
*
......@@ -14,7 +15,8 @@ public class M_JoinGame extends Message {
private StatGame game;
private String role;
public M_JoinGame(StatGame g) {
public M_JoinGame(PublicUser user, StatGame g) {
super(user);
game = g;
}
@Override
......
......@@ -6,6 +6,7 @@
package com.utclo23.com.messages;
import com.utclo23.data.facade.IDataCom;
import com.utclo23.data.structure.PublicUser;
/**
*
......@@ -14,11 +15,13 @@ import com.utclo23.data.facade.IDataCom;
public class M_JoinGameResponse extends Message {
private boolean success;
public M_JoinGameResponse(boolean s){
public M_JoinGameResponse(PublicUser user, boolean s){
super(user);
success = s;
}
@Override
public void callback(IDataCom iDataCom){
//if success call xxx
//else connexionImpossible()
}
}
......@@ -6,12 +6,18 @@
package com.utclo23.com.messages;
import com.utclo23.data.facade.IDataCom;
import com.utclo23.data.structure.PublicUser;
/**
*
* @author thibault
*/
public class M_LeaveGame extends Message {
public M_LeaveGame(PublicUser user){
super(user);
}
@Override
public void callback(IDataCom iDataCom){
iDataCom.opponentHasLeftGame();
......
......@@ -6,6 +6,7 @@
package com.utclo23.com.messages;
import com.utclo23.data.facade.IDataCom;
import com.utclo23.data.structure.Mine;
import com.utclo23.data.structure.PublicUser;
/**
*
* @author thibault
......@@ -13,7 +14,8 @@ import com.utclo23.data.structure.Mine;
public class M_PlaceMine extends Message {
private Mine mine;
public M_PlaceMine(Mine m){
public M_PlaceMine(PublicUser user, Mine m){
super(user);
mine = m;
}
@Override
......
......@@ -5,6 +5,7 @@
*/
package com.utclo23.com.messages;
import com.utclo23.data.facade.IDataCom;
import com.utclo23.data.structure.PublicUser;
import com.utclo23.data.structure.Ship;
import java.util.List;
/**
......@@ -14,7 +15,8 @@ import java.util.List;
public class M_PlaceShip extends Message {
private List<Ship> ships;
public M_PlaceShip(List<Ship> s){
public M_PlaceShip(PublicUser user, List<Ship> s){
super(user);
ships = s;
}
@Override
......
......@@ -11,10 +11,9 @@ import com.utclo23.data.structure.PublicUser;
* @author thibault
*/
public class M_PlayerInfo extends Message {
private PublicUser user;
public M_PlayerInfo(PublicUser u){
user = u;
public M_PlayerInfo(PublicUser user){
super(user);
}
@Override
public void callback(IDataCom iDataCom){
......
......@@ -10,6 +10,7 @@ import com.utclo23.com.KnownIPController;
import com.utclo23.com.Sender;
import com.utclo23.data.facade.IDataCom;
import com.utclo23.data.structure.LightPublicUser;
import com.utclo23.data.structure.PublicUser;
import com.utclo23.data.structure.StatGame;
import java.net.Inet4Address;
import java.net.UnknownHostException;
......@@ -30,7 +31,8 @@ public class M_ReturnIP extends Message {
private KnownIPController kic;
private DiscoveryController discoCtrl;
public M_ReturnIP(List<StatGame> listGames, List<LightPublicUser> listUsers, HashMap<String, Inet4Address> idToIp) {
public M_ReturnIP(PublicUser user, List<StatGame> listGames, List<LightPublicUser> listUsers, HashMap<String, Inet4Address> idToIp) {
super(user);
this.listGames = listGames;
this.listUsers = listUsers;
this.idToIp = idToIp;
......@@ -54,32 +56,27 @@ public class M_ReturnIP extends Message {
discoCtrl = DiscoveryController.getInstance();
List<LightPublicUser> listUsersTmp = iDataCom.getConnectedUsers();
List<StatGame> listGamesTmp = iDataCom.getGameList();
// if SenderNode in GetIPIssuedList
// send all available data EXCEPT IP for nodes in the GetIPIssuedList
// else (we got this returnIp from a newly updated node
// send only our OWN data to all new IP
if (discoCtrl.isIn(IP_sender)) {
// TODO: add fonction to get the data from DATA
List<LightPublicUser> listUsers = null; // = iDataCom.getConnectedUsers();
List<StatGame> listGames = null; // = iDataCom.getGameList();
// get the hasmap of our IP to send it to the requesting node.
HashMap<String, Inet4Address> IdToIp = kic.getNewIpHashMap();
// send back the data this node has about its known network.
M_ReturnIP returnIp = new M_ReturnIP(listGames, listUsers, IdToIp);
M_ReturnIP returnIp = new M_ReturnIP(user, listGamesTmp, listUsers, IdToIp);
Sender os = new Sender(IP_sender.toString(), 80, returnIp);
Sender os = new Sender(IP_sender.getHostAddress(), 80, returnIp);
new Thread(os).start();
} else {
String myId = iDataCom.getMyPublicUserProfile().getLightPublicUser().getId();
// TODO: add fonction to get the data from DATA
List<LightPublicUser> listUsersTmp = null; // = iDataCom.getConnectedUsers();
List<StatGame> listGames = null; // = iDataCom.getGameList();
List<LightPublicUser> listUsers = null;
for (int i = 0; i < listUsersTmp.size(); i++) {
if (listUsers.get(i).getId() == myId) {
listUsers.add(listUsersTmp.get(i));
......@@ -94,8 +91,8 @@ public class M_ReturnIP extends Message {
Logger.getLogger(M_ReturnIP.class.getName()).log(Level.SEVERE, null, ex);
}
M_ReturnIP returnIp = new M_ReturnIP(listGames, listUsers, IdToIp);
Sender os = new Sender(IP_sender.toString(), 80, returnIp);
M_ReturnIP returnIp = new M_ReturnIP(user, listGames, listUsers, IdToIp);
Sender os = new Sender(IP_sender.getHostAddress(), 80, returnIp);
Thread thread = new Thread(os);
thread.start();
......
......@@ -19,15 +19,11 @@ public abstract class Message implements Serializable{
protected Inet4Address IP_sender;
public abstract void callback(IDataCom iDataCom);
public void Message(IDataCom idc) throws UnknownHostException{
setUser(idc.getMyPublicUserProfile());
setIpSender((Inet4Address)Inet4Address.getLocalHost());
public Message(PublicUser user){
this.user = user;
}
public void setIpSender(Inet4Address ip){
IP_sender = ip;
}
public void setUser(PublicUser u){
user = u;
}
}
\ No newline at end of file
......@@ -27,6 +27,8 @@ import com.utclo23.ihmtable.IHMTableFacade;
import com.utclo23.ihmtable.IIHMTableToData;
import java.io.File;
import java.net.InterfaceAddress;
import java.net.NetworkInterface;
import java.util.List;
......@@ -279,7 +281,7 @@ public class DataFacade implements IDataCom, IDataIHMTable, IDataIHMMain {
*/
@Override
public List<Ship> getShips() throws DataException{
public List<Ship> getTemplateShips() throws DataException{
if(this.gameMediator.getCurrentGame()!=null)
{
return this.gameMediator.getCurrentGame().getTemplateShips();
......@@ -521,4 +523,13 @@ public class DataFacade implements IDataCom, IDataIHMTable, IDataIHMMain {
this.gameMediator.gameConnectionRequestGame(id, role);
}
@Override
public void setNetworkInterface(InterfaceAddress net_interface) {
if(this.getComfacade()!=null)
{
this.getComfacade().setUsedInterface(net_interface);
}
}
}
......@@ -6,6 +6,7 @@
package com.utclo23.data.facade;
import com.utclo23.data.structure.*;
import java.net.NetworkInterface;
import java.rmi.server.UID;
import java.util.List;
......@@ -14,6 +15,7 @@ import java.util.List;
* @author Davy
*/
public interface IDataCom {
public void addNewGame(StatGame game);
public void setEnnemyShips(List<Ship> ships);
public void forwardCoordinates(Mine mine);
......
......@@ -7,6 +7,7 @@ package com.utclo23.data.facade;
import com.utclo23.data.module.DataException;
import com.utclo23.data.structure.*;