javamanager/javacaptain/src/amc.h
branchRCL_3
changeset 14 04becd199f91
equal deleted inserted replaced
13:f5050f1da672 14:04becd199f91
       
     1 /*
       
     2 * Copyright (c) 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:  Amc
       
    15 *
       
    16 */
       
    17 
       
    18 #ifndef AMC_H
       
    19 #define AMC_H
       
    20 
       
    21 #include <list>
       
    22 #include <map>
       
    23 
       
    24 #include "logger.h"
       
    25 #include "scopedlocks.h"
       
    26 #include "javauid.h"
       
    27 
       
    28 #include "commslistener.h"
       
    29 #include "commsmessage.h"
       
    30 
       
    31 
       
    32 #include "applicationruntimeeventsinterface.h"
       
    33 #include "coreinterface.h"
       
    34 #include "rtcinterface.h"
       
    35 #include "amcinterface.h"
       
    36 #include "amcmessages.h"
       
    37 
       
    38 using namespace java::comms;
       
    39 using java::util::Uid;
       
    40 
       
    41 namespace java
       
    42 {
       
    43 
       
    44 namespace captain
       
    45 {
       
    46 
       
    47 class ApplicationManagementEventsInterface;
       
    48 
       
    49 using java::util::ScopedLock;
       
    50 
       
    51 class Amc : public AmcInterface,
       
    52         public ApplicationRuntimeEventsInterface,
       
    53         public CommsListener
       
    54 {
       
    55 public:
       
    56     Amc();
       
    57     virtual ~Amc();
       
    58 
       
    59     bool start(CoreInterface* aCore,
       
    60                ApplicationManagementEventsInterface* aAmEventsDispatcher);
       
    61     bool stop();
       
    62 
       
    63     // ApplicationRuntimeEventsInterface
       
    64     virtual void arLaunched(const Uid& aUID, const int& aRuntimeCommsAddress);
       
    65     virtual void arTerminated(const Uid& aUID, const int& aExitCode);
       
    66 
       
    67     // CommsListener methods
       
    68     virtual void processMessage(CommsMessage& aMessage);
       
    69 
       
    70 private:
       
    71 
       
    72     // Helper functions for request handling
       
    73     void handleStart(CommsMessage&);
       
    74     void handleStop(CommsMessage&);
       
    75     void handleUpdate(CommsMessage&);
       
    76 
       
    77     class StopRequest
       
    78     {
       
    79     public:
       
    80         StopRequest(CoreInterface* aCore, const int& aAddress, const int& aModuleId, const int& aMsgRef,
       
    81                     const int& aNumOfUIDS, Uid* aUIDS)
       
    82                 :mCore(aCore), mAddress(aAddress), mModuleId(aModuleId), mMsgRef(aMsgRef)
       
    83         {
       
    84             JELOG2(EJavaCaptain);
       
    85             java::util::ScopedLock lock(mUidsCollectionsMutex);
       
    86             for (int i = 0 ; i<aNumOfUIDS ; i++)
       
    87             {
       
    88                 mUidsToBeStopped.push_back(aUIDS[i]);
       
    89             }
       
    90         }
       
    91 
       
    92         bool stopUids()
       
    93         {
       
    94             JELOG2(EJavaCaptain);
       
    95             ScopedLock lock(mUidsCollectionsMutex);
       
    96 
       
    97             uids_t::iterator iter = mUidsToBeStopped.begin();
       
    98             while (iter != mUidsToBeStopped.end())
       
    99             {
       
   100                 mCore->getRtc()->disable(*iter);
       
   101                 if (mCore->getRtc()->terminate(*iter))
       
   102                 {
       
   103                     // Was not running in the first place can be directly put to stopped list
       
   104                     mUidStatuses.insert(std::make_pair(*iter, 0));
       
   105                     iter = mUidsToBeStopped.erase(iter);
       
   106                 }
       
   107                 else
       
   108                 {
       
   109                     ++iter;
       
   110                 }
       
   111             }
       
   112 
       
   113             // Was the request fulfilled.
       
   114             if (!mUidsToBeStopped.size())
       
   115             {
       
   116                 notifyRequester();
       
   117                 return true;
       
   118             }
       
   119 
       
   120             return false; // Nope some requests are still pending.
       
   121         }
       
   122 
       
   123         bool updateStatus(const Uid& aUID, const int& status)
       
   124         {
       
   125             JELOG2(EJavaCaptain);
       
   126             ScopedLock lock(mUidsCollectionsMutex);
       
   127 
       
   128             for (uids_t::iterator iter = mUidsToBeStopped.begin() ;
       
   129                     iter != mUidsToBeStopped.end() ; ++iter)
       
   130             {
       
   131                 if (*iter == aUID)
       
   132                 {
       
   133                     mUidStatuses.insert(std::make_pair(aUID, status));
       
   134                     mUidsToBeStopped.erase(iter);
       
   135                     break;
       
   136                 }
       
   137             }
       
   138 
       
   139             // Was this the last one ie. the request has been fulfilled.
       
   140             if (!mUidsToBeStopped.size())
       
   141             {
       
   142                 notifyRequester();
       
   143                 return true;
       
   144             }
       
   145 
       
   146             return false; // Nope some requests are still pending.
       
   147         }
       
   148 
       
   149         void notifyRequester()
       
   150         {
       
   151             JELOG2(EJavaCaptain);
       
   152             CommsMessage message;
       
   153             message.setReceiver(mAddress);
       
   154             message.setModuleId(mModuleId);
       
   155             message.setMessageRef(mMsgRef);
       
   156             setAmcResponseParamaters(message, mUidStatuses.size());
       
   157 
       
   158             for (uidStatuses_t::iterator iter = mUidStatuses.begin() ;
       
   159                     iter != mUidStatuses.end() ; ++iter)
       
   160             {
       
   161                 message << iter->first << iter->second;
       
   162             }
       
   163 
       
   164             mCore->getComms()->send(message);
       
   165         }
       
   166 
       
   167         void sendStopNotAllowed(const int& aNumOfUIDS, Uid* aUIDS)
       
   168         {
       
   169             JELOG2(EJavaCaptain);
       
   170             for (int i = 0 ; i<aNumOfUIDS ; i++)
       
   171             {
       
   172                 updateStatus(aUIDS[i], -1);
       
   173             }
       
   174         }
       
   175 
       
   176 
       
   177     private:
       
   178         typedef std::list<Uid>  uids_t;
       
   179         uids_t                  mUidsToBeStopped;
       
   180 
       
   181         typedef std::map<Uid, int> uidStatuses_t;
       
   182         uidStatuses_t           mUidStatuses;
       
   183         java::util::ScopedMutex mUidsCollectionsMutex;
       
   184 
       
   185         CoreInterface*          mCore;
       
   186 
       
   187         const int               mAddress;
       
   188         const int               mModuleId;
       
   189         const int               mMsgRef;
       
   190     };
       
   191 
       
   192     CoreInterface*                          mCore;
       
   193     ApplicationManagementEventsInterface*   mAmEventsDispatcher;
       
   194     StopRequest*                            mStopRequest; // only one at a time
       
   195 };
       
   196 
       
   197 } // namespace captain
       
   198 } // namespace java
       
   199 
       
   200 #endif // AMC_H
       
   201