1 public class QueryThread extends Task {
7 public QueryThread(Queue qq, Queue ql, int depth, int searchDepth) {
10 this.maxDepth = depth;
11 this.maxSearchDepth = searchDepth;
16 public void execute(Object mywork) {
17 Query q = (Query)mywork;
18 GlobalString ghostname;
22 ghostname = q.getHostName();
26 String hostname = new String(GlobalString.toLocalCharArray(ghostname));
27 String path = new String(GlobalString.toLocalCharArray(gpath));
29 System.printString("Processing ");
30 System.printString(hostname + "\n");
31 System.printString(" ");
32 System.printString(path);
33 System.printString("\n");
35 Socket s = new Socket(hostname, 80);
37 requestQuery(hostname, path, s);
38 // System.printString("Wait for 5 secs\n");
39 // Thread.sleep(2000000);
42 // System.printString("Wait for 5 secs\n");
43 // Thread.sleep(2000000);
46 // System.printString("Wait for 5 secs\n");
47 // Thread.sleep(2000000);
49 processPage(q, (QueryList)doneList);
53 public void requestQuery(String hostname, String path, Socket sock) {
54 StringBuffer req = new StringBuffer("GET ");
57 req.append(" HTTP/1.1\r\nHost:");
59 req.append("\r\n\r\n");
60 System.printString("req : " + req + "\n");
61 sock.write(req.toString().getBytes());
64 public void readResponse(Query q, Socket sock) {
75 int numchars=sock.read(b);
82 } else if (state==1) {
84 int numchars=sock.read(b);
91 } else if (state==2) {
93 int numchars=sock.read(b);
100 } else if (state==3) {
101 byte[] b=new byte[1];
102 int numchars=sock.read(b);
111 byte[] buffer=new byte[1024];
112 int numchars=sock.read(buffer);
116 String curr=(new String(buffer)).subString(0,numchars);
117 q.response.append(curr);
123 public void done(Object obj) {
125 // System.printString("Size of todoList : " + todoList.size() + "\n");
126 // Thread.sleep(5000000);
129 public void processPage(Query q, QueryList doneList) {
131 String href = new String("href=\"");
132 String searchstr = q.response.toLocalString();
135 while(cont && (searchDepthCnt < maxSearchDepth)) {
136 int mindex = searchstr.indexOf(href,index);
138 int endquote = searchstr.indexOf('"', mindex+href.length());
139 if (endquote != -1) {
140 String match = searchstr.subString(mindex+href.length(), endquote);
142 GlobalString gmatch2;
145 gmatch = global new GlobalString(match);
146 gmatch2 = q.makewebcanonical(gmatch);
148 if (gmatch2 != null && !doneList.checkQuery(gmatch2)) {
149 // doneList.push(gmatch2);
151 if (depthCnt < maxDepth) {
153 System.printString("Depth : " + depthCnt + "\n");
155 newq = global new Query(q.getHostName(gmatch), q.getPathName(gmatch));
157 System.printString("Size of todoList : " + todoList.size() + "\n");