bug fixes
[IRC.git] / Robust / src / Interface / WebInterface.java
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) {e.printStackTrace();System.exit(-1);}
107         return null;
108     }
109
110     private String printTask(TaskDescriptor td, PrintWriter pw){
111         try{
112
113                 for(int i=0; i < td.numParameters();i++){
114                         pw.println("FlagState Graph:&nbsp;&nbsp;<a href=\"/"+td.getParamType(i)+".html\">"+td.getParamType(i)+"</a><br>");
115                         pw.println("Task Graph:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href=\"/"+td.getParamType(i)+"-t.html\">"
116                                 +td.getParamType(i)+"</a><br>");
117                 }                       
118                 pw.flush();
119         }catch(Exception e) {e.printStackTrace();System.exit(-1);}
120         return null;
121    }
122
123     private String sourcenode(Vector cd_nodeid,OutputStream out, HTTPResponse resp){
124         Vector rootnodes=taskanalysis.getRootNodes((ClassDescriptor)cd_nodeid.elementAt(0));
125         for(Iterator it_rootnodes=rootnodes.iterator();it_rootnodes.hasNext();){
126                 FlagState root=(FlagState)it_rootnodes.next();
127                 if (root.getLabel().equals((String)cd_nodeid.elementAt(1))){
128                 try{
129                         PrintWriter pw=new PrintWriter(out);
130                         pw.println("<br><br><h3>Allocating tasks for "+root.getTextLabel()+":</h3><br>");
131                         Vector tasks=root.getAllocatingTasks();
132                         for(Iterator it_tasks=tasks.iterator();it_tasks.hasNext();){
133                                 TaskDescriptor td=(TaskDescriptor)it_tasks.next();
134                                 pw.println("<br><strong>Task:&nbsp;&nbsp;&nbsp;"+td.toString()+"</strong><br>");
135                                 printTask(td,pw);
136                         }
137                 
138                 } catch (Exception e) {e.printStackTrace();System.exit(-1);}
139                 break;
140                 }
141                 
142         }
143         return null;
144    }
145
146     private String flagstate(ClassDescriptor cd, OutputStream out, HTTPResponse resp) {
147         Set objects=taskanalysis.getFlagStates(cd);
148         File file=new File(cd.getSymbol()+".dot");
149         File mapfile;
150         String str;
151         Vector namers=new Vector();
152         namers.add(new Namer());
153         namers.add(garbageanalysis);
154         namers.add(new Allocations());
155         namers.add(new TaskEdges());
156         try {
157             //Generate jpg
158             Runtime r=Runtime.getRuntime();
159
160             FileOutputStream dotstream=new FileOutputStream(file,false);
161             FlagState.DOTVisitor.visit(dotstream, objects, namers);
162             dotstream.close();
163             Process p=r.exec("dot -Tcmapx -o"+cd.getSymbol()+".map -Tjpg -o"+cd.getSymbol()+".jpg "+cd.getSymbol()+".dot");
164             p.waitFor();
165             p=r.exec("dot -Tps "+cd.getSymbol()+".dot -o"+cd.getSymbol()+".ps");
166             p.waitFor();
167
168             mapfile=new File(cd.getSymbol()+".map");
169             BufferedReader mapbr=new BufferedReader(new FileReader(mapfile));
170             PrintWriter pw=new PrintWriter(out);
171             pw.println("<a href=\"/"+ cd.getSymbol()+".ps\">ps</a><br>");
172             //pw.println("<a href=\"/"+ cd.getSymbol()+".map\"><img src=\"/"+ cd.getSymbol()+".gif\" ismap=\"ismap\"></A>");
173             pw.println("<img src=\""+cd.getSymbol()+".jpg\" usemap=\"#dotvisitor\" />");
174             while((str=mapbr.readLine())!=null){
175                 pw.println(str);
176             }
177             
178             pw.flush();
179         } catch (Exception e) {e.printStackTrace();System.exit(-1);}
180         return null;
181     }
182
183     private String taskstate(ClassDescriptor cd, OutputStream out, HTTPResponse resp) {
184         Set objects=taskgraph.getTaskNodes(cd);
185         File file=new File(cd.getSymbol()+"-t.dot");
186         File mapfile;
187         String str;
188         Vector namers=new Vector();
189         namers.add(new Namer());
190         namers.add(new TaskNodeNamer());
191
192         try {
193             //Generate jpg
194             Runtime r=Runtime.getRuntime();
195             FileOutputStream dotstream=new FileOutputStream(file,false);
196             FlagState.DOTVisitor.visit(dotstream, objects,namers);
197             dotstream.close();
198             Process p=r.exec("dot -Tcmapx -o"+cd.getSymbol()+"-t.map -Tjpg -o"+cd.getSymbol()+"-t.jpg "+cd.getSymbol()+"-t.dot");
199             p.waitFor();
200             p=r.exec("dot -Tps "+cd.getSymbol()+".dot -o"+cd.getSymbol()+"-t.ps");
201             
202             p.waitFor();
203
204             mapfile=new File(cd.getSymbol()+"-t.map");
205             BufferedReader mapbr=new BufferedReader(new FileReader(mapfile));
206             PrintWriter pw=new PrintWriter(out);
207             pw.println("<a href=\"/"+ cd.getSymbol()+"-t.ps\">ps</a><br>");
208            // pw.println("<a href=\"/"+ cd.getSymbol()+"-t.map\"><img src=\"/"+ cd.getSymbol()+"-t.gif\" ismap=\"ismap\"></A>");
209             pw.println("<img src=\""+cd.getSymbol()+"-t.jpg\" usemap=\"#dotvisitor\" />");
210
211             while((str=mapbr.readLine())!=null){
212                 pw.println(str);
213             }
214             pw.flush();
215         } catch (Exception e) {e.printStackTrace();System.exit(-1);}
216         return null;
217     }
218     
219    /* public void taskgraph(
220 */
221
222     private String indexpage(OutputStream out, HTTPResponse resp) {
223
224         PrintWriter pw=new PrintWriter(out);
225         for(Iterator it_classes=state.getClassSymbolTable().getDescriptorsIterator();it_classes.hasNext();) {
226             ClassDescriptor cd=(ClassDescriptor) it_classes.next();
227             if (cd.hasFlags()){
228             if (taskanalysis.getFlagStates(cd)!=null) {
229                 pw.println("<a href=\""+cd.getSymbol()+".html\">"+ cd.getSymbol() +"</a>");
230                 pw.println("<br>");
231                 flagstatemap.put("/"+cd.getSymbol()+".html", cd);
232             }
233             if (taskgraph.getTaskNodes(cd)!=null) {
234                 pw.println("<a href=\""+cd.getSymbol()+"-t.html\">Task Graph "+ cd.getSymbol() +"</a>");
235                 pw.println("<br>");
236                 taskgraphmap.put("/"+cd.getSymbol()+"-t.html", cd);
237             }
238         }
239         }
240         pw.println("<br><br><a href=\"/UnifiedTaskGraph.html\">Program flow</a>");
241         pw.flush();
242         return null;
243     }
244     
245     private String unifiedTaskGraph(OutputStream out, HTTPResponse resp){
246         Set objects=taskgraph.getAllTaskNodes();
247         File file=new File("UnifiedTaskGraph.dot");
248         String str;
249         Vector namers=new Vector();
250         namers.add(new Namer());
251         namers.add(new TaskNodeNamer());
252
253         try {
254             //Generate jpg
255             Runtime r=Runtime.getRuntime();
256             FileOutputStream dotstream=new FileOutputStream(file,false);
257             FlagState.DOTVisitor.visit(dotstream, objects, namers);
258             dotstream.close();
259             Process p=r.exec("dot -Tjpg -oUnifiedTaskGraph.jpg -Tcmapx -oUnifiedTaskGraph.map UnifiedTaskGraph.dot");
260             p.waitFor();
261             p=r.exec("dot -Tps UnifiedTaskGraph.dot -oUnifiedTaskGraph.ps");
262             
263             p.waitFor();
264
265             File mapfile=new File("UnifiedTaskGraph.map");
266             BufferedReader mapbr=new BufferedReader(new FileReader(mapfile));
267             PrintWriter pw=new PrintWriter(out);
268             pw.println("<a href=\"/UnifiedTaskGraph.ps\">ps</a><br>");
269            // pw.println("<a href=\"/"+ cd.getSymbol()+"-t.map\"><img src=\"/"+ cd.getSymbol()+"-t.gif\" ismap=\"ismap\"></A>");
270             pw.println("<img src=\"/UnifiedTaskGraph.jpg\" usemap=\"#dotvisitor\"  />");
271               
272             while((str=mapbr.readLine())!=null)
273                         pw.println(str);
274                     
275             pw.flush();
276         } catch (Exception e) {e.printStackTrace();System.exit(-1);}
277         return null;
278     }
279
280 }