Repository navigation
Expand file tree
/
Copy pathQueryTask.java
More file actions
257 lines (221 loc) · 6.31 KB
/
Copy pathQueryTask.java
File metadata and controls
257 lines (221 loc) · 6.31 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
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
public class QueryTask extends Task {
int maxDepth;
Queue toprocess;
DistributedHashMap results;
GlobalString workingURL;
public QueryTask(Queue todoList, DistributedHashMap doneList, int maxDepth, DistributedHashMap results) {
this.todoList = todoList;
this.doneList = doneList;
this.maxDepth = maxDepth;
this.results = results;
}
public void execute() {
int depth;
int max;
atomic {
depth = ((GlobalQuery)myWork).getDepth();
max = this.maxDepth;
}
if (depth < max) {
/* global variables */
GlobalQuery gq;
/* local variables */
LocalQuery lq;
String hostname;
String path;
atomic {
gq = (GlobalQuery)myWork;
hostname = new String(GlobalString.toLocalCharArray(gq.getHostName()));
path = new String(GlobalString.toLocalCharArray(gq.getPath()));
GlobalStringBuffer gsb = global new GlobalStringBuffer(hostname);
gsb.append("/");
gsb.append(path);
workingURL = global new GlobalString(gsb.toGlobalString());
}
lq = new LocalQuery(hostname, path, depth);
System.printString(lq.getDepth()+" ");
System.printString("Processing - Hostname : ");
System.printString(hostname);
System.printString(", Path : ");
System.printString(path);
System.printString("\n");
Socket s = new Socket(hostname, 80);
requestQuery(hostname, path, s);
readResponse(lq, s);
atomic {
processList(lq, workingURL, results);
}
atomic {
toprocess = processPage(lq);
}
s.close();
}
}
public void done(Object obj) {
GlobalString str = global new GlobalString("true");
doneList.put(workingURL, str);
while(!toprocess.isEmpty()) {
GlobalQuery q = (GlobalQuery)toprocess.pop();
GlobalString hostname = global new GlobalString(q.getHostName());
GlobalString path = global new GlobalString(q.getPath());
GlobalStringBuffer gsb = global new GlobalStringBuffer(hostname);
gsb.append("/");
gsb.append(path);
if (!doneList.containsKey(gsb.toGlobalString())) {
todoList.push(q);
}
}
}
public static void requestQuery(String hostname, String path, Socket sock) {
StringBuffer req = new StringBuffer("GET ");
req.append("/");
req.append(path);
req.append(" HTTP/1.1\r\nHost:");
req.append(hostname);
req.append("\r\n\r\n");
sock.write(req.toString().getBytes());
}
public static void readResponse(LocalQuery lq, Socket sock) {
// state 0 - nothing
// state 1 - \r
// state 2 - \r\n
// state 3 - \r\n\r
// state 4 - \r\n\r\n
int state=0;
while(true) {
if (state<4) {
if (state==0) {
byte[] b=new byte[1];
int numchars=sock.read(b);
if ((numchars==1)) {
if (b[0]=='\r') {
state++;
}
} else
return;
} else if (state==1) {
byte[] b=new byte[1];
int numchars=sock.read(b);
if (numchars==1) {
if (b[0]=='\n')
state++;
else
state=0;
} else return;
} else if (state==2) {
byte[] b=new byte[1];
int numchars=sock.read(b);
if (numchars==1) {
if (b[0]=='\r')
state++;
else
state=0;
} else return;
} else if (state==3) {
byte[] b=new byte[1];
int numchars=sock.read(b);
if (numchars==1) {
if (b[0]=='\n')
state++;
else
state=0;
} else return;
}
} else {
byte[] buffer=new byte[1024];
int numchars=sock.read(buffer);
if (numchars==0)
return;
else {
String curr=(new String(buffer)).subString(0,numchars);
lq.response.append(curr);
}
}
}
}
public static void processList(LocalQuery lq, GlobalString url, DistributedHashMap results) {
String sTitle = new String("<title>");
String eTitle = new String("</title>");
String searchstr = lq.response.toString();
LinkedList ll;
int sIndex = searchstr.indexOf(sTitle);
if (sIndex != -1) {
int eIndex = searchstr.indexOf(eTitle, sIndex+sTitle.length());
String title = new String(searchstr.subString(sIndex+sTitle.length(), eIndex));
ll = tokenize(title);
Queue q;
while (!ll.isEmpty()) {
GlobalString word = global new GlobalString(ll.pop().toString());
// q = (Queue)(results.get(word));
// if (q == null) {
if (!results.containsKey(word)) {
q = global new Queue();
}
else {
q = (Queue)(results.get(word));
}
q.push(url);
results.put(word, q);
System.out.println("Key : ["+word.toLocalString()+"],["+q.size()+"]");
/*
for (int i = 0; i < q.size(); i++) {
Object obj = q.elements[i];
GlobalString str = global new GlobalString((GlobalString)obj);
System.out.println("\t["+i+"] : "+str.toLocalString());
}*/
}
}
}
public static LinkedList tokenize(String str) {
LinkedList ll;
int sIndex = 0;
int eIndex = 0;
String token;
ll = new LinkedList();
// and, or, of, at, but, '.', ',', ':' ';', '"', ' ', '-', '='
while (true) {
eIndex = str.indexOf(' ', sIndex);
if (eIndex == -1) {
token = str.subString(sIndex);
ll.add(token);
break;
}
else {
token = str.subString(sIndex, eIndex);
ll.add(token);
sIndex = eIndex+1;
}
}
return ll;
}
public static Queue processPage(LocalQuery lq) {
int index = 0;
String href = new String("href=\"");
String searchstr = lq.response.toString();
int depth;
boolean cont = true;
Queue toprocess;
depth = lq.getDepth() + 1;
toprocess = global new Queue();
while(cont) {
int mindex = searchstr.indexOf(href,index);
if (mindex != -1) {
int endquote = searchstr.indexOf('"', mindex+href.length());
if (endquote != -1) {
String match = searchstr.subString(mindex+href.length(), endquote);
String match2 = lq.makewebcanonical(match);
GlobalString ghostname;
GlobalString gpath;
ghostname = global new GlobalString(lq.getHostName(match));
gpath = global new GlobalString(lq.getPathName(match));
if (match2 != null) {
GlobalQuery gq = global new GlobalQuery(ghostname, gpath, depth);
toprocess.push(gq);
}
index = endquote;
} else cont = false;
} else cont = false;
}
return toprocess;
}
}