javamanager/javacaptain/tsrc/test_rtcplugin2/src/test2runtime.cpp
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:  Test2Runtime
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include "logger.h"
       
    20 
       
    21 #include "javaprocessconstants.h"
       
    22 #include "commsendpoint.h"
       
    23 
       
    24 #include "coreinterface.h"
       
    25 #include "pmcinterface.h"
       
    26 #include "rtcmessages.h"
       
    27 
       
    28 #include "test2runtime.h"
       
    29 
       
    30 const int EXIT_TIMER_TIMEOUT    = 2;
       
    31 const int LAUNCH_TIMER_TIMEOUT  = 5;
       
    32 
       
    33 namespace java
       
    34 {
       
    35 namespace captain
       
    36 {
       
    37 
       
    38 Test2Runtime::Test2Runtime(const Uid& aUID,
       
    39                            CoreInterface* aCore)
       
    40         :mUid(aUID), mCore(aCore), mPid(-1),
       
    41         mRunning(false), mExiting(false),
       
    42         mRuntimeAddress(0), mTimerId(0)
       
    43 {
       
    44     JELOG4(EJavaCaptain, EInfoHeavyLoad);
       
    45 }
       
    46 
       
    47 Test2Runtime::~Test2Runtime()
       
    48 {
       
    49     JELOG4(EJavaCaptain, EInfoHeavyLoad);
       
    50 }
       
    51 
       
    52 const char* const APP_UID_ARGUMENT          = "-appid";
       
    53 const char* const PUSH_ARGUMENT             = "-push";
       
    54 const char* const PREWARM_ARGUMENT          = "-prewarm";
       
    55 const char* const AUTO_INVOCATION_ARGUMENT  = "-autoinvocation";
       
    56 const char* const DEBUG_ARGUMENT            = "-debug";
       
    57 const char* const BG_START_ARGUMENT         = "-background";
       
    58 
       
    59 // RtcRuntimeInterface
       
    60 bool Test2Runtime::launch(const rtcLaunchInfo& aLaunchInfo)
       
    61 {
       
    62     JELOG4(EJavaCaptain, EInfoHeavyLoad);
       
    63 
       
    64     bool launchSuccess = false;
       
    65 
       
    66     std::vector<std::string> params;
       
    67 
       
    68     params.push_back("javatestruntime");
       
    69     params.push_back(java::runtime::JAVA_MIDP_STARTER_DLL); //The name of the dll, that runs MIDlets
       
    70     params.push_back(APP_UID_ARGUMENT);
       
    71 
       
    72     char* appUID = java::util::JavaCommonUtils::wstringToUtf8(mUid.toString());
       
    73     params.push_back(appUID);
       
    74     delete [] appUID;
       
    75 
       
    76     switch (aLaunchInfo.mLaunchType)
       
    77     {
       
    78     case RTC_LAUNCH_TYPE_PUSH_C:
       
    79         params.push_back(PUSH_ARGUMENT);
       
    80         break;
       
    81 
       
    82     case RTC_LAUNCH_TYPE_AUTO_INVOCATION_C:
       
    83         params.push_back(AUTO_INVOCATION_ARGUMENT);
       
    84         break;
       
    85 
       
    86     case RTC_LAUNCH_TYPE_BACKGROUND_C:
       
    87         params.push_back(BG_START_ARGUMENT);
       
    88         break;
       
    89 
       
    90     case RTC_LAUNCH_TYPE_NORMAL_C: // fall through
       
    91     default:
       
    92         break;
       
    93 
       
    94     }
       
    95 
       
    96     mPid = mCore->getPmc()->launch(params, 0);
       
    97 
       
    98     if (mPid > 0)
       
    99     {
       
   100         launchSuccess = true;
       
   101         startTimer(LAUNCH_TIMER_TIMEOUT);
       
   102     }
       
   103 
       
   104     return launchSuccess;
       
   105 }
       
   106 
       
   107 int Test2Runtime::pid() const
       
   108 {
       
   109     JELOG4(EJavaCaptain, EInfoHeavyLoad);
       
   110     return mPid;
       
   111 }
       
   112 
       
   113 Uid Test2Runtime::uid() const
       
   114 {
       
   115     JELOG4(EJavaCaptain, EInfoHeavyLoad);
       
   116     return mUid;
       
   117 }
       
   118 
       
   119 bool Test2Runtime::isRunning() const
       
   120 {
       
   121     JELOG4(EJavaCaptain, EInfoHeavyLoad);
       
   122 
       
   123     return mRunning;
       
   124 }
       
   125 
       
   126 bool Test2Runtime::terminate(const rtcTerminateInfo& /*aTerminateInfo*/)
       
   127 {
       
   128     JELOG4(EJavaCaptain, EInfoHeavyLoad);
       
   129 
       
   130     bool alreadyTerminated = false;
       
   131 
       
   132     CommsMessage msg;
       
   133     setTerminateApplicationReqParams(msg, mUid, RTC_TERMINATE_OPTIONS_NONE_C);
       
   134     msg.setReceiver(mRuntimeAddress);
       
   135 
       
   136     if (0 == mCore->getComms()->send(msg))
       
   137     {
       
   138         startTimer(EXIT_TIMER_TIMEOUT);
       
   139         alreadyTerminated = false;
       
   140         mExiting = true;
       
   141     }
       
   142     else
       
   143     {
       
   144         if (mPid > 0 && mCore)
       
   145         {
       
   146             mCore->getPmc()->kill(mPid);
       
   147             alreadyTerminated = false;
       
   148             mExiting = true;
       
   149         }
       
   150         else
       
   151         {
       
   152             alreadyTerminated = true;
       
   153         }
       
   154     }
       
   155 
       
   156     return alreadyTerminated;
       
   157 }
       
   158 
       
   159 // ProcessManagementEventsInterface
       
   160 void Test2Runtime::pmcTerminated(const int& /*aPid*/, const int& /*exitCode*/)
       
   161 {
       
   162     JELOG4(EJavaCaptain, EInfoHeavyLoad);
       
   163 
       
   164     stopTimer();
       
   165 
       
   166     mPid = -1;
       
   167     mRuntimeAddress = 0;
       
   168     mRunning = false;
       
   169     mExiting = false;
       
   170 }
       
   171 
       
   172 void Test2Runtime::runningInd(const int& aRuntimeAddress, const int& /*aStatus*/)
       
   173 {
       
   174     JELOG4(EJavaCaptain, EInfoHeavyLoad);
       
   175 
       
   176     mRuntimeAddress = aRuntimeAddress;
       
   177 
       
   178     if (!mExiting)
       
   179     {
       
   180         stopTimer();
       
   181         mRunning = true;
       
   182     }
       
   183 }
       
   184 
       
   185 void Test2Runtime::terminatedInd(const int& /*aStatus*/)
       
   186 {
       
   187     if (!mExiting)
       
   188     {
       
   189         startTimer(EXIT_TIMER_TIMEOUT);
       
   190     }
       
   191 }
       
   192 
       
   193 // CommsListener
       
   194 void Test2Runtime::processMessage(CommsMessage& aMessage)
       
   195 {
       
   196     JELOG4(EJavaCaptain, EInfoHeavyLoad);
       
   197 
       
   198     switch (aMessage.getMessageId())
       
   199     {
       
   200     default:
       
   201         ELOG1(EJavaCaptain, "Unknown message forwarded to Test2Runtime %d", aMessage.getMessageId());
       
   202 
       
   203     }
       
   204 }
       
   205 
       
   206 void Test2Runtime::startTimer(const int& aTimeout)
       
   207 {
       
   208     JELOG4(EJavaCaptain, EInfoHeavyLoad);
       
   209 
       
   210     // Only cancel if already started
       
   211     if (0 != mTimerId)
       
   212     {
       
   213         mCore->getTimerServer()->timerCancel(mTimerId);
       
   214         mTimerId = 0;
       
   215     }
       
   216     mTimerId = mCore->getTimerServer()->timerCreateSeconds(aTimeout, this);
       
   217 }
       
   218 void Test2Runtime::stopTimer()
       
   219 {
       
   220     JELOG4(EJavaCaptain, EInfoHeavyLoad);
       
   221 
       
   222     if (0 != mTimerId)
       
   223     {
       
   224         mCore->getTimerServer()->timerCancel(mTimerId);
       
   225         mTimerId = 0;
       
   226     }
       
   227 }
       
   228 
       
   229 
       
   230 // TimerServerEventsInterface methods
       
   231 void Test2Runtime::timerTimeout(const int& aTimerId)
       
   232 {
       
   233     JELOG4(EJavaCaptain, EInfoHeavyLoad);
       
   234 
       
   235     if (aTimerId == mTimerId)
       
   236     {
       
   237         mTimerId = 0;
       
   238         if (mPid > 0 && mCore)
       
   239         {
       
   240             mCore->getPmc()->kill(mPid);
       
   241         }
       
   242     }
       
   243     else
       
   244     {
       
   245         LOG1WSTR(EJavaCaptain, EError, "timerTimeout(%s)", mUid.toString());
       
   246         ELOG2(EJavaCaptain, "Wrong timerId! ok:%d FAIL:%d", mTimerId, aTimerId);
       
   247     }
       
   248 }
       
   249 
       
   250 } // namespace captain
       
   251 } // namespace java
       
   252