summaryrefslogtreecommitdiff
path: root/TAO/orbsvcs/orbsvcs/Notify/Event_Map_T.cpp
blob: 9adf05f6298aaf3b9dc4dbffce9bd4483365cb4f (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
171
172
173
174
175
176
// $Id$

#ifndef TAO_NS_EVENT_MAP_T_C
#define TAO_NS_EVENT_MAP_T_C

#include "Event_Map_T.h"
#include "Properties.h"
#include "Factory.h"
#include "orbsvcs/ESF/ESF_Proxy_Collection.h"

#if ! defined (__ACE_INLINE__)
#include "Event_Map_T.inl"
#endif /* __ACE_INLINE__ */

ACE_RCSID(RT_Notify, TAO_NS_Event_Map_T, "$Id$")

template <class PROXY>
TAO_NS_Event_Map_Entry_T<PROXY>::TAO_NS_Event_Map_Entry_T (void)
  : collection_ (0), count_ (0)
{
}

template <class PROXY>
TAO_NS_Event_Map_Entry_T<PROXY>::~TAO_NS_Event_Map_Entry_T ()
{
  delete collection_;
}

template <class PROXY> void
TAO_NS_Event_Map_Entry_T<PROXY>::init (ACE_ENV_SINGLE_ARG_DECL)
{
  TAO_NS_Factory* factory = TAO_NS_PROPERTIES::instance ()->factory ();

  factory->create (collection_ ACE_ENV_ARG_PARAMETER);
}

template <class PROXY> void
TAO_NS_Event_Map_Entry_T<PROXY>::connected (PROXY* proxy ACE_ENV_ARG_DECL)
{
  this->collection_->connected (proxy ACE_ENV_ARG_PARAMETER);
  ++count_;
}

template <class PROXY> void
TAO_NS_Event_Map_Entry_T<PROXY>::disconnected (PROXY* proxy ACE_ENV_ARG_DECL)
{
  this->collection_->disconnected (proxy ACE_ENV_ARG_PARAMETER);
  --count_;
}

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

template <class PROXY, class ACE_LOCK>
TAO_NS_Event_Map_T<PROXY, ACE_LOCK>::TAO_NS_Event_Map_T (void)
  :count_ (0)
{

}

template <class PROXY, class ACE_LOCK>
TAO_NS_Event_Map_T<PROXY, ACE_LOCK>::~TAO_NS_Event_Map_T ()
{
}

template <class PROXY, class ACE_LOCK> TAO_NS_Event_Map_T<PROXY, ACE_LOCK>::COLLECTION* 
TAO_NS_Event_Map_T<PROXY, ACE_LOCK>::create_entry (const TAO_NS_EventType& event_type ACE_ENV_ARG_DECL)
{
  TAO_NS_Event_Map_Entry_T<PROXY>* entry;

  int result = -1;

  {
    ACE_READ_GUARD_RETURN (ACE_LOCK, ace_mon, this->lock_, 0);
    
    result = this->map_.find (event_type, entry);
  }

  if (result == -1)
    {
      ACE_NEW_THROW_EX (entry,
			ENTRY (),
			CORBA::NO_MEMORY ());
      ACE_CHECK_RETURN (0);

      entry->init (ACE_ENV_SINGLE_ARG_PARAMETER);
      ACE_CHECK_RETURN (0);

      ACE_WRITE_GUARD_RETURN (ACE_LOCK, ace_mon, this->lock_, 0);

      if (map_.bind (event_type, entry) == -1)
	ACE_THROW_RETURN (CORBA::NO_MEMORY (), 0);
  }

  return entry->collection ();
}

template <class PROXY, class ACE_LOCK> int
TAO_NS_Event_Map_T<PROXY, ACE_LOCK>::insert (PROXY* proxy, const TAO_NS_EventType& event_type ACE_ENV_ARG_DECL)
{
  TAO_NS_Event_Map_Entry_T<PROXY>* entry;

  int result = -1;

  {
    ACE_READ_GUARD_RETURN (ACE_LOCK, ace_mon, this->lock_, -1);

    result = this->map_.find (event_type, entry);
  }

  if (result == -1)
  {
    ACE_NEW_THROW_EX (entry,
		      ENTRY (),
		      CORBA::NO_MEMORY ());
    ACE_CHECK_RETURN (-1);

    entry->init (ACE_ENV_SINGLE_ARG_PARAMETER);
    ACE_CHECK_RETURN (-1);

	entry->connected (proxy ACE_ENV_ARG_PARAMETER);
    ACE_CHECK_RETURN (-1);

    ACE_WRITE_GUARD_RETURN (ACE_LOCK, ace_mon, this->lock_, -1);

    if (map_.bind (event_type, entry) == -1)
      ACE_THROW_RETURN (CORBA::NO_MEMORY (), -1);

    return ++count_;
  }
  else
    {
      entry->connected (proxy ACE_ENV_ARG_PARAMETER);
      ACE_CHECK_RETURN (-1);

      ACE_WRITE_GUARD_RETURN (ACE_LOCK, ace_mon, this->lock_, -1);
      return ++count_;
    }
}

template <class PROXY, class ACE_LOCK> int
TAO_NS_Event_Map_T<PROXY, ACE_LOCK>::remove (PROXY* proxy, const TAO_NS_EventType& event_type ACE_ENV_ARG_DECL)
{
  TAO_NS_Event_Map_Entry_T<PROXY>* entry;

  int result = -1;
  {
    ACE_READ_GUARD_RETURN (ACE_LOCK, ace_mon, this->lock_, -1);

    result = this->map_.find (event_type, entry);
  }

  if (result == 0)
    {
      entry->disconnected (proxy ACE_ENV_ARG_PARAMETER);
      ACE_CHECK_RETURN (-1);

      ACE_WRITE_GUARD_RETURN (ACE_LOCK, ace_mon, this->lock_, -1);            
      return --count_;

      if (entry->count () == 0)
	{
	  /// @@TODO: Exec a strategy for removing entries.
	}
    }

  return -1;
}

template <class PROXY, class ACE_LOCK> int
TAO_NS_Event_Map_T<PROXY, ACE_LOCK>::count (void)
{
  return this->count_;
}

#endif /* TAO_NS_EVENT_MAP_T_C */