root/Enterprise/branches/0075_TR_SCRIPTING/AppServer/src/ApplicationServer.hpp @ 3454

Revision 3454, 8.5 KB (checked in by trichards, 6 months ago)

Cleaning up the implementation of the App Server.

Line 
1//-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~
2// Zen Enterprise Framework
3//
4// Copyright (C) 2001 - 2010 Tony Richards
5// Copyright (C) 2008 - 2010 Matthew Alan Gray
6// Copyright (C)        2009 Joshua Cassity
7//
8//  This software is provided 'as-is', without any express or implied
9//  warranty.  In no event will the authors be held liable for any damages
10//  arising from the use of this software.
11//
12//  Permission is granted to anyone to use this software for any purpose,
13//  including commercial applications, and to alter it and redistribute it
14//  freely, subject to the following restrictions:
15//
16//  1. The origin of this software must not be misrepresented; you must not
17//     claim that you wrote the original software. If you use this software
18//     in a product, an acknowledgment in the product documentation would be
19//     appreciated but is not required.
20//  2. Altered source versions must be plainly marked as such, and must not be
21//     misrepresented as being the original software.
22//  3. This notice may not be removed or altered from any source distribution.
23//
24//  Tony Richards trichards@indiezen.com
25//  Matthew Alan Gray mgray@indiezen.org
26//  Joshua Cassity jcassity@indiezen.org
27//-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~
28#ifndef ZEN_APPSERVER_APPLICATION_SERVER_HPP_INCLUDED
29#define ZEN_APPSERVER_APPLICATION_SERVER_HPP_INCLUDED
30
31#include "../I_ApplicationServer.hpp"
32
33#include <Zen/Core/Threading/I_Thread.hpp>
34#include <Zen/Core/Threading/ThreadPool.hpp>
35#include <Zen/Core/Event/I_EventService.hpp>
36#include <Zen/Core/Plugins/I_Configuration.hpp>
37
38#include <map>
39#include <set>
40
41//-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~
42namespace Zen {
43    namespace Database {
44        class I_DatabaseService;
45        class I_DatabaseConnection;
46    }   // namespace Database
47namespace Enterprise {
48namespace AppServer {
49//-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~
50;
51
52class ApplicationServer
53:   public I_ApplicationServer
54{
55    /// @name Types
56    /// @{
57public:
58    typedef std::map<std::string, pProtocolService_type>                ProtocolServices_type;
59    typedef std::map<pResourceLocation_type, pApplicationService_type>  ApplicationServices_type;
60    /// @}
61
62    /// @name Enums
63    /// @{
64public:
65    enum ServerStates
66    {
67        INITIALIZED,
68        STARTING,
69        STARTED,
70        STOPPING,
71        STOPPED
72    };
73    /// @}
74
75    /// @name I_ApplicationServer implementation
76    /// @{
77public:
78    virtual Zen::Threading::I_Condition* start();
79    virtual void stop();
80    virtual void registerDefaultScriptEngine(pScriptEngine_type _pEngine);
81    virtual pScriptEngine_type getDefaultScriptEngine();
82    virtual pEventService_type getEventService();
83    virtual void installProtocols(pConfig_type _pProtocolsConfig);
84    virtual void installProtocol(pProtocolService_type _pProtocolService, const std::string& _protocolName);
85    virtual pProtocolService_type getProtocol(const std::string& _protocolName);
86    virtual void installApplications(pConfig_type _pAppServicesConfig);
87    virtual void installApplication(pApplicationService_type _pApplicationService, pResourceLocation_type _pRootLocation);
88    virtual void configureApplication(pApplicationService_type _pApplicationService, pConfig_type _pConfig);
89    virtual pApplicationService_type getApplication(pResourceLocation_type _pServiceLocation) const;
90    virtual void getResourceLocations(I_ResourceLocationVisitor& _visitor) const;
91    virtual pMessageRegistry_type getMessageRegistry();
92    virtual void handleMessage(pMessage_type _pMessage);
93    virtual void handleRequest(pRequest_type _pRequest, pResponseHandler_type _pResponseHandler);
94    virtual void handleSessionEvent(pSessionEvent_type _pSessionEvent);
95    virtual void installDatabaseConnections(pConfig_type _pDatabasesConfig);
96    void createDatabaseEntry(const std::string& _connectionName, const std::string& _databaseType, config_type& _config);
97    virtual pDatabaseConnection_type getDatabaseConnection(const std::string& _connectionName);
98    /// @}
99
100    /// @name ApplicationServer implementation
101    /// @{
102public:
103    /// Install a protocol service.
104    ///
105    /// This is called by an InstallProtocolServiceTask that is queued into m_installQueue
106    ///
107    /// @note Internal use only
108    void handleInstallProtocol(pProtocolService_type _pProtocolService, const std::string& _protocolName);
109
110    /// Install an application service.
111    ///
112    /// This is called by an InstallApplicationServiceTask that is queued into m_installQueue
113    ///
114    /// @note Internal use only
115    void handleInstallApplication(pApplicationService_type _pApplicationService, pResourceLocation_type _pRootLocation);
116
117    void handleConfigureApplication(pApplicationService_type _pApplicationService, pConfig_type _pConfig);
118
119    Threading::ThreadPool& getSharedThreadPool() { return m_sharedThreadPool; }
120
121    /// @return true if _location is a local destination.
122    bool isLocalDestination(pEndpoint_type _pDestination);
123    /// @}
124
125    /// @name Inner classes
126    /// @{
127private:
128    class DatabaseConnections
129    {
130        /// @name Types
131        /// @{
132    public:
133        typedef Zen::Memory::managed_ptr<Zen::Database::I_DatabaseService>              pDatabaseService_type;
134        typedef std::map<std::string,std::string>                                       config_type;
135        typedef Zen::Memory::managed_ptr<Zen::Database::I_DatabaseConnection>           pDatabaseConnection_type;
136        typedef std::map<Zen::Threading::I_Thread::ThreadId,pDatabaseConnection_type>   DatabaseConnections_type;
137        /// @}
138
139        /// @name DatabaseConnections implementation
140        /// @{
141    public:
142        pDatabaseConnection_type getConnection(Zen::Threading::I_Thread::ThreadId& _threadId);
143        /// @}
144
145        /// @name 'Structors
146        /// @{
147                 DatabaseConnections(pDatabaseService_type _pService, config_type _connectionConfig);
148                ~DatabaseConnections();
149        /// @}
150
151        /// @name Member Variables
152        /// @{
153    private:
154        pDatabaseService_type           m_pDatabaseService;
155        config_type                     m_connectionConfig;
156        DatabaseConnections_type        m_databaseConnections;
157        Zen::Threading::I_Mutex*        m_databaseConnectionsMutex;
158        /// @}
159
160    };  // class DatabaseConnections
161    /// @}
162
163    /// @name Events
164    /// @{
165public:
166    /// @}
167
168    /// @name 'Structors
169    /// @{
170public:
171             ApplicationServer();
172    virtual ~ApplicationServer();
173    /// @}
174
175    /// @name Member Variables
176    /// @{
177private:
178    ServerStates                m_currentState;
179
180    /// Default script engine.
181    pScriptEngine_type          m_pScriptEngine;
182
183    /// Primary event service.
184    pEventService_type          m_pEventService;
185
186    /// ThreadPool that's shared among all of the services and protocols.
187    Threading::ThreadPool       m_sharedThreadPool;
188
189    /// Queue of items that require installation
190    /// plus the thread that is used to perform
191    /// the installation.
192    Threading::ThreadPool       m_installQueue;
193
194    /// Queue of items that require shutdown
195    /// plus the thread that is used to perform
196    /// the shutdown.
197    Threading::ThreadPool       m_shutdownQueue;
198
199    /// Guard for m_protocolServices consistency
200    Threading::I_Mutex*         m_pProtocolGuard;
201
202    /// Collection of installed protocol services
203    ProtocolServices_type       m_protocolServices;
204
205    /// Guard for the m_applicatinoServices consistency
206    Threading::I_Mutex*         m_pApplicationGuard;
207
208    /// Collection of installed application services
209    ApplicationServices_type    m_applicationServices;
210
211    pMessageRegistry_type       m_pMessageRegistry_type;
212
213    typedef Zen::Memory::managed_ptr<DatabaseConnections>       pDatabaseConnections_type;
214    typedef std::map<std::string, pDatabaseConnections_type>    DatabaseConnectionsMap_type;
215    DatabaseConnectionsMap_type m_databaseConnectionsMap;
216    /// @}
217
218};  // interface I_ApplicationServer
219
220//-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~
221}   // namespace AppServer
222}   // namespace Enterprise
223}   // namespace Zen
224//-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~
225
226#endif // ZEN_APPSERVER_APPLICATION_SERVER_HPP_INCLUDED
Note: See TracBrowser for help on using the browser.