-
Notifications
You must be signed in to change notification settings - Fork 0
/
ClientTreatment.java
223 lines (179 loc) · 8.53 KB
/
ClientTreatment.java
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
import java.io.*;
import java.net.Socket;
import java.util.List;
class ClientTreatment {
Socket socketClient;
ServerContainer container;
PrintWriter output;
BufferedReader input;
ClientTreatment(Socket socketClient, ServerContainer container) {
this.socketClient = socketClient;
this.container = container;
}
// Copie du fichier d'entrée vers fichier de sortie
protected void copy(File file, File copy) throws IOException {
FileInputStream in = new FileInputStream(file);
FileOutputStream out = new FileOutputStream(copy);
int i;
while ((i = in.read())!=-1)
{
out.write(i);
}
in.close();
out.close();
}
String sendQuestion(String[] msg) throws IOException {
for (String sentence : msg){
output.println(sentence);
}
output.flush();
return input.readLine();
}
void start() throws IOException {
output = new PrintWriter(socketClient.getOutputStream());
input = new BufferedReader(new InputStreamReader(socketClient.getInputStream()));
output.println("Bienvenue sur le serveur de karaoke !!!");
String path_input = "src/musicInput/";
String path_output = "src/musicOutput/";
Statistics user = new Statistics(new File("src/statistics/user.txt"));
Statistics bestTrack = new Statistics(new File("src/statistics/track.txt"));
String username = sendQuestion(new String[]{"Entrez votre nom utilisateur : "});
while (!user.getUsername(username)) {
username = sendQuestion(new String[]{"Erreur. Entrez votre nom utilisateur : "});
}
boolean connexion = true;
while (connexion) {
String[] menu = {"\nMenu", "1 : Consulter la playlist", "2 : Consulter les statistiques", "3 : Se deconnecter\n", "Choix : "};
String choice = sendQuestion(menu);
System.out.println("Le client a choisi l'option :" + choice);
switch (choice) {
case "1":
System.out.println("Envoi de la playlist au client...");
container.sendPlaylist(output);
String trackId = sendQuestion(new String[]{"Choix du morceau : "});
// Retour au menu
if (container.getSong(Integer.parseInt(trackId)) == null)
{
break;
}
Song currentSong = container.getSong(Integer.parseInt(trackId));
String track_name = currentSong.title;
System.out.println("Le client a choisi le morceau " + track_name);
// Desactivation des voix
output.println("Ce morceau contient " + currentSong.voices.size() + " voix.");
if (currentSong.voices.size() > 1)
{
String changeVoice = sendQuestion(new String[]{"Voulez-vous desactiver une des voix ? (oui ou non)"});
if (changeVoice.compareTo("oui") == 0)
{
currentSong.sendSingers(output);
String voiceAnswer = sendQuestion(new String[]{"\nQuelle voix souhaitez-vous desactiver ?"});
try{
currentSong.getVoice(Integer.parseInt(voiceAnswer)).setEnable(false);
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
}
String tempoAnswer = sendQuestion(new String[]{"Voulez-vous changer le tempo ? (oui ou non) "});
if (tempoAnswer.compareTo("oui") == 0) {
try{
String tempo = sendQuestion(new String[]{"Entrez le tempo (>0) : "});
currentSong.setTempo(Integer.parseInt(tempo));
output.println(tempo);
output.flush();
}
catch (Exception ex)
{
System.out.println(ex.getMessage());
}
}
String velocityAnswer = sendQuestion(new String[]{"Voulez-vous changer la hauteur du morceau ? (oui ou non) "});
if (velocityAnswer.compareTo("oui") == 0) {
try{
String velocity = sendQuestion(new String[]{"Entrez la hauteur (entre -30 et 30) : "});
currentSong.setVelocity(Integer.parseInt(velocity));
output.println(velocity);
output.flush();
}
catch (Exception ex)
{
System.out.println(ex.getMessage());
}
}
output.println("Envoi du morceau : ");
output.flush();
// Envoi des infos concernant le morceau demandé au client
ObjectOutputStream out = new ObjectOutputStream(socketClient.getOutputStream());
try {
out.writeObject(currentSong);
out.flush();
}
catch (Exception ex)
{
System.out.println(ex.getMessage());
}
finally {
if (socketClient.isClosed())
{
out.close();
}
}
output.println("Tapez 'stop' pour arreter le morceau a tout moment\n");
output.flush();
output.println("Lancement du morceau : ");
output.flush();
List<String[]> infoTrack = bestTrack.getInfo();
List<String[]> infoUser = user.getInfo();
bestTrack.update(infoTrack, currentSong.id);
user.update(infoUser,username);
File fileRequested = new File(path_input + track_name + ".mid");
File copy = new File(path_output + track_name + "_Copie" + ".mid");
copy(fileRequested, copy);
ObjectOutputStream outFile = new ObjectOutputStream(socketClient.getOutputStream());
// Envoi du fichier midi
try {
outFile.writeObject(fileRequested);
outFile.flush();
}
catch (Exception ex)
{
System.out.println(ex.getMessage());
}
finally {
if (socketClient.isClosed())
{
outFile.close();
}
}
System.out.println("Morceau envoye");
break;
case "2":
System.out.println("Envoi des statistiques au client...");
user = new Statistics(new File("src/statistics/user.txt"));
bestTrack = new Statistics(new File("src/statistics/track.txt"));
List<String> user_info = user.max();
List<String> track_info = bestTrack.max();
output.println(user_info.get(0) + " est l'utilisateur qui a le plus d'écoutes avec " + user_info.get(1) + " ecoute(s).");
output.println("Le morceau " + container.getSong(Integer.parseInt(track_info.get(0))).title + " est le morceau qui a le plus d'écoutes avec " + track_info.get(1) + " ecoute(s).");
break;
case "3":
System.out.println("Le client veut se deconnecter");
output.flush();
connexion = false;
break;
default:
//System.out.println("Option inexistante");
//output.println("Option inexistante");
//output.flush();
break;
}
}
output.close();
socketClient.close();
System.out.println("Deconnexion du client");
System.out.println("Traitement client termine !");
}
}