summaryrefslogtreecommitdiff
path: root/cpp/demo/Freeze/phonebook/PhoneBookI.cpp
blob: b826e904b1fb1189ed217f6f258f6f47d772a780 (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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
// **********************************************************************
//
// Copyright (c) 2001
// Mutable Realms, Inc.
// Huntsville, AL, USA
//
// All Rights Reserved
//
// **********************************************************************

#include <PhoneBookI.h>

using namespace std;
using namespace Ice;
using namespace Freeze;

ContactI::ContactI(const PhoneBookIPtr& phoneBook, const EvictorPtr& evictor) :
    _phoneBook(phoneBook),
    _evictor(evictor)
{
}

void
ContactI::setIdentity(const Identity& ident)
{
    _identity = ident;
}

string
ContactI::getName(const Ice::Current&) const
{
    IceUtil::RWRecMutex::RLock sync(*this);
    return name;
}

void
ContactI::setName(const string& name, const Ice::Current&)
{
    IceUtil::RWRecMutex::WLock sync(*this);

    assert(!_identity.name.empty());
    _phoneBook->move(_identity, this->name, name);
    this->name = name;
}

string
ContactI::getAddress(const Ice::Current&) const
{
    IceUtil::RWRecMutex::RLock sync(*this);
    return address;
}

void
ContactI::setAddress(const string& address, const Ice::Current&)
{
    IceUtil::RWRecMutex::WLock sync(*this);
    this->address = address;
}

string
ContactI::getPhone(const Ice::Current&) const
{
    IceUtil::RWRecMutex::RLock sync(*this);
    return phone;
}

void
ContactI::setPhone(const string& phone, const Ice::Current&)
{
    IceUtil::RWRecMutex::WLock sync(*this);
    this->phone = phone;
}

void
ContactI::destroy(const Ice::Current&)
{
    IceUtil::RWRecMutex::RLock sync(*this);

    try
    {
	assert(!_identity.name.empty());
	_phoneBook->remove(_identity, name);

	//
	// This can throw EvictorDeactivatedException (which indicates
	// an internal error). The exception is currently ignored.
	//
	_evictor->destroyObject(_identity);
    }
    catch(const Freeze::DBNotFoundException&)
    {
	//
	// Raised by remove. Ignore.
	//
    }
    catch(const Freeze::DBException& ex)
    {
	DatabaseException e;
	e.message = ex.message;
	throw e;
    }
}

PhoneBookI::PhoneBookI(const ObjectAdapterPtr& adapter, const DBPtr& db, const EvictorPtr& evictor) :
    _adapter(adapter),
    _db(db),
    _evictor(evictor),
    _nameIdentitiesDict(db)
{
}

class IdentityToContact
{
public:

    IdentityToContact(const ObjectAdapterPtr& adapter) :
	_adapter(adapter)
    {
    }

    ContactPrx operator()(const Identity& ident)
    {
	return ContactPrx::uncheckedCast(_adapter->createProxy(ident));
    }

private:

    ObjectAdapterPtr _adapter;
};

ContactPrx
PhoneBookI::createContact(const Ice::Current&)
{
    IceUtil::RWRecMutex::WLock sync(*this);
    
    //
    // Get a new unique identity.
    //
    Identity ident = getNewIdentity();

    //
    // Create a new Contact Servant.
    //
    ContactIPtr contact = new ContactI(this, _evictor);
    contact->setIdentity(ident);
    
    //
    // Create a new Ice Object in the evictor, using the new identity
    // and the new Servant.
    //
    // This can throw EvictorDeactivatedException (which indicates
    // an internal error). The exception is currently ignored.
    //
    _evictor->createObject(ident, contact);

    //
    // Add the identity to our name/identities map. The initial name
    // is the empty string. See the comment in getNewIdentity why the
    // prefix "N" is needed.
    //
    try
    {
	NameIdentitiesDict::iterator p = _nameIdentitiesDict.find("N");
	Identities identities;
	if(p != _nameIdentitiesDict.end())
	{
	    identities = p->second;
	}
	
	identities.push_back(ident);
	_nameIdentitiesDict.insert(make_pair(string("N"), identities));
	
	//
	// Turn the identity into a Proxy and return the Proxy to the
	// caller.
	//
	return IdentityToContact(_adapter)(ident);
    }
    catch(const Freeze::DBException& ex)
    {
	DatabaseException e;
	e.message = ex.message;
	throw e;
    }
}

Contacts
PhoneBookI::findContacts(const string& name, const Ice::Current&) const
{
    IceUtil::RWRecMutex::RLock sync(*this);
    
    try
    {
	//
	// Lookup all phone book contacts that match a name, and
	// return them to the caller. See the comment in
	// getNewIdentity why the prefix "N" is needed.
	//
	NameIdentitiesDict::const_iterator p = _nameIdentitiesDict.find("N" + name);
	Identities identities;
	if(p !=  _nameIdentitiesDict.end())
	{
	    identities = p->second;
	}

	Contacts contacts;
	contacts.reserve(identities.size());
	transform(identities.begin(), identities.end(), back_inserter(contacts), IdentityToContact(_adapter));

	return contacts;
    }
    catch(const Freeze::DBException& ex)
    {
	DatabaseException e;
	e.message = ex.message;
	throw e;
    }
}

void
PhoneBookI::setEvictorSize(Int size, const Ice::Current&)
{
    //
    // No synchronization necessary, _evictor is immutable.
    //
    _evictor->setSize(size);
}

void
PhoneBookI::shutdown(const Ice::Current&) const
{
    //
    // No synchronization necessary, _adapter is immutable.
    //
    _adapter->getCommunicator()->shutdown();
}

void
PhoneBookI::remove(const Identity& ident, const string& name)
{
    IceUtil::RWRecMutex::WLock sync(*this);

    try
    {
	removeI(ident, name);
    }
    catch(const Freeze::DBException& ex)
    {
	DatabaseException e;
	e.message = ex.message;
	throw e;
    }
}

void
PhoneBookI::move(const Identity& ident, const string& oldName, const string& newName)
{
    IceUtil::RWRecMutex::WLock sync(*this);

    try
    {
	//
	// Called by ContactI in case the name has been changed. See
	// the comment in getNewIdentity why the prefix "N" is needed.
	//
	removeI(ident, oldName);
	NameIdentitiesDict::iterator p = _nameIdentitiesDict.find("N" + newName);
	Identities identities;
	if(p != _nameIdentitiesDict.end())
	{
	    identities = p->second;
	}
	identities.push_back(ident);
	_nameIdentitiesDict.insert(make_pair("N" + newName, identities));
    }
    catch(const Freeze::DBNotFoundException&)
    {
	//
	// Raised by remove. This should only happen under very rare
	// circumstances if destroy() had gotten to the object prior
	// to the setName() operation being dispatched. Ignore the
	// exception.
	//
    }
    catch(const Freeze::DBException& ex)
    {
	DatabaseException e;
	e.message = ex.message;
	throw e;
    }
}

Identity
PhoneBookI::getNewIdentity()
{
    try
    {
	//
	// This code is a bit of a hack. It stores the last identity
	// that has been used (or the name component thereof, to be
	// more precise) in the _nameIdentitiesDict, with the special
	// prefix "ID". Because of this, all "real" names need to be
	// prefixed with "N", so that there is no potential for a name
	// clash.
	//

	Ice::Long n;
	Identities ids;
	NameIdentitiesDict::iterator p = _nameIdentitiesDict.find("ID");
	if(p == _nameIdentitiesDict.end())
	{
	    n = 0;
	}
	else
	{
	    ids = p->second;
	    assert(ids.size() == 1);
#ifdef _WIN32
	    n = _atoi64(ids.front().name.c_str()) + 1;
#else
	    n = atoll(ids.front().name.c_str()) + 1;
#endif
	}

	char s[20];
#ifdef _WIN32
	sprintf(s, "%I64d", n);
#else
	sprintf(s, "%lld", n);
#endif
    
	Identity id;

	id.name = s;
	ids.clear();
	ids.push_back(id);

	_nameIdentitiesDict.insert(make_pair(string("ID"), ids));

	id.name = s;
	id.category = "contact";
	return id;
    }
    catch(const Freeze::DBException& ex)
    {
	DatabaseException e;
	e.message = ex.message;
	throw e;
    }
}

//
// Called with the RWRecMutex already acquired.
//
void
PhoneBookI::removeI(const Identity& ident, const string& name)
{
    //
    // See the comment in getNewIdentity why the prefix "N" is
    // needed.
    //
    NameIdentitiesDict::iterator p = _nameIdentitiesDict.find("N" + name);

    //
    // If the name isn't found then raise a record not found
    // exception.
    //
    if(p == _nameIdentitiesDict.end())
    {
	throw Freeze::DBNotFoundException(__FILE__, __LINE__);
    }

    Identities identities  = p->second;
    identities.erase(remove_if(identities.begin(), identities.end(), bind2nd(equal_to<Ice::Identity>(), ident)),
		     identities.end());

    if(identities.empty())
    {
	_nameIdentitiesDict.erase(p);
    }
    else
    {
	//
	// See the comment in getNewIdentity why the prefix "N" is
	// needed.
	//
	_nameIdentitiesDict.insert(make_pair("N" + name, identities));
    }
}