-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClientTCP.java
More file actions
121 lines (111 loc) · 4.79 KB
/
ClientTCP.java
File metadata and controls
121 lines (111 loc) · 4.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import java.util.*;
import java.io.*;
import java.net.*;
//Composant du Client qui s'occupe de la communication avec le ServeurCentrale, l'elaboration des reponses du
//Serveur et l'envoie des donnees aux clients connectes au Peer To Peer
public class ClientTCP implements Runnable{
private ArrayList<ClientInfo> listeClients;
private int tcpServerPort, udpServerPort;
private String serverAddress;
private String donnees;
private Socket s;
public ClientTCP(String serverAddress, int tcpServerPort, int udpServerPort, String donnees){
this.serverAddress = serverAddress;
this.tcpServerPort = tcpServerPort;
this.udpServerPort = udpServerPort;
this.donnees = donnees;
this.listeClients = new ArrayList<ClientInfo>();
try{
this.s = new Socket(serverAddress, tcpServerPort);
}
catch(UnknownHostException e){ e.printStackTrace(); }
catch(IOException e){ e.printStackTrace(); }
}
public void run(){
if(this.connect() != 0){
this.demarrePartage();
this.waitForUpdates();
}//si on est le premier client a se connecter on s'attend pas une liste des clients connectes
else
this.waitForUpdates();
}
//a chaque fois qu'un nouveau client se connecte, le ServeurCentrale nous envoie une mise-a-jour
public void waitForUpdates(){
BufferedReader in;
String tmp = "";
//reception liste Clients sous forme de String "ipADDR#udpPORT"
try{
in = new BufferedReader(new InputStreamReader(this.s.getInputStream()));
while(true){
tmp = in.readLine();
if(!tmp.startsWith(Protocole.prefixUpdateRequest)) continue;
String [] informationClient = new String[2];
informationClient = Protocole.biSplit(tmp.substring(Protocole.prefixUpdateRequest.length(), tmp.length()), '#');
if(tmp.equals(Protocole.clientListEnd)) continue;
ClientInfo tmpInfo = new ClientInfo(informationClient[0], Integer.parseInt(informationClient[1]));
ClientUDP udpClient = new ClientUDP(tmpInfo.getUdpServerPort(), tmpInfo.getAddress(), this.donnees);
udpClient.sendData();
}
}
catch(NullPointerException npe){
System.out.println("\nSERVER CONNECTION LOST...\nSTOPPING EXECUTION...");
System.exit(0);
}
catch(SocketException se){ se.printStackTrace(); }
catch(IOException e){ e.printStackTrace(); }
}
public void demarrePartage(){
for(ClientInfo c : this.listeClients){
ClientUDP tmp = new ClientUDP(c.getUdpServerPort(), c.getAddress(), this.donnees);
tmp.sendData();
}
}
public int getContactsInfo(){
int clientNumber = 0;
String tmp = "";
BufferedReader in;
//reception liste Clients sous forme de String "ipADDR#udpPORT"
ArrayList<String> listeClientsStr = new ArrayList<String>();
try{
in = new BufferedReader(new InputStreamReader(this.s.getInputStream()));
while(true){
tmp = in.readLine();
boolean alreadyReceived = false;
for(String tmpString : listeClientsStr){
if(tmp.equals(tmpString)) alreadyReceived = true;
}
if(alreadyReceived) continue;
if(!tmp.equals(Protocole.clientListEnd)){
listeClientsStr.add(tmp);
clientNumber++;
}
else{
if(clientNumber == 0)
return 0;
else
break;
}
}
}
catch(NullPointerException npe){ npe.printStackTrace(); }
catch(SocketException se){ se.printStackTrace(); }
catch(IOException e){ e.printStackTrace(); }
//remplissage liste des client sous forme d'objets ClientInfo
for(String s : listeClientsStr){
String [] informationClient = new String[2];
informationClient = Protocole.biSplit(s, '#');
this.listeClients.add(new ClientInfo(informationClient[0], Integer.parseInt(informationClient[1])));
}
return clientNumber;
}
//tentatif de connexion au ServeurCentrale pour recevoir la liste clients connectes
public int connect(){
try{
PrintWriter pw = new PrintWriter(new OutputStreamWriter(this.s.getOutputStream()));
pw.println(Protocole.connectToServer(udpServerPort));
pw.flush();
}
catch(Exception e){ e.printStackTrace(); }
return this.getContactsInfo();
}
}