summaryrefslogtreecommitdiffstats
path: root/src/Common/RequestManager.h
blob: 07a7c05e69dd659521e903327a957f9298a140e6 (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
/*
 * 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 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 General Public License for more details.
 *
 * You should have received a copy of the GNU 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 <map>
#include <memory>
#include <string>

#include <boost/thread/locks.hpp>
#include <boost/thread/shared_mutex.hpp>

namespace Mad {

namespace Net {
class Packet;
}

namespace Common {

class RequestManager : boost::noncopyable {
  private:
    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);
    };



    class RequestHandlerFactory {
      protected:
        RequestHandlerFactory() {}

      public:
        virtual boost::shared_ptr<RequestHandler> createRequestHandler() = 0;
        virtual ~RequestHandlerFactory() {}
    };

    template<class T> class SpecificRequestHandlerFactory : public RequestHandlerFactory {
      public:
        virtual boost::shared_ptr<RequestHandler> createRequestHandler() {
          return boost::shared_ptr<RequestHandler>(new T());
        }
    };

    static RequestManager requestManager;

    boost::shared_mutex mutex;

    RequestMap requestMap;

    bool server;
    boost::uint16_t lastRequestId;

    std::map<std::string, boost::shared_ptr<RequestHandlerFactory> > requestHandlerFactories;

    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();

  public:
    static RequestManager* get() {
      return &requestManager;
    }

    bool isServer() {
      boost::shared_lock<boost::shared_mutex> lock(mutex);
      return server;
    }

    void setServer(bool newServer) {
      boost::lock_guard<boost::shared_mutex> lock(mutex);

      server = newServer;

      if(server)
        lastRequestId &= ~0x01;
      else
        lastRequestId |= 0x01;
    }

    void registerConnection(Connection *connection);

    void unregisterConnection(Connection *connection) {
      boost::lock_guard<boost::shared_mutex> lock(mutex);
      requestMap.unregisterConnection(connection);
    }

    template <class T> void registerPacketType(const std::string &type) {
      boost::lock_guard<boost::shared_mutex> lock(mutex);
      requestHandlerFactories.insert(std::make_pair(type, boost::shared_ptr<SpecificRequestHandlerFactory<T> >(new SpecificRequestHandlerFactory<T>())));
    }

    void unregisterPacketType(const std::string &type) {
      boost::lock_guard<boost::shared_mutex> lock(mutex);
      requestHandlerFactories.erase(type);
    }

    bool sendRequest(Connection *connection, boost::shared_ptr<Request> request);
};

}
}

#endif /* MAD_COMMON_REQUESTMANAGER_H_ */