summaryrefslogtreecommitdiff
path: root/ace/Service_Repository.cpp
blob: 9aece865ba2d5b048d8a9e4aeb4cb5c7ecec1770 (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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
// Service_Repository.cpp
// $Id$

#define ACE_BUILD_DLL
#include "ace/Service_Repository.h"

#if !defined (__ACE_INLINE__)
#include "ace/Service_Repository.i"
#endif /* __ACE_INLINE__ */

ACE_ALLOC_HOOK_DEFINE(ACE_Service_Repository)

void
ACE_Service_Repository::dump (void) const
{
  ACE_TRACE ("ACE_Service_Repository::dump");
}

ACE_Service_Repository::ACE_Service_Repository (void)
  : service_vector_ (0),
    current_size_ (0),
    total_size_ (0)
{
  ACE_TRACE ("ACE_Service_Repository::ACE_Service_Repository");
}

// Initialize the Repository to a clean slate.

int 
ACE_Service_Repository::open (int size)
{
  ACE_TRACE ("ACE_Service_Repository::open");

  this->total_size_ = size;
  this->service_vector_ = 
    (const ACE_Service_Record **) new ACE_Service_Record *[size];
  if (this->service_vector_ == 0)
    {
      errno = ENOMEM;
      return -1;
    }

  return 0;
}

ACE_Service_Repository::ACE_Service_Repository (int size)
  : current_size_ (0)
{
  ACE_TRACE ("ACE_Service_Repository::ACE_Service_Repository");

  if (this->open (size) == -1)
    ACE_ERROR ((LM_ERROR, "%p\n", "ACE_Service_Repository"));
}

/* Close down all the services */

int
ACE_Service_Repository::close (void)
{
  ACE_TRACE ("ACE_Service_Repository::close");
  ACE_MT (ACE_GUARD_RETURN (ACE_Thread_Mutex, ace_mon, this->lock_, -1));

  if (this->service_vector_ != 0)
    {
      for (int i = 0; i < this->current_size_; i++)
	{
	  ACE_DEBUG ((LM_DEBUG, "shutting down %s\n", 
		     this->service_vector_[i]->name ()));
	  delete (ACE_Service_Record *) this->service_vector_[i];
	}
      
      delete [] this->service_vector_;
      this->service_vector_ = 0;
      this->current_size_  = 0;
    }
  return 0;
}

ACE_Service_Repository::~ACE_Service_Repository (void)
{
  ACE_TRACE ("ACE_Service_Repository::~ACE_Service_Repository");
  this->close ();
}

// Locate an entry with NAME in the table.  If IGNORE_SUSPENDED is set
// then only consider services marked as resumed.  If the caller wants
// the located entry, pass back a pointer to the located entry via
// SRP.  If NAME is not found -1 is returned.  If NAME is found, but
// it is suspended and the caller wants to ignore suspended services a
// -2 is returned.  Must be called with locks held.

int 
ACE_Service_Repository::find_i (const char name[],
				const ACE_Service_Record **srp,
				int ignore_suspended)
{
  ACE_TRACE ("ACE_Service_Repository::find_i");
  int i;

  for (i = 0; i < this->current_size_; i++)
    if (ACE_OS::strcmp (name, this->service_vector_[i]->name ()) == 0)
      break;
  
  if (i < this->current_size_)
    {
      if (srp != 0)
	*srp = this->service_vector_[i];
      if (ignore_suspended && this->service_vector_[i]->active () == 0)
	return -2;
      return i;
    }
  else
    return -1;
}

int 
ACE_Service_Repository::find (const char name[],
			      const ACE_Service_Record **srp,
			      int ignore_suspended)
{
  ACE_TRACE ("ACE_Service_Repository::find");
  ACE_MT (ACE_GUARD_RETURN (ACE_Thread_Mutex, ace_mon, this->lock_, -1));

  return this->find_i (name, srp, ignore_suspended);
}


// Insert the ACE_Service_Record SR into the repository.  Note that
// services may be inserted either resumed or suspended.

int 
ACE_Service_Repository::insert (const ACE_Service_Record *sr)
{
  ACE_TRACE ("ACE_Service_Repository::insert");
  ACE_MT (ACE_GUARD_RETURN (ACE_Thread_Mutex, ace_mon, this->lock_, -1));
  int i;

  for (i = 0; i < this->current_size_; i++)
    if (ACE_OS::strcmp (sr->name (), 
		  this->service_vector_[i]->name ()) == 0)
      break;

  if (i < this->current_size_) // Replacing an existing entry
    {
      // Check for self-assignment...
      if (sr == this->service_vector_[i]) 
	return 0;
      delete (ACE_Service_Record *) this->service_vector_[i];
      this->service_vector_[i] = sr;
      return 0;
    }
  else if (i < this->total_size_) // Adding a new entry.
    {
      this->service_vector_[i] = sr;
      this->current_size_++;
      return 0;
    }
  else
    return -1;
}
 
// Re-resume a service that was previously suspended. 

int
ACE_Service_Repository::resume (const char name[], 
				const ACE_Service_Record **srp)
{
  ACE_TRACE ("ACE_Service_Repository::resume");
  ACE_MT (ACE_GUARD_RETURN (ACE_Thread_Mutex, ace_mon, this->lock_, -1));

  int i = this->find_i (name, srp, 0);

  if (i == -1)
    return -1;

  this->service_vector_[i]->resume ();
  return 0;
}

// Suspend a service so that it will not be considered active under
// most circumstances by other portions of the ACE_Service_Repository.

int
ACE_Service_Repository::suspend (const char name[], 
				 const ACE_Service_Record **srp)
{
  ACE_TRACE ("ACE_Service_Repository::suspend");
  ACE_MT (ACE_GUARD_RETURN (ACE_Thread_Mutex, ace_mon, this->lock_, -1));
  int i = this->find_i (name, srp, 0);

  if (i == -1)
    return -1;

  this->service_vector_[i]->suspend ();
  return 0;
}

// Completely remove a <name> entry from the Repository and
// dynamically unlink it if it was originally dynamically linked.
// Since the order of services in the Respository does not matter, we
// simply overwrite the entry being deleted with the final entry in
// the array and decrement the <service_count> by 1.

int 
ACE_Service_Repository::remove (const char name[])
{
  ACE_TRACE ("ACE_Service_Repository::remove");
  ACE_MT (ACE_GUARD_RETURN (ACE_Thread_Mutex, ace_mon, this->lock_, -1));
  int i = this->find_i (name, 0, 0);

  if (i == -1)
    return -1;
  else
    {
      const void *handle = this->service_vector_[i]->handle ();
      delete (ACE_Service_Record *) this->service_vector_[i];

      if (handle != 0)
	ACE_OS::dlclose ((void *) handle);

      if (--this->current_size_ >= 1)
	this->service_vector_[i] 
	  = this->service_vector_[this->current_size_];
      return 0;
    }
}

ACE_ALLOC_HOOK_DEFINE(ACE_Service_Repository_Iterator)

void
ACE_Service_Repository_Iterator::dump (void) const
{
  ACE_TRACE ("ACE_Service_Repository_Iterator::dump");
}

// Initializes the iterator and skips over any suspended entries at
// the beginning of the table, if necessary.  Note, you must not
// perform destructive operations on elements during this iteration...

ACE_Service_Repository_Iterator::ACE_Service_Repository_Iterator 
  (ACE_Service_Repository &sr, int ignr_suspended)
  : svc_rep_ (sr),
    next_ (-1),
    ignore_suspended_ (ignr_suspended)
{
  this->advance ();
}

// Obtains a pointer to the next valid service in the table.  If there
// are no more entries, returns 0, else 1.

int 
ACE_Service_Repository_Iterator::next (const ACE_Service_Record *&sr)
{
  ACE_TRACE ("ACE_Service_Repository_Iterator::next");
  if (this->next_ < this->svc_rep_.current_size_)
    {
      sr = this->svc_rep_.service_vector_[this->next_];
      return 1;
    }
  else
    return 0;
}

// Advance the iterator by the proper amount.  If we are ignoring
// suspended entries and the current entry is suspended, then we must
// skip over this entry.  Otherwise, we must advance the NEXT index to
// reference the next valid service entry.

int
ACE_Service_Repository_Iterator::advance (void)
{
  ACE_TRACE ("ACE_Service_Repository_Iterator::advance");
  for (++this->next_;
       this->next_ < this->svc_rep_.current_size_
       && this->ignore_suspended_
       && this->svc_rep_.service_vector_[this->next_]->active () == 0;
       this->next_++)
    continue;
  return this->next_;
}