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
|
// **********************************************************************
//
// Copyright (c) 2003-2005 ZeroC, Inc. All rights reserved.
//
// This copy of Ice is licensed to you under the terms described in the
// ICE_LICENSE file included in this distribution.
//
// **********************************************************************
#include <PhoneBookI.h>
#include <IceUtil/UUID.h>
using namespace std;
using namespace Ice;
using namespace Demo;
ContactI::ContactI(const ContactFactoryPtr& contactFactory) :
_factory(contactFactory)
{
}
string
ContactI::getName(const Ice::Current&) const
{
IceUtil::RWRecMutex::RLock sync(*this);
return name;
}
void
ContactI::setName(const string& newName, const Ice::Current&)
{
IceUtil::RWRecMutex::WLock sync(*this);
name = newName;
}
string
ContactI::getAddress(const Ice::Current&) const
{
IceUtil::RWRecMutex::RLock sync(*this);
return address;
}
void
ContactI::setAddress(const string& newAddress, const Ice::Current&)
{
IceUtil::RWRecMutex::WLock sync(*this);
address = newAddress;
}
string
ContactI::getPhone(const Ice::Current&) const
{
IceUtil::RWRecMutex::RLock sync(*this);
return phone;
}
void
ContactI::setPhone(const string& newPhone, const Ice::Current&)
{
IceUtil::RWRecMutex::WLock sync(*this);
phone = newPhone;
}
void
ContactI::destroy(const Ice::Current& c)
{
IceUtil::RWRecMutex::RLock sync(*this);
try
{
_factory->getEvictor()->remove(c.id);
}
catch(const Freeze::DatabaseException& ex)
{
DatabaseException e;
e.message = ex.message;
throw e;
}
}
PhoneBookI::PhoneBookI(const Freeze::EvictorPtr& evictor, const ContactFactoryPtr& contactFactory,
const NameIndexPtr& index) :
_evictor(evictor),
_contactFactory(contactFactory),
_index(index)
{
}
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& c)
{
//
// Get a new unique identity.
//
Identity ident;
ident.name = IceUtil::generateUUID();
ident.category = "contact";
//
// Create a new Contact Servant.
//
ContactIPtr contact = new ContactI(_contactFactory);
//
// Create a new Ice Object in the evictor, using the new identity
// and the new Servant.
//
_evictor->add(contact, ident);
//
// Turn the identity into a Proxy and return the Proxy to the
// caller.
//
return IdentityToContact(c.adapter)(ident);
}
Contacts
PhoneBookI::findContacts(const string& name, const Ice::Current& c) const
{
try
{
vector<Identity> identities = _index->find(name);
Contacts contacts;
contacts.reserve(identities.size());
transform(identities.begin(), identities.end(), back_inserter(contacts), IdentityToContact(c.adapter));
return contacts;
}
catch(const Freeze::DatabaseException& ex)
{
DatabaseException e;
e.message = ex.message;
throw e;
}
}
void
PhoneBookI::setEvictorSize(Int size, const Ice::Current&)
{
_evictor->setSize(size);
}
void
PhoneBookI::shutdown(const Ice::Current& c) const
{
c.adapter->getCommunicator()->shutdown();
}
|