-
Notifications
You must be signed in to change notification settings - Fork 0
/
Client.java
120 lines (93 loc) · 4.75 KB
/
Client.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
import java.io.*;
import java.net.Socket;
import java.util.Scanner;
public class Client {
public static void main(String[] args) {
try {
Scanner sc = new Scanner(System.in);
System.out.print("Veuillez entrer l'adresse IP du serveur : ");
String address = sc.nextLine();
System.out.print("Veuillez entrer le numero de port du serveur : ");
int port = Integer.parseInt(sc.nextLine());
Socket clientSocket = new Socket(address, port);
PrintWriter output = new PrintWriter(clientSocket.getOutputStream());
BufferedReader input = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
Thread send = new Thread(new Runnable() {
String msg;
@Override
public void run() {
while(true){
msg = sc.nextLine();
output.println(msg);
output.flush();
}
}
});
send.start();
Thread receive = new Thread(new Runnable() {
String msg;
Midi playTrack;
Song receivedSong = null;
int tempo;
int velocity;
@Override
public void run() {
try {
msg = input.readLine();
while(msg != null){
System.out.println(msg);
if (msg.compareTo("Lancement du morceau : ") == 0){
try {
ObjectInputStream in = new ObjectInputStream(clientSocket.getInputStream());
Object objectReceived = in.readObject();
File currentFile = (File) objectReceived;
playTrack = new Midi(currentFile, new Window(1000, 600), receivedSong);
Scanner t = new Scanner(System.in);
playTrack.start();
while(t.next().compareTo("stop")!=0 && playTrack.sequencer.isRunning()){
Thread.sleep(1);
}
playTrack.stop();
}
catch (Exception ex)
{
System.out.println(ex.getMessage());
}
}
else if (msg.compareTo("Entrez le tempo (>0) : ") == 0) {
tempo = Integer.parseInt(input.readLine());
}
else if (msg.compareTo("Entrez la hauteur (entre -30 et 30) : ") == 0) {
velocity = Integer.parseInt(input.readLine());
}
else if (msg.compareTo("Envoi du morceau : ") == 0)
{
System.out.println("Reception des informations du morceau");
try{
ObjectInputStream in = new ObjectInputStream(clientSocket.getInputStream());
Object objectReceived = in.readObject();
receivedSong = (Song) objectReceived;
}
catch (Exception ex)
{
System.out.println(ex.getMessage());
}
}
msg = input.readLine();
}
System.out.println("Traitement termine...");
output.close();
clientSocket.close();
System.out.println("Deconnexion...");
System.exit(0);
}catch (Throwable throwable) {
throwable.printStackTrace();
}
}
});
receive.start();
} catch (IOException e) {
e.printStackTrace();
}
}
}