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