1 public class QueryTask extends Task {
4 DistributedHashMap results;
5 GlobalString workingURL;
7 public QueryTask(Queue todoList, DistributedHashMap doneList, int maxDepth, DistributedHashMap results) {
8 this.todoList = todoList;
9 this.doneList = doneList;
10 this.maxDepth = maxDepth;
11 this.results = results;
14 public void execute() {
19 depth = ((GlobalQuery)myWork).getDepth();
24 /* global variables */
33 gq = (GlobalQuery)myWork;
34 hostname = new String(GlobalString.toLocalCharArray(gq.getHostName()));
35 path = new String(GlobalString.toLocalCharArray(gq.getPath()));
37 GlobalStringBuffer gsb = global new GlobalStringBuffer(hostname);
40 workingURL = global new GlobalString(gsb.toGlobalString());
42 lq = new LocalQuery(hostname, path, depth);
44 System.printString(lq.getDepth()+" ");
45 System.printString("Processing - Hostname : ");
46 System.printString(hostname);
47 System.printString(", Path : ");
48 System.printString(path);
49 System.printString("\n");
51 Socket s = new Socket(hostname, 80);
53 requestQuery(hostname, path, s);
57 processList(lq, workingURL, results);
61 toprocess = processPage(lq);
68 public void done(Object obj) {
69 GlobalString str = global new GlobalString("true");
70 doneList.put(workingURL, str);
72 while(!toprocess.isEmpty()) {
73 GlobalQuery q = (GlobalQuery)toprocess.pop();
75 GlobalString hostname = global new GlobalString(q.getHostName());
76 GlobalString path = global new GlobalString(q.getPath());
78 GlobalStringBuffer gsb = global new GlobalStringBuffer(hostname);
82 if (!doneList.containsKey(gsb.toGlobalString())) {
88 public static void requestQuery(String hostname, String path, Socket sock) {
89 StringBuffer req = new StringBuffer("GET ");
92 req.append(" HTTP/1.1\r\nHost:");
94 req.append("\r\n\r\n");
95 sock.write(req.toString().getBytes());
98 public static void readResponse(LocalQuery lq, Socket sock) {
103 // state 4 - \r\n\r\n
108 byte[] b=new byte[1];
109 int numchars=sock.read(b);
116 } else if (state==1) {
117 byte[] b=new byte[1];
118 int numchars=sock.read(b);
125 } else if (state==2) {
126 byte[] b=new byte[1];
127 int numchars=sock.read(b);
134 } else if (state==3) {
135 byte[] b=new byte[1];
136 int numchars=sock.read(b);
145 byte[] buffer=new byte[1024];
146 int numchars=sock.read(buffer);
150 String curr=(new String(buffer)).subString(0,numchars);
151 lq.response.append(curr);
157 public static void processList(LocalQuery lq, GlobalString url, DistributedHashMap results) {
158 String sTitle = new String("<title>");
159 String eTitle = new String("</title>");
160 String searchstr = lq.response.toString();
163 int sIndex = searchstr.indexOf(sTitle);
165 int eIndex = searchstr.indexOf(eTitle, sIndex+sTitle.length());
166 String title = new String(searchstr.subString(sIndex+sTitle.length(), eIndex));
167 ll = tokenize(title);
170 while (!ll.isEmpty()) {
171 GlobalString word = global new GlobalString(ll.pop().toString());
172 // q = (Queue)(results.get(word));
175 if (!results.containsKey(word)) {
176 q = global new Queue();
179 q = (Queue)(results.get(word));
182 results.put(word, q);
184 System.out.println("Key : ["+word.toLocalString()+"],["+q.size()+"]");
186 for (int i = 0; i < q.size(); i++) {
187 Object obj = q.elements[i];
188 GlobalString str = global new GlobalString((GlobalString)obj);
189 System.out.println("\t["+i+"] : "+str.toLocalString());
195 public static LinkedList tokenize(String str) {
201 ll = new LinkedList();
203 // and, or, of, at, but, '.', ',', ':' ';', '"', ' ', '-', '='
205 eIndex = str.indexOf(' ', sIndex);
207 token = str.subString(sIndex);
212 token = str.subString(sIndex, eIndex);
221 public static Queue processPage(LocalQuery lq) {
223 String href = new String("href=\"");
224 String searchstr = lq.response.toString();
229 depth = lq.getDepth() + 1;
231 toprocess = global new Queue();
234 int mindex = searchstr.indexOf(href,index);
236 int endquote = searchstr.indexOf('"', mindex+href.length());
237 if (endquote != -1) {
238 String match = searchstr.subString(mindex+href.length(), endquote);
239 String match2 = lq.makewebcanonical(match);
241 GlobalString ghostname;
244 ghostname = global new GlobalString(lq.getHostName(match));
245 gpath = global new GlobalString(lq.getPathName(match));
247 if (match2 != null) {
248 GlobalQuery gq = global new GlobalQuery(ghostname, gpath, depth);