Clavardage/Application/.metadata/.plugins/org.eclipse.core.resources/.history/81/00df846ec02f001b1e3d9e6aa3539da2
2020-11-26 11:28:52 +01:00

146 lines
3.1 KiB
Text

import java.awt.List;
import java.io.IOException;
import java.net.*;
import java.time.Duration;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.Scanner; // Import the Scanner class
public class User{
protected InetAddress addIP;
protected String pseudo;
protected int nport;
protected boolean actif;
protected User[] remoteUserList;
public User(int nport) throws IOException{
try
{
this.addIP = InetAddress.getLocalHost();
}
catch(UnknownHostException e) {
System.out.println("Could not find local address!");
}
this.nport = nport;
this.setPseudo();
this.actif = true;
}
public User(int nport,String pseudo) {
try
{
this.addIP = InetAddress.getLocalHost();
}
catch(UnknownHostException e) {
System.out.println("Could not find local address!");
}
this.pseudo = pseudo;
this.nport = nport;
this.actif = true;
UserListeningThread th = new UserListeningThread("UDP Listening thread",this);
th.start();
}
public InetAddress getAddIP() {
return addIP;
}
public void setAddIP(InetAddress addIP) {
this.addIP = addIP;
}
public String getPseudo() {
return pseudo;
}
public void setPseudo() throws IOException {
Scanner myObj = new Scanner(System.in); // Create a Scanner object
System.out.println("Enter pseudo :");
String tmpPseudo = myObj.nextLine(); // Read user input
while(!(this.validatePseudo(tmpPseudo))) {
System.out.println("Already taken, choose another one :");
tmpPseudo = myObj.nextLine(); // Read user input
}
this.pseudo = tmpPseudo;
}
public Boolean validatePseudo(String tmpPseudo) throws IOException {
// Call broadcast
InetAddress broadcastIP = InetAddress.getLocalHost(); // change to broadcast
DatagramSocket dgramSocket = new DatagramSocket(5001,this.addIP);
byte[] buffer = new byte[256];
DatagramPacket outPacket= new DatagramPacket(tmpPseudo.getBytes(), tmpPseudo.length(),broadcastIP, 5002);
dgramSocket.send(outPacket);
Boolean valid = true;
Date oldDate = new Date();
Date newDate = new Date();
while( (newDate.getTime()-oldDate.getTime()) < 5000 && valid) {
System.out.println("Wait for pseudo validation ...");
DatagramPacket inPacket= new DatagramPacket(buffer, buffer.length);
dgramSocket.receive(inPacket);
buffer = inPacket.getData();
String response = new String(buffer);
System.out.println(response);
String[] lstresponse = response.split(":");
ArrayList<String> stringList = new ArrayList<String>(Arrays.asList(lstresponse));
valid=stringList.contains(tmpPseudo);
newDate = new Date();
}
System.out.println("Fin");
dgramSocket.close();
return valid;
}
public int getNport() {
return nport;
}
public void setNport(int nport) {
this.nport = nport;
}
public boolean isActif() {
return actif;
}
public void setActif(boolean actif) {
this.actif = actif;
}
public static void main(String[] args) throws IOException {
User activeUsr1 = new User(5002,"Mike");
User usr1 = new User(3);
usr1.setPseudo();
}
}