This commit was manufactured by cvs2svn to create tag 'buildscript'.
[IRC.git] /
1 package Interface;
2 import java.io.*;
3 import Analysis.TaskStateAnalysis.*;
4 import IR.*;
5 import java.util.*;
6 import Util.Namer;
7
8 public class WebInterface {
9   TaskAnalysis taskanalysis;
10   TaskGraph taskgraph;
11   TagAnalysis taganalysis;
12   State state;
13   Hashtable flagstatemap;
14   Hashtable taskgraphmap;
15   Hashtable sourcenodemap;   //to hold the filenames for each of the pages linked to the source nodes.
16   Hashtable taskmap;    // to hold the filenames for each of the pages linked to tasks in the program.
17   GarbageAnalysis garbageanalysis;
18
19   public WebInterface(State state, TaskAnalysis taskanalysis, TaskGraph taskgraph, GarbageAnalysis garbageanalysis, TagAnalysis taganalysis) {
20     this.state=state;
21     this.taskanalysis=taskanalysis;
22     this.taskgraph=taskgraph;
23     this.garbageanalysis=garbageanalysis;
24     this.taganalysis=taganalysis;
25
26     flagstatemap=new Hashtable();
27     taskgraphmap=new Hashtable();
28     taskmap = new Hashtable();
29     sourcenodemap=new Hashtable();
30
31     for(Iterator it_tasks=state.getTaskSymbolTable().getDescriptorsIterator(); it_tasks.hasNext();) {
32       TaskDescriptor td=(TaskDescriptor)it_tasks.next();
33       taskmap.put("/"+td.getSymbol()+".html",td);
34     }
35
36     for(Iterator it_classes=state.getClassSymbolTable().getDescriptorsIterator(); it_classes.hasNext();) {
37       ClassDescriptor cd=(ClassDescriptor) it_classes.next();
38       if(cd.hasFlags()) {
39         Vector rootnodes=taskanalysis.getRootNodes(cd);
40
41         if(rootnodes!=null)
42           for(Iterator it_rootnodes=rootnodes.iterator(); it_rootnodes.hasNext();) {
43             FlagState root=(FlagState)it_rootnodes.next();
44             Vector cd_nodeid=new Vector();                     //Vector is designed to contain only 2 elements: ClassDescriptor,Node label
45             // Both the values are required to correctly resolve the rootnode.
46             // Should think of a better way to do this, instead of using a vector(maybe a class)
47             cd_nodeid.addElement(cd);                      //adding the ClassDescriptor
48             cd_nodeid.addElement(root.getLabel());                     //adding the Node label
49             System.out.println(cd+" "+root.getLabel());
50             sourcenodemap.put("/"+cd.getSymbol()+"_"+root.getLabel()+".html",cd_nodeid);
51           }
52       }
53     }
54   }
55
56   public boolean specialRequest(String filename) {
57     System.out.println(filename);
58     if (filename.equals("/index.html"))
59       return true;
60     if (filename.equals("/UnifiedTaskGraph.html"))
61       return true;
62     if (flagstatemap.containsKey(filename))
63       return true;
64     if (taskgraphmap.containsKey(filename))
65       return true;
66     if (taskmap.containsKey(filename))
67       return true;
68     if (sourcenodemap.containsKey(filename))
69       return true;
70     return false;
71   }
72
73   public String handleresponse(String filename, OutputStream out, HTTPResponse resp) {
74     if (filename.equals("/index.html"))
75       return indexpage(out, resp);
76     if (filename.equals("/UnifiedTaskGraph.html"))
77       return unifiedTaskGraph(out,resp);
78     if (flagstatemap.containsKey(filename))
79       return flagstate((ClassDescriptor) flagstatemap.get(filename), out, resp);
80     if (taskgraphmap.containsKey(filename))
81       return taskstate((ClassDescriptor) taskgraphmap.get(filename), out, resp);
82     if (taskmap.containsKey(filename))
83       return task((TaskDescriptor)taskmap.get(filename),out,resp);
84     if (sourcenodemap.containsKey(filename))
85       return sourcenode((Vector) sourcenodemap.get(filename), out, resp);
86     return "NORESP";
87   }
88
89   private String task(TaskDescriptor td, OutputStream out, HTTPResponse resp) {
90     try {
91       PrintWriter pw=new PrintWriter(out);
92       pw.println("<br><br><h3>Task:&nbsp;&nbsp;&nbsp;"+td.toString()+"</h3><br>");
93       printTask(td,pw);
94
95       //printing out the classes that are instantiated by this task
96       pw.println("<br><h3>Instantiated Classes:</h3>");
97       Set newstates=taganalysis.getFlagStates(td);
98       for(Iterator fsit=newstates.iterator(); fsit.hasNext();) {
99         FlagState fsnew=(FlagState) fsit.next();
100         ClassDescriptor cd=fsnew.getClassDescriptor();
101         pw.println("&nbsp;&nbsp;<a href=\"/"+cd.getSymbol()+".html\">"+cd.getSymbol()+"</a><br>");
102         pw.println("&nbsp;&nbsp;&nbsp;&nbsp;"+fsnew.getTextLabel()+"<br>");
103       }
104
105       pw.flush();
106     } catch (Exception e) {
107       e.printStackTrace(); System.exit(-1);
108     }
109     return null;
110   }
111
112   private String printTask(TaskDescriptor td, PrintWriter pw) {
113     try {
114
115       for(int i=0; i < td.numParameters(); i++) {
116         pw.println("FlagState Graph:&nbsp;&nbsp;<a href=\"/"+td.getParamType(i)+".html\">"+td.getParamType(i)+"</a><br>");
117         pw.println("Task Graph:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href=\"/"+td.getParamType(i)+"-t.html\">"
118                    +td.getParamType(i)+"</a><br>");
119       }
120       pw.flush();
121     } catch(Exception e) {
122       e.printStackTrace(); System.exit(-1);
123     }
124     return null;
125   }
126
127   private String sourcenode(Vector cd_nodeid,OutputStream out, HTTPResponse resp) {
128     Vector rootnodes=taskanalysis.getRootNodes((ClassDescriptor)cd_nodeid.elementAt(0));
129     for(Iterator it_rootnodes=rootnodes.iterator(); it_rootnodes.hasNext();) {
130       FlagState root=(FlagState)it_rootnodes.next();
131       if (root.getLabel().equals((String)cd_nodeid.elementAt(1))) {
132         try {
133           PrintWriter pw=new PrintWriter(out);
134           pw.println("<br><br><h3>Allocating tasks for "+root.getTextLabel()+":</h3><br>");
135           Vector tasks=root.getAllocatingTasks();
136           for(Iterator it_tasks=tasks.iterator(); it_tasks.hasNext();) {
137             TaskDescriptor td=(TaskDescriptor)it_tasks.next();
138             pw.println("<br><strong>Task:&nbsp;&nbsp;&nbsp;"+td.toString()+"</strong><br>");
139             printTask(td,pw);
140           }
141
142         } catch (Exception e) {
143           e.printStackTrace(); System.exit(-1);
144         }
145         break;
146       }
147
148     }
149     return null;
150   }
151
152   private String flagstate(ClassDescriptor cd, OutputStream out, HTTPResponse resp) {
153     Set objects=taskanalysis.getFlagStates(cd);
154     File file=new File(cd.getSymbol()+".dot");
155     File mapfile;
156     String str;
157     Vector namers=new Vector();
158     namers.add(new Namer());
159     namers.add(garbageanalysis);
160     namers.add(new Allocations());
161     namers.add(new TaskEdges());
162     try {
163       //Generate jpg
164       Runtime r=Runtime.getRuntime();
165
166       FileOutputStream dotstream=new FileOutputStream(file,false);
167       FlagState.DOTVisitor.visit(dotstream, objects, namers);
168       dotstream.close();
169       Process p=r.exec("dot -Tcmapx -o"+cd.getSymbol()+".map -Tjpg -o"+cd.getSymbol()+".jpg "+cd.getSymbol()+".dot");
170       p.waitFor();
171       p=r.exec("dot -Tps "+cd.getSymbol()+".dot -o"+cd.getSymbol()+".ps");
172       p.waitFor();
173
174       mapfile=new File(cd.getSymbol()+".map");
175       BufferedReader mapbr=new BufferedReader(new FileReader(mapfile));
176       PrintWriter pw=new PrintWriter(out);
177       pw.println("<a href=\"/"+ cd.getSymbol()+".ps\">ps</a><br>");
178       //pw.println("<a href=\"/"+ cd.getSymbol()+".map\"><img src=\"/"+ cd.getSymbol()+".gif\" ismap=\"ismap\"></A>");
179       pw.println("<img src=\""+cd.getSymbol()+".jpg\" usemap=\"#dotvisitor\" />");
180       while((str=mapbr.readLine())!=null) {
181         pw.println(str);
182       }
183
184       pw.flush();
185     } catch (Exception e) {
186       e.printStackTrace(); System.exit(-1);
187     }
188     return null;
189   }
190
191   private String taskstate(ClassDescriptor cd, OutputStream out, HTTPResponse resp) {
192     Set objects=taskgraph.getTaskNodes(cd);
193     File file=new File(cd.getSymbol()+"-t.dot");
194     File mapfile;
195     String str;
196     Vector namers=new Vector();
197     namers.add(new Namer());
198     namers.add(new TaskNodeNamer());
199
200     try {
201       //Generate jpg
202       Runtime r=Runtime.getRuntime();
203       FileOutputStream dotstream=new FileOutputStream(file,false);
204       FlagState.DOTVisitor.visit(dotstream, objects,namers);
205       dotstream.close();
206       Process p=r.exec("dot -Tcmapx -o"+cd.getSymbol()+"-t.map -Tjpg -o"+cd.getSymbol()+"-t.jpg "+cd.getSymbol()+"-t.dot");
207       p.waitFor();
208       p=r.exec("dot -Tps "+cd.getSymbol()+".dot -o"+cd.getSymbol()+"-t.ps");
209
210       p.waitFor();
211
212       mapfile=new File(cd.getSymbol()+"-t.map");
213       BufferedReader mapbr=new BufferedReader(new FileReader(mapfile));
214       PrintWriter pw=new PrintWriter(out);
215       pw.println("<a href=\"/"+ cd.getSymbol()+"-t.ps\">ps</a><br>");
216       // pw.println("<a href=\"/"+ cd.getSymbol()+"-t.map\"><img src=\"/"+ cd.getSymbol()+"-t.gif\" ismap=\"ismap\"></A>");
217       pw.println("<img src=\""+cd.getSymbol()+"-t.jpg\" usemap=\"#dotvisitor\" />");
218
219       while((str=mapbr.readLine())!=null) {
220         pw.println(str);
221       }
222       pw.flush();
223     } catch (Exception e) {
224       e.printStackTrace(); System.exit(-1);
225     }
226     return null;
227   }
228
229   /* public void taskgraph(
230    */
231
232   private String indexpage(OutputStream out, HTTPResponse resp) {
233
234     PrintWriter pw=new PrintWriter(out);
235     for(Iterator it_classes=state.getClassSymbolTable().getDescriptorsIterator(); it_classes.hasNext();) {
236       ClassDescriptor cd=(ClassDescriptor) it_classes.next();
237       if (cd.hasFlags()) {
238         if (taskanalysis.getFlagStates(cd)!=null) {
239           pw.println("<a href=\""+cd.getSymbol()+".html\">"+ cd.getSymbol() +"</a>");
240           pw.println("<br>");
241           flagstatemap.put("/"+cd.getSymbol()+".html", cd);
242         }
243         if (taskgraph.getTaskNodes(cd)!=null) {
244           pw.println("<a href=\""+cd.getSymbol()+"-t.html\">Task Graph "+ cd.getSymbol() +"</a>");
245           pw.println("<br>");
246           taskgraphmap.put("/"+cd.getSymbol()+"-t.html", cd);
247         }
248       }
249     }
250     pw.println("<br><br><a href=\"/UnifiedTaskGraph.html\">Program flow</a>");
251     pw.flush();
252     return null;
253   }
254
255   private String unifiedTaskGraph(OutputStream out, HTTPResponse resp) {
256     Set objects=taskgraph.getAllTaskNodes();
257     File file=new File("UnifiedTaskGraph.dot");
258     String str;
259     Vector namers=new Vector();
260     namers.add(new Namer());
261     namers.add(new TaskNodeNamer());
262
263     try {
264       //Generate jpg
265       Runtime r=Runtime.getRuntime();
266       FileOutputStream dotstream=new FileOutputStream(file,false);
267       FlagState.DOTVisitor.visit(dotstream, objects, namers);
268       dotstream.close();
269       Process p=r.exec("dot -Tjpg -oUnifiedTaskGraph.jpg -Tcmapx -oUnifiedTaskGraph.map UnifiedTaskGraph.dot");
270       p.waitFor();
271       p=r.exec("dot -Tps UnifiedTaskGraph.dot -oUnifiedTaskGraph.ps");
272
273       p.waitFor();
274
275       File mapfile=new File("UnifiedTaskGraph.map");
276       BufferedReader mapbr=new BufferedReader(new FileReader(mapfile));
277       PrintWriter pw=new PrintWriter(out);
278       pw.println("<a href=\"/UnifiedTaskGraph.ps\">ps</a><br>");
279       // pw.println("<a href=\"/"+ cd.getSymbol()+"-t.map\"><img src=\"/"+ cd.getSymbol()+"-t.gif\" ismap=\"ismap\"></A>");
280       pw.println("<img src=\"/UnifiedTaskGraph.jpg\" usemap=\"#dotvisitor\"  />");
281
282       while((str=mapbr.readLine())!=null)
283         pw.println(str);
284
285       pw.flush();
286     } catch (Exception e) {
287       e.printStackTrace(); System.exit(-1);
288     }
289     return null;
290   }
291
292 }