squelette servlet
This commit is contained in:
parent
c13fb59f25
commit
b0206bc0dd
7 changed files with 382 additions and 16 deletions
194
POO_Server/src/communication/CommunicationUDP.java
Normal file
194
POO_Server/src/communication/CommunicationUDP.java
Normal file
|
@ -0,0 +1,194 @@
|
|||
package communication;
|
||||
|
||||
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.*;
|
||||
|
||||
|
||||
public class CommunicationUDP extends Thread {
|
||||
|
||||
// public enum Mode {PREMIERE_CONNEXION, CHANGEMENT_PSEUDO, DECONNEXION};
|
||||
|
||||
private UDPClient client;
|
||||
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 {
|
||||
this.portServer = portServer;
|
||||
this.portOthers = this.getArrayListFromArray(portsOther);
|
||||
new UDPServer(portServer, this).start();
|
||||
this.client = new UDPClient(portClient);
|
||||
}
|
||||
|
||||
public void setObserver (Observer obs) {
|
||||
this.observer=obs;
|
||||
}
|
||||
|
||||
public ArrayList<Utilisateur> getListUsers(){
|
||||
return users;
|
||||
}
|
||||
|
||||
protected boolean containsUserFromID(String id) {
|
||||
for(Utilisateur u : users) {
|
||||
if(u.getId().equals(id) ) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean containsUserFromPseudo(String pseudo) {
|
||||
for(Utilisateur u : users) {
|
||||
if(u.getPseudo().equals(pseudo) ) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
//Marche pas
|
||||
private int getIndexFromID(String id) {
|
||||
int index = -1;
|
||||
for(int i=0; i < users.size() ; i++) {
|
||||
if(users.get(i).getId().contentEquals(id) ) {
|
||||
index=i;
|
||||
}
|
||||
}
|
||||
return index;
|
||||
}
|
||||
|
||||
private int getIndexFromIP(InetAddress ip) {
|
||||
for(int i=0; i < users.size() ; i++) {
|
||||
if(users.get(i).getIp().equals(ip)) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
||||
protected synchronized void addUser(String idClient, String pseudoClient, InetAddress ipClient) throws IOException {
|
||||
users.add(new Utilisateur(idClient, pseudoClient, ipClient));
|
||||
observer.update(this, users);
|
||||
}
|
||||
|
||||
protected synchronized void changePseudoUser(String idClient, String pseudoClient, InetAddress ipClient) {
|
||||
int index = getIndexFromID(idClient);
|
||||
users.get(index).setPseudo(pseudoClient);
|
||||
observer.update(this, users);
|
||||
}
|
||||
|
||||
|
||||
protected synchronized void removeUser(String idClient, String pseudoClient,InetAddress ipClient) {
|
||||
int index = getIndexFromIP(ipClient);
|
||||
//System.out.println("index : "+index);
|
||||
if( index != -1) {
|
||||
users.remove(index);
|
||||
}
|
||||
observer.update(this, users);
|
||||
}
|
||||
|
||||
public void removeAll(){
|
||||
int oSize = users.size();
|
||||
for(int i=0; i<oSize;i++) {
|
||||
users.remove(0);
|
||||
}
|
||||
}
|
||||
|
||||
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) {
|
||||
try {
|
||||
this.client.sendMessageUDP_local(new MessageSysteme(Message.TypeMessage.JE_SUIS_CONNECTE), port, InetAddress.getLocalHost());
|
||||
} catch (MauvaisTypeMessageException e) {/*Si ça marche pas essayer là*/}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Send the message "add,id,pseudo" to localhost on all the ports in
|
||||
// "portOthers"
|
||||
// This allows the receivers' agent (portOthers) to create or modify an entry with the
|
||||
// data of this agent
|
||||
//Typically used to notify of a name change
|
||||
public void sendMessageInfoPseudo() throws UnknownHostException, IOException {
|
||||
|
||||
Utilisateur self = Utilisateur.getSelf();
|
||||
|
||||
String pseudoSelf =self.getPseudo();
|
||||
String idSelf = self.getId();
|
||||
|
||||
Message msout = null;
|
||||
try {
|
||||
msout = new MessageSysteme(Message.TypeMessage.INFO_PSEUDO, pseudoSelf, idSelf);
|
||||
for(int port : this.portOthers) {
|
||||
this.client.sendMessageUDP_local(msout, port, InetAddress.getLocalHost());
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
//Same, but on only one port
|
||||
//Typically used to give your current name and id to a newly arrived host
|
||||
public void sendMessageInfoPseudo(int portOther) throws UnknownHostException, IOException {
|
||||
|
||||
Utilisateur self = Utilisateur.getSelf();
|
||||
try {
|
||||
Message msout = new MessageSysteme(Message.TypeMessage.INFO_PSEUDO, self.getPseudo(), self.getId());
|
||||
this.client.sendMessageUDP_local(msout, portOther, InetAddress.getLocalHost());
|
||||
} catch (MauvaisTypeMessageException e) {e.printStackTrace();}
|
||||
}
|
||||
|
||||
|
||||
// Send the message "del,id,pseudo" to localhost on all the ports in
|
||||
// "portOthers"
|
||||
// This allows the receivers' agent (portOthers) to delete the entry
|
||||
// corresponding to this agent
|
||||
public void sendMessageDelete() throws UnknownHostException, IOException {
|
||||
for(int port : this.portOthers) {
|
||||
try {
|
||||
this.client.sendMessageUDP_local(new MessageSysteme(Message.TypeMessage.JE_SUIS_DECONNECTE), port, InetAddress.getLocalHost());
|
||||
} catch (MauvaisTypeMessageException e) {}
|
||||
}
|
||||
}
|
||||
|
||||
//Pas encore adapte message
|
||||
// private void sendIDPseudo_broadcast(String prefixe) throws UnknownHostException, IOException {
|
||||
// Utilisateur self = Utilisateur.getSelf();
|
||||
// String idSelf = self.getId();
|
||||
// String pseudoSelf = self.getPseudo();
|
||||
//
|
||||
// String message = prefixe+","+idSelf + "," + pseudoSelf;
|
||||
//
|
||||
//
|
||||
// this.client.sendMessageUDP_broadcast(message, this.portServer);
|
||||
//
|
||||
// }
|
||||
|
||||
// public synchronized void createSenderUDP(int port, Mode mode) throws SocketException {
|
||||
// new SenderUDP(mode, port).start();
|
||||
// }
|
||||
|
||||
}
|
41
POO_Server/src/communication/UDPClient.java
Normal file
41
POO_Server/src/communication/UDPClient.java
Normal file
|
@ -0,0 +1,41 @@
|
|||
package communication;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.DatagramPacket;
|
||||
import java.net.DatagramSocket;
|
||||
import java.net.InetAddress;
|
||||
import java.net.NetworkInterface;
|
||||
import java.net.SocketException;
|
||||
import java.net.UnknownHostException;
|
||||
|
||||
import messages.*;
|
||||
|
||||
public class UDPClient {
|
||||
|
||||
private DatagramSocket sockUDP;
|
||||
private InetAddress broadcast;
|
||||
|
||||
public UDPClient(int port) throws SocketException, UnknownHostException {
|
||||
this.sockUDP = new DatagramSocket(port);
|
||||
|
||||
InetAddress localHost = InetAddress.getLocalHost();
|
||||
NetworkInterface networkInterface = NetworkInterface.getByInetAddress(localHost);
|
||||
this.broadcast = networkInterface.getInterfaceAddresses().get(0).getBroadcast();
|
||||
}
|
||||
|
||||
|
||||
//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();
|
||||
DatagramPacket outpacket = new DatagramPacket(messageString.getBytes(), messageString.length(), clientAddress, port);
|
||||
this.sockUDP.send(outpacket);
|
||||
|
||||
}
|
||||
|
||||
// protected void sendMessageUDP_broadcast(String message, int port) throws IOException{
|
||||
// String messageString=message.toString();
|
||||
// DatagramPacket outpacket = new DatagramPacket(messageString.getBytes(), messageString.length(), this.broadcast, port);
|
||||
// this.sockUDP.send(outpacket);
|
||||
// }
|
||||
|
||||
}
|
71
POO_Server/src/communication/UDPServer.java
Normal file
71
POO_Server/src/communication/UDPServer.java
Normal file
|
@ -0,0 +1,71 @@
|
|||
package communication;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.DatagramPacket;
|
||||
import java.net.DatagramSocket;
|
||||
import java.net.SocketException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
|
||||
import messages.*;
|
||||
|
||||
|
||||
|
||||
public class UDPServer extends Thread {
|
||||
|
||||
private DatagramSocket sockUDP;
|
||||
private CommunicationUDP commUDP;
|
||||
private byte[] buffer;
|
||||
|
||||
public UDPServer(int port, CommunicationUDP commUDP) throws SocketException {
|
||||
this.commUDP = commUDP;
|
||||
this.sockUDP = new DatagramSocket(port);
|
||||
this.buffer = new byte[256];
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
while (true) {
|
||||
|
||||
try {
|
||||
|
||||
DatagramPacket inPacket = new DatagramPacket(buffer, buffer.length);
|
||||
this.sockUDP.receive(inPacket);
|
||||
String msgString = new String(inPacket.getData(), 0, inPacket.getLength());
|
||||
Message msg = Message.stringToMessage(msgString);
|
||||
|
||||
switch(msg.getTypeMessage()) {
|
||||
case JE_SUIS_CONNECTE :
|
||||
//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());
|
||||
}
|
||||
else {
|
||||
|
||||
commUDP.addUser(((MessageSysteme) msg).getId(), ((MessageSysteme) msg).getPseudo(), inPacket.getAddress());
|
||||
//System.out.println(((MessageSysteme) msg).getId()+", "+((MessageSysteme) msg).getPseudo());
|
||||
}
|
||||
break;
|
||||
|
||||
case JE_SUIS_DECONNECTE :
|
||||
commUDP.removeUser( ((MessageSysteme) msg).getId() , ((MessageSysteme) msg).getPseudo(), inPacket.getAddress());
|
||||
break;
|
||||
|
||||
default : //Others types of messages are ignored because they are supposed to be transmitted by TCP and not UDP
|
||||
}
|
||||
|
||||
} catch (IOException e) {
|
||||
System.out.println("receive exception");
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,5 +0,0 @@
|
|||
package main;
|
||||
|
||||
public class Main {
|
||||
|
||||
}
|
|
@ -1,7 +1,8 @@
|
|||
package main;
|
||||
|
||||
//il faut lier le serveur tomcat, voir avec K ou le prof
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.annotation.WebServlet;
|
||||
import javax.servlet.http.HttpServlet;
|
||||
|
@ -15,19 +16,37 @@ import javax.servlet.http.HttpServletResponse;
|
|||
public class ServletPresence extends HttpServlet {
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
/**
|
||||
* Default constructor.
|
||||
*/
|
||||
//suscribe(), publish(), notify()
|
||||
|
||||
private ArrayList<Utilisateur> localUsers;
|
||||
private ArrayList<Utilisateur> remoteUsers;
|
||||
|
||||
public ServletPresence() {
|
||||
// TODO Auto-generated constructor stub
|
||||
localUsers = new ArrayList<Utilisateur>();
|
||||
remoteUsers = new ArrayList<Utilisateur>();
|
||||
}
|
||||
|
||||
//Permet a un utilisateur externe de s'ajouter/s'enlever à la liste des utilisateurs externes : au tout début de l'application
|
||||
private void suscribe() {
|
||||
}
|
||||
|
||||
private void unsubscribe() {
|
||||
}
|
||||
|
||||
//Permet de dire si on est connecté/déconnecté
|
||||
private void publish() {
|
||||
}
|
||||
|
||||
//Informe de la modification de la liste tous les utilisateurs iinternes et externes
|
||||
private void snotify() {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
|
||||
*/
|
||||
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
|
||||
// TODO Auto-generated method stub
|
||||
response.getWriter().append("Served at: ").append(request.getContextPath());
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
50
POO_Server/src/main/Utilisateur.java
Normal file
50
POO_Server/src/main/Utilisateur.java
Normal file
|
@ -0,0 +1,50 @@
|
|||
package main;
|
||||
import java.io.Serializable;
|
||||
import java.net.*;
|
||||
|
||||
public class Utilisateur implements Serializable{
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
private String id;
|
||||
private String pseudo;
|
||||
private InetAddress ip;
|
||||
|
||||
private static Utilisateur self;
|
||||
|
||||
public Utilisateur(String id, String pseudo, InetAddress ip) throws UnknownHostException {
|
||||
this.id = id;
|
||||
this.pseudo = pseudo;
|
||||
this.ip = ip;
|
||||
}
|
||||
|
||||
|
||||
public String getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public String getPseudo() {
|
||||
return pseudo;
|
||||
}
|
||||
|
||||
public void setPseudo(String pseudo) {
|
||||
this.pseudo = pseudo;
|
||||
}
|
||||
|
||||
public InetAddress getIp() {
|
||||
return ip;
|
||||
}
|
||||
|
||||
public static void setSelf(String id, String pseudo,String host) throws UnknownHostException {
|
||||
if(Utilisateur.self == null) {
|
||||
Utilisateur.self = new Utilisateur(id, pseudo, InetAddress.getByName(host));
|
||||
}
|
||||
}
|
||||
|
||||
public static Utilisateur getSelf() {
|
||||
return Utilisateur.self;
|
||||
}
|
||||
}
|
|
@ -1,17 +1,13 @@
|
|||
package messages;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.lang.instrument.Instrumentation;
|
||||
import java.util.Arrays;
|
||||
|
||||
import messages.Message.TypeMessage;
|
||||
|
||||
public abstract class Message implements Serializable {
|
||||
|
||||
public enum TypeMessage {JE_SUIS_CONNECTE, JE_SUIS_DECONNECTE, INFO_PSEUDO, TEXTE, IMAGE, FICHIER, MESSAGE_NUL}
|
||||
protected TypeMessage type;
|
||||
private static final long serialVersionUID = 1L;
|
||||
private static Instrumentation inst;
|
||||
|
||||
public TypeMessage getTypeMessage() {
|
||||
return this.type;
|
||||
|
|
Loading…
Reference in a new issue