avant checkout application
This commit is contained in:
parent
ea734e0ccc
commit
d1b75e16bf
6 changed files with 214 additions and 39 deletions
|
@ -4,11 +4,10 @@ import java.io.IOException;
|
|||
import java.net.InetAddress;
|
||||
import java.net.UnknownHostException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
|
||||
import main.Observer;
|
||||
import main.Utilisateur;
|
||||
import main.VueStandard;
|
||||
|
||||
import messages.*;
|
||||
|
||||
|
||||
|
@ -20,6 +19,7 @@ public class CommunicationUDP extends Thread {
|
|||
private int portServer;
|
||||
private ArrayList<Integer> portOthers;
|
||||
private ArrayList<Utilisateur> users = new ArrayList<Utilisateur>();
|
||||
|
||||
private Observer observer;
|
||||
|
||||
public CommunicationUDP(int portClient, int portServer, int[] portsOther) throws IOException {
|
||||
|
@ -29,12 +29,19 @@ public class CommunicationUDP extends Thread {
|
|||
this.client = new UDPClient(portClient);
|
||||
}
|
||||
|
||||
public void setObserver (Observer obs) {
|
||||
this.observer=obs;
|
||||
|
||||
private ArrayList<Integer> getArrayListFromArray(int ports[]) {
|
||||
ArrayList<Integer> tmp = new ArrayList<Integer>();
|
||||
for (int port : ports) {
|
||||
tmp.add(port);
|
||||
}
|
||||
tmp.remove(Integer.valueOf(portServer));
|
||||
|
||||
return tmp;
|
||||
}
|
||||
|
||||
public ArrayList<Utilisateur> getListUsers(){
|
||||
return users;
|
||||
public void setObserver (Observer obs) {
|
||||
this.observer=obs;
|
||||
}
|
||||
|
||||
protected boolean containsUserFromID(String id) {
|
||||
|
@ -56,15 +63,23 @@ public class CommunicationUDP extends Thread {
|
|||
return false;
|
||||
}
|
||||
|
||||
//Marche pas
|
||||
private int getIndexFromID(String id) {
|
||||
int index = -1;
|
||||
public int getPortFromPseudo(String pseudo) {
|
||||
for(int i=0; i < users.size() ; i++) {
|
||||
if(users.get(i).getId().contentEquals(id) ) {
|
||||
index=i;
|
||||
|
||||
if(users.get(i).getPseudo().equals(pseudo) ) {
|
||||
return users.get(i).getPort();
|
||||
}
|
||||
}
|
||||
return index;
|
||||
return -1;
|
||||
}
|
||||
|
||||
private int getIndexFromID(String id) {
|
||||
for(int i=0; i < users.size() ; i++) {
|
||||
if(users.get(i).getId().equals(id) ) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
private int getIndexFromIP(InetAddress ip) {
|
||||
|
@ -77,21 +92,21 @@ public class CommunicationUDP extends Thread {
|
|||
}
|
||||
|
||||
|
||||
protected synchronized void addUser(String idClient, String pseudoClient, InetAddress ipClient) throws IOException {
|
||||
users.add(new Utilisateur(idClient, pseudoClient, ipClient));
|
||||
protected synchronized void addUser(String idClient, String pseudoClient, InetAddress ipClient, int port) throws IOException {
|
||||
users.add(new Utilisateur(idClient, pseudoClient, ipClient, port));
|
||||
observer.update(this, users);
|
||||
|
||||
}
|
||||
|
||||
protected synchronized void changePseudoUser(String idClient, String pseudoClient, InetAddress ipClient) {
|
||||
protected synchronized void changePseudoUser(String idClient, String pseudoClient, InetAddress ipClient, int port) {
|
||||
int index = getIndexFromID(idClient);
|
||||
users.get(index).setPseudo(pseudoClient);
|
||||
observer.update(this, users);
|
||||
}
|
||||
|
||||
|
||||
protected synchronized void removeUser(String idClient, String pseudoClient,InetAddress ipClient) {
|
||||
protected synchronized void removeUser(String idClient, String pseudoClient,InetAddress ipClient, int port) {
|
||||
int index = getIndexFromIP(ipClient);
|
||||
//System.out.println("index : "+index);
|
||||
if( index != -1) {
|
||||
users.remove(index);
|
||||
}
|
||||
|
@ -105,16 +120,6 @@ public class CommunicationUDP extends Thread {
|
|||
}
|
||||
}
|
||||
|
||||
private ArrayList<Integer> getArrayListFromArray(int ports[]) {
|
||||
ArrayList<Integer> tmp = new ArrayList<Integer>();
|
||||
for (int port : ports) {
|
||||
tmp.add(port);
|
||||
}
|
||||
tmp.remove(Integer.valueOf(portServer));
|
||||
|
||||
return tmp;
|
||||
}
|
||||
|
||||
|
||||
public void sendMessageConnecte() throws UnknownHostException, IOException {
|
||||
for(int port : this.portOthers) {
|
||||
|
@ -136,10 +141,11 @@ public class CommunicationUDP extends Thread {
|
|||
|
||||
String pseudoSelf =self.getPseudo();
|
||||
String idSelf = self.getId();
|
||||
int portSelf = self.getPort();
|
||||
|
||||
Message msout = null;
|
||||
try {
|
||||
msout = new MessageSysteme(Message.TypeMessage.INFO_PSEUDO, pseudoSelf, idSelf);
|
||||
msout = new MessageSysteme(Message.TypeMessage.INFO_PSEUDO, pseudoSelf, idSelf, portSelf);
|
||||
for(int port : this.portOthers) {
|
||||
this.client.sendMessageUDP_local(msout, port, InetAddress.getLocalHost());
|
||||
}
|
||||
|
@ -156,7 +162,7 @@ public class CommunicationUDP extends Thread {
|
|||
|
||||
Utilisateur self = Utilisateur.getSelf();
|
||||
try {
|
||||
Message msout = new MessageSysteme(Message.TypeMessage.INFO_PSEUDO, self.getPseudo(), self.getId());
|
||||
Message msout = new MessageSysteme(Message.TypeMessage.INFO_PSEUDO, self.getPseudo(), self.getId(), self.getPort());
|
||||
this.client.sendMessageUDP_local(msout, portOther, InetAddress.getLocalHost());
|
||||
} catch (MauvaisTypeMessageException e) {e.printStackTrace();}
|
||||
}
|
||||
|
|
57
POO/src/communication/TCPClient.java
Normal file
57
POO/src/communication/TCPClient.java
Normal file
|
@ -0,0 +1,57 @@
|
|||
package communication;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.ObjectInputStream;
|
||||
import java.io.ObjectOutputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.io.PrintWriter;
|
||||
import java.net.InetAddress;
|
||||
import java.net.Socket;
|
||||
|
||||
import main.Observer;
|
||||
//import main.VueSession;
|
||||
import messages.MessageTexte;
|
||||
import messages.MauvaisTypeMessageException;
|
||||
import messages.Message;
|
||||
import messages.Message.TypeMessage;
|
||||
|
||||
public class TCPClient {
|
||||
|
||||
private Socket sockTCP;
|
||||
private ObjectOutputStream output;
|
||||
private TCPInputThread inputThread;
|
||||
|
||||
//Constructor from a TCP-client socket
|
||||
public TCPClient(Socket sockTCP) throws IOException {
|
||||
this.sockTCP = sockTCP;
|
||||
|
||||
this.output = new ObjectOutputStream(sockTCP.getOutputStream()) ;
|
||||
ObjectInputStream input = new ObjectInputStream(sockTCP.getInputStream());
|
||||
this.inputThread = new TCPInputThread(input);
|
||||
}
|
||||
|
||||
//Constructor from an IP address and a port
|
||||
public TCPClient(InetAddress addr, int port) throws IOException {
|
||||
this(new Socket(addr, port)) ;
|
||||
|
||||
}
|
||||
|
||||
|
||||
public void startInputThread() {
|
||||
this.inputThread.start();
|
||||
}
|
||||
|
||||
|
||||
public void sendMessage(String contenu) throws IOException, MauvaisTypeMessageException {
|
||||
System.out.println("dans write");
|
||||
MessageTexte message = new MessageTexte(TypeMessage.TEXTE, contenu);
|
||||
this.output.writeObject(message);
|
||||
}
|
||||
|
||||
public void setObserverInputThread(Observer o) {
|
||||
this.inputThread.setObserver(o);
|
||||
}
|
||||
}
|
65
POO/src/communication/TCPInputThread.java
Normal file
65
POO/src/communication/TCPInputThread.java
Normal file
|
@ -0,0 +1,65 @@
|
|||
package communication;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.ObjectInputStream;
|
||||
import java.util.Arrays;
|
||||
|
||||
import main.Observer;
|
||||
import messages.Message;
|
||||
|
||||
public class TCPInputThread extends Thread {
|
||||
|
||||
private ObjectInputStream input;
|
||||
private boolean running;
|
||||
private char[] buffer;
|
||||
private Observer obs;
|
||||
|
||||
public TCPInputThread(ObjectInputStream input) {
|
||||
this.input = input;
|
||||
this.running = true;
|
||||
this.buffer = new char[200];
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
|
||||
while (this.running) {
|
||||
try {
|
||||
|
||||
|
||||
System.out.println("dans read");
|
||||
Object o = this.input.readObject();
|
||||
this.obs.update(this, o);
|
||||
|
||||
|
||||
} catch (IOException | ClassNotFoundException e) {
|
||||
this.interrupt();
|
||||
//e.printStackTrace();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void interrupt() {
|
||||
|
||||
try {
|
||||
this.running = false;
|
||||
this.input.close();
|
||||
//this.obs.update(this, this.input);
|
||||
} catch (IOException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
private void flushBuffer() {
|
||||
Arrays.fill(this.buffer, '\u0000');
|
||||
}
|
||||
|
||||
protected void setObserver(Observer o) {
|
||||
this.obs = o;
|
||||
}
|
||||
|
||||
}
|
40
POO/src/communication/TCPServer.java
Normal file
40
POO/src/communication/TCPServer.java
Normal file
|
@ -0,0 +1,40 @@
|
|||
package communication;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.InetAddress;
|
||||
import java.net.ServerSocket;
|
||||
import java.net.Socket;
|
||||
import java.net.UnknownHostException;
|
||||
|
||||
import main.Observer;
|
||||
|
||||
|
||||
public class TCPServer extends Thread {
|
||||
|
||||
private ServerSocket sockListenTCP;
|
||||
private Observer obs;
|
||||
|
||||
public TCPServer(int port) throws UnknownHostException, IOException {
|
||||
this.sockListenTCP = new ServerSocket(port, 5, InetAddress.getLocalHost());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
System.out.println("TCP running");
|
||||
Socket sockAccept;
|
||||
while(true) {
|
||||
try {
|
||||
sockAccept = this.sockListenTCP.accept();
|
||||
this.obs.update(this, sockAccept);
|
||||
} catch (IOException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
public void addObserver(Observer obs) {
|
||||
this.obs = obs;
|
||||
}
|
||||
}
|
|
@ -26,7 +26,7 @@ public class UDPClient {
|
|||
|
||||
//Send a message casted as string to the specified port on localhost
|
||||
protected void sendMessageUDP_local(Message message, int port, InetAddress clientAddress) throws IOException {
|
||||
String messageString=message.toString();
|
||||
String messageString= message.toString();
|
||||
DatagramPacket outpacket = new DatagramPacket(messageString.getBytes(), messageString.length(), clientAddress, port);
|
||||
this.sockUDP.send(outpacket);
|
||||
|
||||
|
|
|
@ -7,6 +7,7 @@ import java.net.SocketException;
|
|||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
|
||||
import main.Utilisateur;
|
||||
import messages.*;
|
||||
|
||||
|
||||
|
@ -36,27 +37,33 @@ public class UDPServer extends Thread {
|
|||
|
||||
switch(msg.getTypeMessage()) {
|
||||
case JE_SUIS_CONNECTE :
|
||||
//System.out.println("first co");
|
||||
int portClient = inPacket.getPort();
|
||||
int portServer = portClient+1;
|
||||
|
||||
this.commUDP.sendMessageInfoPseudo(portServer);
|
||||
if(Utilisateur.getSelf() != null) {
|
||||
//System.out.println("first co");
|
||||
int portClient = inPacket.getPort();
|
||||
int portServer = portClient+1;
|
||||
|
||||
this.commUDP.sendMessageInfoPseudo(portServer);
|
||||
}
|
||||
|
||||
|
||||
break;
|
||||
|
||||
case INFO_PSEUDO :
|
||||
|
||||
if (commUDP.containsUserFromID(((MessageSysteme) msg).getId())) {
|
||||
commUDP.changePseudoUser(((MessageSysteme) msg).getId(), ((MessageSysteme) msg).getPseudo(), inPacket.getAddress());
|
||||
|
||||
if (this.commUDP.containsUserFromID(((MessageSysteme) msg).getId())) {
|
||||
this.commUDP.changePseudoUser(((MessageSysteme) msg).getId(), ((MessageSysteme) msg).getPseudo(), inPacket.getAddress(),((MessageSysteme) msg).getPort());
|
||||
}
|
||||
else {
|
||||
|
||||
commUDP.addUser(((MessageSysteme) msg).getId(), ((MessageSysteme) msg).getPseudo(), inPacket.getAddress());
|
||||
//System.out.println(((MessageSysteme) msg).getId()+", "+((MessageSysteme) msg).getPseudo());
|
||||
this.commUDP.addUser(((MessageSysteme) msg).getId(), ((MessageSysteme) msg).getPseudo(), inPacket.getAddress(), ((MessageSysteme) msg).getPort() );
|
||||
System.out.println(((MessageSysteme) msg).getId()+", "+((MessageSysteme) msg).getPseudo());
|
||||
}
|
||||
break;
|
||||
|
||||
case JE_SUIS_DECONNECTE :
|
||||
commUDP.removeUser( ((MessageSysteme) msg).getId() , ((MessageSysteme) msg).getPseudo(), inPacket.getAddress());
|
||||
this.commUDP.removeUser( ((MessageSysteme) msg).getId() , ((MessageSysteme) msg).getPseudo(), inPacket.getAddress(), inPacket.getPort());
|
||||
break;
|
||||
|
||||
default : //Others types of messages are ignored because they are supposed to be transmitted by TCP and not UDP
|
||||
|
|
Loading…
Reference in a new issue