summaryrefslogtreecommitdiff
path: root/TAO/orbsvcs/tests/AVStreams/server_discovery/Server_Discovery_Perf.java
blob: aa88b9da559f879b61d0c1cfa99293b7a31bdb10 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import java.awt.*;
import com.sun.java.swing.*;
import com.sun.java.swing.event.*;
import java.util.*;
import Strip_Chart;

public class Server_Discovery_Perf
  extends JInternalFrame
  implements Runnable
{
  private static final int HSPACE = 10;
  private static final int VSPACE = 10;
  private static final int MAX_WIDTH = 5;
  private static final int MAX_HEIGHT = 5;
  private static final int GRAPH_DIMENSION = 175;

  private boolean continue_ = true;
  private JPanel[] graph_panels_ = new JPanel[MAX_HEIGHT];
  private Hashtable props_ = new Hashtable ();
  private String server_name_ = null;
  // Vector containing each of the dynamic properties we will
  // periodically poll for their status.

  class Prop_Struct
  {
    public String dp_;
    public Strip_Chart graph_;

    public Prop_Struct (String dp, Strip_Chart graph)
      {
        this.dp_ = dp;
        this.graph_ = graph;
      }
  }  
  
  public Server_Discovery_Perf (String server_name)
    {
      super (server_name, true, true, true, true);
      this.setSize (MAX_WIDTH*(GRAPH_DIMENSION + HSPACE), GRAPH_DIMENSION);
      this.setLocation (375, 15);
      this.server_name_ = server_name;
      
      // Create each of the five graph rows.
      this.setLayout (new GridLayout (5, 1, HSPACE, 0));
      FlowLayout panel_layout = new FlowLayout (FlowLayout.CENTER, 0, VSPACE);
      for (int i = 0; i < MAX_HEIGHT; i++)
        {
          this.graph_panels_[i] = new JPanel ();
          this.graph_panels_[i].setLayout (panel_layout);
          this.getContentPane ().add (this.graph_panels_[i]);
        }

      // When the frame closes, end the graph updating.
      this.addInternalFrameListener (new InternalFrameAdapter ()
                                     {
                                       public void internalFrameClosing (InternalFrameEvent event)
                                         {
                                           cease_updates ();
                                         }
                                     }
                                      );
    }

  public void add_dynamic_property (String prop_name)
    {
      Prop_Struct ps = new Prop_Struct (prop_name, new Strip_Chart (prop_name));
      this.props_.put (prop_name, ps);

      int num_graphs = this.props_.size ();
      int row_panel_index = num_graphs % MAX_HEIGHT;
      this.graph_panels_[row_panel_index].add (ps.graph_);
    }
  
  public Dimension getPreferredSize ()
    {
      // The preferred size is: height = the height of the number of
      // rows; width = static --- always the width of 5 graphs.
      int num_graphs = this.props_.size ();
      int num_rows = (num_graphs % MAX_HEIGHT) + 1;

      return new Dimension (MAX_WIDTH * (GRAPH_DIMENSION + HSPACE),
                            num_rows * (GRAPH_DIMENSION + VSPACE));
    }
  
  public void run ()
    {
      // Periodically awaken and poll the dynamic properties, updating 
      // the graphs with the new values.

      for (;;)
        {
          synchronized (this)
            {
              // An atomic operation.
              if (! this.continue_)
                break;
            }

          // Update each dynamic property's graph.
          Enumeration dprops = this.props_.elements ();
          while (dprops.hasMoreElements ())
            {
              Prop_Struct ps = (Prop_Struct) dprops.nextElement ();

              // Pull the new value of the dynamic property and insert 
              // it into the corresponding strip chart.
              try
                {
                  float new_point =
                    Server_Discovery_Util.evaluate_performance_property (this.server_name_, ps.dp_);
                  ps.graph_.update (new_point);
                }
              catch (Exception excp) {}
            }

          // Go to sleep for 2 seconds.
          try
            {
              Thread.sleep (2000);
            }
          catch (Exception excp) {}
        }
    }

  private void cease_updates ()
    {      
      synchronized (this)
        {
          // An atomic operation.
          this.continue_ = false;
        }
    }
}