1 public class QueryThread extends Thread {
9 Query[] currentWorkList;
11 public QueryThread(Queue todoList, Queue doneList, int maxDepth, int maxSearchDepth,int mid,int NUM_THREADS,Query[] currentWorkList) {
12 this.todoList = todoList;
13 this.doneList = doneList;
14 this.maxDepth = maxDepth;
15 this.maxSearchDepth = maxSearchDepth;
16 this.currentWorkList = currentWorkList;
18 this.NUM_THREADS = NUM_THREADS;
29 System.out.println("Thread " + workMID + " has started");
35 myWork = (Query)todoList.pop();
37 if(null == myWork) // no work in todolist
39 chk = checkCurrentWorkList(this);
42 currentWorkList[workMID] = myWork;
47 if(chk == 1) { // it has query
52 currentWorkList[workMID] = null;
55 else if(chk == -1) { // finished all work
58 else { // wait for other thread
65 System.out.println("\n\nDoneSize = " + doneList.size());
68 System.out.println("\n\n\n I'm done");
71 public static int checkCurrentWorkList(QueryThread qt) {
80 num_threads = qt.NUM_THREADS;
82 for(i = 0 ; (i < num_threads); i++) {
87 s = qt.currentWorkList[i];
97 if(chk == false) // wait for other machine's work
100 return 0; // others are still working wait until they finish work
103 public static void execute(QueryThread qt) {
109 if(qt.myWork == null) {
110 System.out.println("What!!!!!!!!!!!!!!!");
113 depth = ((Query)qt.myWork).getDepth();
115 maxSearch = qt.maxSearchDepth;
119 /* global variables */
121 GlobalString ghostname;
124 /* local variables */
125 QueryQueue toprocess;
131 q = (Query)(qt.myWork);
132 ghostname = q.getHostName();
134 hostname = new String(GlobalString.toLocalCharArray(ghostname));
135 path = new String(GlobalString.toLocalCharArray(gpath));
137 lq = new LocalQuery(hostname, path, depth);
139 System.printString("Processing - Hostname : ");
140 System.printString(hostname);
141 System.printString(", Path : ");
142 System.printString(path);
143 System.printString("\n");
145 Socket s = new Socket(hostname, 80);
147 requestQuery(hostname, path, s);
149 toprocess = processPage(lq,maxSearch);
153 while(!toprocess.isEmpty()) {
154 lq = toprocess.pop();
155 ghostname = global new GlobalString(lq.getHostName());
156 gpath = global new GlobalString(lq.getPath());
158 q = global new Query(ghostname, gpath, lq.getDepth());
165 public static void requestQuery(String hostname, String path, Socket sock) {
166 StringBuffer req = new StringBuffer("GET ");
169 req.append(" HTTP/1.1\r\nHost:");
170 req.append(hostname);
171 req.append("\r\n\r\n");
172 sock.write(req.toString().getBytes());
175 public static void readResponse(LocalQuery lq, Socket sock) {
180 // state 4 - \r\n\r\n
185 byte[] b=new byte[1];
186 int numchars=sock.read(b);
193 } else if (state==1) {
194 byte[] b=new byte[1];
195 int numchars=sock.read(b);
202 } else if (state==2) {
203 byte[] b=new byte[1];
204 int numchars=sock.read(b);
211 } else if (state==3) {
212 byte[] b=new byte[1];
213 int numchars=sock.read(b);
222 byte[] buffer=new byte[1024];
223 int numchars=sock.read(buffer);
227 String curr=(new String(buffer)).subString(0,numchars);
228 lq.response.append(curr);
234 public void doneWork(Object obj) {
238 public static QueryQueue processPage(LocalQuery lq,int maxSearchDepth) {
240 String href = new String("href=\"");
241 String searchstr = lq.response.toString();
245 QueryQueue toprocess = new QueryQueue();
246 depth = lq.getDepth() + 1;
248 int searchDepthCnt = 0;
249 while(cont && (searchDepthCnt < maxSearchDepth)) {
250 int mindex = searchstr.indexOf(href,index);
252 int endquote = searchstr.indexOf('"', mindex+href.length());
253 if (endquote != -1) {
254 String match = searchstr.subString(mindex+href.length(), endquote);
255 String match2 = lq.makewebcanonical(match);
257 if (match2 != null) {
258 LocalQuery newlq = new LocalQuery(lq.getHostName(match), lq.getPathName(match), depth);
260 toprocess.push(newlq);