*** empty log message ***
[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     State state;
12     Hashtable flagstatemap;
13     Hashtable taskgraphmap;
14     Hashtable sourcenodemap; //to hold the filenames for each of the pages linked to the source nodes.
15     Hashtable taskmap;  // to hold the filenames for each of the pages linked to tasks in the program.
16     GarbageAnalysis garbageanalysis;
17
18     public WebInterface(State state, TaskAnalysis taskanalysis, TaskGraph taskgraph, GarbageAnalysis garbageanalysis) {
19         this.state=state;
20         this.taskanalysis=taskanalysis;
21         this.taskgraph=taskgraph;
22         this.garbageanalysis=garbageanalysis;
23
24         flagstatemap=new Hashtable();
25         taskgraphmap=new Hashtable();
26         taskmap = new Hashtable();
27         sourcenodemap=new Hashtable();
28         for(Iterator it_tasks=state.getTaskSymbolTable().getDescriptorsIterator();it_tasks.hasNext();){
29                 TaskDescriptor td=(TaskDescriptor)it_tasks.next();
30                 taskmap.put("/"+td.getSymbol()+".html",td);
31         } 
32         for(Iterator it_classes=state.getClassSymbolTable().getDescriptorsIterator();it_classes.hasNext();) {
33             ClassDescriptor cd=(ClassDescriptor) it_classes.next();
34             Vector rootnodes=taskanalysis.getRootNodes(cd);
35             if(rootnodes!=null)
36             for(Iterator it_rootnodes=rootnodes.iterator();it_rootnodes.hasNext();){
37                 FlagState root=(FlagState)it_rootnodes.next();
38                 Vector cd_nodeid=new Vector(); //Vector is designed to contain only 2 elements: ClassDescriptor,Node label
39                                                // Both the values are required to correctly resolve the rootnode.
40                                                // Should think of a better way to do this, instead of using a vector(maybe a class)
41                 cd_nodeid.addElement(cd);  //adding the ClassDescriptor 
42                 cd_nodeid.addElement(root.getLabel()); //adding the Node label
43                 sourcenodemap.put("/"+cd.getSymbol()+"_"+root.getLabel()+".html",cd_nodeid);
44             }
45         }
46     }
47     
48     public boolean specialRequest(String filename) {
49         System.out.println(filename);
50         if (filename.equals("/index.html"))
51             return true;
52         if (flagstatemap.containsKey(filename))
53             return true;
54         if (taskgraphmap.containsKey(filename))
55             return true;
56         if (taskmap.containsKey(filename))
57                 return true;
58         if (sourcenodemap.containsKey(filename))
59                 return true;
60         return false;
61     }
62
63     public String handleresponse(String filename, OutputStream out, HTTPResponse resp) {
64         if (filename.equals("/index.html"))
65             return indexpage(out, resp);
66         if (flagstatemap.containsKey(filename))
67             return flagstate((ClassDescriptor) flagstatemap.get(filename), out, resp);
68         if (taskgraphmap.containsKey(filename))
69             return taskstate((ClassDescriptor) taskgraphmap.get(filename), out, resp);
70         if (taskmap.containsKey(filename))
71             return task((TaskDescriptor)taskmap.get(filename),out,resp);
72         if (sourcenodemap.containsKey(filename))
73             return sourcenode((Vector) sourcenodemap.get(filename), out, resp);
74         return "NORESP";
75     }
76
77     private String task(TaskDescriptor td, OutputStream out, HTTPResponse resp){
78         try{
79         PrintWriter pw=new PrintWriter(out);
80         pw.println("<br><br><h3>Task:&nbsp;&nbsp;&nbsp;"+td.toString()+"</h3><br>");
81         printTask(td,pw);
82         pw.flush();
83         } catch (Exception e) {e.printStackTrace();System.exit(-1);}
84         return null;
85     }
86
87     private String printTask(TaskDescriptor td, PrintWriter pw){
88         try{
89
90                 for(int i=0; i < td.numParameters();i++){
91                         pw.println("FlagState Graph:&nbsp;&nbsp;<a href=\"/"+td.getParamType(i)+".html\">"+td.getParamType(i)+"</a><br>");
92                         pw.println("Task Graph:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href=\"/"+td.getParamType(i)+"-t.html\">"
93                                 +td.getParamType(i)+"</a><br>");
94                 }                       
95                 pw.flush();
96         }catch(Exception e) {e.printStackTrace();System.exit(-1);}
97         return null;
98    }
99
100     private String sourcenode(Vector cd_nodeid,OutputStream out, HTTPResponse resp){
101         Vector rootnodes=taskanalysis.getRootNodes((ClassDescriptor)cd_nodeid.elementAt(0));
102         for(Iterator it_rootnodes=rootnodes.iterator();it_rootnodes.hasNext();){
103                 FlagState root=(FlagState)it_rootnodes.next();
104                 if (root.getLabel().equals((String)cd_nodeid.elementAt(1))){
105                 try{
106                         PrintWriter pw=new PrintWriter(out);
107                         pw.println("<br><br><h3>Allocating tasks for "+root.getTextLabel()+":</h3><br>");
108                         Vector tasks=root.getAllocatingTasks();
109                         for(Iterator it_tasks=tasks.iterator();it_tasks.hasNext();){
110                                 TaskDescriptor td=(TaskDescriptor)it_tasks.next();
111                                 pw.println("<br><strong>Task:&nbsp;&nbsp;&nbsp;"+td.toString()+"</strong><br>");
112                                 printTask(td,pw);
113                         }
114                 
115                 } catch (Exception e) {e.printStackTrace();System.exit(-1);}
116                 break;
117                 }
118                 
119         }
120         return null;
121    }
122
123     private String flagstate(ClassDescriptor cd, OutputStream out, HTTPResponse resp) {
124         Set objects=taskanalysis.getFlagStates(cd);
125         File file=new File(cd.getSymbol()+".dot");
126         File mapfile;
127         String str;
128         Vector namers=new Vector();
129         namers.add(new Namer());
130         namers.add(garbageanalysis);
131         namers.add(new Allocations());
132         namers.add(new TaskEdges());
133         try {
134             //Generate jpg
135             Runtime r=Runtime.getRuntime();
136
137             FileOutputStream dotstream=new FileOutputStream(file,false);
138             FlagState.DOTVisitor.visit(dotstream, objects, namers);
139             dotstream.close();
140             Process p=r.exec("dot -Tcmapx -o"+cd.getSymbol()+".map -Tjpg -o"+cd.getSymbol()+".jpg "+cd.getSymbol()+".dot");
141             p.waitFor();
142             p=r.exec("dot -Tps "+cd.getSymbol()+".dot -o"+cd.getSymbol()+".ps");
143             p.waitFor();
144
145             mapfile=new File(cd.getSymbol()+".map");
146             BufferedReader mapbr=new BufferedReader(new FileReader(mapfile));
147             PrintWriter pw=new PrintWriter(out);
148             pw.println("<a href=\"/"+ cd.getSymbol()+".ps\">ps</a><br>");
149             //pw.println("<a href=\"/"+ cd.getSymbol()+".map\"><img src=\"/"+ cd.getSymbol()+".gif\" ismap=\"ismap\"></A>");
150             pw.println("<img src=\""+cd.getSymbol()+".jpg\" usemap=\"#dotvisitor\" />");
151             while((str=mapbr.readLine())!=null){
152                 pw.println(str);
153             }
154             
155             pw.flush();
156         } catch (Exception e) {e.printStackTrace();System.exit(-1);}
157         return null;
158     }
159
160     private String taskstate(ClassDescriptor cd, OutputStream out, HTTPResponse resp) {
161         Set objects=taskgraph.getTaskNodes(cd);
162         File file=new File(cd.getSymbol()+"-t.dot");
163         File mapfile;
164         String str;
165         Vector namers=new Vector();
166         namers.add(new Namer());
167         namers.add(new TaskNodeNamer());
168
169         try {
170             //Generate jpg
171             Runtime r=Runtime.getRuntime();
172             FileOutputStream dotstream=new FileOutputStream(file,false);
173             FlagState.DOTVisitor.visit(dotstream, objects,namers);
174             dotstream.close();
175             Process p=r.exec("dot -Tcmapx -o"+cd.getSymbol()+"-t.map -Tjpg -o"+cd.getSymbol()+"-t.jpg "+cd.getSymbol()+"-t.dot");
176             p.waitFor();
177             p=r.exec("dot -Tps "+cd.getSymbol()+".dot -o"+cd.getSymbol()+"-t.ps");
178             
179             p.waitFor();
180
181             mapfile=new File(cd.getSymbol()+"-t.map");
182             BufferedReader mapbr=new BufferedReader(new FileReader(mapfile));
183             PrintWriter pw=new PrintWriter(out);
184             pw.println("<a href=\"/"+ cd.getSymbol()+"-t.ps\">ps</a><br>");
185            // pw.println("<a href=\"/"+ cd.getSymbol()+"-t.map\"><img src=\"/"+ cd.getSymbol()+"-t.gif\" ismap=\"ismap\"></A>");
186             pw.println("<img src=\""+cd.getSymbol()+"-t.jpg\" usemap=\"#dotvisitor\" />");
187
188             while((str=mapbr.readLine())!=null){
189                 pw.println(str);
190             }
191             pw.flush();
192         } catch (Exception e) {e.printStackTrace();System.exit(-1);}
193         return null;
194     }
195     
196    /* public void taskgraph(
197 */
198
199     private String indexpage(OutputStream out, HTTPResponse resp) {
200
201         PrintWriter pw=new PrintWriter(out);
202         for(Iterator it_classes=state.getClassSymbolTable().getDescriptorsIterator();it_classes.hasNext();) {
203             ClassDescriptor cd=(ClassDescriptor) it_classes.next();
204             if (taskanalysis.getFlagStates(cd)!=null) {
205                 pw.println("<a href=\""+cd.getSymbol()+".html\">"+ cd.getSymbol() +"</a>");
206                 pw.println("<br>");
207                 flagstatemap.put("/"+cd.getSymbol()+".html", cd);
208             }
209             if (taskgraph.getTaskNodes(cd)!=null) {
210                 pw.println("<a href=\""+cd.getSymbol()+"-t.html\">Task Graph "+ cd.getSymbol() +"</a>");
211                 pw.println("<br>");
212                 taskgraphmap.put("/"+cd.getSymbol()+"-t.html", cd);
213             }
214         }
215         pw.flush();
216         return null;
217     }
218
219 }