Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Quentin DRUAULT-AUBIN
lo23-project
Commits
d1d713fd
Commit
d1d713fd
authored
Dec 18, 2017
by
dkonam
Browse files
Merge branch 'IHM-Main/secondinteg' into Data/develop
parents
1815c4b5
299b8aa7
Changes
37
Hide whitespace changes
Inline
Side-by-side
Battleship/src/main/java/com/utclo23/com/ComFacade.java
View file @
d1d713fd
...
...
@@ -10,10 +10,18 @@ import com.utclo23.data.structure.Mine;
import
com.utclo23.data.structure.StatGame
;
import
com.utclo23.com.messages.*
;
import
com.utclo23.data.facade.IDataCom
;
import
com.utclo23.data.structure.Game
;
import
com.utclo23.data.structure.LightPublicUser
;
import
java.net.Inet4Address
;
import
java.net.InterfaceAddress
;
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
...
...
@@ -27,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
)
{
...
...
@@ -54,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
_p
laceship
=
new
M_PlaceShip
(
iDataCom
.
getMyPublicUserProfile
(),
listShips
);
M_PlaceShip
m
P
laceship
=
new
M_PlaceShip
(
iDataCom
.
getMyPublicUserProfile
(),
listShips
);
for
(
LightPublicUser
recipient
:
recipients
)
{
Sender
os
=
new
Sender
(
kIpCtrl
.
getHashMap
().
get
(
recipient
.
getId
()).
getHostAddress
(),
kIpCtrl
.
getPort
(),
m
_p
laceship
);
Sender
os
=
new
Sender
(
kIpCtrl
.
getHashMap
().
get
(
recipient
.
getId
()).
getHostAddress
(),
kIpCtrl
.
getPort
(),
m
P
laceship
);
new
Thread
(
os
).
start
();
}
}
...
...
@@ -66,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
_d
econnection
=
new
M_Deconnection
(
iDataCom
.
getMyPublicUserProfile
());
M_Deconnection
m
D
econnection
=
new
M_Deconnection
(
iDataCom
.
getMyPublicUserProfile
());
for
(
Inet4Address
ip
:
kIpCtrl
.
getHashMap
().
values
())
{
Sender
os
=
new
Sender
(
ip
.
getHostAddress
(),
kIpCtrl
.
getPort
(),
m
_d
econnection
);
Sender
os
=
new
Sender
(
ip
.
getHostAddress
(),
kIpCtrl
.
getPort
(),
m
D
econnection
);
new
Thread
(
os
).
start
();
}
}
...
...
@@ -91,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
_c
hat
=
new
M_Chat
(
iDataCom
.
getMyPublicUserProfile
(),
message
,
message
.
getTimestamp
()
);
M_Chat
m
C
hat
=
new
M_Chat
(
iDataCom
.
getMyPublicUserProfile
(),
message
);
for
(
LightPublicUser
recipient
:
message
.
getRecipients
())
{
Sender
os
=
new
Sender
(
kIpCtrl
.
getHashMap
().
get
(
recipient
.
getId
()).
getHostAddress
(),
kIpCtrl
.
getPort
(),
m
_c
hat
);
Sender
os
=
new
Sender
(
kIpCtrl
.
getHashMap
().
get
(
recipient
.
getId
()).
getHostAddress
(),
kIpCtrl
.
getPort
(),
m
C
hat
);
new
Thread
(
os
).
start
();
}
}
...
...
@@ -105,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
_p
lace
m
ine
=
new
M_PlaceMine
(
iDataCom
.
getMyPublicUserProfile
(),
mine
);
M_PlaceMine
m
P
lace
M
ine
=
new
M_PlaceMine
(
iDataCom
.
getMyPublicUserProfile
(),
mine
);
for
(
LightPublicUser
recipient
:
recipients
)
{
Sender
os
=
new
Sender
(
kIpCtrl
.
getHashMap
().
get
(
recipient
.
getId
()).
getHostAddress
(),
kIpCtrl
.
getPort
(),
m
_p
lace
m
ine
);
Sender
os
=
new
Sender
(
kIpCtrl
.
getHashMap
().
get
(
recipient
.
getId
()).
getHostAddress
(),
kIpCtrl
.
getPort
(),
m
P
lace
M
ine
);
new
Thread
(
os
).
start
();
}
}
...
...
@@ -118,11 +117,12 @@ public class ComFacade {
*
* @param game is the new created game
*/
public
void
notifyNewGame
(
StatGame
game
)
{
M_CreationGame
m
_c
reation
g
ame
=
new
M_CreationGame
(
iDataCom
.
getMyPublicUserProfile
(),
game
);
public
void
notifyNewGame
(
StatGame
game
)
{
M_CreationGame
m
C
reation
G
ame
=
new
M_CreationGame
(
iDataCom
.
getMyPublicUserProfile
(),
game
);
for
(
Inet4Address
ip
:
kIpCtrl
.
getHashMap
().
values
())
{
Sender
os
=
new
Sender
(
ip
.
getHostAddress
(),
kIpCtrl
.
getPort
(),
m_creationgame
);
new
Thread
(
os
).
start
();
Sender
os
=
new
Sender
(
ip
.
getHostAddress
(),
kIpCtrl
.
getPort
(),
mCreationGame
);
Thread
thread
=
new
Thread
(
os
);
thread
.
start
();
}
}
...
...
@@ -130,12 +130,13 @@ public class ComFacade {
* Called to send a request to join a specified game.
*
* @param game is a game the user wants to join
* @param role is player or spectator
*/
public
void
connectionToGame
(
StatGame
game
)
{
public
void
connectionToGame
(
StatGame
game
,
String
role
)
{
M_JoinGame
m
_j
oin
g
ame
=
new
M_JoinGame
(
iDataCom
.
getMyPublicUserProfile
(),
game
);
M_JoinGame
m
J
oin
G
ame
=
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
_j
oin
g
ame
);
Sender
os
=
new
Sender
(
adr
.
getHostAddress
(),
kIpCtrl
.
getPort
(),
m
J
oin
G
ame
);
new
Thread
(
os
).
start
();
}
...
...
@@ -143,9 +144,9 @@ public class ComFacade {
* Called to send "leave game" notification to everybody.
*/
public
void
leaveGame
()
{
M_LeaveGame
m
_l
eave
g
ame
=
new
M_LeaveGame
(
iDataCom
.
getMyPublicUserProfile
());
M_LeaveGame
m
L
eave
G
ame
=
new
M_LeaveGame
(
iDataCom
.
getMyPublicUserProfile
());
for
(
Inet4Address
ip
:
kIpCtrl
.
getHashMap
().
values
())
{
Sender
os
=
new
Sender
(
ip
.
getHostAddress
(),
kIpCtrl
.
getPort
(),
m
_l
eave
g
ame
);
Sender
os
=
new
Sender
(
ip
.
getHostAddress
(),
kIpCtrl
.
getPort
(),
m
L
eave
G
ame
);
new
Thread
(
os
).
start
();
}
}
...
...
@@ -156,11 +157,31 @@ public class ComFacade {
* @param listIpTarget is the list of known ip targets
*/
public
void
sendDiscovery
(
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
(),
kIpCtrl
.
getPort
(),
m_getIp
);
new
Thread
(
os
).
start
();
discoCtrl
.
addIP
(
listIpTarget
.
get
(
i
));
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
());
// Suppression des doublons et de nous même au cas où
// dans listIpTarget
Set
set
=
new
HashSet
();
set
.
addAll
(
listIpTarget
);
if
(
set
.
contains
(
kIpCtrl
.
getMyInetAddress
()))
set
.
remove
(
kIpCtrl
.
getMyInetAddress
());
List
<
Inet4Address
>
newIpTarget
=
new
ArrayList
(
set
);
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
mBleu
=
new
M_Bleu
(
iDataCom
.
getMyPublicUserProfile
(),
tmpHash
,
tmp
,
iDataCom
.
getGameList
(),
otherTargets
);
Sender
os
=
new
Sender
(
ipDest
.
getHostAddress
(),
kIpCtrl
.
getPort
(),
mBleu
);
new
Thread
(
os
).
start
();
}
}
}
...
...
@@ -170,8 +191,8 @@ public class ComFacade {
* @param id is the UID of the recipient
*/
public
void
getPublicUserProfile
(
String
id
)
{
M_GetPlayerInfo
m
_g
et
p
layer
i
nfo
=
new
M_GetPlayerInfo
(
iDataCom
.
getMyPublicUserProfile
());
Sender
os
=
new
Sender
(
kIpCtrl
.
getHashMap
().
get
(
id
).
getHostAddress
(),
kIpCtrl
.
getPort
(),
m
_g
et
p
layer
i
nfo
);
M_GetPlayerInfo
m
G
et
P
layer
I
nfo
=
new
M_GetPlayerInfo
(
iDataCom
.
getMyPublicUserProfile
());
Sender
os
=
new
Sender
(
kIpCtrl
.
getHashMap
().
get
(
id
).
getHostAddress
(),
kIpCtrl
.
getPort
(),
m
G
et
P
layer
I
nfo
);
new
Thread
(
os
).
start
();
}
...
...
@@ -185,15 +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
_j
oin
g
ame
r
esponse
);
Sender
os
=
new
Sender
(
ip
.
getHostAddress
(),
kIpCtrl
.
getPort
(),
m
J
oin
G
ame
R
esponse
);
new
Thread
(
os
).
start
();
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
_j
oin
g
ame
r
esponse
);
Sender
os
=
new
Sender
(
kIpCtrl
.
getHashMap
().
get
(
id
).
getHostAddress
(),
kIpCtrl
.
getPort
(),
m
J
oin
G
ame
R
esponse
);
new
Thread
(
os
).
start
();
Logger
.
getLogger
(
ComFacade
.
class
.
getName
()).
log
(
Level
.
INFO
,
null
,
"Fail joinGame"
);
}
}
}
Battleship/src/main/java/com/utclo23/com/DiscoveryController.java
deleted
100644 → 0
View file @
1815c4b5
/*
* 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
);
}
}
Battleship/src/main/java/com/utclo23/com/KnownIPController.java
View file @
d1d713fd
...
...
@@ -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
>
tmp
h
ash
=
knownIp
;
HashMap
<
String
,
Inet4Address
>
tmp
H
ash
=
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
;
}
...
...
Battleship/src/main/java/com/utclo23/com/Receiver.java
View file @
d1d713fd
...
...
@@ -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
e
x
){
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;
}*/
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
);
}
}
}
}
Battleship/src/main/java/com/utclo23/com/Sender.java
View file @
d1d713fd
...
...
@@ -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
(
"m
essage sent"
+
request
.
getClass
().
toString
());
Logger
.
getLogger
(
ComFacade
.
class
.
getName
()).
log
(
Level
.
INFO
,
null
,
"M
essage 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
);
}
}
}
Battleship/src/main/java/com/utclo23/com/messages/M_Bleu.java
0 → 100644
View file @
d1d713fd
/*
* 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.messages
;
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.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
{
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
.
hashMapReceived
=
hashMap
;
this
.
usersReceived
=
users
;
this
.
listGamesReceived
=
listGames
;
this
.
otherTargets
=
oTargets
;
}
@Override
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
>
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
)
{
// 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
(),
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
(
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
();
newPairs
.
put
(
entry
.
getKey
(),
entry
.
getValue
());
}
}
for
(
LightPublicUser
key
:
this
.
usersReceived
)
{
if
(!
key
.
equals
(
iDataCom
.
getMyPublicUserProfile
().
getLightPublicUser
())
&&
!
myUsersProfile
.
contains
(
key
))
{
newUsersProfile
.
add
(
key
);
}
}
// Maj de nos listes avec les nouvelles données reçues
// maj de la liste des games
for
(
StatGame
game
:
this
.
listGamesReceived
)
{
if
(!
iDataCom
.
getGameList
().
contains
(
game
))
{
iDataCom
.
addNewGame
(
game
);
}
}
for
(
Map
.
Entry
<
String
,
Inet4Address
>
entry
:
newPairs
.
entrySet
())
{
KnownIPController
.
getInstance
().
getHashMap
().
put
(
entry
.
getKey
(),
entry
.
getValue
());
}