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

156 lines
3.4 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 Boolean haveToStopThread=false;
public User(int nport) throws IOException{
//localUser
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) {
//remoteUser
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(this.nport,this.addIP);
byte[] buffer = new byte[256];
DatagramPacket outPacket= new DatagramPacket(tmpPseudo.getBytes(), tmpPseudo.length(),broadcastIP, 12458);
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("Pseudo valide");
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(12458,"Mike");
User usr1 = new User(12459);
Scanner myObj = new Scanner(System.in); // Create a Scanner object
System.out.println("Do you want to stop :");
String response = myObj.nextLine(); // Read user input
if(response=="Yes") {
activeUsr1.haveToStopThread=true;
}
}
}