-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathMain.java
More file actions
164 lines (132 loc) · 5.89 KB
/
Copy pathMain.java
File metadata and controls
164 lines (132 loc) · 5.89 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
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class Main {
public static void main(String[] args) {
String line;
int lineNb = 0;
int videoNb = 0;
int endpointNb = 0;
int requests = 0;
int cachesNb = 0;
int cacheSize = 0;
int endpointCachesNb = 0;
// map for the videos (video numebr, size of video)
HashMap<Integer, Integer> videosMap = new HashMap<Integer, Integer>();
// map for the endpoint latencies (destination, latency)
HashMap<Integer, Integer> endpointLatencies = new HashMap<Integer, Integer>();
// list that stores latency hashmap of each endpoint
List<HashMap<Integer,Integer>> endpointLatenciesList = new ArrayList<HashMap<Integer,Integer>>();
// map for the endpoint requests (video, nb requests)
HashMap<Integer, Integer> endpointRequests = new HashMap<Integer, Integer>();
// list that stores endpoint requests
HashMap<Integer, HashMap<Integer, Integer>> endpointRequestsList = new HashMap<Integer, HashMap<Integer, Integer>>();
int[] intArray;
String filename = "src/kittens.in";
try {
BufferedReader reader = new BufferedReader(new FileReader(filename));
while ((line = reader.readLine()) != null) {
// read the line and parse it to an int array
intArray = toIntArray(line.split("\\s+"));
if (lineNb == 0) {
// extract the first line information
videoNb = intArray[0];
endpointNb = intArray[1];
requests = intArray[2];
cachesNb = intArray[3];
cacheSize = intArray[4];
} else if (lineNb == 1) {
// extract the videos size's
for (int j=0; j<videoNb; j++) {
videosMap.put(j, intArray[j]);
}
}
if (lineNb > 2) {
// if it is a endpoint to destination latency
if (intArray.length == 2) {
if (endpointCachesNb == 0) {
// parsing the latencies
endpointLatencies.put(-1, intArray[0]);
endpointCachesNb = intArray[1];
} else {
// extracting the latencies to each cache
if (endpointCachesNb > 0) {
// add the latency to the endpointLatencies hashmap
endpointLatencies.put(intArray[0], intArray[1]);
endpointCachesNb--;
} else {
// add the endpointLatencies hashmap to the endpointList
endpointLatenciesList.add(endpointLatencies);
}
}
}
if (intArray.length == 3){
// dealing with the requests
int video = intArray[0];
int ep = intArray[1];
int rqst = intArray[2];
if (endpointLatenciesList.contains(ep)) {
endpointRequestsList.get(ep).put(video, rqst);
} else {
endpointRequestsList.put(ep, new HashMap<>());
endpointRequestsList.get(ep).put(video, rqst);
}
}
}
lineNb++;
}
reader.close();
}
catch (Exception e) {
System.err.format("Exception occurred trying to read '%s'.", filename);
e.printStackTrace();
}
// list of VideoCaches
List<VideoCache> videoCachesList = new ArrayList<VideoCache>();
for(int i=0;i<cachesNb; i++) {
videoCachesList.add(new VideoCache(i,cacheSize, videosMap));
}
List<EndPoint> endpointList = new ArrayList<EndPoint>();
// list of the endpoints
HashMap<VideoCache,Integer> epLatencies = new HashMap<>();
for(int i=0;i<endpointNb; i++) {
HashMap<Integer, Integer> epl = endpointLatenciesList.get(i);
for(Map.Entry<Integer, Integer> entry : epl.entrySet()) {
Integer key = entry.getKey();
Integer value = entry.getValue();
VideoCache v = videoCachesList.get(key);
epLatencies = new HashMap<VideoCache,Integer>();
epLatencies.put(v, value);
}
endpointList.add(new EndPoint(epLatencies, endpointRequestsList.get(i), i));
}
for(int i=0;i<endpointList.size();i++) {
endpointList.get(i).calculateBestLatency();
}
ArrayList<Integer> vidz = new ArrayList<Integer>();
HashMap<Integer, Integer> hmp = new HashMap<Integer, Integer>();
for (int i=0; i<videoCachesList.size();i++) {
vidz = videoCachesList.get(i).computeCacheTable();
for (int j=0; j<vidz.size(); j++) {
hmp.put(i, vidz.get(j));
}
}
for (Map.Entry<Integer, Integer> entry : hmp.entrySet()) {
Integer key = entry.getKey();
Integer value = entry.getValue();
System.out.println(key);
System.out.println(value);
System.out.println("------------");
}
}
public static int[] toIntArray(String[] numberStrs) {
int[] numbers = new int[numberStrs.length];
for(int i = 0;i < numberStrs.length;i++) {
numbers[i] = Integer.parseInt(numberStrs[i]);
}
return numbers;
}
}