sensorservices/sensorserver/inc/server/sensrvproxymanager.h
changeset 0 4e1aa6a622a0
equal deleted inserted replaced
-1:000000000000 0:4e1aa6a622a0
       
     1 /*
       
     2 * Copyright (c) 2006-2008 Nokia Corporation and/or its subsidiary(-ies). 
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:  Sensor server proxy manager
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #ifndef SENSRVPROXYMANAGER_H
       
    20 #define SENSRVPROXYMANAGER_H
       
    21 
       
    22 #include <e32base.h>
       
    23 #include <ecom/ecom.h>
       
    24 #include "sensrvproxymanager.h"
       
    25 #include "sensrvtypes.h"
       
    26 #include "sensrvpluginproxy.h"
       
    27 #include "sensrvecomchangelistener.h"
       
    28 #include "sensrvshutdown.h"
       
    29 
       
    30 class CSensrvSession;
       
    31 class CSensrvTransactionQueue;
       
    32 class CSensrvServer;
       
    33 
       
    34 
       
    35 typedef TPckgBuf<TSensrvResourceChannelInfo> TSensrvResourceChannelInfoPckgBuf;
       
    36 
       
    37 /**
       
    38 *  Manages proxies and transactions not targeting single channel.
       
    39 *
       
    40 *  @since S60 5.0
       
    41 */
       
    42 class CSensrvProxyManager : public CBase, 
       
    43                             public MSensrvEcomChangeObserver
       
    44     {
       
    45     public:
       
    46     
       
    47         /**
       
    48         * Two phase constructor
       
    49         *
       
    50         * @since S60 5.0
       
    51         * @return New CSensrvProxyManager instance
       
    52         * @param Reference to server main class.
       
    53         * @exception KErrNoMemory Out of memory
       
    54         */
       
    55         static CSensrvProxyManager* NewL(CSensrvServer& aServer);
       
    56 
       
    57         /**
       
    58         * Destructor.
       
    59         */
       
    60         virtual ~CSensrvProxyManager();
       
    61 
       
    62         /**
       
    63         * Get pointer to server main thread heap
       
    64         *
       
    65         * @since S60 5.0
       
    66         * @return Heap pointer
       
    67         */
       
    68         inline RHeap* ServerHeap() {return iHeap;}
       
    69 
       
    70         /**
       
    71         * Get transaction timeout
       
    72         *
       
    73         * @since S60 5.0
       
    74         * @return Transaction timeout in microseconds
       
    75         */
       
    76         inline TTimeIntervalMicroSeconds32& TransactionTimeout() {return iTransactionTimeout;}
       
    77 
       
    78         /**
       
    79         * Get SSY stack size
       
    80         *
       
    81         * @since S60 5.0
       
    82         * @return SSY thread stack size in bytes
       
    83         */
       
    84         inline TInt SsyStackSize() {return iSsyStackSize;}
       
    85 
       
    86         /**
       
    87         * Get SSY heap max size
       
    88         *
       
    89         * @since S60 5.0
       
    90         * @return SSY thread heap max size in bytes
       
    91         */
       
    92         inline TInt SsyHeapMaxSize() {return iSsyHeapMaxSize;}
       
    93 
       
    94         /**
       
    95         * Get thread termination grace period
       
    96         *
       
    97         * @since S60 5.0
       
    98         * @return Thread termination grace period in microseconds
       
    99         */
       
   100         inline TTimeIntervalMicroSeconds32& ThreadTerminationGracePeriod() {return iThreadTerminationGracePeriod;}
       
   101 
       
   102         /**
       
   103         * Get SSY inactivity period before unloading
       
   104         *
       
   105         * @since S60 5.0
       
   106         * @return SSY inactivity period in microseconds
       
   107         */
       
   108         inline TTimeIntervalMicroSeconds32& SsyInactivityPeriod() {return iSsyInactivityPeriod;}
       
   109 
       
   110         /**
       
   111         * Get channel data buffer size multiplier.
       
   112         *
       
   113         * @since S60 5.0
       
   114         * @return Buffer size multiplier
       
   115         */
       
   116         inline TInt BufferSizeMultiplier() {return iBufferSizeMultiplier;}
       
   117 
       
   118         /**
       
   119         * Handles client message. 
       
   120         * Message completion responsibility transfers.
       
   121         *
       
   122         * @since S60 5.0
       
   123         * @param aMessage The message containing the client request
       
   124         */
       
   125         void DispatchMessage( CSensrvMessage& aMessage );
       
   126                 
       
   127         /**
       
   128         * Generates new unique id (within context of this server instance).
       
   129         *
       
   130         * @since S60 5.0
       
   131         * @return New unique id.
       
   132         */
       
   133         TInt GenerateUniqueId();
       
   134         
       
   135         /**
       
   136         * Notifies that a proxy has finished its initialization.
       
   137         * This triggers wait queue handling.
       
   138         *
       
   139         * Note that the notifying proxy might or might not
       
   140         * have initialized properly.
       
   141         *
       
   142         * @since S60 5.0
       
   143         */
       
   144         void NotifyProxyInitialized();
       
   145         
       
   146         /**
       
   147         * Duplicates mutex handle to one held by main thread.
       
   148         *
       
   149         * @since S60 5.0
       
   150         * @param aSrc Source handle
       
   151         * @param aDst Destination handle
       
   152         * @return Normal Symbian errors
       
   153         */
       
   154         TInt DuplicateMutexHandle(const RMutex& aSrc, RMutex& aDst) const;
       
   155 
       
   156         /**
       
   157         * Cleans up session related data.
       
   158         *
       
   159         * @since S60 5.0
       
   160         * @param aSession The session that was deleted
       
   161         */
       
   162         void SessionTerminated( CSensrvSession* aSession );
       
   163         
       
   164         /**
       
   165         * Shuts server down gracefully.
       
   166         *
       
   167         * @since S60 5.0
       
   168         * @param aMessage Message related to shutdown.
       
   169         */
       
   170         void ShutdownServer(const RMessage2& aMessage);
       
   171 
       
   172         void ShutdownServer();
       
   173 
       
   174         /**
       
   175         * Notifies that proxy thread has been terminated
       
   176         *
       
   177         * @since S60 5.0
       
   178         */
       
   179         void SsyThreadTerminated();
       
   180         
       
   181         // From MSensrvEcomChangeObserver
       
   182         
       
   183         /** 
       
   184         * Called when the ecom has been scanned
       
   185         *
       
   186         * @since S60 5.0
       
   187         * @param aImplementationInfos Found SSY implementation infos
       
   188         *        The observer is responsible for cleanup of this array,
       
   189         *        if change was not blocked (return ETrue).
       
   190         * @return EFalse if changing is currently blocked. Try again later.
       
   191         */
       
   192         virtual TBool EcomChanged( RImplInfoPtrArray* aImplementationInfos );
       
   193         
       
   194         /**
       
   195         * Notifies clients that a change has happened in available channels.
       
   196         *
       
   197         * @since S60 5.0
       
   198         * @param[in] aChangedChannel Channel information of the channel.
       
   199         * @param[in] aChangeType If ESensrvChannelAdded, channel is a newly available
       
   200         *                        channel. 
       
   201         *                        If ESensrvChannelRemoved, channel was removed and is  
       
   202         *                        no longer available.
       
   203         */
       
   204         void NotifyChannelChange(const TSensrvResourceChannelInfo& aChangedChannel,
       
   205                                  TSensrvChannelChangeType aChangeType );
       
   206                                  
       
   207         
       
   208         void AddSession();
       
   209 
       
   210 #ifdef COMPONENT_TRACE_DEBUG        
       
   211         /**
       
   212         * Traces a channel info list contents to component trace
       
   213         */
       
   214         void TraceChannelInfoList(const RSensrvResourceChannelInfoList& aInfoList);
       
   215         
       
   216         /**
       
   217         * Traces a channel info list contents to component trace
       
   218         */
       
   219         void TraceChannelInfoList(const RSensrvChannelInfoList& aInfoList);
       
   220 #endif        
       
   221 
       
   222         
       
   223         inline TTimeIntervalMicroSeconds32& TerminationPeriod() {return iTerminationPeriod;}
       
   224 
       
   225         /**
       
   226         * Loads the dynamic channel SSYs that matches to given query if not already loaded.
       
   227         *
       
   228         * @since S60 5.0
       
   229         * @param[in] aQueryInfo Channel query info
       
   230         * @param[in] aMessage Message from client for capability checking
       
   231         * @param[in] aAddChannelChangeListener Updates the listener reference count if ETrue
       
   232         * @return ETrue if loading of any required SSY is pending. Otherwise EFalse.
       
   233         */
       
   234         TBool LoadDynamicChannelSsysIfNeeded(const TSensrvResourceChannelInfo& aQueryInfo,
       
   235                                              const RMessage2& aQueryMessage,
       
   236                                              TBool aAddChannelChangeListener);
       
   237 
       
   238         /**
       
   239         * Removes channel change listener from the matching dynamic channel SSYs to allow unloading.
       
   240         *
       
   241         * @since S60 5.0
       
   242         * @param[in] aQueryInfo Channel query info
       
   243         * @param[in] aMessage Message from client for capability checking
       
   244         */
       
   245         void RemoveChannelChangeListenerFromDynamicChannelSsys(const TSensrvResourceChannelInfo& aQueryInfo,
       
   246                                                                const RMessage2& aQueryMessage);
       
   247 
       
   248 
       
   249     private:  // Member functions
       
   250         
       
   251         /**
       
   252         * C++ constructor
       
   253         */
       
   254         CSensrvProxyManager(CSensrvServer& aServer);
       
   255 
       
   256         /**
       
   257         * Second phase of construction.
       
   258         */
       
   259         void ConstructL();
       
   260 
       
   261         /**
       
   262         * Handles waiting transactions.
       
   263         *
       
   264         * @since S60 5.0
       
   265         */
       
   266         void HandleTransactionsQueryChannels();
       
   267 
       
   268         void HandleTransactionsOpenChannel();
       
   269         
       
   270         /**
       
   271         * Requests info about channels asynchronously.
       
   272         * Any errors are indicated in message completion code.
       
   273         *
       
   274         * @since S60 5.0
       
   275         * @param aMessage The message related to this request. Ownership passed here.
       
   276         */
       
   277         void QueryChannels(CSensrvMessage& aMessage);
       
   278 
       
   279         void OpenChannel(CSensrvMessage& aMessage);
       
   280         
       
   281         /**
       
   282         * Gets the proxy that handles the specified channel.
       
   283         *
       
   284         * @since S60 5.0
       
   285         * @param aChannelId The channel id of desired channel.
       
   286         * @return Pointer to the proxy handling the specified channel or NULL if not found.
       
   287         */
       
   288         CSensrvPluginProxy* GetProxyForChannel(TSensrvChannelId aChannelId) const;
       
   289 
       
   290         /**
       
   291         * Fetches configurable settings from cenrep and
       
   292         * stores them in member variables.
       
   293         *
       
   294         * @since S60 5.0
       
   295         */
       
   296         void GetSettings();
       
   297 
       
   298         /**
       
   299         * Adds proxy to proxy list
       
   300         *
       
   301         * @since S60 5.0
       
   302         * @param aProxyUid Implementation UID of the proxy to add
       
   303         * @return Pointer to new proxy or NULL if failed
       
   304         */
       
   305         CSensrvPluginProxy* AddProxy(const TUid& aProxyUid);
       
   306         
       
   307         /**
       
   308         * Callback method for proxy cleaner periodic object.
       
   309         * Check which proxies are marked for deletion and deletes them if they are
       
   310         * ready for cleanup.
       
   311         * 
       
   312         * @since S60 5.0
       
   313         * @param aObject Pointer to this
       
   314         * @return Irrelevant, always zero
       
   315         */
       
   316         static TInt ProxyCleanerCallback( TAny* aObject );
       
   317         
       
   318 
       
   319     private:
       
   320     
       
   321         /**
       
   322         * Reference to server main class.
       
   323         * Not own
       
   324         */
       
   325         CSensrvServer& iServer;
       
   326     
       
   327         /**
       
   328         * Holds transactions while all proxies finish initializing.
       
   329         * Queued transactions are owned by queue.
       
   330         * Own.
       
   331         */
       
   332         CSensrvTransactionQueue* iWaitQueueQueryChannels;
       
   333 
       
   334         /**
       
   335         * Holds transactions while all proxies finish initializing.
       
   336         * Queued transactions are owned by queue.
       
   337         * Own.
       
   338         */
       
   339         CSensrvTransactionQueue* iWaitQueueOpenChannel;
       
   340         
       
   341         /**
       
   342         * Master list or proxies.
       
   343         * Own proxies contained in array.
       
   344         */
       
   345         RPointerArray<CSensrvPluginProxy> iProxyList;
       
   346         
       
   347         /**
       
   348         * Counter used to generate unique ids.
       
   349         */
       
   350         TInt iIdCounter;
       
   351         
       
   352         /**
       
   353         * Critical section to protect access to iIdCounter
       
   354         */
       
   355         RMutex iProxyManagerMutex;
       
   356         
       
   357         /**
       
   358         * Indicates all proxies have been initialized
       
   359         */
       
   360         TBool iAllProxiesInitialized;
       
   361         
       
   362         /**
       
   363         * Server thread handle
       
   364         */
       
   365         RThread iServerThread;
       
   366         
       
   367         /**
       
   368         * Pointer to server main thread heap.
       
   369         * Not own.
       
   370         */
       
   371         RHeap* iHeap;
       
   372 
       
   373         /**
       
   374         * Transaction timeout for plugin transactions
       
   375         */
       
   376         TTimeIntervalMicroSeconds32 iTransactionTimeout;
       
   377         
       
   378         /**
       
   379         * SSY thread stack size
       
   380         */
       
   381         TInt iSsyStackSize;
       
   382         
       
   383         /**
       
   384         * SSY thread maximum heap size
       
   385         */
       
   386         TInt iSsyHeapMaxSize;
       
   387 
       
   388         /**
       
   389         * Thread termination grace period        
       
   390         */
       
   391         TTimeIntervalMicroSeconds32 iThreadTerminationGracePeriod;
       
   392 
       
   393         /**
       
   394         * SSY inactivity period before unloading 
       
   395         */
       
   396         TTimeIntervalMicroSeconds32 iSsyInactivityPeriod;
       
   397         
       
   398         /**
       
   399         * Number of SSY threads still active during shutdown.
       
   400         */
       
   401         TInt iShuttingDownCount;
       
   402         
       
   403         /**
       
   404         * Shutting down message. 
       
   405         */
       
   406         RMessagePtr2 iShutdownMessage;
       
   407         
       
   408         /**
       
   409         * Buffer size multiplier.
       
   410         */
       
   411         TInt iBufferSizeMultiplier;
       
   412         
       
   413         /**
       
   414         * Implementation info array of SSYs
       
   415         * Own.
       
   416         */
       
   417         RImplInfoPtrArray* iSsyImplInfoArray;
       
   418         
       
   419         /**
       
   420         * Listener for SSY plugin changes
       
   421         * Own
       
   422         */
       
   423         CSensrvEcomChangeListener* iEcomChangeListener;
       
   424         
       
   425         /**
       
   426         * Used to delete uninstalled proxies asynchronously
       
   427         * Own
       
   428         */
       
   429         CPeriodic* iProxyCleaner;
       
   430         
       
   431         /**
       
   432         * Keep track of current sessions
       
   433         */
       
   434         TInt iSessionCounter;
       
   435         
       
   436         /**
       
   437         * A pointer to server shutdown, owned
       
   438         */
       
   439         CSensrvShutdown* iServerShutdown;
       
   440         
       
   441         /**
       
   442         * Automatic server termination grace period        
       
   443         */
       
   444         TTimeIntervalMicroSeconds32 iTerminationPeriod;
       
   445         
       
   446         /**
       
   447         * Indicates if automatic server termination has been started.
       
   448         */
       
   449         TBool iShutdown;
       
   450         
       
   451     };
       
   452 
       
   453 
       
   454 
       
   455 #endif // SENSRVPROXYMANAGER_H