summaryrefslogtreecommitdiff
path: root/TAO/tao/LF_CH_Event.cpp
blob: 621f6a589469fb4f1eafcd100d8df9fac98e8346 (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
// $Id$

#include "tao/LF_CH_Event.h"
#include "tao/LF_Follower.h"
#include "tao/debug.h"
#include "tao/Connection_Handler.h"
#include "tao/Transport.h"

TAO_BEGIN_VERSIONED_NAMESPACE_DECL

TAO_LF_CH_Event::TAO_LF_CH_Event (void)
  : TAO_LF_Event (),
    prev_state_ (TAO_LF_Event::LFS_IDLE)

{
}

TAO_LF_CH_Event::~TAO_LF_CH_Event (void)
{
}

int
TAO_LF_CH_Event::bind (TAO_LF_Follower *follower)
{
  return this->followers_.bind (follower, 0);
}

int
TAO_LF_CH_Event::unbind (TAO_LF_Follower *follower)
{
  return this->followers_.unbind (follower);
}

void
TAO_LF_CH_Event::state_changed_i (int new_state)
{
  if (this->state_ != new_state)
    {
      this->validate_state_change (new_state);

      if (TAO_debug_level > 9)
        {
          size_t id = 0;
          TAO_Connection_Handler *ch = 0;
          if ((ch = dynamic_cast<TAO_Connection_Handler *> (this))
              && ch->transport ())
            {
              id = ch->transport ()->id ();
            }

          ACE_DEBUG ((LM_DEBUG, "TAO (%P|%t) - TAO_LF_CH_Event[%d]::"
                      "state_changed_i, state %C->%C\n",
                      id,
                      TAO_LF_Event::state_name(prev_state_),
                      TAO_LF_Event::state_name(state_)));
        }
    }

  ACE_MT (ACE_GUARD (TAO_SYNCH_MUTEX, guard, this->followers_.mutex ()));

  HASH_MAP::iterator end_it = this->followers_.end ();
  for (HASH_MAP::iterator it = this->followers_.begin (); it != end_it ; ++it)
    {
      it->ext_id_->signal ();
    }
}

void
TAO_LF_CH_Event::validate_state_change (int new_state)
{
  if (this->state_ == TAO_LF_Event::LFS_IDLE)
    {
      // From the LFS_IDLE state we can only become active.
      if (new_state == TAO_LF_Event::LFS_CONNECTION_WAIT)
        {
          this->prev_state_ = this->state_;
          this->state_ = new_state;
        }
      return;
    }
  else if (this->state_ == TAO_LF_Event::LFS_CONNECTION_WAIT)
    {
      // Only a few states are possible from CONNECTION_WAIT states
      if (new_state == TAO_LF_Event::LFS_CONNECTION_CLOSED
          || new_state == TAO_LF_Event::LFS_SUCCESS)
        {
          this->prev_state_ = this->state_;
          this->state_ = new_state;
        }

      return;
    }
  else if (this->state_ == TAO_LF_Event::LFS_SUCCESS)
    {
      if (new_state == TAO_LF_Event::LFS_CONNECTION_CLOSED)
        {
          this->prev_state_ = this->state_;
          this->state_ = new_state;
        }
      return;
    }
  else if (this->state_ == TAO_LF_Event::LFS_TIMEOUT)
    {
      if (new_state == TAO_LF_Event::LFS_CONNECTION_CLOSED)
        {
          // Dont reset the previous state
          this->state_ = new_state;
        }
    }
  return;
}


int
TAO_LF_CH_Event::successful (void) const
{
  if (this->prev_state_ == TAO_LF_Event::LFS_CONNECTION_WAIT)
    return this->state_ == TAO_LF_Event::LFS_SUCCESS;

  return this->state_ == TAO_LF_Event::LFS_CONNECTION_CLOSED;
}

int
TAO_LF_CH_Event::error_detected (void) const
{
  if (this->prev_state_ == TAO_LF_Event::LFS_CONNECTION_WAIT)
    return this->state_ == TAO_LF_Event::LFS_CONNECTION_CLOSED;

  return this->state_ == TAO_LF_Event::LFS_TIMEOUT;
}

void
TAO_LF_CH_Event::set_state (int new_state)
{
  // @@ NOTE: Is this still required?
  if (this->is_state_final () == 0
      && new_state == TAO_LF_Event::LFS_TIMEOUT)
    {
      this->state_ = new_state;
      if (TAO_debug_level > 9)
        {
          size_t id = 0;
          TAO_Connection_Handler *ch = 0;
          if ((ch = dynamic_cast<TAO_Connection_Handler *> (this)) &&
              ch->transport ())
            {
              id = ch->transport ()->id ();
            }
          ACE_DEBUG ((LM_DEBUG, "TAO (%P|%t) - TAO_LF_CH_Event[%d]::set_state, "
                      "state_ is LFS_TIMEOUT\n", id));
        }
    }
}

int
TAO_LF_CH_Event::is_state_final (void)
{
  return this->state_ == TAO_LF_Event::LFS_CONNECTION_CLOSED;
}

TAO_END_VERSIONED_NAMESPACE_DECL