summaryrefslogtreecommitdiffstats
path: root/src/Common/RequestManager.cpp
blob: c30ddc23602a089fc3f2d80e0bdd87f30b4a2596 (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
/*
 * RequestManager.cpp
 *
 * 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/>.
 */

#include "RequestManager.h"
#include "RequestHandlers/DisconnectRequestHandler.h"

#include <boost/bind.hpp>

namespace Mad {
namespace Common {

void RequestManager::RequestMap::unregisterConnection(Connection *connection) {
  std::map<Connection*, IdMap>::iterator idMap = connectionMap.find(connection);
  if(idMap == connectionMap.end())
    return;

  for(IdMap::iterator request = idMap->second.begin(); request != idMap->second.end(); ++request)
    handlerMap.erase(request->second.get());

  connectionMap.erase(idMap);
}

bool RequestManager::RequestMap::addRequest(Connection *con, boost::uint16_t id, boost::shared_ptr<RequestHandler> request) {
  std::map<Connection*, IdMap>::iterator idMap = connectionMap.find(con);
  if(idMap == connectionMap.end())
    return false;

  if(!idMap->second.insert(std::make_pair(id, request)).second)
    return false;

  if(!handlerMap.insert(std::make_pair(request.get(), std::make_pair(con, id))).second) {
    idMap->second.erase(id);
    return false;
  }

  return true;
}

boost::shared_ptr<RequestHandler> RequestManager::RequestMap::findRequest(Connection *con, boost::uint16_t id) {
  std::map<Connection*, IdMap>::iterator idMap = connectionMap.find(con);
  if(idMap == connectionMap.end())
    return boost::shared_ptr<RequestHandler>();

  IdMap::iterator it = idMap->second.find(id);
  if(it == idMap->second.end())
    return boost::shared_ptr<RequestHandler>();

  return it->second;
}

void RequestManager::RequestMap::deleteRequest(Connection *con, boost::uint16_t id) {
  std::map<Connection*, IdMap>::iterator idMap = connectionMap.find(con);
  if(idMap == connectionMap.end())
    return;

  IdMap::iterator it = idMap->second.find(id);
  if(it == idMap->second.end())
    return;

  handlerMap.erase(it->second.get());
  idMap->second.erase(it);
}

std::pair<Connection*, boost::uint16_t> RequestManager::RequestMap::getRequestInfo(const RequestHandler *requestHandler) {
  HandlerMap::iterator it = handlerMap.find(requestHandler);
  if(it == handlerMap.end())
    return std::pair<Connection*, boost::uint16_t>(0, 0);
  else
    return it->second;
}


void RequestManager::receiveHandler(Connection *connection, boost::shared_ptr<const XmlPacket> packet, boost::uint16_t requestId) {
  boost::upgrade_lock<boost::shared_mutex> lock(mutex);

  boost::shared_ptr<RequestHandler> request = requestMap.findRequest(connection, requestId);
  if(request) {
    lock.unlock();
    application->getThreadManager()->pushWork(boost::bind(&RequestHandler::handlePacket, request, packet));

    return;
  }

  if(!requestMap.isConnectionRegistered(connection)) {
    // TODO: Error
    application->log(Core::LoggerBase::LOG_ERROR, "Received a packet from an unregistered connection.");

    return;
  }

  std::map<std::string, boost::shared_ptr<RequestHandlerGroup> >::iterator rgh = requestHandlerGroups.find(packet->getType());
  if(rgh != requestHandlerGroups.end()) {
    request = rgh->second->createRequestHandler(application, packet->getType());

    if(request) {
      {
        boost::upgrade_to_unique_lock<boost::shared_mutex> upgradeLock(lock);

        requestMap.addRequest(connection, requestId, request);
        request->connectSignalFinished(boost::bind(&RequestManager::handleRequestFinished, this, connection, requestId));

      }

      lock.unlock();
      application->getThreadManager()->pushWork(boost::bind(&RequestHandler::handlePacket, request, packet));

      return;
    }
  }

  lock.unlock();

  application->logf(Core::LoggerBase::LOG_ERROR, "Received an unexpected packet with type '%s'.", packet->getType().c_str());

  XmlPacket ret;
  ret.setType("Error");
  ret.set("ErrorCode", Core::Exception::UNEXPECTED_PACKET);

  connection->sendPacket(ret, requestId);
}

bool RequestManager::sendRequest(Connection *connection, boost::shared_ptr<Request> request) {
  boost::unique_lock<boost::shared_mutex> lock(mutex);

  if(!requestMap.isConnectionRegistered(connection)) {
    application->log(Core::LoggerBase::LOG_CRITICAL, "Trying to send a request over an unregistered connecion.");
    return false;
  }

  boost::uint16_t requestId = _getUnusedRequestId(connection);

  if(request->isFinished || !requestMap.addRequest(connection, requestId, request)) {
    application->log(Core::LoggerBase::LOG_CRITICAL, "Trying resend a request.");
    return false;
  }
  request->connectSignalFinished(boost::bind(&RequestManager::handleRequestFinished, this, connection, requestId));
  lock.unlock();

  application->getThreadManager()->pushWork(boost::bind(&Request::sendRequest, request));

  return true;
}

boost::uint16_t RequestManager::_getUnusedRequestId(Connection *connection) {
  while(true) {
    boost::uint16_t requestId = _getRequestId();
    if(!requestMap.findRequest(connection, requestId))
      return requestId;
  }
}

void RequestManager::registerConnection(Connection *connection) {
  boost::lock_guard<boost::shared_mutex> lock(mutex);

  requestMap.registerConnection(connection);
  connection->connectSignalReceive(boost::bind(&RequestManager::receiveHandler, this, connection, _1, _2));
}

RequestManager::RequestManager(Application *application0, bool server) : application(application0), lastRequestId(server ? 0 : -1) {
  registerPacketType<RequestHandlers::DisconnectRequestHandler>("Disconnect");
}

}
}