javamanager/javacaptain/src.s60/pmc.cpp
branchRCL_3
changeset 19 04becd199f91
equal deleted inserted replaced
16:f5050f1da672 19: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:  Pmc
       
    15  *
       
    16 */
       
    17 
       
    18 
       
    19 #include <sstream>
       
    20 #include <memory>
       
    21 
       
    22 #include "javasymbianoslayer.h"
       
    23 #include "logger.h"
       
    24 
       
    25 #include "pmc.h"
       
    26 #include "processmanagementeventsinterface.h"
       
    27 #include "coreinterface.h"
       
    28 
       
    29 #include "midpruntimearguments.h"
       
    30 
       
    31 using namespace java::comms;
       
    32 
       
    33 namespace java
       
    34 {
       
    35 namespace captain
       
    36 {
       
    37 
       
    38 Pmc::Pmc():mCore(0), mProcessManagementEventsDispatcher(0)
       
    39 {
       
    40     JELOG4(EJavaCaptain, EInfo);
       
    41 }
       
    42 
       
    43 Pmc::~Pmc()
       
    44 {
       
    45     JELOG4(EJavaCaptain, EInfo);
       
    46     mCore = 0;
       
    47     mProcessManagementEventsDispatcher = 0;
       
    48 }
       
    49 
       
    50 bool Pmc::start(CoreInterface* aCore,
       
    51                 ProcessManagementEventsInterface* aProcessManagementEventsDispatcher)
       
    52 {
       
    53     JELOG4(EJavaCaptain, EInfo);
       
    54     mCore = aCore;
       
    55     mProcessManagementEventsDispatcher = aProcessManagementEventsDispatcher;
       
    56 
       
    57     return true;
       
    58 }
       
    59 
       
    60 bool Pmc::stop()
       
    61 {
       
    62     JELOG4(EJavaCaptain, EInfo);
       
    63 
       
    64     ScopedLock lock(mProcessesMutex);
       
    65     for (processes_t::iterator iter = mProcesses.begin() ; iter != mProcesses.end() ; /*emty*/)
       
    66     {
       
    67         delete iter->second;
       
    68         mProcesses.erase(iter++);
       
    69     }
       
    70     return true;
       
    71 }
       
    72 
       
    73 int Pmc::launch(const cmdLine_t& cmdLine, const int& /*options*/)
       
    74 {
       
    75     JELOG4(EJavaCaptain, EInfo);
       
    76     int cmdLineSize = cmdLine.size();
       
    77     if (cmdLineSize > 0)
       
    78     {
       
    79         std::auto_ptr<HBufC> runtimeProcess(stringToDes(cmdLine[0].c_str()));
       
    80 
       
    81         std::ostringstream params;
       
    82         for (unsigned int i = 1 ; i < cmdLineSize ; i++)
       
    83         {
       
    84             params << cmdLine[i] << " ";
       
    85         }
       
    86         std::auto_ptr<HBufC> runtimeArgs(stringToDes(params.str().c_str()));
       
    87         ILOG2(EJavaCaptain, "launch command line: %s : %s",
       
    88               cmdLine[0].c_str(), params.str().c_str());
       
    89 
       
    90         ScopedLock lock(mProcessesMutex);
       
    91 
       
    92         processes_t::iterator iter = mProcesses.begin();
       
    93         while (iter != mProcesses.end())
       
    94         {
       
    95             if (iter->second->isExited())
       
    96             {
       
    97                 delete iter->second;
       
    98                 mProcesses.erase(iter);
       
    99                 iter = mProcesses.begin();
       
   100             }
       
   101             else
       
   102             {
       
   103                 ++iter;
       
   104             }
       
   105         }
       
   106 
       
   107         std::auto_ptr<RProcess> rp(new RProcess);
       
   108         bool prewarm = false;
       
   109         if (std::find(cmdLine.begin(), cmdLine.end(), java::runtime::PREWARM_ARGUMENT) != cmdLine.end())
       
   110         {
       
   111             prewarm = true;
       
   112         }
       
   113         JavaOsLayer::startUpTrace("CAPTAIN: Creating process", -1, -1);
       
   114         if (KErrNone == rp->Create(TPtr(runtimeProcess->Des()), TPtr(runtimeArgs->Des())))
       
   115         {
       
   116             int pid = rp->Id();
       
   117             mProcesses.insert(std::make_pair(pid, new ProcessActive(rp.release(), this, prewarm)));
       
   118             return pid;
       
   119         }
       
   120     }
       
   121 
       
   122     return 0;
       
   123 }
       
   124 
       
   125 int Pmc::terminate(const int& pid)
       
   126 {
       
   127     JELOG4(EJavaCaptain, EInfo);
       
   128 
       
   129     ScopedLock lock(mProcessesMutex);
       
   130     processes_t::iterator iter = mProcesses.find(pid);
       
   131     if (iter != mProcesses.end())
       
   132     {
       
   133         iter->second->Terminate(KErrNone);
       
   134     }
       
   135 
       
   136     return 0;
       
   137 }
       
   138 
       
   139 int Pmc::kill(const int& pid)
       
   140 {
       
   141     JELOG4(EJavaCaptain, EInfo);
       
   142 
       
   143     ScopedLock lock(mProcessesMutex);
       
   144     processes_t::iterator iter = mProcesses.find(pid);
       
   145     if (iter != mProcesses.end())
       
   146     {
       
   147         iter->second->Kill(KErrNone);
       
   148     }
       
   149 
       
   150     return 0;
       
   151 }
       
   152 
       
   153 void Pmc::handleTerminated(const int& pid, const int& exitCode)
       
   154 {
       
   155     JELOG4(EJavaCaptain, EInfo);
       
   156     if (mProcessManagementEventsDispatcher)
       
   157         mProcessManagementEventsDispatcher->pmcTerminated(pid, exitCode);
       
   158 }
       
   159 
       
   160 } // namespace captain
       
   161 } // namespace java
       
   162