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
|
// **********************************************************************
//
// Copyright (c) 2003-2004 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 <Glacier2/SessionRouterI.h>
#include <Glacier2/RouterI.h>
using namespace std;
using namespace Ice;
using namespace Glacier2;
class SessionLocator : public ServantLocator
{
public:
SessionLocator(const SessionRouterIPtr& sessionRouter) :
_sessionRouter(sessionRouter)
{
}
virtual ObjectPtr
locate(const Current& current, LocalObjectPtr&)
{
assert(current.con);
return _sessionRouter->getRouter(current.con)->getClientBlobject();
}
virtual void
finished(const Current&, const ObjectPtr&, const LocalObjectPtr&)
{
}
virtual void
deactivate(const std::string&)
{
}
private:
const SessionRouterIPtr _sessionRouter;
};
Glacier2::SessionRouterI::SessionRouterI(const ObjectAdapterPtr& clientAdapter,
const ObjectAdapterPtr& serverAdapter) :
_logger(clientAdapter->getCommunicator()->getLogger()),
_clientAdapter(clientAdapter),
_serverAdapter(serverAdapter),
_traceLevel(clientAdapter->getCommunicator()->getProperties()->getPropertyAsInt("Glacier2.Trace.Session")),
_sessionThread(new SessionThread(this)),
_routersHint(_routers.end()),
_destroy(false)
{
_clientAdapter->addServantLocator(new SessionLocator(this), "");
}
Glacier2::SessionRouterI::~SessionRouterI()
{
assert(_destroy);
assert(_routers.empty());
}
void
Glacier2::SessionRouterI::destroy()
{
{
IceUtil::Monitor<IceUtil::Mutex>::Lock lock(*this);
assert(!_destroy);
_destroy = true;
for_each(_routers.begin(), _routers.end(),
Ice::secondVoidMemFun<const ConnectionPtr, RouterI>(&RouterI::destroy));
_routers.clear();
_routersHint = _routers.end();
notify();
}
_sessionThread->getThreadControl().join();
}
ObjectPrx
Glacier2::SessionRouterI::getClientProxy(const Current& current) const
{
return getRouter(current.con)->getClientProxy(current); // Forward to the per-client router.
}
ObjectPrx
Glacier2::SessionRouterI::getServerProxy(const Current& current) const
{
return getRouter(current.con)->getServerProxy(current); // Forward to the per-client router.
}
void
Glacier2::SessionRouterI::addProxy(const ObjectPrx& proxy, const Current& current)
{
getRouter(current.con)->addProxy(proxy, current); // Forward to the per-client router.
}
void
Glacier2::SessionRouterI::createSession(const std::string& userId, const std::string& password, const Current& current)
{
IceUtil::Monitor<IceUtil::Mutex>::Lock lock(*this);
assert(!_destroy);
//
// Add a new per-client router.
//
RouterIPtr router = new RouterI(_clientAdapter, _serverAdapter, current.con);
_routersHint = _routers.insert(_routersHint, pair<const ConnectionPtr, RouterIPtr>(current.con, router));
if(_traceLevel >= 1)
{
Trace out(_logger, "Glacier2");
out << "added session for:\n";
out << current.con->toString();
}
}
RouterIPtr
Glacier2::SessionRouterI::getRouter(const ConnectionPtr& connection) const
{
IceUtil::Monitor<IceUtil::Mutex>::Lock lock(*this);
assert(!_destroy);
if(_routersHint != _routers.end() && _routersHint->first == connection)
{
return _routersHint->second;
}
map<ConnectionPtr, RouterIPtr>::iterator p =
const_cast<map<ConnectionPtr, RouterIPtr>&>(_routers).find(connection);
if(p != _routers.end())
{
_routersHint = p;
return p->second;
}
else
{
return 0;
}
}
void
Glacier2::SessionRouterI::run()
{
IceUtil::Monitor<IceUtil::Mutex>::Lock lock(*this);
while(!_destroy)
{
wait();
}
}
Glacier2::SessionRouterI::SessionThread::SessionThread(const SessionRouterIPtr& sessionRouter) :
_sessionRouter(sessionRouter)
{
}
void
Glacier2::SessionRouterI::SessionThread::run()
{
_sessionRouter->run();
_sessionRouter = 0; // Break cyclic dependencies.
}
|