javaextensions/wma/sms_cbs/src/smsconnection.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:
       
    15  *
       
    16 */
       
    17 
       
    18 
       
    19 #include "logger.h"
       
    20 #include "smsconnection.h"
       
    21 #include "smsserverconnectionfactory.h"
       
    22 #include "cbsserverconnectionfactory.h"
       
    23 
       
    24 using namespace java::push;
       
    25 namespace java
       
    26 {
       
    27 namespace wma
       
    28 {
       
    29 
       
    30 SmsConnection::SmsConnection(std::wstring aUri,bool aServerConnection)
       
    31         :mError(0),
       
    32         mUri(aUri),
       
    33         mMessagesOnQueue(0),
       
    34         mServerConnection(aServerConnection),
       
    35         mReceiveOperation(aServerConnection),
       
    36         mServerConn(0),
       
    37         mMsgConn(0),
       
    38         mMessageMonitor(0),
       
    39         mServerConnectionFactory(0)
       
    40 {
       
    41     JELOG2(EWMA);
       
    42 }
       
    43 
       
    44 void SmsConnection::initialize()
       
    45 {
       
    46     JELOG2(EWMA);
       
    47     mMsgConn = SmsPlatformService::getNewInstance(mUri);
       
    48     if (mServerConnection)
       
    49     {
       
    50         //Get the factory instance based on the protocol
       
    51         if (mUri.find(L"cbs://:") != std::wstring::npos)
       
    52         {
       
    53             mServerConnectionFactory =
       
    54                 &CbsServerConnectionFactory::getFactory();
       
    55         }
       
    56         else
       
    57         {
       
    58             mServerConnectionFactory =
       
    59                 &SmsServerConnectionFactory::getFactory();
       
    60         }
       
    61         mMessageMonitor = java::util::Monitor::createMonitor();
       
    62         //Create the server connection
       
    63         mServerConn = reinterpret_cast<ServerConnectionBase*>
       
    64                       (mServerConnectionFactory->create(mUri));
       
    65         mServerConn->open(this, true);
       
    66     }
       
    67 }
       
    68 
       
    69 int SmsConnection ::numberOfDataSegments(const int aType,const int aLength,
       
    70         const int aPort,const char* aHostAddress,const char* aData)
       
    71 {
       
    72     JELOG2(EWMA);
       
    73     return mMsgConn->getSmsDataSegments(aType, aLength, aPort,
       
    74                                         aHostAddress, aData);
       
    75 }
       
    76 
       
    77 int SmsConnection :: send()
       
    78 {
       
    79     JELOG2(EWMA);
       
    80     return mMsgConn->send();
       
    81 }
       
    82 
       
    83 SmsConnection :: ~SmsConnection()
       
    84 {
       
    85     JELOG2(EWMA);
       
    86     close();
       
    87     delete mMessageMonitor;
       
    88     delete mMsgConn;
       
    89 }
       
    90 
       
    91 void SmsConnection :: close()
       
    92 {
       
    93     JELOG2(EWMA);
       
    94     if (mServerConnection)
       
    95     {
       
    96         mReceiveOperation = false;
       
    97         if (0 != mMessageMonitor)
       
    98         {
       
    99             mMessageMonitor->notify();
       
   100         }
       
   101         if ((0 != mServerConnectionFactory) && (0 != mServerConn))
       
   102         {
       
   103             mServerConnectionFactory->releaseConnection(mUri);
       
   104             mServerConnectionFactory = 0;
       
   105             mServerConn = 0;
       
   106         }
       
   107     }
       
   108 }
       
   109 
       
   110 void SmsConnection :: msgArrived(const std::wstring&)
       
   111 {
       
   112     JELOG2(EWMA);
       
   113     mMessageMonitor->notify();
       
   114 }
       
   115 
       
   116 void SmsConnection :: error(const std::wstring& /*aUri*/,int aErrCode,
       
   117                             const std::string& /*aErrText*/)
       
   118 {
       
   119     JELOG2(EWMA);
       
   120     ELOG1(EWMA,"WMA : Error Receiving Message : %d",aErrCode);
       
   121     mError = aErrCode;
       
   122     mMessageMonitor->notify();
       
   123 }
       
   124 
       
   125 void SmsConnection :: open(JNIEnv& aJni, jobject aPeer)
       
   126 {
       
   127     JELOG2(EWMA);
       
   128     int status = 0;
       
   129     jclass sessionClass = aJni.FindClass(
       
   130                               "com/nokia/mj/impl/sms/SMSConnectionImpl");
       
   131     jmethodID MessageReceiveCallbackMethod = aJni.GetMethodID(sessionClass,
       
   132             "messageReceiveCallback", "(II)I");
       
   133     // Keeps notifying the java side about incoming messages till
       
   134     // the connection is closed
       
   135     while (mReceiveOperation && (0 == status))
       
   136     {
       
   137         //waits for the notification from server connection
       
   138         mMessageMonitor->wait();
       
   139         // check whether its a message notification or connection close
       
   140         // notification
       
   141         if (0 == mError && mReceiveOperation)
       
   142         {
       
   143             //Get no of messages available in store
       
   144             int messagesOnStore = mServerConn->getMessagesOnStore();
       
   145             //Calculate no of new messages to be notified to java
       
   146             int newMessages = messagesOnStore - mMessagesOnQueue;
       
   147             mMessagesOnQueue = messagesOnStore;
       
   148             LOG1(EWMA, EInfo , "SMS : %d New messages to be notified", newMessages);
       
   149             if (newMessages > 0)
       
   150             {
       
   151                 status = aJni.CallIntMethod(aPeer,MessageReceiveCallbackMethod,
       
   152                                             newMessages,0);
       
   153             }
       
   154         }
       
   155         else if (0 != mError)
       
   156         {
       
   157             ELOG1(EWMA, "WMA : Error while receiving %d", mError);
       
   158             //If there is an error report it to java
       
   159             status = aJni.CallIntMethod(aPeer,MessageReceiveCallbackMethod,0,mError);
       
   160             mError = 0;
       
   161         }
       
   162     }
       
   163 }
       
   164 
       
   165 int SmsConnection :: getMessageType()
       
   166 {
       
   167     JELOG2(EWMA);
       
   168     return mMsgConn->receivedMessageType(*mServerConn);
       
   169 }
       
   170 
       
   171 jlong SmsConnection :: getTimestamp()
       
   172 {
       
   173     JELOG2(EWMA);
       
   174     return mMsgConn->getTimeStamp();
       
   175 }
       
   176 
       
   177 jstring SmsConnection :: getRemoteHostAddress(JNIEnv& aJni)
       
   178 {
       
   179     JELOG2(EWMA);
       
   180     return mMsgConn->getHostAddress(aJni);
       
   181 }
       
   182 
       
   183 jobject SmsConnection :: getData(JNIEnv& aJni,const int aMsgType)
       
   184 {
       
   185     JELOG2(EWMA);
       
   186     return mMsgConn->getMessageData(aJni,aMsgType);
       
   187 }
       
   188 
       
   189 } //namespace wma
       
   190 } //namespace java
       
   191