summaryrefslogtreecommitdiff
path: root/cpp/test/Freeze/evictor/TestI.cpp
blob: 2c68325bab2b41867a1cb8ae7b61959d9e7bc217 (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
// **********************************************************************
//
// Copyright (c) 2002
// ZeroC, Inc.
// Huntsville, AL, USA
//
// All Rights Reserved
//
// **********************************************************************

#include <IceUtil/IceUtil.h>
#include <Freeze/Freeze.h>
#include <TestI.h>

using namespace std;
using namespace Ice;

Test::ServantI::ServantI()
{
}

Test::ServantI::ServantI(const RemoteEvictorIPtr& remoteEvictor, const Freeze::EvictorPtr& evictor, Ice::Int val) :
    _remoteEvictor(remoteEvictor),
    _evictor(evictor)
{
    this->value = val;
}

void
Test::ServantI::init(const RemoteEvictorIPtr& remoteEvictor, const Freeze::EvictorPtr& evictor)
{
    _remoteEvictor = remoteEvictor;
    _evictor = evictor;
}

Int
Test::ServantI::getValue(const Current&) const
{
    return value;
}

void
Test::ServantI::setValue(Int value, const Current&)
{
    this->value = value;
}

void
Test::ServantI::destroy(const Current& current)
{
    _evictor->destroyObject(current.id);
}

void
Test::ServantI::__write(IceInternal::BasicStream* os) const
{
    _remoteEvictor->setLastSavedValue(value);
    Servant::__write(os);
}

void
Test::ServantI::__marshal(const StreamPtr& os) const
{
    _remoteEvictor->setLastSavedValue(value);
    Servant::__marshal(os);
}

Test::RemoteEvictorI::RemoteEvictorI(const ObjectAdapterPtr& adapter, const string& category, const Freeze::DBPtr& db,
                                     const Freeze::EvictorPtr& evictor) :
    _adapter(adapter),
    _category(category),
    _db(db),
    _evictor(evictor),
    _lastSavedValue(-1)
{
}

void
Test::RemoteEvictorI::setSize(Int size, const Current&)
{
    _evictor->setSize(size);
}

Test::ServantPrx
Test::RemoteEvictorI::createServant(Int value, const Current&)
{
    Identity id;
    id.category = _category;
    ostringstream ostr;
    ostr << value;
    id.name = ostr.str();
    ServantPtr servant = new ServantI(this, _evictor, value);
    _evictor->createObject(id, servant);
    return ServantPrx::uncheckedCast(_adapter->createProxy(id));
}

Int
Test::RemoteEvictorI::getLastSavedValue(const Current&) const
{
    Int result = _lastSavedValue;
    (const_cast<RemoteEvictorI*>(this))->_lastSavedValue = -1;
    return result;
}

void
Test::RemoteEvictorI::clearLastSavedValue(const Current&)
{
    _lastSavedValue = -1;
}

void
Test::RemoteEvictorI::deactivate(const Current& current)
{
    _adapter->removeServantLocator(_category);
    _adapter->remove(stringToIdentity(_category));
    _db->close();
}

void
Test::RemoteEvictorI::setLastSavedValue(Int value)
{
    _lastSavedValue = value;
}

class Initializer : public Freeze::ServantInitializer
{
public:

    Initializer(const Test::RemoteEvictorIPtr& remoteEvictor, const Freeze::EvictorPtr& evictor) :
        _remoteEvictor(remoteEvictor),
        _evictor(evictor)
    {
    }

    virtual void
    initialize(const ObjectAdapterPtr& adapter, const Identity& ident, const ObjectPtr& servant)
    {
        Test::ServantI* servantI = dynamic_cast<Test::ServantI*>(servant.get());
        servantI->init(_remoteEvictor, _evictor);
    }

private:

    Test::RemoteEvictorIPtr _remoteEvictor;
    Freeze::EvictorPtr _evictor;
};

Test::RemoteEvictorFactoryI::RemoteEvictorFactoryI(const ObjectAdapterPtr& adapter,
                                                   const Freeze::DBEnvironmentPtr& dbEnv) :
    _adapter(adapter),
    _dbEnv(dbEnv)
{
}

::Test::RemoteEvictorPrx
Test::RemoteEvictorFactoryI::createEvictor(const string& name,
                                           Test::EvictorPersistenceMode mode,
                                           const Current& current)
{
    Freeze::DBPtr db = _dbEnv->openDB(name, true);

    Freeze::EvictorPersistenceMode fMode;
    if(mode == Test::SaveUponEviction)
    {
        fMode = Freeze::SaveUponEviction;
    }
    else
    {
        fMode = Freeze::SaveAfterMutatingOperation;
    }
    Freeze::EvictorPtr evictor = db->createEvictor(fMode);
    _adapter->addServantLocator(evictor, name);

    RemoteEvictorIPtr remoteEvictor = new RemoteEvictorI(_adapter, name, db, evictor);
    Freeze::ServantInitializerPtr initializer = new Initializer(remoteEvictor, evictor);
    evictor->installServantInitializer(initializer);
    return RemoteEvictorPrx::uncheckedCast(_adapter->add(remoteEvictor, stringToIdentity(name)));
}

void
Test::RemoteEvictorFactoryI::shutdown(const Current&)
{
    _dbEnv->getCommunicator()->shutdown();
}