javatools/javaappconverter/src.s60/convertserver.cpp
branchRCL_3
changeset 18 9ac0a0a7da70
parent 17 0fd27995241b
child 19 71c436fe3ce0
equal deleted inserted replaced
17:0fd27995241b 18:9ac0a0a7da70
     1 /*
       
     2 * Copyright (c) 2008 - 2009 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:  Comms server, part of Java platform 2.0 javappconverter process
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include <iostream>
       
    20 #include <unistd.h>
       
    21 
       
    22 #include "comms.h"
       
    23 #include "logger.h"
       
    24 #include "javasymbianoslayer.h"
       
    25 #include "convertserver.h"
       
    26 
       
    27 using namespace java::comms;
       
    28 using namespace std;
       
    29 
       
    30 ConvertServer::ConvertServer()
       
    31 {
       
    32 }
       
    33 
       
    34 ConvertServer::~ConvertServer()
       
    35 {
       
    36     iInstallFiles.clear();
       
    37 }
       
    38 
       
    39 void ConvertServer::setInstallFiles(RPointerArray<HBufC> &aInstallFiles)
       
    40 {
       
    41     JELOG2(EJavaConverters);
       
    42 
       
    43     // file index will contain the number of valid install file urls
       
    44     iFileIndex = 0;
       
    45     // clear the old install file paths if any
       
    46     iInstallFiles.clear();
       
    47 
       
    48     int nPointers = aInstallFiles.Count();
       
    49     iInstallFiles.reserve(nPointers);
       
    50 
       
    51     // index is decremented because iFileIndex will also be decremented when
       
    52     // installing -> total effect is that install files will be given to
       
    53     // Java Installer in the original order
       
    54     for (int i = nPointers - 1; i > -1; i--)
       
    55     {
       
    56         HBufC* pBuf = aInstallFiles[i];
       
    57 
       
    58         if (NULL == pBuf)
       
    59         {
       
    60             // skip this NULL pointer
       
    61             continue;
       
    62         }
       
    63 
       
    64         TPtr16 ptrName(pBuf->Des());
       
    65         // Convert each HBufC to std:wstring before adding to iInstallFiles
       
    66         iInstallFiles.push_back(std::wstring(desToWstring(ptrName)));
       
    67         iFileIndex++;
       
    68     }
       
    69 
       
    70     LOG1(EJavaConverters, EInfo, "ConvertServer: number of install files = %d", iFileIndex);
       
    71     iFilesTotal = iFileIndex;
       
    72 }
       
    73 
       
    74 int ConvertServer::start()
       
    75 {
       
    76     LOG(EJavaConverters, EInfo,
       
    77         "ConvertServer:start called");
       
    78 
       
    79     iRunning = 1;
       
    80     iComms.registerDefaultListener(this);
       
    81     return iComms.start(IPC_ADDRESS_JAVA_APPCONVERTER_C);
       
    82 }
       
    83 
       
    84 int ConvertServer::stop()
       
    85 {
       
    86     if (iRunning > 0)
       
    87     {
       
    88         iRunning = 0;
       
    89         iComms.unregisterDefaultListener(this);
       
    90         return iComms.stop();
       
    91     }
       
    92     else
       
    93     {
       
    94         return 0;
       
    95     }
       
    96 }
       
    97 
       
    98 /**
       
    99  * Communicates with Java Installer. The following messages are used.
       
   100  *
       
   101  * Message Name        Id      Contents
       
   102  *
       
   103  * Get Next Operation  500     None
       
   104  *
       
   105  * Operation           501     int operation   (install = 0, uninstall = 1, exit = 2)
       
   106  *                             String url      (present if operation = install)
       
   107  *                             String uid      (present if operation = uninstall)
       
   108  *                             int current     (1..total, present if operation = install
       
   109  *                                              and caller is appconverter)
       
   110  *                             int total       (total number of apps to convert, present
       
   111  *                                              if operation = install and caller is appconverter)
       
   112  *
       
   113  * Operation Result    502     int operation   (install = 0, uninstall = 1, exit = 2)
       
   114  *                             int result      (ok = 0, error codes are negative)
       
   115  *                             int uidCount    (present if operation = install, value = uids.length)
       
   116  *                             String[] uids   (present if operation = install)
       
   117  */
       
   118 void ConvertServer::processMessage(CommsMessage& aMessage)
       
   119 {
       
   120     LOG1(EJavaConverters, EInfo, "ConvertServer::processMessage, sender    = %d", aMessage.getSender());
       
   121     LOG1(EJavaConverters, EInfo, "ConvertServer::processMessage, messageId = %d", aMessage.getMessageId());
       
   122     LOG1(EJavaConverters, EInfo, "ConvertServer::processMessage, moduleId  = %d", aMessage.getModuleId());
       
   123     LOG1(EJavaConverters, EInfo, "ConvertServer::processMessage, messageRef= %d", aMessage.getMessageRef());
       
   124 
       
   125     switch (aMessage.getMessageId())
       
   126     {
       
   127     case GET_NEXT_OPERATION_MESSAGE_ID:
       
   128     {
       
   129         // Java Installer asks for the next install operation.
       
   130         // Reply with 'Operation' message.
       
   131 
       
   132         CommsMessage reply;
       
   133         reply.replyTo(aMessage);
       
   134 
       
   135         reply.setMessageId(OPERATION_MESSAGE_ID);
       
   136 
       
   137         iFileIndex--;
       
   138         if (iFileIndex <= -1)
       
   139         {
       
   140             // all install files have been handled, ask Java Installer to exit
       
   141             reply << EXIT_OPERATION;
       
   142 
       
   143             // Java Installer sends reply to 'exit' message and then
       
   144             // it exits which wakes up javaappconverter main thread
       
   145         }
       
   146         else
       
   147         {
       
   148             // ask Java Installer to install next
       
   149             int current = (iFilesTotal - iFileIndex);
       
   150             reply << INSTALL_OPERATION << iInstallFiles[iFileIndex] << current << iFilesTotal;
       
   151         }
       
   152 
       
   153         int err = iComms.send(reply);
       
   154         if (err != 0)
       
   155         {
       
   156             ELOG1(EJavaConverters,
       
   157                   "ConvertServer: Sending reply to Java Installer failed, err %d", err);
       
   158             // Propably Java Installer has crashed and the execution of
       
   159             // CSilentMIDletConvert is already proceeding. This server will
       
   160             // be stopped when the execution reaches EExit state.
       
   161         }
       
   162     }
       
   163     break;
       
   164 
       
   165     case OPERATION_RESULT_MESSAGE_ID:
       
   166     {
       
   167         // Log the result of the operation. Javaappconverter does not need
       
   168         // the Uids of the installed Java Applications so they are not
       
   169         // parsed.
       
   170         int operation;
       
   171         int result;
       
   172 
       
   173         aMessage >> operation >> result;
       
   174 
       
   175         LOG2(EJavaConverters, EInfo,
       
   176              "ConvertServer: operation = %d, result = %d", operation, result);
       
   177 
       
   178         if (result < 0)
       
   179         {
       
   180             // Conversion failed.
       
   181             ELOG2(EJavaConverters, "ConvertServer: Converting (installing) "
       
   182                   "application number %d failed with code %d",
       
   183                   (iFilesTotal - iFileIndex), result);
       
   184         }
       
   185     }
       
   186     break;
       
   187 
       
   188     default:
       
   189     {
       
   190         // Unknown message. Ignore it.
       
   191         WLOG1(EJavaConverters,
       
   192               "ConvertServer: Unknown message received. Msg Id = %d",
       
   193               aMessage.getMessageId());
       
   194     }
       
   195     break;
       
   196     }
       
   197 }