Handle hostnames with upper-case letters
[webmin.git] / proc / Tracer.java
1 import java.awt.*;
2 import java.net.*;
3 import java.io.*;
4 import java.util.*;
5 import java.applet.*;
6
7 public class Tracer extends Applet implements Runnable,CbButtonCallback
8 {
9         MultiColumn log;
10         StringBuffer logbuffer = new StringBuffer();
11         LineInputStream is;
12         Thread th;
13         CbButton pause, button;
14         boolean paused = false;
15         int MAX_ROWS = 1000;
16         Vector buffer = new Vector();
17
18         public void init()
19         {
20         // Create the UI
21         setLayout(new BorderLayout());
22         String cols[] = { "Time", "System Call", "Parameters", "Return" };
23         add("Center", log = new MultiColumn(cols));
24         float widths[] = { .1f, .15f, .65f, .1f };
25         log.setWidths(widths);
26         Util.setFont(new Font("TimesRoman", Font.PLAIN, 12));
27         Panel bot = new Panel();
28         bot.setBackground(Color.white);
29         bot.setForeground(Color.white);
30         bot.setLayout(new FlowLayout(FlowLayout.RIGHT));
31         bot.add(pause = new CbButton("  Pause  ", this));
32         add("South", bot);
33         }
34
35         public void start()
36         {
37         // Start download thread
38         log.clear();
39         th = new Thread(this);
40         th.start();
41         }
42
43         public void stop()
44         {
45         // Stop download
46         try {
47                 String killurl = getParameter("killurl");
48                 if (killurl != null) {
49                         // Call this CGI at stop time
50                         try {
51                                 URL u = new URL(getDocumentBase(), killurl);
52                                 URLConnection uc = u.openConnection();
53                                 String session = getParameter("session");
54                                 if (session != null)
55                                     uc.setRequestProperty("Cookie", session);
56                                 uc.getInputStream().close();
57                                 }
58                         catch(Exception e2) { }
59                         }
60                 if (is != null) is.close();
61                 if (th != null) th.stop();
62                 }
63         catch(Exception e) {
64                 // ignore it
65                 e.printStackTrace();
66                 }
67         }
68
69         public void run()
70         {
71         try {
72                 URL u = new URL(getDocumentBase(), getParameter("url"));
73                 URLConnection uc = u.openConnection();
74                 String session = getParameter("session");
75                 if (session != null)
76                         uc.setRequestProperty("Cookie", session);
77                 is = new LineInputStream(uc.getInputStream());
78                 while(true) {
79                         StringSplitter tok =
80                                 new StringSplitter(is.gets(), '\t', false);
81                         if (tok.countTokens() == 4) {
82                                 Object row[] = { tok.nextToken(),
83                                                  tok.nextToken(),
84                                                  tok.nextToken(),
85                                                  tok.nextToken() };
86                                 if (paused) {
87                                         // Store in temp buffer
88                                         buffer.addElement(row);
89                                         if (buffer.size() > MAX_ROWS) {
90                                                 buffer.removeElementAt(0);
91                                                 }
92                                         }
93                                 else {
94                                         // Add immediately
95                                         log.addItem(row);
96                                         cleanup();
97                                         log.scrollto(log.count()-1);
98                                         }
99                                 }
100                         }
101                 }
102         catch(EOFException e) {
103                 // end of file ..
104                 }
105         catch(IOException e) {
106                 // shouldn't happen!
107                 e.printStackTrace();
108                 }
109         }
110
111         void cleanup()
112         {
113         while(log.count() > MAX_ROWS) {
114                 log.deleteItem(0);
115                 }
116         }
117
118         public void click(CbButton b) {
119                 if (b == pause) {
120                         if (paused) {
121                                 // Resume display, and add missed stuff
122                                 pause.setText("  Pause  ");
123                                 for(int i=0; i<buffer.size(); i++) {
124                                         Object row[] =
125                                                 (Object[])buffer.elementAt(i);
126                                         log.addItem(row);
127                                         }
128                                 cleanup();
129                                 log.scrollto(log.count()-1);
130                                 buffer.removeAllElements();
131                         } else {
132                                 // Stop display
133                                 pause.setText("Resume");
134                         }
135                         paused = !paused;
136                 }
137         }
138 }
139