summaryrefslogtreecommitdiff
path: root/cpp/demo/Freeze/phonebook/PhoneBookI.cpp
blob: 61da749bece4da360d96ed6739f58826c0d98ab7 (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
// **********************************************************************
//
// Copyright (c) 2003-2011 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 <IceUtil/IceUtil.h>
#include <PhoneBookI.h>

using namespace std;
using namespace Demo;

ContactI::ContactI(const ContactFactoryPtr& contactFactory) :
    _factory(contactFactory)
{
}

string
ContactI::getName(const Ice::Current&) const
{
    IceUtil::Mutex::Lock lock(*this);
    return name;
}

void
ContactI::setName(const string& newName, const Ice::Current&)
{
    IceUtil::Mutex::Lock lock(*this);
    name = newName;
}

string
ContactI::getAddress(const Ice::Current&) const
{
    IceUtil::Mutex::Lock lock(*this);
    return address;
}

void
ContactI::setAddress(const string& newAddress, const Ice::Current&)
{
    IceUtil::Mutex::Lock lock(*this);
    address = newAddress;
}

string
ContactI::getPhone(const Ice::Current&) const
{
    IceUtil::Mutex::Lock lock(*this);
    return phone;
}

void
ContactI::setPhone(const string& newPhone, const Ice::Current&)
{
    IceUtil::Mutex::Lock lock(*this);
    phone = newPhone;
}

void
ContactI::destroy(const Ice::Current& c)
{
    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 Ice::ObjectAdapterPtr& adapter) :
        _adapter(adapter)
    {
    }

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

private:

    const Ice::ObjectAdapterPtr _adapter;
};

ContactPrx
PhoneBookI::createContact(const Ice::Current& c)
{
    //
    // Get a new unique identity.
    //
    Ice::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<Ice::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(Ice::Int size, const Ice::Current&)
{
    _evictor->setSize(size);
}

void
PhoneBookI::shutdown(const Ice::Current& c)
{
    c.adapter->getCommunicator()->shutdown();
}