javaextensions/midppush/pushcontroller/src/pushcontrollerstarterimpl.cpp
changeset 21 2a9601315dfc
equal deleted inserted replaced
18:e8e63152f320 21:2a9601315dfc
       
     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:
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include <errno.h>
       
    20 #include <pthread.h>
       
    21 #include <iostream>
       
    22 #include <sstream>
       
    23 
       
    24 #include "logger.h"
       
    25 
       
    26 #include "dynamiclibloader.h"
       
    27 #include "runtimeexception.h"
       
    28 #include "javaosheaders.h"
       
    29 
       
    30 #include "midpruntimepushsupport.h"
       
    31 
       
    32 #include "pushcontrollerstarterimpl.h"
       
    33 #include "serverconnectionfactory.h"
       
    34 #include "serverconnection.h"
       
    35 #include "pushregistrymessages.h"
       
    36 #include "comms.h"
       
    37 #include "commsclientendpoint.h"
       
    38 #include "commsmessage.h"
       
    39 #include "javacommonutils.h"
       
    40 #include "pusherrorcodes.h"
       
    41 #include "pushexception.h"
       
    42 #include "pushconstant.h"
       
    43 #include "javauid.h"
       
    44 #include "libraryloaderexception.h"
       
    45 
       
    46 using namespace java::push;
       
    47 using namespace java::runtime;
       
    48 using namespace java::comms;
       
    49 using namespace java::util;
       
    50 using namespace std;
       
    51 
       
    52 #if defined(__SYMBIAN32__) && defined(__WINSCW__)
       
    53 #include <pls.h>
       
    54 #else
       
    55 static PushControllerStarterImpl* sPushContStarter = 0;
       
    56 #endif
       
    57 
       
    58 #ifdef __SYMBIAN32__
       
    59 PushControllerStarter& getPushControllerStarter()
       
    60 {
       
    61 #else
       
    62 extern "C" PushControllerStarter& getPushControllerStarter()
       
    63 {
       
    64 #endif
       
    65 
       
    66     JELOG2(EJavaPush);
       
    67 
       
    68 #if defined(__SYMBIAN32__) && defined(__WINSCW__)
       
    69     TUid uid = TUid::Uid(0xE0000050);
       
    70     PushControllerStarterImpl* retObj = Pls<PushControllerStarterImpl>(uid);
       
    71     return *retObj;
       
    72 #else
       
    73     if (sPushContStarter == 0)
       
    74     {
       
    75         sPushContStarter = new PushControllerStarterImpl();
       
    76     }
       
    77     return *sPushContStarter;
       
    78 #endif
       
    79 }
       
    80 
       
    81 /**
       
    82  *
       
    83  */
       
    84 PushControllerStarterImpl::PushControllerStarterImpl()
       
    85         : mSrvConnPluginMgr(0),mAppStateController(0),mState(INITIAL_STATE),
       
    86         mThreadId(0),mMonitor(Monitor::createMonitor()),mConnCompletedMonitor(0),
       
    87         mIsUpdate(false),mNumOfPendingUpdates(0)
       
    88 {
       
    89     JELOG2(EJavaPush);
       
    90     mSrvConnPluginMgr.reset(new PushServerConnPluginManager(this,this,this));
       
    91 }
       
    92 
       
    93 /**
       
    94  *
       
    95  */
       
    96 PushControllerStarterImpl::~PushControllerStarterImpl()
       
    97 {
       
    98     JELOG2(EJavaPush);
       
    99     try
       
   100     {
       
   101         CommsClientEndpoint& comms = getComms();
       
   102         comms.unregisterListener(PLUGIN_ID_PUSH_CONTROLLER_C,this);
       
   103     }
       
   104     catch (...) {}
       
   105 }
       
   106 
       
   107 /**
       
   108  * This operation throws exception if initialization of all connections
       
   109  * fails. RuntimeStarter closes application if it gets exception from this
       
   110  * operation.
       
   111  * MidpStarterPushSupport::closeRuntimeRequestFromPush()
       
   112  * operation is called from error() operation if opening all connections fails.
       
   113  */
       
   114 void PushControllerStarterImpl::startListen(const java::util::Uid& aUid,
       
   115         MidpStarterPushSupport* aAppStateController)
       
   116 {
       
   117     JELOG2(EJavaPush);
       
   118 
       
   119     setState(LOADING_PUSH_CONNS);
       
   120     mAppStateController = aAppStateController;
       
   121 
       
   122     registerCommsListener();
       
   123     mIsUpdate = false;
       
   124     mUid = aUid;
       
   125     bool retValue = sendGetAllPushRegsMsg(false);
       
   126     if (false == retValue)
       
   127         setState(INITIAL_STATE);
       
   128 }
       
   129 
       
   130 /**
       
   131  *
       
   132  */
       
   133 void PushControllerStarterImpl::updatePushRegs(const java::util::Uid& aUid,
       
   134         MidpStarterPushSupport* aAppStateController)
       
   135 
       
   136 {
       
   137     JELOG2(EJavaPush);
       
   138     updatePushRegs2(aUid,aAppStateController,false);
       
   139 }
       
   140 
       
   141 /**
       
   142  * This operation can not throw exceptions.
       
   143  * This operation is always called from comms thread so locks for mNumOfPendingUpdates
       
   144  * datamember is not needed. Value of mNumOfPendingUpdates datamember is increased if
       
   145  * previous updating is ongoing when next update request arrives.
       
   146  */
       
   147 void PushControllerStarterImpl::updatePushRegs2(const java::util::Uid& aUid,
       
   148         MidpStarterPushSupport* aAppStateController,
       
   149         bool aThrowingExceptionAllowed)
       
   150 {
       
   151     JELOG2(EJavaPush);
       
   152 
       
   153     try
       
   154     {
       
   155         if (LOADING_PUSH_CONNS == getState())
       
   156         {
       
   157             ++mNumOfPendingUpdates;
       
   158             return;
       
   159         }
       
   160 
       
   161         registerCommsListener();
       
   162         mUid = aUid;
       
   163         mAppStateController = aAppStateController;
       
   164         mIsUpdate = true;
       
   165         sendGetAllPushRegsMsg(aThrowingExceptionAllowed);
       
   166         ++mNumOfPendingUpdates;
       
   167     }
       
   168     catch (...)
       
   169     {
       
   170         ELOG(EJavaPush,"ERROR!!! updatePushRegs2(): Unexpected exception was caught");
       
   171         if (true == aThrowingExceptionAllowed)
       
   172             throw;
       
   173     }
       
   174 }
       
   175 
       
   176 /**
       
   177  *
       
   178  */
       
   179 void PushControllerStarterImpl::close()
       
   180 {
       
   181     JELOG2(EJavaPush);
       
   182     try
       
   183     {
       
   184         //Destructor of PushServerConnPluginManager class calls
       
   185         //ServerConnectionFactory::closeAllConnections() operation for each plugin.
       
   186         delete mSrvConnPluginMgr.release();
       
   187         setState(INITIAL_STATE);
       
   188     }
       
   189     catch (...)
       
   190     {
       
   191         ELOG(EJavaPush,"ERROR!!! PushControllerStarterImpl::close(): Unexpected exception was caught");
       
   192     }
       
   193 }
       
   194 
       
   195 /**
       
   196  *
       
   197  */
       
   198 void PushControllerStarterImpl::msgArrived(const std::wstring& aInfo)
       
   199 {
       
   200     JELOG2(EJavaPush);
       
   201 
       
   202     if (0 != mAppStateController)
       
   203     {
       
   204         mAppStateController->startMidletRequestFromPush(aInfo);
       
   205     }
       
   206 }
       
   207 
       
   208 /**
       
   209  *
       
   210  */
       
   211 void PushControllerStarterImpl::error(const std::wstring& aUri,int aErrCode,
       
   212                                       const std::string& aErrText)
       
   213 {
       
   214     JELOG2(EJavaPush);
       
   215 
       
   216     ELOG2(EJavaPush, "ERROR!!! err code: %d, err txt: %s",aErrCode,aErrText.c_str());
       
   217     int numOfSuccessfulConns = getPushServerConnPluginManager().setConnFailed(aUri);
       
   218     if (0 == numOfSuccessfulConns)
       
   219     {
       
   220         sendAllConnsFailedMsg();
       
   221         closeRuntime();
       
   222     }
       
   223 }
       
   224 
       
   225 /**
       
   226  *
       
   227  */
       
   228 void PushControllerStarterImpl::processMessage(CommsMessage& aMessage)
       
   229 {
       
   230     JELOG2(EJavaPush);
       
   231 
       
   232     switch (aMessage.getMessageId())
       
   233     {
       
   234     case IPC_PR_LIST_OF_ALL_PUSH_REGS:
       
   235     {
       
   236         handleListOfAllPushRegistrationsMsg(aMessage);
       
   237         break;
       
   238     }
       
   239     case IPC_PR_CONN_MANAGED_BY_OTHER_MIDLET:
       
   240     {
       
   241         mConnCompletedMonitor->notify();
       
   242         break;
       
   243     }
       
   244     default:
       
   245         ELOG1(EJavaPush, "ERROR!!! Unsupported method arrives to processMessage(): %d",
       
   246               aMessage.getMessageId());
       
   247     }//end switch
       
   248 }
       
   249 
       
   250 /**
       
   251  *
       
   252  */
       
   253 bool PushControllerStarterImpl::sendGetAllPushRegsMsg(bool aThrowingExceptionAllowed)
       
   254 {
       
   255     JELOG2(EJavaPush);
       
   256 
       
   257     CommsMessage getAllPushRegsMsg;
       
   258     getAllPushRegsMsg.setReceiver(IPC_ADDRESS_JAVA_CAPTAIN_C);
       
   259     getAllPushRegsMsg.setSender(IPC_ADDRESS_JAVA_CAPTAIN_C);
       
   260     getAllPushRegsMsg.setModuleId(PLUGIN_ID_PUSH_REGISTRY_C);
       
   261     getAllPushRegsMsg.setMessageId(IPC_PR_GET_ALL_PUSH_REGS);
       
   262 
       
   263     getAllPushRegsMsg << mUid;
       
   264     CommsClientEndpoint& comms = getComms();
       
   265     EState stateOfObj = getState();
       
   266     setState(LOADING_PUSH_CONNS);
       
   267     int retValue = comms.send(getAllPushRegsMsg);
       
   268     if (0 != retValue)
       
   269     {
       
   270         setState(stateOfObj);
       
   271         std::string errTxt("Sending a comms request failed: ");
       
   272         errTxt.append(java::util::JavaCommonUtils::intToString(retValue));
       
   273         ELOG1(EJavaPush, "ERROR!!! %s",errTxt.c_str());
       
   274         if (false == aThrowingExceptionAllowed)
       
   275         {
       
   276             sendAllConnsFailedMsg();
       
   277             closeRuntime();
       
   278             return false;
       
   279         }
       
   280         throw PushException(GENERAL_COMMS_ERROR,errTxt,__FILE__,__FUNCTION__,__LINE__);
       
   281     }
       
   282     //setState(LOADING_PUSH_CONNS);
       
   283     return true;
       
   284 }
       
   285 
       
   286 /**
       
   287  *
       
   288  */
       
   289 void PushControllerStarterImpl::handleListOfAllPushRegistrationsMsg(CommsMessage& aMessage)
       
   290 {
       
   291     JELOG2(EJavaPush);
       
   292 
       
   293     try
       
   294     {
       
   295         //std::string tmpErrTxt("Retrieving push registrations failed");
       
   296         validateCommonParamsOfReplyCommsMsg(0,IPC_PR_LIST_OF_ALL_PUSH_REGS,
       
   297                                             aMessage,mUid);
       
   298         std::wstring uriStr;
       
   299         std::wstring filterStr;
       
   300         PushServerConnPluginManager::UriList_t uriList;
       
   301         while ((aMessage >> uriStr))
       
   302         {
       
   303             if (!(aMessage >> filterStr))
       
   304             {
       
   305                 throw PushException(INCORRECT_COMMS_MSG,"Could not resolve 'filter' parameter",
       
   306                                     __FILE__,__FUNCTION__,__LINE__);
       
   307             }
       
   308             PushRegData tmpObj(uriStr,filterStr);
       
   309             uriList.push_back(tmpObj);
       
   310         }
       
   311 
       
   312         if (false == mIsUpdate)
       
   313             getPushServerConnPluginManager().startListen(uriList,false,true);
       
   314         else
       
   315         {
       
   316             getPushServerConnPluginManager().updatePushRegs(uriList,*this);
       
   317             //In this case updatePushRegs() has called closeRuntime() operation
       
   318             //so no sense to retrieve new updates.
       
   319             if (INITIAL_STATE == getState())
       
   320                 return;
       
   321 
       
   322             if (1 < mNumOfPendingUpdates)
       
   323             {
       
   324                 --mNumOfPendingUpdates;
       
   325                 sendGetAllPushRegsMsg(false);
       
   326                 return;
       
   327             }
       
   328             else if (1 == mNumOfPendingUpdates)
       
   329                 --mNumOfPendingUpdates;
       
   330         }//end else
       
   331 
       
   332         setState(PUSH_CONNS_LOADED);
       
   333     }
       
   334     catch (PushException& ex)
       
   335     {
       
   336         setState(INITIAL_STATE);
       
   337         ELOG1(EJavaPush, "ERROR!!! PushControllerStarterImpl::getAndInitAllPushRegs(): %s",
       
   338               ex.toString().c_str());
       
   339         //"All connection has failed" message is sent to pushregistryplugin if any error occurs
       
   340         //when push connections are retrieved. This prevents infinite loop if pushregistryplugin
       
   341         //has problems to provide push connections.
       
   342         sendAllConnsFailedMsg();
       
   343         closeRuntime();
       
   344 
       
   345     }
       
   346     catch (...)
       
   347     {
       
   348         setState(INITIAL_STATE);
       
   349         ELOG(EJavaPush,"ERROR!!! getAndInitAllPushRegs(): Unexpected exception was caught");
       
   350         sendAllConnsFailedMsg();
       
   351         closeRuntime();
       
   352     }
       
   353     mIsUpdate = false;
       
   354     mMonitor->notify();
       
   355 }
       
   356 
       
   357 /**
       
   358  *
       
   359  */
       
   360 void PushControllerStarterImpl::registerDynamicPushConn(const std::wstring& aUri,
       
   361         const std::wstring& aMidlet,
       
   362         const std::wstring& aFilter,
       
   363         const Uid& aUid,
       
   364         bool aIsUidOfThisMidlet)
       
   365 {
       
   366     JELOG2(EJavaPush);
       
   367 
       
   368     bool pushSrvConnObjectCreated = false;
       
   369     try
       
   370     {
       
   371         int isUidOfThisMidlet = 0;
       
   372         if (true == aIsUidOfThisMidlet)
       
   373         {
       
   374             loadPushConns(aUid);
       
   375             isUidOfThisMidlet = 1;
       
   376             PushRegData tmpRegData(aUri,aFilter);
       
   377             PushServerConnPluginManager::UriList_t pushList;
       
   378             pushList.push_back(tmpRegData);
       
   379             getPushServerConnPluginManager().startListen(pushList,true,false);
       
   380             pushSrvConnObjectCreated = true;
       
   381         }
       
   382 
       
   383         if (false == aIsUidOfThisMidlet)
       
   384         {
       
   385             registerCommsListener();
       
   386             //We have to open a connection in this situation to be sure that connection
       
   387             //is not already reserved. Connection is closed immediately.
       
   388             if (false == getPushServerConnPluginManager().isValidPushUri(aUri))
       
   389             {
       
   390                 throw PushException(PUSH_CONNECTION_ALREADY_EXISTS,
       
   391                                     "Connection is reserved by another application",
       
   392                                     __FILE__,__FUNCTION__,__LINE__);
       
   393             }
       
   394         }
       
   395 
       
   396         sendRegDynPushConnMsg(aUri,aMidlet,aFilter,aUid,isUidOfThisMidlet);
       
   397 
       
   398         if (false == aIsUidOfThisMidlet)
       
   399         {
       
   400             startToWaitCompletionOfOper();
       
   401         }
       
   402     }
       
   403     catch (PushException& ex)
       
   404     {
       
   405         ELOG1(EJavaPush, "ERROR!!! PushControllerStarterImpl::registerDynamicPushConn(): %s",
       
   406               ex.toString().c_str());
       
   407         if (true == pushSrvConnObjectCreated)
       
   408         {
       
   409             //ScopedLock is not needed in this situation.
       
   410             getPushServerConnPluginManager().deletePushServerConn(aUri);
       
   411         }
       
   412         throw;
       
   413     }
       
   414     catch (java::util::LibraryLoaderException& ex)
       
   415     {
       
   416         ELOG1(EJavaPush, "ERROR!!! PushControllerStarterImpl::registerDynamicPushConn(): %s",
       
   417               ex.toString().c_str());
       
   418         throw;
       
   419     }
       
   420     catch (...)
       
   421     {
       
   422         ELOG(EJavaPush,"ERROR!!! Unexpected exception was caught");
       
   423         if (true == pushSrvConnObjectCreated)
       
   424             getPushServerConnPluginManager().deletePushServerConn(aUri);
       
   425         throw java::util::ExceptionBase("Unexpected error occurred",__FILE__,__FUNCTION__,__LINE__);
       
   426     }
       
   427 }
       
   428 
       
   429 /**
       
   430  *
       
   431  */
       
   432 void PushControllerStarterImpl::listConnections(bool aAvailable,const java::util::Uid& aUid,
       
   433         std::list<std::wstring>& aUriList)
       
   434 {
       
   435     JELOG2(EJavaPush);
       
   436 
       
   437     //loadPushConns(aUid);
       
   438     //mSrvConnPluginMgr->listConnections(aAvailable,aUriList);
       
   439 
       
   440     int available = 0;
       
   441     if (true == aAvailable)
       
   442         available = 1;
       
   443 
       
   444     CommsMessage getPushConns;
       
   445     getPushConns.setReceiver(IPC_ADDRESS_JAVA_CAPTAIN_C);
       
   446     getPushConns.setSender(IPC_ADDRESS_JAVA_CAPTAIN_C);
       
   447     getPushConns.setModuleId(PLUGIN_ID_PUSH_REGISTRY_C);
       
   448     getPushConns.setMessageId(IPC_PR_LIST_CONNECTIONS);
       
   449     getPushConns << aUid;
       
   450     getPushConns << available;
       
   451     CommsClientEndpoint& comms = getComms();
       
   452     CommsMessage replyMsgToListConns;
       
   453     int retValue = comms.sendReceive(getPushConns,replyMsgToListConns,COMMS_TIMEOUT);
       
   454     //std::string tmpErrTxt("Retrieving push URIs failed");
       
   455     validateCommonParamsOfReplyCommsMsg(retValue,IPC_PR_LIST_CONNECTIONS_REPLY,
       
   456                                         replyMsgToListConns,aUid);
       
   457 
       
   458     std::wstring uriStr;
       
   459     while ((replyMsgToListConns >> uriStr))
       
   460     {
       
   461         aUriList.push_back(uriStr);
       
   462     }
       
   463 }
       
   464 
       
   465 /**
       
   466  *
       
   467  */
       
   468 std::wstring PushControllerStarterImpl::getFilter(const std::wstring& aUri,
       
   469         const java::util::Uid& aSuiteUid)
       
   470 {
       
   471     JELOG2(EJavaPush);
       
   472 
       
   473     //loadPushConns(aUid);
       
   474     //ServerConnection& conn = mSrvConnPluginMgr->getPushConnection(aUri);
       
   475     //return conn.getFilter();
       
   476 
       
   477     return sendPushDataQuery(aUri,aSuiteUid,FILTER_OF_PUSH_CONN);
       
   478 }
       
   479 
       
   480 /**
       
   481  *
       
   482  */
       
   483 std::wstring PushControllerStarterImpl::getClassNameOfMidlet(const std::wstring& aUri,
       
   484         const java::util::Uid& aSuiteUid)
       
   485 {
       
   486     JELOG2(EJavaPush);
       
   487 
       
   488     return sendPushDataQuery(aUri,aSuiteUid,CLASSNAME_OF_PUSH_CONN);
       
   489 }
       
   490 
       
   491 /**
       
   492  *
       
   493  */
       
   494 bool PushControllerStarterImpl::isPushConnection(const std::wstring& aUri,
       
   495         const java::util::Uid& aUid)
       
   496 {
       
   497     JELOG2(EJavaPush);
       
   498 
       
   499     try
       
   500     {
       
   501         loadPushConns(aUid);
       
   502         getPushServerConnPluginManager().getPushConnection(aUri);
       
   503         return true;
       
   504     }
       
   505     catch (...)
       
   506     {
       
   507         ELOG(EJavaPush,"ERROR!!! Exception was caught");
       
   508         return false;
       
   509     }
       
   510 }
       
   511 
       
   512 /**
       
   513  *
       
   514  */
       
   515 void PushControllerStarterImpl::unregisterDynamicPushConn(const std::wstring& aUri,
       
   516         const Uid& aUid,
       
   517         bool aIsUidOfThisMidlet)
       
   518 {
       
   519     JELOG2(EJavaPush);
       
   520 
       
   521     int isUidOfThisMidlet = 0;
       
   522     if (true == aIsUidOfThisMidlet)
       
   523     {
       
   524         loadPushConns(aUid);
       
   525         isUidOfThisMidlet = 1;
       
   526     }
       
   527     else
       
   528     {
       
   529         registerCommsListener();
       
   530     }
       
   531 
       
   532     CommsMessage regDynPushRegMsg;
       
   533     regDynPushRegMsg.setReceiver(IPC_ADDRESS_JAVA_CAPTAIN_C);
       
   534     regDynPushRegMsg.setSender(IPC_ADDRESS_JAVA_CAPTAIN_C);
       
   535     regDynPushRegMsg.setModuleId(PLUGIN_ID_PUSH_REGISTRY_C);
       
   536     regDynPushRegMsg.setMessageId(IPC_PR_UNREGISTER_DYNAMIC_PUSH_CONN);
       
   537     regDynPushRegMsg << aUid;
       
   538     regDynPushRegMsg << aUri;
       
   539     regDynPushRegMsg << isUidOfThisMidlet;
       
   540     CommsClientEndpoint& comms = getComms();
       
   541     CommsMessage statusOfUnRegDynConnMsg;
       
   542     int retValue = comms.sendReceive(regDynPushRegMsg,statusOfUnRegDynConnMsg,COMMS_TIMEOUT);
       
   543     //std::string tmpErrTxt("Unegistering a push connection failed");
       
   544     validateCommonParamsOfReplyCommsMsg(retValue,IPC_PR_UNREGISTER_DYNAMIC_PUSH_CONN_REPLY,
       
   545                                         statusOfUnRegDynConnMsg,aUid);
       
   546 
       
   547     //Unregister dynamic push connection from ServerConnection Plugin if it belongs to 'this'
       
   548     //MIDlet.
       
   549     if (true == aIsUidOfThisMidlet)
       
   550         getPushServerConnPluginManager().unregisterDynamicPushConn(aUri);
       
   551     else
       
   552     {
       
   553         startToWaitCompletionOfOper();
       
   554     }
       
   555 }
       
   556 
       
   557 /**
       
   558  *
       
   559  */
       
   560 long long PushControllerStarterImpl::registerAlarm(const long long& aAlarmTimeInMilliSecs,
       
   561         const Uid& aSuiteUid,
       
   562         const std::wstring& aClassNameOfMidlet,
       
   563         bool aIsUidOfThisMidlet)
       
   564 {
       
   565     JELOG2(EJavaPush);
       
   566 
       
   567     LOG1WSTR(EJavaPush,EInfo, "registerAlarm() aClassNameOfMidlet: %s",aClassNameOfMidlet);
       
   568 
       
   569     int isUidOfThisMidlet = 0;
       
   570     if (true == aIsUidOfThisMidlet)
       
   571         isUidOfThisMidlet = 1;
       
   572 
       
   573     CommsMessage regAlarmMsg;
       
   574     regAlarmMsg.setReceiver(IPC_ADDRESS_JAVA_CAPTAIN_C);
       
   575     regAlarmMsg.setSender(IPC_ADDRESS_JAVA_CAPTAIN_C);
       
   576     regAlarmMsg.setModuleId(PLUGIN_ID_PUSH_REGISTRY_C);
       
   577     regAlarmMsg.setMessageId(IPC_PR_REGISTER_ALARM);
       
   578     regAlarmMsg << aSuiteUid;
       
   579     regAlarmMsg << aAlarmTimeInMilliSecs;
       
   580     regAlarmMsg << aClassNameOfMidlet;
       
   581     regAlarmMsg << isUidOfThisMidlet;
       
   582 
       
   583     CommsClientEndpoint& comms = getComms();
       
   584     CommsMessage statusOfRegAlarmMsg;
       
   585     int retValue = comms.sendReceive(regAlarmMsg,statusOfRegAlarmMsg,COMMS_TIMEOUT);
       
   586     //std::string tmpErrTxt("Setting an alarm failed");
       
   587     validateCommonParamsOfReplyCommsMsg(retValue,IPC_PR_REGISTER_ALARM_REPLY,statusOfRegAlarmMsg,
       
   588                                         aSuiteUid);
       
   589 
       
   590     long long retTime = 0LL;
       
   591     if (!(statusOfRegAlarmMsg >> retTime))
       
   592     {
       
   593         std::string errTxt("Cannot resolve 'time' from the IPC_PR_REGISTER_ALARM_REPLY msg");
       
   594         throw PushException(INCORRECT_COMMS_MSG,errTxt,__FILE__,__FUNCTION__,__LINE__);
       
   595     }
       
   596 
       
   597     return retTime;
       
   598 }
       
   599 
       
   600 /**
       
   601  * Note: This operation ignores all errors.
       
   602  */
       
   603 void PushControllerStarterImpl::sendAllConnsFailedMsg()
       
   604 {
       
   605     JELOG2(EJavaPush);
       
   606 
       
   607     try
       
   608     {
       
   609         CommsMessage allConnsFailedMsg;
       
   610         allConnsFailedMsg.setReceiver(IPC_ADDRESS_JAVA_CAPTAIN_C);
       
   611         allConnsFailedMsg.setSender(IPC_ADDRESS_JAVA_CAPTAIN_C);
       
   612         allConnsFailedMsg.setModuleId(PLUGIN_ID_PUSH_REGISTRY_C);
       
   613         allConnsFailedMsg.setMessageId(IPC_PR_ALL_CONNS_FAILED);
       
   614         allConnsFailedMsg << mUid;
       
   615         CommsClientEndpoint& comms = getComms();
       
   616         int retValue = comms.send(allConnsFailedMsg);
       
   617         if (0 != retValue)
       
   618         {
       
   619             std::string errTxt("sendAllConnsFailedMsg() Sending a comms request failed: ");
       
   620             errTxt.append(java::util::JavaCommonUtils::intToString(retValue));
       
   621             ELOG1(EJavaPush, "ERROR!!! %s",errTxt.c_str());
       
   622         }
       
   623     }
       
   624     catch (...)
       
   625     {
       
   626         ELOG(EJavaPush, "ERROR!!! Unexpected exception caught in the PushControllerStarterImpl::sendAllConnsFailedMsg()");
       
   627     }
       
   628 }
       
   629 
       
   630 /**
       
   631  *
       
   632  */
       
   633 void PushControllerStarterImpl::closeRuntime()
       
   634 {
       
   635     JELOG2(EJavaPush);
       
   636 
       
   637     if (0 != mAppStateController)
       
   638     {
       
   639         mAppStateController->closeRuntimeRequestFromPush();
       
   640         setState(INITIAL_STATE);
       
   641     }
       
   642 }
       
   643 
       
   644 /**
       
   645  *
       
   646  */
       
   647 void PushControllerStarterImpl::setPendingStatus(const std::wstring& aUri,
       
   648         bool aMsgWaitsHandling)
       
   649 {
       
   650     JELOG2(EJavaPush);
       
   651 
       
   652     try
       
   653     {
       
   654         int msgWaitsHandling = 0;
       
   655         if (true == aMsgWaitsHandling)
       
   656             msgWaitsHandling = 1;
       
   657 
       
   658         CommsMessage pendingStatusMsg;
       
   659         pendingStatusMsg.setReceiver(IPC_ADDRESS_JAVA_CAPTAIN_C);
       
   660         pendingStatusMsg.setSender(IPC_ADDRESS_JAVA_CAPTAIN_C);
       
   661         pendingStatusMsg.setModuleId(PLUGIN_ID_PUSH_REGISTRY_C);
       
   662         pendingStatusMsg.setMessageId(IPC_PR_PENDING_STATUS);
       
   663         pendingStatusMsg << mUid;
       
   664         pendingStatusMsg << aUri;
       
   665         pendingStatusMsg << msgWaitsHandling;
       
   666         CommsClientEndpoint& comms = getComms();
       
   667         int retValue = comms.send(pendingStatusMsg);
       
   668         if (0 != retValue)
       
   669         {
       
   670             std::string errTxt("setPendingStatus(): Sending a comms request failed: ");
       
   671             errTxt.append(java::util::JavaCommonUtils::intToString(retValue));
       
   672             ELOG1(EJavaPush, "ERROR!!! %s",errTxt.c_str());
       
   673         }
       
   674     }
       
   675     catch (...)
       
   676     {
       
   677         ELOG(EJavaPush, "ERROR!!! Unexpected exception caught in the PushControllerStarterImpl::setPendingStatus()");
       
   678     }
       
   679 }
       
   680 
       
   681 /**
       
   682  *
       
   683  */
       
   684 void PushControllerStarterImpl::connCompleted(const wstring& aUri)
       
   685 {
       
   686     JELOG2(EJavaPush);
       
   687 
       
   688     try
       
   689     {
       
   690         CommsMessage unRegCompletedMsg;
       
   691         unRegCompletedMsg.setReceiver(IPC_ADDRESS_JAVA_CAPTAIN_C);
       
   692         unRegCompletedMsg.setSender(IPC_ADDRESS_JAVA_CAPTAIN_C);
       
   693         unRegCompletedMsg.setModuleId(PLUGIN_ID_PUSH_REGISTRY_C);
       
   694         unRegCompletedMsg.setMessageId(IPC_PR_CONN_COMPLETED);
       
   695         unRegCompletedMsg << aUri;
       
   696         CommsClientEndpoint& comms = getComms();
       
   697         int retValue = comms.send(unRegCompletedMsg);
       
   698         if (0 != retValue)
       
   699         {
       
   700             std::string errTxt("connCompleted(): Sending a comms request failed: ");
       
   701             errTxt.append(java::util::JavaCommonUtils::intToString(retValue));
       
   702             ELOG1(EJavaPush, "ERROR!!! %s",errTxt.c_str());
       
   703         }
       
   704     }
       
   705     catch (...)
       
   706     {
       
   707         ELOG(EJavaPush,
       
   708              "ERROR!!! Unexpected exception caught in the PushControllerStarterImpl::unregCompleted()");
       
   709     }
       
   710 }
       
   711 
       
   712 /**
       
   713  *
       
   714  */
       
   715 CommsClientEndpoint& PushControllerStarterImpl::getComms()
       
   716 {
       
   717     JELOG2(EJavaPush);
       
   718 
       
   719     CommsClientEndpoint* commsObj = CommsClientEndpoint::find(L"javacaptain");
       
   720     if (0 == commsObj)
       
   721     {
       
   722         throw PushException(GENERAL_COMMS_ERROR,"Retrieving CommsClientEndpoint object failed",
       
   723                             __FILE__,__FUNCTION__,__LINE__);
       
   724     }
       
   725     return *commsObj;
       
   726 }
       
   727 
       
   728 /**
       
   729  *
       
   730  */
       
   731 void PushControllerStarterImpl::validateUid(const Uid& aUid,CommsMessage& aMessage,
       
   732         const std::string& aFile,
       
   733         const std::string& aFunction,int aLine)
       
   734 {
       
   735     JELOG2(EJavaPush);
       
   736     Uid uid;
       
   737     if (!(aMessage >> uid))
       
   738     {
       
   739         std::string errTxt("Cannot resolve uid from the comms msg");
       
   740         throw PushException(INCORRECT_COMMS_MSG,errTxt,aFile.c_str(),aFunction.c_str(),aLine);
       
   741     }
       
   742     if (uid != aUid)
       
   743     {
       
   744         std::wstring errTxt(L"Push comms msg with incorrect uid arrived: ");
       
   745         errTxt.append(uid.toString());
       
   746         ELOG1WSTR(EJavaPush,"ERROR!!!", errTxt);
       
   747         throw PushException(INCORRECT_COMMS_MSG,errTxt,aFile,aFunction,aLine);
       
   748     }
       
   749 }
       
   750 
       
   751 /**
       
   752  *
       
   753  */
       
   754 void PushControllerStarterImpl::checkErrorParamsOfCommsMsg(CommsMessage& aMessage)
       
   755 {
       
   756     JELOG2(EJavaPush);
       
   757 
       
   758     int status = 0;
       
   759     if (!(aMessage >> status))
       
   760     {
       
   761         std::string errTxt("Cannot resolve status from the comms msg");
       
   762         throw PushException(INCORRECT_COMMS_MSG,errTxt,__FILE__,__FUNCTION__,__LINE__);
       
   763     }
       
   764 
       
   765     if (0 != status)
       
   766     {
       
   767         std::string errStatusText("");
       
   768         //Error check is not needed here.
       
   769         aMessage >> errStatusText;
       
   770 
       
   771         //Exception text already contains error code.
       
   772         throw PushException(0,errStatusText,__FILE__,__FUNCTION__,__LINE__);
       
   773     }
       
   774 
       
   775     //Error text is skipped in the success case.
       
   776     std::string errTxt;
       
   777     aMessage >> errTxt;
       
   778 }
       
   779 
       
   780 /**
       
   781  *
       
   782  */
       
   783 void PushControllerStarterImpl::validateCommonParamsOfReplyCommsMsg
       
   784 (int aRetValue,int aMsgId,CommsMessage& aMsg,const Uid& aUid)
       
   785 {
       
   786     JELOG2(EJavaPush);
       
   787 
       
   788     if (0 != aRetValue)
       
   789     {
       
   790         std::string errTxt("Sending a comms request failed: ");
       
   791         errTxt.append(java::util::JavaCommonUtils::intToString(aRetValue));
       
   792         ELOG1(EJavaPush, "ERROR!!! %s",errTxt.c_str());
       
   793         throw PushException(GENERAL_COMMS_ERROR,errTxt,__FILE__,__FUNCTION__,__LINE__);
       
   794     }
       
   795 
       
   796     if (aMsgId != aMsg.getMessageId())
       
   797     {
       
   798         std::string errTxt("Incorrect message arrived: ");
       
   799         errTxt.append(java::util::JavaCommonUtils::intToString(aMsg.getMessageId()));
       
   800         ELOG1(EJavaPush, "ERROR!!! %s",errTxt.c_str());
       
   801         throw PushException(GENERAL_COMMS_ERROR,errTxt,__FILE__,__FUNCTION__,__LINE__);
       
   802     }
       
   803 
       
   804     validateUid(aUid,aMsg,__FILE__,__FUNCTION__,__LINE__);
       
   805     checkErrorParamsOfCommsMsg(aMsg);
       
   806 }
       
   807 
       
   808 /**
       
   809  * We have to call this operation in each methods of PushRegistry interface
       
   810  * because it might be that push connections are not retrieved before MIDlet
       
   811  * calls methods of PushRegistry. Other case is that all existing connections are
       
   812  * failed before a new push connection is registered to teh MIDlet. In this case push
       
   813  * connections must be retrieved from java captain.
       
   814  */
       
   815 void PushControllerStarterImpl::loadPushConns(const java::util::Uid& aUid)
       
   816 {
       
   817     JELOG2(EJavaPush);
       
   818 
       
   819     EState state = getState();
       
   820     if (INITIAL_STATE == state)
       
   821     {
       
   822         updatePushRegs2(aUid,mAppStateController,true);
       
   823         mMonitor->wait();
       
   824     }
       
   825     else if (LOADING_PUSH_CONNS == state)
       
   826     {
       
   827         mMonitor->wait();
       
   828     }
       
   829 }
       
   830 
       
   831 /**
       
   832  *
       
   833  */
       
   834 std::wstring PushControllerStarterImpl::sendPushDataQuery(const std::wstring& aUri,
       
   835         const java::util::Uid& aSuiteUid,
       
   836         const EPushInfoQueryParams& aQueryCondition)
       
   837 {
       
   838     JELOG2(EJavaPush);
       
   839 
       
   840     CommsMessage pushQuery;
       
   841     pushQuery.setReceiver(IPC_ADDRESS_JAVA_CAPTAIN_C);
       
   842     pushQuery.setSender(IPC_ADDRESS_JAVA_CAPTAIN_C);
       
   843     pushQuery.setModuleId(PLUGIN_ID_PUSH_REGISTRY_C);
       
   844     pushQuery.setMessageId(IPC_PR_DATA_OF_PUSH_CONN_QUERY);
       
   845     pushQuery << aSuiteUid;
       
   846     pushQuery << aUri;
       
   847     pushQuery << (int) aQueryCondition;
       
   848     CommsClientEndpoint& comms = getComms();
       
   849     CommsMessage statusOfPushQueryMsg;
       
   850     int retValue = comms.sendReceive(pushQuery,statusOfPushQueryMsg,COMMS_TIMEOUT);
       
   851     //std::string tmpErrTxt("Retrieving data of the push connection failed");
       
   852     validateCommonParamsOfReplyCommsMsg(retValue,IPC_PR_DATA_OF_PUSH_CONN_QUERY_REPLY,
       
   853                                         statusOfPushQueryMsg,aSuiteUid);
       
   854 
       
   855     std::wstring returnedStr;
       
   856     if (!(statusOfPushQueryMsg >> returnedStr))
       
   857     {
       
   858         std::string errTxt("Cannot resolve 'filter' from the IPC_PR_DATA_OF_PUSH_CONN_QUERY_REPLY msg");
       
   859         throw PushException(INCORRECT_COMMS_MSG,errTxt,__FILE__,__FUNCTION__,__LINE__);
       
   860     }
       
   861     return returnedStr;
       
   862 }
       
   863 
       
   864 /**
       
   865  *
       
   866  */
       
   867 void PushControllerStarterImpl::sendRegDynPushConnMsg(const std::wstring& aUri,
       
   868         const std::wstring& aMidlet,
       
   869         const std::wstring& aFilter,
       
   870         const java::util::Uid& aUid,
       
   871         int aIsUidOfThisMidlet)
       
   872 {
       
   873     JELOG2(EJavaPush);
       
   874 
       
   875     CommsMessage regDynPushRegMsg;
       
   876     regDynPushRegMsg.setReceiver(IPC_ADDRESS_JAVA_CAPTAIN_C);
       
   877     regDynPushRegMsg.setSender(IPC_ADDRESS_JAVA_CAPTAIN_C);
       
   878     regDynPushRegMsg.setModuleId(PLUGIN_ID_PUSH_REGISTRY_C);
       
   879     regDynPushRegMsg.setMessageId(IPC_PR_REGISTER_DYNAMIC_PUSH_CONN);
       
   880     regDynPushRegMsg << aUid;
       
   881     regDynPushRegMsg << aUri;
       
   882     regDynPushRegMsg << aMidlet;
       
   883     regDynPushRegMsg << aFilter;
       
   884     regDynPushRegMsg << aIsUidOfThisMidlet;
       
   885     CommsClientEndpoint& comms = getComms();
       
   886     CommsMessage statusOfDynRegMsg;
       
   887     int retValue = comms.sendReceive(regDynPushRegMsg,statusOfDynRegMsg,COMMS_TIMEOUT);
       
   888     //std::string tmpErrTxt("Registering a push connection failed");
       
   889     validateCommonParamsOfReplyCommsMsg(retValue,IPC_PR_REGISTER_DYNAMIC_PUSH_CONN_REPLY,
       
   890                                         statusOfDynRegMsg,aUid);
       
   891 }
       
   892 
       
   893 /**
       
   894  *
       
   895  */
       
   896 PushServerConnPluginManager& PushControllerStarterImpl::getPushServerConnPluginManager()
       
   897 {
       
   898     if (0 == mSrvConnPluginMgr.get())
       
   899         mSrvConnPluginMgr.reset(new PushServerConnPluginManager(this,this,this));
       
   900     return *mSrvConnPluginMgr.get();
       
   901 }
       
   902 
       
   903 /**
       
   904  *
       
   905  */
       
   906 void PushControllerStarterImpl::setState(const EState& aState)
       
   907 {
       
   908     JELOG2(EJavaPush);
       
   909     ScopedLock lockObj(mMutex);
       
   910     mState = aState;
       
   911 }
       
   912 
       
   913 /**
       
   914  *
       
   915  */
       
   916 PushControllerStarterImpl::EState PushControllerStarterImpl::getState() const
       
   917 {
       
   918     JELOG2(EJavaPush);
       
   919     ScopedLock lockObj(mMutex);
       
   920     return mState;
       
   921 }
       
   922 
       
   923 /**
       
   924  *
       
   925  */
       
   926 void PushControllerStarterImpl::registerCommsListener()
       
   927 {
       
   928     CommsClientEndpoint& comms = getComms();
       
   929     int retValue = comms.registerListener(PLUGIN_ID_PUSH_CONTROLLER_C,this);
       
   930     if ((0 != retValue) && (retValue != EEXIST))
       
   931     {
       
   932         std::string errTxt("Registering to comms failed: ");
       
   933         errTxt.append(java::util::JavaCommonUtils::intToString(retValue));
       
   934         ELOG1(EJavaPush, "ERROR!!! %s",errTxt.c_str());
       
   935         throw PushException(GENERAL_COMMS_ERROR,errTxt,__FILE__,__FUNCTION__,__LINE__);
       
   936     }
       
   937 }
       
   938 
       
   939 /**
       
   940  *
       
   941  */
       
   942 void PushControllerStarterImpl::startToWaitCompletionOfOper()
       
   943 {
       
   944     JELOG2(EJavaPush);
       
   945 
       
   946     if (0 == mConnCompletedMonitor.get())
       
   947         mConnCompletedMonitor.reset(Monitor::createMonitor());
       
   948     mConnCompletedMonitor->wait();
       
   949 }
       
   950