summaryrefslogtreecommitdiff
path: root/TAO/orbsvcs/examples/FaultTolerance/FLARe/Worker/Worker_i.cpp
blob: e7806502566d0c7c5851bd536c7be721e5e7a6c7 (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
#include "Worker_i.h"

Failure_Task::Failure_Task (CORBA::ORB_ptr orb,
                            long limit,
                            long & count)
  : condition_ (lock_),
    orb_ (CORBA::ORB::_duplicate (orb)),
    limit_ (limit),
    count_ (count),
    stop_ (false)
{
}

int
Failure_Task::svc (void)
{
  ACE_Guard <ACE_Thread_Mutex> guard (lock_);

  while (((limit_ == 0) || (count_ < limit_)) && !stop_)
    {
      condition_.wait ();
    }

  orb_->shutdown (true);

  return 0;
}

void 
Failure_Task::signal (void)
{
  condition_.signal ();
}

void
Failure_Task::stop (void)
{
  stop_ = true;
  condition_.signal ();
}

Worker_i::Worker_i (CORBA::ORB_ptr orb,
                    PortableServer::POA_ptr poa,
                    const std::string & object_id,
		    StateSynchronizationAgent_ptr agent,
		    long invocations)
  : orb_ (CORBA::ORB::_duplicate (orb)),
    poa_ (PortableServer::POA::_duplicate (poa)),
    object_id_ (object_id),
    agent_ (StateSynchronizationAgent::_duplicate (agent)),
    state_ (0),
    suicidal_count_ (invocations),
    task_ (orb_.in (), suicidal_count_, state_)
{
  timer_.calibrate ();
  task_.activate ();
}

CORBA::ULong
Worker_i::run_task (CORBA::Double execution_time)
{
  timer_.start ();

  this->cpu_.run (static_cast <size_t> (execution_time));

  ++state_;

  agent_->state_changed (object_id_.c_str ());

  timer_.stop ();

  timer_.elapsed_time (last_execution_time_);

  ACE_DEBUG ((LM_TRACE, "x=%d ", last_execution_time_.msec ()));

  task_.signal ();

  return last_execution_time_.msec ();
}

void Worker_i::stop ()
{
  task_.stop ();
}

void
Worker_i::set_state (const CORBA::Any & state_value)
{
  // extract value to an intermediate long variable since it's not possible
  // to extract to a long & directly
  CORBA::Long value;

  if (state_value >>= value)
    state_ = value;
  else
    ACE_DEBUG ((LM_WARNING,
                "(%P|%t) Worker_i::set_state () "
                "could not extract state value from Any."));

  ACE_DEBUG ((LM_TRACE, "Worker_i::set_state (%d) called.\n", value));
}

CORBA::Any *
Worker_i::get_state ()
{
  // create new any object
  CORBA::Any_var state (new CORBA::Any);
  
  // create intermediate object with the value
  CORBA::Long value = state_;

  ACE_DEBUG ((LM_DEBUG, "(%P|%t) Worker_i::get_state returns %d.\n", value));

  // insert value into the any object
  *state <<= value;

  return state._retn ();
}

StateSynchronizationAgent_ptr
Worker_i::agent (void)
{
  return StateSynchronizationAgent::_duplicate (agent_.in ());
}
  
void
Worker_i::agent (StateSynchronizationAgent_ptr agent)
{
  agent_ = agent;
}

char *
Worker_i::object_id (void)
{
  return CORBA::string_dup (object_id_.c_str ());
}
  
void
Worker_i::object_id (const char * object_id)
{
  object_id_ = object_id;
}