-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConsumer.java
More file actions
208 lines (161 loc) · 7.39 KB
/
Consumer.java
File metadata and controls
208 lines (161 loc) · 7.39 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
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
import java.io.*;
import java.net.Socket;
import java.net.UnknownHostException;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.HashMap;
public class Consumer extends Node {
private static final int N=getN(); //Num of brokers
private static String BrokerIp;
private static int BrokerPort;
private static String[][] availableBrokers = new String[N][2]; //broker1: brokerIP, Integer.parseInt(brokerPort)
private static ArrayList<ArrayList<String>> artists = new ArrayList<ArrayList<String>>();
private HashMap<String,Info> brokerArtists = new HashMap<String,Info>();
public static void main(String[] args) throws IOException, NoSuchAlgorithmException {
Consumer cons = new Consumer("127.0.0.1", 5000);
cons.initialization();
cons.openConsumer();
}
public void initialization(){
init(BrokerIp,BrokerPort,availableBrokers);
Socket socket;
ObjectInputStream in;
ObjectOutputStream out;
//makes a list of each artist of a broker
try {
for (int i=0; i<N; i++) {
socket = new Socket(availableBrokers[i][0], Integer.parseInt(availableBrokers[i][1]));
out = new ObjectOutputStream(socket.getOutputStream());
in = new ObjectInputStream(socket.getInputStream());
out.writeObject("artist names");
out.flush();
//
ArrayList<String> a = new ArrayList<>();
artists.add(a);
//
String name = (String) in.readObject();
Info broker = new Info(availableBrokers[i][0], Integer.parseInt(availableBrokers[i][1]));
while (!(name.equalsIgnoreCase("end"))) {
//hashmap artist->broker
brokerArtists.put(name.toLowerCase(), broker);
artists.get(i).add(name);
name = (String) in.readObject();
}
//print artist names
System.out.println("\n---- Broker " + (i+1) + " ----");
for (String b : artists.get(i)){
System.out.println(b);
}
in.close();
out.close();
socket.close();
}
System.out.println("-----------------\n");
} catch (UnknownHostException u) {
System.out.println(u);
} catch (IOException i) {
System.out.println(i);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public void openConsumer() throws IOException {
ObjectInputStream in;
DataInputStream input; //takes input from terminal
ObjectOutputStream out;
Socket socket;
String requestArtist;
String requestSong;
do {
ArrayList<byte[]> chunkList = new ArrayList<>();//queue for chunks
input = new DataInputStream(System.in);
System.out.print("Artist name: ");
requestArtist = input.readLine().trim();
if (requestArtist.equalsIgnoreCase("over")) {
break;
}
try {
//search for this artist name
if (brokerArtists.containsKey(requestArtist.toLowerCase())) {
Info broker = brokerArtists.get(requestArtist.toLowerCase());
//connect with the responsible broker
socket = new Socket(broker.getIP(), broker.getPort());
out = new ObjectOutputStream(socket.getOutputStream());
in = new ObjectInputStream(socket.getInputStream());
System.out.println("Consumer Connected: " + socket);
//sends user's request
out.writeObject(requestArtist);
out.flush();
System.out.print("Song title: ");
requestSong = input.readLine().trim();
out.writeObject(requestSong);
out.flush();
//waiting for response
String response = (String) in.readObject();
if (response.equalsIgnoreCase("Found")) {
System.out.println("-----------------");
System.out.println("Song found");
//takes the song from broker
MusicFile chunk = (MusicFile) in.readObject();
chunkList.add(chunk.getMusicFileExtract());
System.out.println("received: " + chunk.getChunkNumber() + " chunk");
for (int ch = 0; ch < chunk.getTotalChunks() - 1; ch++) {
chunk = (MusicFile) in.readObject();
System.out.println("received: " + chunk.getChunkNumber() + " chunk");
chunkList.add(chunk.getMusicFileExtract());
}
byte[] mp3File = recreateFile(chunkList);
FileOutputStream stream = new FileOutputStream(requestSong + "(new).mp3");
stream.write(mp3File);
System.out.println("Song received successfully! ");
System.out.println("-----------------");
} else if (response.equalsIgnoreCase("Not Found")) {
System.out.println("The song doesn't exist!");
} else {
System.out.println("Oops! A problem occurred. Try again..");
}
in.close();
out.close();
socket.close();
}else{
System.out.println("Sorry.. There are no songs of this Artist..");
System.out.println("Try an other one..");
}
} catch(IOException e){
e.printStackTrace();
} catch(NumberFormatException e){
e.printStackTrace();
} catch(ClassNotFoundException e){
e.printStackTrace();
}
}while(true);
}
public Consumer(String BrokerIp, int BrokerPort){
this.BrokerIp=BrokerIp;
this.BrokerPort=BrokerPort;
}
//recreate mp3 file
public byte[] recreateFile(ArrayList<byte[]> ChunkList) {
int chunkSize = 524288;
int lastChunkSize = ChunkList.get(ChunkList.size()-1).length;
int numOfBytes = (ChunkList.size()-1)*chunkSize + ChunkList.get(ChunkList.size()-1).length;
byte[] Mp3ByteArray = new byte[numOfBytes];
int indexOfChunk = 1;
int indexOfByte = 0;
for (byte[] chunk : ChunkList) {
if(indexOfChunk < ChunkList.size()){
for(int i = 0 ; i < chunkSize ; i++){
Mp3ByteArray[indexOfByte] = chunk[i];
indexOfByte++;
}
}else{
for(int i = 0 ; i < lastChunkSize ; i++){
Mp3ByteArray[indexOfByte] = chunk[i];
indexOfByte++;
}
}
indexOfChunk++;
}
return Mp3ByteArray;
}
}