summaryrefslogtreecommitdiff
path: root/TAO/examples/AMI/FL_Callback/Progress_i.cpp
blob: 91e6c571118a4beded5d54d7db78fe503b2d8558 (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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
// $Id$

#include "Progress_i.h"

#include <FL/Fl_Slider.h>
#include <FL/Fl_Button.h>

ACE_RCSID (FL_Cube,
           test_i,
           "$Id$")

Progress_i::Progress_i (Progress_Window *window)
  :  window_ (window)
{
}

void
Progress_i::sent_request (CORBA::Long id
                     ACE_ENV_ARG_DECL_NOT_USED)
    ACE_THROW_SPEC ((CORBA::SystemException))
{
  //ACE_DEBUG ((LM_DEBUG, "Progress (%t) - sent request %d\n", id));
  this->window_->sent_request (id);
}

void
Progress_i::recv_reply (CORBA::Long id
                        ACE_ENV_ARG_DECL_NOT_USED)
    ACE_THROW_SPEC ((CORBA::SystemException))
{
  this->window_->recv_reply (id);
}

CORBA::Long
Progress_i::bind (Peer_ptr a_peer
                  ACE_ENV_ARG_DECL)
    ACE_THROW_SPEC ((CORBA::SystemException))
{
  ACE_DEBUG ((LM_DEBUG, "Progress (%t) - peer bound\n"));
  return this->window_->bind (a_peer ACE_ENV_ARG_PARAMETER);
}

// ****************************************************************

Progress_Window::Progress_Window (int n_peers,
                                  int n_iterations,
                                  int x, int y,
                                  int w, int h,
                                  const char* l)
  :  Fl_Box (x, y, w, h, l),
     n_peers_ (n_peers),
     peers_ (n_peers),
     n_iterations_ (n_iterations)
{
  ACE_NEW (this->request_progress_, Fl_Widget*[this->n_peers_]);
  ACE_NEW (this->reply_progress_, Fl_Widget*[this->n_peers_]);

  // @@ leak...
  Fl_Widget *button = new Fl_Button (x + 5, y + 5,
                                     50, h - 10,
                                     "Start");
  button->callback (Progress_Window::start_callback, this);

  // Compute the slider height, leaving a little margin...
  int sh = h / ( 2 * this->n_peers_);
  sh = sh - 5;

  // The initial position, with margin...
  int sy = y + 5;

  // Set the width of the sliders...
  int sx = x + 60;
  int sw = w - 65;
  int max = this->n_peers_ * this->n_iterations_;
  for (int i = 0; i < this->n_peers_; ++i)
    {
      Fl_Slider *obj;

      this->request_progress_[i] = obj =
        new Fl_Slider (FL_HOR_FILL_SLIDER, sx, sy, sw, sh, "");
      obj->color (FL_BLACK);
      obj->selection_color (FL_RED);
      obj->bounds (0, max);
      obj->precision (0);
      obj->value (0);
      sy += sh + 5;

      this->reply_progress_[i] = obj =
        new Fl_Slider (FL_HOR_FILL_SLIDER, sx, sy, sw, sh, "");
      obj->color (FL_BLACK);
      obj->selection_color (FL_GREEN);
      obj->bounds (0, max);
      obj->precision (0);
      obj->value (0);
      sy += sh + 5;
    }
}

void
Progress_Window::sent_request (CORBA::Long id)
{
  if (id < 0 || id >= this->n_peers_)
    return;
  Fl_Slider *obj =
    dynamic_cast<Fl_Slider*> (this->request_progress_[id]);
  obj->value (obj->value () + 1);
  obj->redraw ();

  int i = static_cast<int> (obj->value ());
  if (i % 100 == 0)
    ACE_DEBUG ((LM_DEBUG, "Progress (%t) - recv reply %d\n", i));
}

void
Progress_Window::recv_reply (CORBA::Long id)
{
  if (id < 0 || id >= this->n_peers_)
    return;
  Fl_Slider *obj =
    dynamic_cast<Fl_Slider*> (this->reply_progress_[id]);
  obj->value (obj->value () + 1);
  obj->redraw ();

  int i = static_cast<int> (obj->value ());
  if (i % 100 == 0)
    ACE_DEBUG ((LM_DEBUG, "Progress (%t) - recv reply %d\n", i));
}

CORBA::Long
Progress_Window::bind (Peer_ptr a_peer
                       ACE_ENV_ARG_DECL_NOT_USED)
{
  CORBA::ULong l = this->peers_.length ();
  this->peers_.length (l + 1);
  this->peers_[l] = Peer::_duplicate (a_peer);

  return l;
}

void
Progress_Window::start_callback (Fl_Widget* /* widget */,
                                 void *data)
{
  Progress_Window *self = static_cast<Progress_Window*> (data);
  self->start ();
}

void
Progress_Window::start ()
{
  if (this->peers_.length () < CORBA::ULong (this->n_peers_))
    return;

  ACE_DECLARE_NEW_CORBA_ENV;
  for (CORBA::ULong i = 0; i != this->peers_.length (); ++i)
    {
      ACE_TRY
        {
          this->peers_[i]->start (this->peers_,
                                  this->n_iterations_
                                  ACE_ENV_ARG_PARAMETER);
          ACE_TRY_CHECK;
        }
      ACE_CATCHANY
        {
          // Ignore exceptions
        }
      ACE_ENDTRY;
    }
}