Commit 9994d270 authored by Gregoire Martinache's avatar Gregoire Martinache

Merge branch 'network-interface' into 'master'

Network interface

See merge request !40
parents 0d4b07b8 1b9513cb
<?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>
/*
* 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.battleship;
/**
*
* Basic exception for project
* @author Davy
*/
public class GameException extends Exception {
public GameException() {
}
public GameException(String message) {
super(message);
}
}
......@@ -47,6 +47,7 @@ public class MainApp extends Application {
stage
);
IHMMainToIhmTable iHMMainToIhmTable = ihmMainFacade;
IHMMainToData iHMMainToData = ihmMainFacade;
// set link from IhmMainFacade to IhmTable
ihmTableFacade.setIhmMainLink(iHMMainToIhmTable);
......
......@@ -4,72 +4,152 @@
* and open the template in the editor.
*/
package com.utclo23.com;
import com.utclo23.data.structure.PublicUser;
import com.utclo23.data.structure.Ship;
import com.utclo23.data.structure.Mine;
import com.utclo23.data.structure.StatGame;
import com.utclo23.com.messages.M_GetIP;
import com.utclo23.com.messages.Message;
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;
/**
* Facade for the communication module
*
* @author Thibault CHICHE
*/
public class ComFacade {
//private HashMap<UID, InetAddress> UID_IP;
/**
* The data facade
*/
public IDataCom iDataCom;
private DiscoveryController discoCtrl;
private KnownIPController kIpCtrl;
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
kIpCtrl.initIpList(iDataCom);
discoCtrl = DiscoveryController.getInstance();
kIpCtrl = KnownIPController.getInstance(); // creation of KnownIPController
// TODO: Instanciate receiver
receiver = new Receiver(80, iDataCom);
new Thread(receiver).start();
}
public void setUsedInterface(InterfaceAddress uif){
kIpCtrl.setUsedInterface(uif);
}
public void sendShipsToEnnemy(Ship[] listShips, PublicUser dest){
// envoi au dest
public void sendShipsToEnnemy(List<Ship> listShips, PublicUser dest){
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();
}
public void notifyUserSignedIn(PublicUser user){
// envoi à tout le monde
// c'est sendDiscovery qui fait ça en fait non ?
// dans le doute je l'implémente -> Thibault
public void notifyUserSignedIn(PublicUser user){
kIpCtrl.initIpList(iDataCom);
/*M_Connexion m_connexion = new M_Connexion(user);
for(Inet4Address ip : kIpCtrl.getHashMap().values()){
Sender os = new Sender(ip.getHostAddress(), 80, m_connexion);
new Thread(os).start();
}*/
}
public void notifyUserSignedOut(PublicUser user){
// envoi à tout le monde
public void notifyUserSignedOut(PublicUser user){
M_Deconnexion m_deconnexion = new M_Deconnexion(user);
for(Inet4Address ip : kIpCtrl.getHashMap().values()){
Sender os = new Sender(ip.getHostAddress(), 80, m_deconnexion);
new Thread(os).start();
}
}
public void notifyNewMessage(Message message){
// envoi à tout ceux présents dans le game
public void notifyNewMessage(com.utclo23.data.structure.Message message) {
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();
}
}
public void notifyNewCoordinates(Mine mine, PublicUser[] recipient){
// envoi à tout ceux dans le game
public void notifyNewCoordinates(Mine mine, List<LightPublicUser> recipients){
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();
}
}
public void notifyNewGame(StatGame game){
// à tout le monde
public void notifyNewGame(StatGame 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();
}
}
public void connectionToGame(StatGame game){
// envoi à la machine qui a crée la game
public void connectionToGame(StatGame 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();
}
// envoi à tout ceux qui sont dans la game logiquement, paramètre à revoir
public void leaveGame(PublicUser user){
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();
}
}
// envoi à tout le monde
public void sendDiscovery(PublicUser user, List<Inet4Address> listIpTarget) {
for (int i = 0; i < listIpTarget.size(); i++) {
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));
}
}
public void sendDiscovery(PublicUser user, List<Inet4Address> listIpTarget){
for (int i = 0; i < listIpTarget.size(); i++) {
M_GetIP m_getIp = new M_GetIP();
Sender os = new Sender(listIpTarget.get(i).toString(), 80, m_getIp);
new Thread(os).start();
discoCtrl.addIP(listIpTarget.get(i));
}
// envoi à l'id
public void getPublicUserProfile(String id){
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 si success
public void joinGameResponse (boolean success, String id, StatGame game){
/*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();
}*/
}
}
......@@ -6,6 +6,7 @@
package com.utclo23.com;
import java.net.Inet4Address;
import java.util.ArrayList;
import java.util.List;
/**
......@@ -13,36 +14,35 @@ import java.util.List;
* @author Thomas
*/
public class DiscoveryController {
// TODO: add a lock on this one
private List<Inet4Address>getIpIssuedList;
private DiscoveryController(){
}
// 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;
}
// 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;
}
}
......@@ -9,109 +9,116 @@ 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;
/**
*
* @author Thomas
*/
public class KnownIPController {
// All IP known by this node
// TODO: add a lock on this one
private HashMap<String, Inet4Address> knownIp;
IDataCom iDataCom;
// private constructor
private KnownIPController(){
}
// SINGLETON
// Holder
private static class SingletonHolder
{
private final static KnownIPController instance = new KnownIPController();
}
// Access point for unique instance of the singleton class
public static KnownIPController getInstance()
{
return SingletonHolder.instance;
}
// 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){
}
}
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;
}
// All IP known by this node
// TODO: add a lock on this one
private final HashMap<String, Inet4Address> knownIp;
IDataCom iDataCom;
InterfaceAddress usedInterface;
// private constructor
private KnownIPController() {
knownIp = new HashMap<>();
}
// SINGLETON
// Holder
private static class SingletonHolder {
private final static KnownIPController INSTANCE = new KnownIPController();
}
// Access point for unique INSTANCE of the singleton class
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) {}
}
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;
}
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;
}
}
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;
}
}
public void addNode(String id, Inet4Address ip) {
knownIp.put(id, ip);
}
public void addNonExistingNodes(HashMap<String, Inet4Address> hashToCheck) {
Iterator it = hashToCheck.entrySet().iterator();
while (it.hasNext()) {
HashMap.Entry pair = (HashMap.Entry) it.next();
if (!knownIp.containsKey(pair.getKey())) {
knownIp.put((String) pair.getKey(), (Inet4Address) pair.getValue());
}
it.remove(); // avoids a ConcurrentModificationException
}
}
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;
}
}
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;
}
}
public void addNode(String id, Inet4Address ip)
{
knownIp.put(id, ip);
}
public void addNonExistingNodes(HashMap<String, Inet4Address> hashToCheck)
{
Iterator it = hashToCheck.entrySet().iterator();
while (it.hasNext()) {
HashMap.Entry pair = (HashMap.Entry)it.next();
if(!knownIp.containsKey(pair.getKey())){
knownIp.put((String)pair.getKey(), (Inet4Address)pair.getValue());
}
it.remove(); // avoids a ConcurrentModificationException
}
}
}
......@@ -22,7 +22,6 @@ public class Receiver implements Runnable {
ServerSocket serverSocket;
Socket client;
Message request;
ObjectOutputStream out;
ObjectInputStream in;
IDataCom iDataCom;
......@@ -31,25 +30,26 @@ public class Receiver implements Runnable {
serverSocket = new ServerSocket(port);
iDataCom = dataCom;
}catch(IOException e){
e.printStackTrace();
}
}
@Override
public void run() {
while (true) {
try {
client = serverSocket.accept();
out = new ObjectOutputStream(client.getOutputStream());
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();
out.close();
in.close();
} catch (IOException e) {
......
......@@ -5,12 +5,16 @@
*/
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;
/**
*
......@@ -23,26 +27,28 @@ public class Sender implements Runnable {
String ip;
Message request;
ObjectOutputStream out;
ObjectInputStream in;
//ObjectInputStream in;
public Sender(String ip, int port, Message request) {
ip = ip;
port = port;
request = request;
this.ip = ip;
this.port = port;
this.request = request;
}
@Override
public void run() {
try {
socket = new Socket(ip, port);
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());
//in = new ObjectInputStream(socket.getInputStream());
request.setIpSender(kIpCtrl.getMyInetAddress());
out.writeObject(request);
in.close();
//in.close();
out.close();
socket.close();
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
......
......@@ -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){