346 行
12 KiB
Java
346 行
12 KiB
Java
package controller;
|
|
|
|
import java.awt.Color;
|
|
import java.beans.PropertyChangeEvent;
|
|
import java.beans.PropertyChangeListener;
|
|
import java.beans.PropertyChangeSupport;
|
|
import java.io.IOException;
|
|
import java.net.InetAddress;
|
|
import java.net.SocketException;
|
|
import java.util.ArrayList;
|
|
|
|
import javax.swing.text.BadLocationException;
|
|
import javax.swing.text.SimpleAttributeSet;
|
|
import javax.swing.text.StyleConstants;
|
|
import javax.swing.text.StyledDocument;
|
|
|
|
import config.Configuration;
|
|
import gui.FenetreChat;
|
|
import gui.FenetreMenu;
|
|
import model.*;
|
|
import network.*;
|
|
import server.Request;
|
|
import test.App;
|
|
|
|
public class Agent implements PropertyChangeListener{
|
|
private Contact user;
|
|
private UDPInput BIn;
|
|
private UDPOutput BOut;
|
|
//private TCPChat TCPSend;
|
|
private TCPServer server;
|
|
//private ListeContacts list;
|
|
private InetAddress localAddress;
|
|
private InetAddress broadcast;
|
|
private ArrayList<TCPChat> listTCPOk;
|
|
private ArrayList<TCPChat> listTCPDeg;
|
|
private DataBase db;
|
|
|
|
private PropertyChangeSupport support;
|
|
|
|
|
|
|
|
public Agent(InetAddress address, int portIn, int portOut) throws IOException {
|
|
this.user = new Contact("", address, portIn);
|
|
this.BIn = new UDPInput(user.getAddress(), portIn);
|
|
this.BOut = new UDPOutput(user, portOut);
|
|
this.server = new TCPServer(user.getPort(), user.getAddress());
|
|
this.server.addPropertyChangeListener(this);
|
|
//this.TCPSend = new TCPChat(user);
|
|
//this.list = ListeContacts.getInstance();
|
|
this.localAddress=Tools.getAdress()[0];
|
|
this.broadcast= Tools.getAdress()[1];
|
|
this.db = DataBase.getInstance();
|
|
this.listTCPOk = new ArrayList<TCPChat>();
|
|
this.listTCPDeg = new ArrayList<TCPChat>();
|
|
this.support = new PropertyChangeSupport(this);
|
|
this.BIn.addPropertyChangeListener(this);
|
|
BIn.start();
|
|
}
|
|
|
|
//user ayant déjà fait une connexion
|
|
public Agent(InetAddress address, int portIn, int portOut, String login) throws IOException {
|
|
this.db = DataBase.getInstance();
|
|
this.user = new Contact(db.getPseudoFromLogin(login), address, portIn);
|
|
this.BIn = new UDPInput(user.getAddress(), portIn);
|
|
this.BOut = new UDPOutput(user, portOut);
|
|
this.server = new TCPServer(user.getPort(), user.getAddress());
|
|
this.server.addPropertyChangeListener(this);
|
|
//this.TCPSend = new TCPChat(user);
|
|
//this.list = ListeContacts.getInstance();
|
|
this.localAddress=Tools.getAdress()[0];
|
|
this.broadcast= Tools.getAdress()[1];
|
|
this.listTCPOk = new ArrayList<TCPChat>();
|
|
this.listTCPDeg = new ArrayList<TCPChat>();
|
|
this.support = new PropertyChangeSupport(this);
|
|
this.BIn.addPropertyChangeListener(this);
|
|
BIn.start();
|
|
}
|
|
|
|
|
|
public void propertyChange(PropertyChangeEvent evt) {
|
|
switch (evt.getPropertyName()) {
|
|
case "Msg UDP Recu" :
|
|
//System.out.println("envoi message");
|
|
Message msg = BIn.getMessage();
|
|
try {
|
|
handleMessage(msg);
|
|
} catch (IOException e) {
|
|
e.printStackTrace();
|
|
}
|
|
break;
|
|
case "session chat demarree" :
|
|
//System.out.println("dans session demarree");
|
|
TCPChat tcpChat = this.server.getConnexionTCPChat();
|
|
tcpChat.addPropertyChangeListener(this);
|
|
this.listTCPOk.add(tcpChat);
|
|
break;
|
|
|
|
case "Msg TCP Recu" :
|
|
SimpleAttributeSet left = new SimpleAttributeSet();
|
|
StyleConstants.setAlignment(left, StyleConstants.ALIGN_LEFT);
|
|
StyleConstants.setForeground(left, Color.RED);
|
|
//System.out.println("dans property change");
|
|
TCPChat tcpChatCo = this.listTCPOk.get(0);
|
|
MessageChat msgRecu = tcpChatCo.getMessageRecu();
|
|
//FenetreChat.getTa().append("kevin"+ " : "+msgRecu.getMessage()+"\n");
|
|
if(FenetreChat.isOuvert()) {
|
|
StyledDocument doc = FenetreChat.getDoc();
|
|
try {
|
|
doc.insertString(doc.getLength(), "\n"+msgRecu.getMessage(), left );
|
|
} catch (BadLocationException e) {
|
|
e.printStackTrace();
|
|
}
|
|
doc.setParagraphAttributes(doc.getLength(), 1, left, false);
|
|
break;
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*type 0 : premier message de broadcast
|
|
* type, 1 : deuxieme message de broadcast avec pseudo
|
|
* type 2 : Message de déconnexion
|
|
* type 3 : message pseudo changé
|
|
* type 4 : Message affichage
|
|
* type 5 : Message chat
|
|
*/
|
|
|
|
public void handleMessage(Message msg) throws IOException {
|
|
switch(msg.getTypeMessage()) {
|
|
case 0 :
|
|
//System.out.println("Envoi message présentation");
|
|
MessagePseudo msgPresentation = new MessagePseudo(this.localAddress, msg.getAddressSrc(), this.user.getPort(), msg.getPortSrc(), 1, this.user.getPseudo(), this.user.getId());
|
|
BOut.send(msgPresentation, msg.getAddressSrc(), msg.getPortSrc());
|
|
break;
|
|
case 1 :
|
|
MessagePseudo msgPseudo = (MessagePseudo) msg;
|
|
boolean pseudoExist = Request.sendPseudo(msgPseudo.getPseudo(), "pseudoOK");
|
|
if(pseudoExist) {
|
|
Contact newUser = new Contact(msgPseudo.getPseudo(), msgPseudo.getAddressSrc(), msgPseudo.getPortSrc());
|
|
Request.sendUser(msgPseudo.getPseudo(), msgPseudo.getAddressSrc().toString(), Integer.toString(msgPseudo.getPortSrc()), "addUser");
|
|
//list.addContact(newUser);
|
|
if(FenetreMenu.ouvert) {
|
|
FenetreMenu.getCb().addItem(newUser.getPseudo());
|
|
}
|
|
}
|
|
break;
|
|
case 2 :
|
|
MessagePseudo messageDeconnexion = (MessagePseudo) msg;
|
|
boolean pseudoExiste = Request.sendPseudo(messageDeconnexion.getPseudo(), "pseudoOK");
|
|
if(!pseudoExiste){
|
|
Request.sendDeconnexion(Integer.toString(messageDeconnexion.getId()),messageDeconnexion.getPseudo(), "deconnexion");
|
|
FenetreMenu.getCb().removeItem(messageDeconnexion.getPseudo());
|
|
}
|
|
case 3 :
|
|
MessagePseudo messageNewPseudo = (MessagePseudo) msg;
|
|
//Contact contact = list.findContact(messageNewPseudo.getPseudo());
|
|
//int id = this.db.getIdFromPseudo(contact.getPseudo());
|
|
for(int i = 0 ; i<FenetreMenu.getCb().getItemCount() ; i++){//Combo étant ton JComboBox
|
|
String pseudo = (String) FenetreMenu.getCb().getItemAt(i);
|
|
if(this.db.getIdFromPseudo(pseudo)==messageNewPseudo.getId()) {
|
|
FenetreMenu.getCb().removeItem(pseudo);
|
|
FenetreMenu.getCb().addItem(messageNewPseudo.getPseudo());
|
|
}
|
|
}
|
|
FenetreMenu.getCb().removeItem(messageNewPseudo.getPseudo());
|
|
}
|
|
|
|
}
|
|
|
|
public boolean connectInscription(String pseudo, String login) throws IOException, InterruptedException {
|
|
MessagePseudo msg_connexion = new MessagePseudo(this.localAddress, this.broadcast, this.user.getPort(), App.portDest, 0, "", this.user.getId());
|
|
BOut.send(msg_connexion, this.broadcast, App.portDest);
|
|
Thread.sleep(5000);
|
|
ArrayList<MessagePseudo> msg_recus = BIn.getListMessage();
|
|
for(int i =0; i<msg_recus.size(); i++) {
|
|
handleMessage(msg_recus.get(i));
|
|
}
|
|
|
|
boolean pseudoOK = choisirPseudo(pseudo);
|
|
//list.addContact(user);
|
|
|
|
if(pseudoOK){
|
|
Request.sendUser(pseudo, this.localAddress.toString(), Integer.toString(App.portSrc), "addUser");
|
|
int id = db.addUser(pseudo, login);
|
|
this.user.setId(id);
|
|
MessagePseudo msgPresentation = new MessagePseudo(this.localAddress, this.broadcast, this.user.getPort(), App.portDest, 1, this.user.getPseudo(), this.user.getId());
|
|
BOut.send(msgPresentation, this.broadcast, App.portDest);
|
|
this.user.setStatut("En ligne");
|
|
//System.out.println("Connexion réussie avec le pseudo : "+pseudo);
|
|
}else {
|
|
System.out.println("Echec de la connexion, veuillez choisir un autre pseudo");
|
|
}
|
|
return pseudoOK;
|
|
}
|
|
|
|
public boolean connectConnexion(String pseudo, String login) throws IOException, InterruptedException {
|
|
MessagePseudo msg_connexion = new MessagePseudo(this.localAddress, this.broadcast, this.user.getPort(), App.portDest, 0, "", this.user.getId());
|
|
BOut.send(msg_connexion, this.broadcast, App.portDest);
|
|
Thread.sleep(5000);
|
|
ArrayList<MessagePseudo> msg_recus = BIn.getListMessage();
|
|
for(int i =0; i<msg_recus.size(); i++) {
|
|
handleMessage(msg_recus.get(i));
|
|
}
|
|
boolean pseudoOK = choisirPseudo(pseudo);
|
|
//list.addContact(user);
|
|
if(pseudoOK){
|
|
Request.sendUser(pseudo, this.localAddress.toString(), Integer.toString(App.portSrc), "addUser");
|
|
int id = db.getIdFromLogin(login);
|
|
this.user.setId(id);
|
|
MessagePseudo msgPresentation = new MessagePseudo(this.localAddress, this.broadcast, this.user.getPort(), App.portDest, 1, this.user.getPseudo(), this.user.getId());
|
|
BOut.send(msgPresentation, this.broadcast, App.portDest);
|
|
db.updateStatus(id, "En ligne");
|
|
this.user.setStatut("En ligne");
|
|
//System.out.println("Connexion réussie avec le pseudo : "+pseudo);
|
|
}else {
|
|
System.out.println("Echec de la connexion, veuillez choisir un autre pseudo");
|
|
}
|
|
return pseudoOK;
|
|
}
|
|
|
|
public boolean choisirPseudo(String pseudo) throws IOException {
|
|
boolean pseudoExist = Request.sendPseudo(pseudo, "pseudoOK");
|
|
|
|
if(pseudoExist){
|
|
user.setPseudo(pseudo);
|
|
//System.out.println("Pseudo set : "+pseudo);
|
|
return true;
|
|
}else {
|
|
System.out.println("Pseudo déjà utilisé");
|
|
return false;
|
|
}
|
|
}
|
|
|
|
public void deconnexion() throws IOException {
|
|
Request.sendDeconnexion(Integer.toString(this.user.getId()),this.user.getPseudo(), "deconnexion");
|
|
MessagePseudo msgDeconnexion = new MessagePseudo(this.localAddress, this.broadcast, App.portSrc, App.portDest, 2, this.user.getPseudo(), this.user.getId());
|
|
BOut.send(msgDeconnexion, this.broadcast, App.portSrc);
|
|
//list.deleteContact(user);
|
|
System.exit(1);
|
|
}
|
|
|
|
public boolean changerPseudo(String pseudo) throws IOException {
|
|
String ancienPseudo = user.getPseudo();
|
|
boolean changeOK = choisirPseudo(pseudo);
|
|
if(changeOK){
|
|
//System.out.println("Pseudo changé à : "+pseudo);
|
|
db.updatePseudo(this.user.getId(), pseudo);
|
|
//list.modifierListe(ancienPseudo, pseudo);
|
|
Request.sendPseudoChange(ancienPseudo, pseudo, "pseudoChanged");
|
|
MessagePseudo msgNewPseudo = new MessagePseudo(this.localAddress, this.broadcast, App.portSrc, App.portDest, 3, pseudo, this.user.getId());
|
|
BOut.send(msgNewPseudo, this.broadcast, App.portSrc);
|
|
return true;
|
|
}else {
|
|
//System.out.println("Veuillez choisir un autre pseudo");
|
|
return false;
|
|
}
|
|
}
|
|
|
|
public boolean chatExists(int destID) {
|
|
boolean chatExists = false;
|
|
for(TCPChat tcp : listTCPDeg) {
|
|
if(tcp.getDestID()==destID) {
|
|
chatExists = true;
|
|
}
|
|
}
|
|
return chatExists;
|
|
}
|
|
|
|
public void createChat (int destId, String destPseudo) throws IOException {
|
|
boolean chatExists = chatExists(destId);
|
|
if(!chatExists) {
|
|
boolean isNotOnList = Request.sendPseudo(destPseudo, "pseudoOK");
|
|
if(!isNotOnList) {
|
|
Contact dest = Request.getUser(destPseudo, "getUser");
|
|
TCPChat connexionTCP = new TCPChat(user, destId);
|
|
listTCPDeg.add(connexionTCP);
|
|
connexionTCP.addPropertyChangeListener(this);
|
|
}
|
|
/*for(Contact user : list.getListe()) {
|
|
if(user.getPseudo().equals(destPseudo)) {
|
|
TCPChat connexionTCP = new TCPChat(user, destId);
|
|
listTCPDeg.add(connexionTCP);
|
|
connexionTCP.addPropertyChangeListener(this);
|
|
}
|
|
}*/
|
|
}
|
|
}
|
|
|
|
|
|
public void addPropertyChangeListener(PropertyChangeListener pcl, String propertyName){
|
|
this.support.addPropertyChangeListener(propertyName, pcl);
|
|
}
|
|
|
|
public void deletePropertyChangeListener(PropertyChangeListener pcl, String propertyName){
|
|
this.support.removePropertyChangeListener(propertyName,pcl);
|
|
}
|
|
|
|
|
|
|
|
public Contact getUser() {
|
|
return user;
|
|
}
|
|
|
|
|
|
|
|
public void setUser(Contact user) {
|
|
this.user = user;
|
|
}
|
|
|
|
|
|
|
|
/*public ListeContacts getList() {
|
|
return list;
|
|
}
|
|
|
|
|
|
|
|
public void setList(ListeContacts list) {
|
|
this.list = list;
|
|
}*/
|
|
|
|
|
|
public DataBase getDb() {
|
|
return db;
|
|
}
|
|
|
|
|
|
|
|
public void setDb(DataBase db) {
|
|
this.db = db;
|
|
}
|
|
|
|
|
|
public ArrayList<TCPChat> getListeTCPChat(){
|
|
return listTCPDeg;
|
|
}
|
|
|
|
public TCPChat getTCPChat() {
|
|
return this.listTCPDeg.remove(0);
|
|
}
|
|
|
|
}
|