-
Notifications
You must be signed in to change notification settings - Fork 2
/
Client.java
833 lines (747 loc) · 25.3 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
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
import javax.sound.sampled.*;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.*;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
class Player{
private static final int MAX = 100;
//Packet control
private int[] missing;
private int last = MAX;
private int dataSize;
private boolean cut = false;
//Decompresser handling
private ConcurrentHashMap<Integer, byte[]> compressed;
private byte[] buffer;
private int index = 0;
private int pos = 0;
private int left = 8;
//Music playback
private AudioFormat audio;
private SourceDataLine sourceLine;
private boolean play = false;
private boolean end = true;
//print
private String name;
private int div = 0;
public Player(){
missing = new int[1500];
compressed = new ConcurrentHashMap<>();
}
private void printAdvice(String advice){
System.out.print("\u001b[s\u001b[2;39f\u001b[0K >> ["+advice+"]\u001b[u");
}
private void printSong(int time){
int min = time/60;
int sec = time - min*60;
System.out.print("\u001b[s\u001b[1;0f\u001b[0K Musica: \""+name+"\" Tempo restante: "+ min +"m"+ sec +"s\u001b[u");
}
private void clear(){
System.out.print("\u001b[s\u001b[1;0f\u001b[0K\u001b[u");
}
public boolean isOn(){
return play;
}
public boolean hasEnded(){
return end;
}
private void begin(){
index = 0;
pos = 0;
left = 8;
}
public void infoPacket(byte[] packet) throws LineUnavailableException {
int channels = (((int) packet[2] & 0xff)<<8) | (((int) packet[1] & 0xff));
int sampleRate = (((int) packet[6] & 0xff)<<24) | (((int) packet[5] & 0xff)<<16) |
(((int) packet[4] & 0xff)<<8) | (((int) packet[3] & 0xff));
int bits = (((int) packet[8] & 0xff)<<8) | ((int) packet[7] & 0xff);
dataSize = (((int) packet[12] & 0xff)<<24) | (((int) packet[11] & 0xff)<<16) |
(((int) packet[10] & 0xff)<<8) | (((int) packet[9] & 0xff));
div = (sampleRate*channels*bits)/8;
audio = new AudioFormat(sampleRate, bits, channels, true, false);
sourceLine = AudioSystem.getSourceDataLine(audio);
sourceLine.open(audio, 4096);
sourceLine.start();
play = true;
end = false;
last = MAX;
int i = 0;
while (i < 1500) missing[i++] = 1;
compressed.clear();
begin();
}
private void printBytes(byte[] p, int size){
for (int i = 0; i < size; i++){
System.out.format("%02X.", p[i]);
}
System.out.println();
}
public void add(int i, byte[] data, boolean last, int size){
if(missing[i] == 1){
missing[i] = 0;
byte[] temp = new byte[size];
try {
if (last)
System.arraycopy(data, 2, temp, 0, size-2);
else
System.arraycopy(data, 2, temp, 0, size);
}catch (ArrayIndexOutOfBoundsException a){
end = true;
play = false;
dataSize = -1;
//printAdvice("Erro na reproducao");
printAdvice("Size: "+ size+" i: "+i+" last: " + last);
return;
}
compressed.put(i, temp);
}
}
public synchronized boolean last(){
//System.out.println((index+1) + " < " + last);
while ((index+1 < last) && !end) {
try {
wait();
} catch (InterruptedException e) {}
}
int i = 0;
while (i < 1500) missing[i++] = 1;
last = MAX;
return false;
}
public byte[] getMissing(int seq){
byte[] packet = new byte[1472];
int p = 1, i = 0;
boolean l = false;
if (seq == 0) seq = last;
else l = true;
packet[0] = (byte) (3<<6);
while (i <= seq){
if (missing[i] == 1){
packet[p++] = (byte) (i>>8);
packet[p++] = (byte) i;
}
i++;
}
packet[p++] = (byte) (1500>>8);
packet[p++] = (byte) 1500;
if (!l){
last += MAX;
if (p != 3) last -= (p>>2 - 1);
} else last = seq;
return packet;
}
private synchronized void inc() throws InterruptedException {
pos++;
if (cut){
cut = false;
dataSize -= 4096;
printSong(dataSize/div);
if (dataSize <= 0) throw new InterruptedException();
}
if (pos == buffer.length){
pos = 0;
index++;
if (index+1 == last) notify();
while (!compressed.containsKey(index))
Thread.sleep(50);
buffer = compressed.remove(index);
}
}
private float getBits(boolean compl, boolean decim, int size) throws InterruptedException {
int mask = 255;
short n = 0;
float f;
if (compl){
left--;
size--;
if (((buffer[pos]>>left) & 1) != 1) compl = false;
if (left == 0){
left = 8;
inc();
}
}
if (decim){
left--;
size--;
if (((buffer[pos]>>left) & 1) != 1) decim = false;
if (left == 0){
left = 8;
inc();
}
}
while (left <= size){
size -= left;
n |= ((((short) buffer[pos] & (mask >> (8-left)))) << size);
left = 8;
inc();
}
if (size > 0){
mask >>= (8 - left);
left -= size;
n |= ((short) buffer[pos] & mask) >> left;
}
if (decim) f = (float) ((float) n + 0.5);
else f = n;
if (compl) f = -f;
return f;
}
public synchronized void stop(){
if (!end) {
end = true;
play = false;
dataSize = -1;
notify();
}else{
printAdvice("Nenhum som a tocar");
}
}
public synchronized void pause(){
if (play) play = false;
else play = true;
}
public void play(String name){
byte[] data = new byte[4096];
this.name = name;
short left, right;
int minAvg, minOff,
maxAvg, maxOff,
size, i, j;
float avg, off,
davg, doff;
try {
while (true){
while (!compressed.containsKey(0));
buffer = compressed.remove(0);
size = 0;
while(size < 2097152){
minAvg = (int) getBits(false, false, 5);
minOff = (int) getBits(false, false, 5);
maxAvg = (int) getBits(false, false, 5);
maxOff = (int) getBits(false, false, 5);
avg = off = 0;
i = j = 0;
while (i < 1024){
if (getBits(false,false, 1) == 0){
davg = getBits(true, true, minAvg);
doff = getBits(true, true, minOff);
}else{
davg = getBits(true, true, maxAvg);
doff = getBits(true, true, maxOff);
}
avg = davg + avg;
off = doff + off;
left = (short) (avg + off);
right = (short) (avg*2 - left);
data[j++] = (byte) ((left) & 0xff);
data[j++] = (byte) ((left >> 8) & 0xff);
data[j++] = (byte) ((right) & 0xff);
data[j++] = (byte) ((right >> 8) & 0xff);
i++;
}
cut = true;
size += j;
while (end) return;
while (!play || end) Thread.sleep(200);
sourceLine.write(data, 0, j);
}
begin();
}
}catch (InterruptedException e){
sourceLine.drain();
sourceLine.close();
end = true;
play = false;
div = 0;
this.name = null;
clear();
return;
}
}
}
class UDP {
private int port;
private InetAddress multicast;
private MulticastSocket socket;
private int timeout = 500;
private int size;
public UDP(int port, String ip) throws IOException {
multicast = InetAddress.getByName(ip);
this.port = port;
socket = new MulticastSocket(this.port);
socket.joinGroup(multicast);
socket.setLoopbackMode(false);
socket.setTimeToLive(1);
socket.setReceiveBufferSize(294400);
socket.setSoTimeout(timeout);
}
public synchronized void send(byte[] data) throws IOException{
DatagramPacket packet = new DatagramPacket(data, data.length, this.multicast, 9999);
socket.send(packet);
}
public synchronized byte[] read() throws IOException, SocketTimeoutException{
byte[] data = new byte[1472];
DatagramPacket packet = new DatagramPacket(data,1472);
socket.receive(packet);
size = packet.getLength();
return data;
}
public synchronized byte[] readNoTimeout() throws IOException{
socket.setSoTimeout(0);
byte[] data = new byte[1472];
DatagramPacket packet = new DatagramPacket(data,1472);
socket.receive(packet);
size = packet.getLength();
socket.setSoTimeout(timeout);
return data;
}
public int getSize(){
return size;
}
}
class TCP{
private int port;
private Socket socket;
private boolean connection;
private int size = 0;
public TCP(int port, String address) throws IOException {
this.port = port;
socket = new Socket(address, port);
connection = true;
}
public void send(byte[] data) throws IOException{
DataOutputStream packet = new DataOutputStream(socket.getOutputStream());
packet.write(data);
}
public byte[] read() throws IOException {
byte[] data = new byte[10000];
DataInputStream packet = new DataInputStream(socket.getInputStream());
size = packet.read(data);
if (size < 1)
connection = false;
return data;
}
public boolean isOn(){
return connection;
}
public void close() throws IOException{ socket.close(); }
}
class StreamPlayer implements Runnable{
private LinkedList<String> playlist;
private LinkedList<String> names;
private UDP udp;
private TCP tcp;
private String ip;
private Player p;
private byte[] packet;
private int sequence = 0;
private boolean last = false;
public StreamPlayer(Player p, String ip, TCP tcp){
playlist = new LinkedList<>();
names = new LinkedList<>();
this.p = p;
this.ip = ip;
this.tcp = tcp;
}
private static void printAdvice(String advice){
System.out.print("\u001b[s\u001b[2;39f\u001b[0K >> ["+advice+"]\u001b[u");
}
public void printPlaylist(){
ListIterator l = names.listIterator();
System.out.println("\u001b[3;0f\u001b[0J+-------------------Playlist-------------------+");
while (l.hasNext())
System.out.format("| %-40s |\n", l.next());
System.out.println("+----------------------------------------------+");
getLine();
}
private int getType(byte[] packet){
return (((int) packet[0] & 0xff)>>6);
}
private int getSequence(byte[] packet){
return (((int) packet[0] & 0x07)<<8) + ((int) packet[1] & 0xff);
}
private void getLine(){
try {
System.in.read();
} catch (IOException ioException) {
printAdvice("IO");
}
}
public boolean isPlaylistEmpty(){
if (p.hasEnded())
return playlist.isEmpty();
return false;
}
public void clearPlaylist(){
names.clear();
playlist.clear();
}
public void addPlaylist(String path, String name){
playlist.add(path);
names.add(name);
if (p.hasEnded()) sendNext();
}
private void sendNext(){
String path = playlist.pop();
byte[] packet = new byte[path.length()+10];
packet[0] = 1;
System.arraycopy(path.getBytes(), 0, packet, 1, path.length());
try {
tcp.send(packet);
} catch (IOException e) {
printAdvice("Erro no Play");
}
}
private void connect(){
while (true){
try {
Thread.sleep(500);
udp = new UDP(8888, ip);
break;
} catch (IOException e) {
printAdvice("Sem ligacao");
} catch (InterruptedException e) {}
}
}
public void Start(){
String n = names.pop();
Thread worker = new Thread(() ->{
p.play(n);
return;
});
worker.start();
}
public void run(){
connect();
while (true){
try {
while (!p.isOn()){
if (!playlist.isEmpty())
sendNext();
while (true){
packet = udp.readNoTimeout();
if (getType(packet) == 0){
p.infoPacket(packet);
Start();
break;
}
}
}
while (!p.hasEnded()){
try {
packet = udp.read();
switch (getType(packet)){
case 1:
sequence = getSequence(packet);
p.add(sequence, packet, false, udp.getSize()-2);
break;
case 2:
last = true;
sequence = getSequence(packet);
p.add(sequence, packet, true, udp.getSize());
throw new SocketTimeoutException();
}
}catch (SocketTimeoutException t){
if (!p.hasEnded()) {
if (last) {
udp.send(p.getMissing(sequence));
last = p.last();
} else {
udp.send(p.getMissing(0));
}
}
}
}
} catch (IOException e) {
connect();
} catch (LineUnavailableException l){
printAdvice("Placa de som em uso");
}
}
}
}
class MusicManagment{
private HashMap <String, LinkedList<String>> artPerGenre;
private HashMap <String, LinkedList<String>> albPerArtis;
private HashMap <String, LinkedList<String>> musPerAlbum;
private HashMap <String, String> pathSongs;
public void print(){
for (String name: musPerAlbum.keySet()){
String key = name;
String value = musPerAlbum.get(name).toString();
System.out.println(key + " " + value);
}
}
public MusicManagment(){
artPerGenre = new HashMap<>();
albPerArtis = new HashMap<>();
musPerAlbum = new HashMap<>();
pathSongs = new HashMap<>();
}
public void addSong(String path){
int i;
String[] item = path.split("/");
String music = item[4].substring(0, item[4].lastIndexOf("."));
if (!pathSongs.containsKey(music)){ //existe a musica
i = 1;
if (artPerGenre.containsKey(item[1])){ //existe o genero
i = 2;
if (albPerArtis.containsKey(item[2])) { //existe o artista
i = 3;
if (musPerAlbum.containsKey(item[3])){ //existe o album
musPerAlbum.get(item[3]).add(music);
pathSongs.put(music, path);
i = 4;
}
}
}
if (i < 2) {
artPerGenre.put(item[1], new LinkedList<>());
artPerGenre.get(item[1]).add(item[2]);
}
if (i < 3){
albPerArtis.put(item[2], new LinkedList<>());
albPerArtis.get(item[2]).add(item[3]);
}
if (i < 4){
musPerAlbum.put(item[3], new LinkedList<>());
musPerAlbum.get(item[3]).add(music);
pathSongs.put(music, path);
}
}
}
public void updateList(String list){
int i = 0;
String[] musics = list.split("\n");
while (i < musics.length-1) {
addSong(musics[i++]);
}
}
public Set<String> getGenres(){
return artPerGenre.keySet();
}
public Set<String> getArts(){
return albPerArtis.keySet();
}
public Set<String> getAlbs(){
return musPerAlbum.keySet();
}
public LinkedList<String> getArtsFrom(String genre){
return artPerGenre.get(genre);
}
public LinkedList<String> getAlbsFrom(String art){
return albPerArtis.get(art);
}
public LinkedList<String> getMusicsFrom(String alb){
return musPerAlbum.get(alb);
}
public String getPath(String music){
return pathSongs.get(music);
}
public Set<String> getAll(){
return pathSongs.keySet();
}
public boolean isEmpty(){
return pathSongs.isEmpty();
}
}
public class Client {
private static Scanner in = new Scanner(System.in);
private static MusicManagment m;
private static Player p;
private static StreamPlayer s;
private static TCP tcp;
private static void printAdvice(String advice){
System.out.print("\u001b[s\u001b[2;39f\u001b[0K >> ["+advice+"]\u001b[s");
}
private static void clearScreen(){
System.out.print("\u001b[0;0f\u001b[2J");
}
private static char getChar(){
char op;
while (true){
try {
op = in.next().toLowerCase().charAt(0);
return op;
} catch (InputMismatchException i){
printAdvice("Input invalido");
}
}
}
private static int getInt(){
int op;
while (true){
try {
op = in.nextInt();
return op;
} catch (InputMismatchException i){
printAdvice("Input invalido");
}
}
}
private static String printSet(Set<String> temp0, String categorie){
int i = 0;
String[] temp1 = new String[temp0.size()];
System.out.println("\u001b[3;0f\u001b[0J+--------------------"+ categorie +"--------------------+");
for (String t: temp0) {
System.out.format("| %-2d: %-40s|\n", (i+1), t);
temp1[i++] = t;
}
System.out.println("| 0 : Abandonar |");
System.out.println("+----------------------------------------------+");
System.out.print(" -> ");
i = getInt();
clearScreen();
if (i == 0)
return null;
return temp1[i-1];
}
private static String printArray(LinkedList<String> temp, String categorie){
int i = -1;
System.out.println("\u001b[3;0f\u001b[0J+--------------------"+ categorie +"--------------------+");
while (++i < temp.size()){
System.out.format("| %-2d: %-40s |\n", (i+1), temp.get(i));
}
System.out.println("| 0 : Abandonar |");
System.out.println("+----------------------------------------------+");
System.out.print(" -> ");
i = getInt();
clearScreen();
if (i == 0)
return null;
return temp.get(i-1);
}
private static void update(){
byte[] packet = new byte[4];
packet[0] = 0;
try {
tcp.send(packet);
packet = tcp.read();
} catch (IOException e) {
printAdvice("Erro na atualizacao");
}
m.updateList(new String(packet));
}
private static void control(int opcode){
byte[] packet = new byte[4];
packet[0] = (byte) opcode;
try {
tcp.send(packet);
} catch (IOException e) {
printAdvice("Erro no Controlo");
}
}
private static void menu(){
char op = 'x';
String temp;
while (op != 'e'){
System.out.println("\u001b[2;0f< [P]lay/Pause > < [N]ext > < [S]top >\u001b[3;0f+---------------------Menu---------------------+");
System.out.println("| G: Ver Generos |");
System.out.println("| I: Ver Interprete |");
System.out.println("| A: Ver Albuns |");
System.out.println("| M: Ver Musicas |");
System.out.println("| L: Ver Playlist |");
System.out.println("| R: Atualizar lista |");
System.out.println("| E: Abandonar |");
System.out.println("+----------------------------------------------+");
System.out.print(" -> \u001b[0K");
op = getChar();
switch (op){
case 'p':
//play/pause
if (!p.hasEnded())
p.pause();
else
printAdvice("Nenhum som a tocar");
break;
case 'n':
//seguinte
p.stop();
control(2);
if (s.isPlaylistEmpty())
printAdvice("Playlist vazia");
break;
case 's':
//stop
s.clearPlaylist();
p.stop();
control(2);
break;
case 'g':
//generos
temp = printSet(m.getGenres(), "Genero");
if (temp != null){
temp = printArray(m.getArtsFrom(temp), "Artist");
if (temp != null){
temp = printArray(m.getAlbsFrom(temp), "Albuns");
if (temp != null){
temp = printArray(m.getMusicsFrom(temp), "Musica");
if (temp != null) {//toca musica
s.addPlaylist(m.getPath(temp), temp);
}
}
}
}
break;
case 'i':
//Artista
temp = printSet(m.getArts(), "Artist");
if (temp != null){
temp = printArray(m.getAlbsFrom(temp), "Albuns");
if (temp != null){
temp = printArray(m.getMusicsFrom(temp), "Musica");
if (temp != null)//toca musica
s.addPlaylist(m.getPath(temp), temp);
}
}
break;
case 'a':
//album
temp = printSet(m.getAlbs(), "Albuns");
if (temp != null){
temp = printArray(m.getMusicsFrom(temp), "Musica");
if (temp != null)//toca musica
s.addPlaylist(m.getPath(temp), temp);
}
break;
case 'm':
//musicas
temp = printSet(m.getAll(), "Musica");
if (temp != null)//toca musica
s.addPlaylist(m.getPath(temp), temp);
break;
case 'l':
//playlist
s.printPlaylist();
clearScreen();
break;
case 'r':
//atualizar
update();
break;
case 'e':
//sair
clearScreen();
System.out.println("Bye");
System.exit(0);
break;
default:
printAdvice("Opcao invalida");
break;
}
}
}
public static void main(String[] args) throws IOException {
clearScreen();
p = new Player();
m = new MusicManagment();
tcp = new TCP(9999, "127.0.0.1");
byte[] packet = tcp.read();
s = new StreamPlayer(p, new String(packet), tcp);
Thread t = new Thread(s);
t.start();
update();
menu();
}
}