-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathP2.java
216 lines (180 loc) · 7.27 KB
/
P2.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
import java.io.*;
import java.net.*;
import java.nio.charset.StandardCharsets;
import java.util.*;
import static java.util.Map.*;
//myTask is a thread
class connectMoreSockets implements Runnable{
BufferedReader stdIn = null;
String strin;
String host = null;
Socket socket;
int port = 0;
P2 p2 =new P2();
public connectMoreSockets(String host,int port) throws IOException {
this.host = host;
this.port = port;
}
@Override
public synchronized void run(){
try {
p2.printReceviedData(host,port);
System.out.println("Connected to Host: "+host);
System.out.println("Port: "+port);
System.out.println("Continue inputting...");
//System.out.println("\nEnter support value: ");
} catch (IOException e) {
System.err.println("Invalid Socket..try again");
return;
}
}
}
public class P2{
private Socket socket =null;
private DataInputStream input = null;
private DataOutputStream out = null;
private PrintWriter pw = null;
private static String host = null;
private static int port = 0;
private BufferedReader in;
private String recvStream = null;
private String recvId = null;
private String recvURL = null;
private Hashtable<Integer, Vector<String>> mfrTable = new Hashtable<Integer, Vector<String>>();
private static LinkedHashMap<Integer, Vector<String>> wcsMap = new LinkedHashMap<Integer, Vector<String>>(); //before, mfrMap
public static LinkedHashMap<Integer, Vector<Vector<String>>> finalmfrMap = new LinkedHashMap<>();
public static Vector<Vector<String>> mfrList = new Vector<>();
private Vector<String> mfrURLs = new Vector<>();
public static int support = 1;
public synchronized void printReceviedData(String host, int port)throws IOException{
socket = new Socket(host, port);
System.out.println("Connected socket(s): "+socket);
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
System.out.println("\nRECEIVED INPUTS from socket("+socket+"):");
while((recvStream = in.readLine()) != null){
if(recvStream.split(",").length == 2){
System.out.println(recvStream);
recvId = recvStream.split(",")[0];
recvURL = recvStream.split(",")[1];
addMFRs(Integer.parseInt(recvId),recvURL);
}
}
}
//Print output after support is entered
private synchronized void printOutput(){
if(!wcsMap.isEmpty()){
System.out.println("\n\nWEB CLICK SEQUENCES FOR EACH USER");
Iterator<Entry<Integer, Vector<String>>> hmI = wcsMap.entrySet().iterator();
while (hmI.hasNext()){
Map.Entry<Integer, Vector<String>> mapElement = hmI.next();
System.out.println("WCS BY>> "+mapElement.getKey()+": \n\t"+mapElement.getValue());
}
}
createMFRs();
}
//Creating MFRs
public synchronized void createMFRs(){
System.out.println("\n\nMAXIMAL FORWARD REFERENCES FOR EACH USER");
Iterator<Map.Entry<Integer, Vector<String>>> hmIterator = wcsMap.entrySet().iterator();
while (hmIterator.hasNext()){
Entry<Integer, Vector<String>> mapElement = hmIterator.next();
Vector<String> tempfrom = new Vector<>();
tempfrom = mapElement.getValue();
finalmfrMap.put( mapElement.getKey(),getSingleMFR(tempfrom));
}
Iterator<Map.Entry<Integer, Vector<Vector<String>>>> hmI = finalmfrMap.entrySet().iterator();
while (hmI.hasNext()){
Entry<Integer, Vector<Vector<String>>> mapElement = hmI.next();
System.out.println("MFR BY>> "+mapElement.getKey()+": \n\t"+mapElement.getValue());
}
// System.out.println(finalmfrMap);
}
private synchronized Vector<Vector<String>> getSingleMFR(Vector<String> wcs){
Vector<Vector<String>> mfr = new Vector<>();
Vector<String> temp = new Vector<>();
for(String s:wcs){
if(temp.contains(s)){
mfr.add(temp);
temp = new Vector<>();
temp.add(s);
}else{
temp.add(s);
}
}
mfr.add(temp);
return mfr;
}
public synchronized void createThread(String host,int port) throws IOException, InterruptedException {
Runnable r = new connectMoreSockets(host,port);
Thread task = new Thread(r);
task.start();
// task.join();
}
private static String convertInputStreamToString(InputStream inputStream)
throws IOException {
ByteArrayOutputStream result = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int length;
while ((length = inputStream.read(buffer)) != -1) {
result.write(buffer, 0, length);
}
return result.toString(StandardCharsets.UTF_8.name());
}
public synchronized static void addMFRs(int key,String value){
Vector<String> mfrURLs;
if(wcsMap.containsKey(key)){
mfrURLs = wcsMap.get(key);
mfrURLs.add(value);
}
else{
mfrURLs = new Vector<>();
mfrURLs.add(value);
wcsMap.put(key,mfrURLs);
}
}
private synchronized static boolean isInteger(String sf) {
if(sf == null){
return false;
}
try{
int i = Integer.parseInt(sf);
}catch (Exception e){
return false;
}
return true;
}
public static void main(String[] args) throws IOException, InterruptedException {
P2 p2 = new P2();
System.out.println("Waiting for the std input for P2..." +
"\nEnter socket in the form <hostname,portNumber> (NO SPACES)" +
"\nNOTE: For multiple socket entries, make sure you include \"Enter (or Return) character\" while copying the data from other resource");
while (true){
String strin;
Scanner con = new Scanner(System.in);
while((strin = con.nextLine()) != null){
if(strin.split(",").length > 1){//test for socket here
try{
host =strin.split(",")[0];
port = Integer.parseInt(strin.split(",")[1]);
p2.createThread(host,port);
} catch (NumberFormatException e) {
System.err.println("Invalid Socket enter in the form <hostname,portNumber> ,no spaces");
continue;
}
}
if(isInteger(strin)){
if(host == null || port == 0){
System.err.println("Please enter atleast one valid Socket!");
continue;
}
support = Integer.parseInt(strin);
System.out.println("Support: "+support);
p2.printOutput();
}
}
// System.out.println("\nMAXIMAL FORWARD REFERENCES FOR ALL THE DATA INPUT SO FAR: ");
//System.out.println(mfrMap);
//System.out.println(mfrList);
}
}
}