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
|
/*
* RequestManager.h
*
* Copyright (C) 2008 Matthias Schiffer <matthias@gamezock.de>
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by the
* Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef MAD_COMMON_REQUESTMANAGER_H_
#define MAD_COMMON_REQUESTMANAGER_H_
#include "Request.h"
#include "RequestHandlerGroup.h"
#include <map>
#include <memory>
#include <string>
#include <boost/thread/locks.hpp>
#include <boost/thread/shared_mutex.hpp>
namespace Mad {
namespace Common {
class Application;
class RequestManager : private boost::noncopyable {
private:
friend class Application;
friend class RequestHandler;
class RequestMap : private boost::noncopyable {
private:
typedef std::map<boost::uint16_t, boost::shared_ptr<RequestHandler> > IdMap;
typedef std::map<const RequestHandler*, std::pair<Connection*, boost::uint16_t> > HandlerMap;
std::map<Connection*, IdMap> connectionMap;
HandlerMap handlerMap;
public:
void registerConnection(Connection *connection) {
connectionMap.insert(std::make_pair(connection, IdMap()));
}
void unregisterConnection(Connection *connection);
bool isConnectionRegistered(Connection *connection) const {
return connectionMap.count(connection);
}
bool addRequest(Connection *con, boost::uint16_t id, boost::shared_ptr<RequestHandler> info);
boost::shared_ptr<RequestHandler> findRequest(Connection *con, boost::uint16_t id);
void deleteRequest(Connection *con, boost::uint16_t id);
std::pair<Connection*, boost::uint16_t> getRequestInfo(const RequestHandler *requestHandler);
};
template<class T> class RequestHandlerFactory : public RequestHandlerGroup {
private:
std::set<std::string> types;
public:
RequestHandlerFactory(const std::string &type) {
types.insert(type);
}
virtual const std::set<std::string>& getPacketTypes() {
return types;
}
virtual boost::shared_ptr<RequestHandler> createRequestHandler(Application *application, const std::string &type _UNUSED_PARAMETER_) {
return boost::shared_ptr<RequestHandler>(new T(application));
}
};
Application *application;
boost::shared_mutex mutex;
RequestMap requestMap;
boost::uint16_t lastRequestId;
std::map<std::string, boost::shared_ptr<RequestHandlerGroup> > requestHandlerGroups;
boost::uint16_t _getRequestId() {
return lastRequestId+=2;
}
void receiveHandler(Connection *connection, boost::shared_ptr<const XmlPacket> packet, boost::uint16_t requestId);
void handleRequestFinished(Connection *con, boost::uint16_t requestId) {
boost::lock_guard<boost::shared_mutex> lock(mutex);
requestMap.deleteRequest(con, requestId);
}
boost::uint16_t _getUnusedRequestId(Connection *connection);
bool send(Request *request);
RequestManager(Application *application0, bool server);
public:
void registerConnection(Connection *connection);
void unregisterConnection(Connection *connection) {
boost::lock_guard<boost::shared_mutex> lock(mutex);
requestMap.unregisterConnection(connection);
}
void registerRequestHandlerGroup(boost::shared_ptr<RequestHandlerGroup> requestHandlerGroup) {
boost::lock_guard<boost::shared_mutex> lock(mutex);
const std::set<std::string> &types = requestHandlerGroup->getPacketTypes();
for(std::set<std::string>::const_iterator type = types.begin(); type != types.end(); ++type)
requestHandlerGroups.insert(std::make_pair(*type, requestHandlerGroup));
}
void unregisterRequestHandlerGroup(boost::shared_ptr<RequestHandlerGroup> requestHandlerGroup) {
boost::lock_guard<boost::shared_mutex> lock(mutex);
const std::set<std::string> &types = requestHandlerGroup->getPacketTypes();
for(std::set<std::string>::const_iterator type = types.begin(); type != types.end(); ++type) {
std::map<std::string, boost::shared_ptr<RequestHandlerGroup> >::iterator it = requestHandlerGroups.find(*type);
if(it == requestHandlerGroups.end() || it->second != requestHandlerGroup)
continue;
requestHandlerGroups.erase(it);
}
}
template <class T> void registerPacketType(const std::string &type) {
registerRequestHandlerGroup(boost::shared_ptr<RequestHandlerFactory<T> >(new RequestHandlerFactory<T>(type)));
}
void unregisterPacketType(const std::string &type) {
boost::lock_guard<boost::shared_mutex> lock(mutex);
requestHandlerGroups.erase(type);
}
bool sendRequest(Connection *connection, boost::shared_ptr<Request> request);
};
}
}
#endif /* MAD_COMMON_REQUESTMANAGER_H_ */
|