root/plugins/branches/0185_GEN_PHYSICS_REFACTOR_2/ZBoostNetworking/src/HyperTextTransportProtocolService.hpp @ 3447

Revision 3447, 6.6 KB (checked in by mgray, 6 months ago)

Debug and refactor of the TCP Protocol Adapter in ZBoostNetworking.

Line 
1//-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~
2// Zen Enterprise Framework
3//
4// Copyright (C) 2001 - 2009 Tony Richards
5//
6//  This software is provided 'as-is', without any express or implied
7//  warranty.  In no event will the authors be held liable for any damages
8//  arising from the use of this software.
9//
10//  Permission is granted to anyone to use this software for any purpose,
11//  including commercial applications, and to alter it and redistribute it
12//  freely, subject to the following restrictions:
13//
14//  1. The origin of this software must not be misrepresented; you must not
15//     claim that you wrote the original software. If you use this software
16//     in a product, an acknowledgment in the product documentation would be
17//     appreciated but is not required.
18//  2. Altered source versions must be plainly marked as such, and must not be
19//     misrepresented as being the original software.
20//  3. This notice may not be removed or altered from any source distribution.
21//
22//  Tony Richards trichards@indiezen.com
23//-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~
24#ifndef ZEN_ENTERPRISE_APPSERVER_HTTP_PROTOCOL_SERVICE_HPP_INCLUDED
25#define ZEN_ENTERPRISE_APPSERVER_HTTP_PROTOCOL_SERVICE_HPP_INCLUDED
26
27#include "../I_WebProtocolService.hpp"
28#include "HTTP/Connection.hpp"
29
30#include <Zen/Core/Memory/managed_ptr.hpp>
31#include <Zen/Core/Memory/managed_weak_ptr.hpp>
32#include <Zen/Core/Memory/managed_self_ref.hpp>
33
34#include <Zen/Core/Threading/I_Thread.hpp>
35#include <Zen/Core/Threading/I_Mutex.hpp>
36
37#include <Zen/Enterprise/AppServer/I_ProtocolService.hpp>
38
39#include <boost/noncopyable.hpp>
40#include <boost/function.hpp>
41#include <boost/asio.hpp>
42
43#include <boost/shared_ptr.hpp>
44
45#include <map>
46#include <string>
47#include <vector>
48
49//-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~
50namespace Zen {
51    namespace Event {
52        class I_Event;
53    }   // namespace Event
54namespace Enterprise {
55namespace AppServer {
56namespace HTTP {
57    class RequestHandler;
58}   // namespace HTTP
59//-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~
60
61class HyperTextTransportProtocolService
62:   public I_WebProtocolService
63,   public Memory::managed_self_ref<I_ProtocolService>
64{
65    /// @name Types
66    /// @{
67public:
68    typedef std::vector<Threading::I_Thread*>                   Threads_type;
69    typedef Memory::managed_weak_ptr<Networking::I_Endpoint>    wpEndpoint_type;
70    typedef std::pair<std::string, pResourceLocation_type>      LocationPair_type;
71    typedef std::list<LocationPair_type>                        Locations_type;
72    /// @}
73
74    /// @name I_HTTPService implementation
75    /// @{
76public:
77    virtual void registerURL(const std::string& _url, pResourceLocation_type _pDestination);
78    /// @}
79
80    /// @name I_ProtocolService implementation
81    /// @{
82public:
83    virtual I_ApplicationServer& getApplicationServer();
84    virtual pEndpoint_type resolveEndpoint(const std::string& _address, const std::string& _port);
85    virtual void sendTo(pMessage_type _pMessage, pEndpoint_type _pEndpoint);
86    virtual Event::I_Event& getConnectedEvent();
87    virtual Event::I_Event& getDisconnectedEvent();
88    /// @}
89
90    /// @name I_StartupShutdownParticipant implementation
91    /// @{
92public:
93    virtual void setConfiguration(const Plugins::I_ConfigurationElement& _config);
94    virtual Threading::I_Condition* prepareToStart(Threading::ThreadPool& _threadPool);
95    virtual void start();
96    virtual Threading::I_Condition* prepareToStop();
97    virtual void stop();
98    /// @}
99
100    /// @name HyperTextTransportProtocolService implementation
101    /// @{
102public:
103    void handleAccept(const boost::system::error_code& _error);
104
105    void createConnection();
106    void asyncAccept();
107
108    void destroyEndpoint(wpEndpoint_type _pEndpoint);
109
110    /// Get the location for this URL
111    /// @see I_WebProtocolService::registerURL()
112    pResourceLocation_type getLocation(const std::string& _url);
113    /// @}
114
115    /// @name 'Structors
116    /// @{
117protected:
118    friend class ProtocolServiceFactory;
119
120    explicit HyperTextTransportProtocolService(I_ApplicationServer& _server);
121    virtual ~HyperTextTransportProtocolService();
122    /// @}
123
124    /// @name Inner Classes
125    /// @{
126public:
127
128    /// @}
129
130    /// @name Member Variables
131    /// @{
132private:
133    /// Reference to the parent application server to which this protocol service is bound
134    AppServer::I_ApplicationServer&    m_appServer;
135
136    /// IO Service to perform asynchronous operations
137    boost::asio::io_service             m_ioService;
138    boost::asio::io_service::work*      m_pWork;
139
140    /// Acceptor used to listen for incoming connections
141    boost::asio::ip::tcp::acceptor      m_acceptor;
142
143    /// Address on which to bind
144    std::string                         m_address;
145
146    /// Port to which to bind
147    std::string                         m_port;
148
149    /// Request handler
150    HTTP::RequestHandler*               m_pRequestHandler;
151
152    /// Number of threads
153    int                                 m_threadCount;
154
155    /// This is a shared pointer instead of a managed pointer
156    /// because boost::bind<> handles shared_ptr correctly
157    boost::shared_ptr<HTTP::Connection> m_pNewConnection;
158
159    /// Collection of threads
160    Threads_type                        m_threads;
161
162    boost::asio::ip::tcp::endpoint      m_endpoint;
163
164    /// All of the locations being handled by this protocol adapter.
165    /// The first entry in the pair is the string used to partially
166    /// match the URL.  The second entry is the location used by
167    /// the application server to dispatch the request to the correct
168    /// application service.  The longest match will be used.
169    ///
170    /// Examples:
171    ///     /       will match / and /xyz
172    ///     /xyz    will match /xyz
173    ///
174    /// If there are more than one matches, the longest match will be used.
175    /// In the above example /xyz will actually end up matching /xyz even
176    /// though / also matches.
177    Locations_type                      m_locations;
178
179    /// Guard for m_locations
180    Threading::I_Mutex*                 m_pLocationsGuard;
181    /// @}
182
183};  // class HyperTextTransportProtocolService
184
185//-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~
186}   // namespace AppServer
187}   // namespace Enterprise
188}   // namespace Zen
189//-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~
190
191#endif // ZEN_ENTERPRISE_APPSERVER_HTTP_PROTOCOL_SERVICE_HPP_INCLUDED
Note: See TracBrowser for help on using the browser.