javaextensions/wma/sms_cbs/src/smsconnectionimpljni.cpp
branchRCL_3
changeset 19 04becd199f91
child 78 71ad690e91f5
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 #include <stdlib.h>
       
    19 #include "logger.h"
       
    20 #include "javajniutils.h"
       
    21 #include "exceptionbase.h"
       
    22 #include "pushexception.h"
       
    23 #include "pusherrorcodes.h"
       
    24 #include "javacommonutils.h"
       
    25 #include "smsconnection.h"
       
    26 #include "com_nokia_mj_impl_sms_SMSConnectionImpl.h"
       
    27 
       
    28 using namespace java::wma;
       
    29 using namespace java::util;
       
    30 /*
       
    31  * Class:     com_nokia_mj_impl_sms_SMSConnectionImpl
       
    32  * Method:    _createConnection
       
    33  * Signature: (Ljava/lang/String;Z)J
       
    34  */
       
    35 JNIEXPORT jint JNICALL Java_com_nokia_mj_impl_sms_SMSConnectionImpl__1createConnection
       
    36 (JNIEnv* aJni, jobject ,jstring aUri,jboolean aServerConnection)
       
    37 {
       
    38     JELOG2(EWMA);
       
    39     std::wstring Uri = java::util::JniUtils::jstringToWstring(aJni,aUri);
       
    40 
       
    41     SmsConnection* smsConn = 0;
       
    42     try
       
    43     {
       
    44         smsConn = new SmsConnection(Uri, aServerConnection);
       
    45         smsConn->initialize();
       
    46         return reinterpret_cast<jint>(smsConn);
       
    47     }
       
    48     catch (java::push::PushException& ex)
       
    49     {
       
    50         ELOG(EWMA,"WMA ERROR!!! CreateConnection() PushException was caught");
       
    51         if (java::push::PUSH_CONNECTION_ALREADY_EXISTS == ex.mErrCode)
       
    52         {
       
    53             ELOG(EWMA,"WMA : PushConnection already Exists");
       
    54             JniUtils::throwNewException(aJni, "java/io/IOException" ,
       
    55                                         "Server Connection already exists");
       
    56         }
       
    57         if (java::push::COMMON_SRV_CONN_PLUGIN_ERROR == ex.mErrCode)
       
    58         {
       
    59             ELOG(EWMA,"WMA : Common pushConnection error");
       
    60             JniUtils::throwNewException(aJni, "java/io/IOException" ,
       
    61                                         "Opening Server Connection failed: "
       
    62                                         + JavaCommonUtils::intToString(ex.mErrCode));
       
    63         }
       
    64     }
       
    65     catch (std::bad_alloc &ex)
       
    66     {
       
    67         ELOG(EWMA,"WMA : Caught bad alloc exception");
       
    68         JniUtils::throwNewException(aJni, "java/lang/OutOfMemoryError" ,
       
    69                                     "Out of memory");
       
    70     }
       
    71     catch (ExceptionBase ex)
       
    72     {
       
    73         ELOG(EWMA,"WMA : Caught an ExceptionBase exception");
       
    74         JniUtils::throwNewException(aJni, "java/io/IOException" ,
       
    75                                     "Opening connection failed");
       
    76     }
       
    77     catch (int error)
       
    78     {
       
    79         ELOG1(EWMA,"+ WMA : Caught an Exception --Value is:%d",error);
       
    80         JniUtils::throwNewException(aJni, "java/io/IOException" ,
       
    81                                     "Opening connection failed: " + JavaCommonUtils::intToString(error));
       
    82     }
       
    83     if (0 != smsConn)
       
    84     {
       
    85         delete smsConn;
       
    86     }
       
    87     return 0;
       
    88 }
       
    89 
       
    90 /*
       
    91  * Class:     com_nokia_mj_impl_sms_SMSConnectionImpl
       
    92  * Method:    _openConnection
       
    93  * Signature: (J)V
       
    94  */
       
    95 JNIEXPORT void JNICALL Java_com_nokia_mj_impl_sms_SMSConnectionImpl__1openConnection
       
    96 (JNIEnv *aJni, jobject aPeer,jint aHandle)
       
    97 {
       
    98     JELOG2(EWMA);
       
    99     SmsConnection* msgConn = reinterpret_cast<SmsConnection*>(aHandle);
       
   100     return msgConn->open(*aJni,aPeer);
       
   101 }
       
   102 
       
   103 /*
       
   104  * Class:     com_nokia_mj_impl_sms_SMSConnectionImpl
       
   105  * Method:    _smsTextDataSegments
       
   106  * Signature: (JILjava/lang/String;Ljava/lang/String;Ljava/lang/String;)I
       
   107  */
       
   108 JNIEXPORT jint JNICALL Java_com_nokia_mj_impl_sms_SMSConnectionImpl__1smsTextDataSegments
       
   109 (JNIEnv* aJni, jobject , jint aHandle, jint aType,jstring aHostAddress,
       
   110  jstring aHostPort, jstring aData)
       
   111 {
       
   112     JELOG2(EWMA);
       
   113     jint numberOfSegments = 0;
       
   114     int hostPort = 0;
       
   115     const char* hostaddress = aJni->GetStringUTFChars(aHostAddress,0);
       
   116     if (aHostPort)
       
   117     {
       
   118         const char* port = aJni->GetStringUTFChars(aHostPort,0);
       
   119         hostPort = atoi(port);
       
   120         aJni->ReleaseStringUTFChars(aHostPort,port);
       
   121     }
       
   122     char* messageData = 0;
       
   123     jint len = 0;
       
   124     if (aData != NULL)
       
   125     {
       
   126         const jchar* data = aJni->GetStringChars(aData,0);
       
   127         len = aJni->GetStringLength(aData);
       
   128         messageData = new char[len*2 + 1];
       
   129         memset(messageData,0,len*2 + 1);
       
   130         memcpy((void*)messageData,(void*)data,len*2);
       
   131         aJni->ReleaseStringChars(aData,data);
       
   132     }
       
   133     SmsConnection* msgConn = reinterpret_cast<SmsConnection*>(aHandle);
       
   134     try
       
   135     {
       
   136         numberOfSegments = msgConn->numberOfDataSegments(aType,len,hostPort,
       
   137                            hostaddress,messageData);
       
   138     }
       
   139     catch (ExceptionBase ex)
       
   140     {
       
   141         JniUtils::throwNewException(aJni, "java/lang/RuntimeException" ,
       
   142                                     "Calculating number of segments failed");
       
   143     }
       
   144     if (numberOfSegments < 0)
       
   145     {
       
   146         JniUtils::throwNewException(aJni, "java/lang/RuntimeException" ,
       
   147                                     "Calculating number of message segments failed: " +
       
   148                                     JavaCommonUtils::intToString(numberOfSegments));
       
   149     }
       
   150     aJni->ReleaseStringUTFChars(aHostAddress,hostaddress);
       
   151     delete[] messageData;
       
   152     return numberOfSegments;
       
   153 }
       
   154 
       
   155 /*
       
   156  * Class:     com_nokia_mj_impl_sms_SMSConnectionImpl
       
   157  * Method:    _smsBinaryDataSegments
       
   158  * Signature: (JILjava/lang/String;Ljava/lang/String;[B)I
       
   159  */
       
   160 JNIEXPORT jint JNICALL Java_com_nokia_mj_impl_sms_SMSConnectionImpl__1smsBinaryDataSegments
       
   161 (JNIEnv* aJni, jobject , jint aHandle, jint aType,jstring aHostAddress,
       
   162  jstring aHostPort, jbyteArray aData)
       
   163 {
       
   164     JELOG2(EWMA);
       
   165     jint numberOfSegments = 0;
       
   166     int hostPort = 0;
       
   167     char* nativeString = 0;
       
   168     int length = 0;
       
   169     const char* hostaddress = aJni->GetStringUTFChars(aHostAddress,0);
       
   170     if (aHostPort)
       
   171     {
       
   172         const char* port = aJni->GetStringUTFChars(aHostPort,0);
       
   173         hostPort = atoi(port);
       
   174         aJni->ReleaseStringUTFChars(aHostPort,port);
       
   175     }
       
   176     if (aData != NULL)
       
   177     {
       
   178         length = aJni->GetArrayLength(aData);
       
   179         nativeString = new char[length + 1];
       
   180         memset(nativeString, 0, length + 1);
       
   181         aJni->GetByteArrayRegion(aData,0,length,(jbyte *)nativeString);
       
   182         aJni->DeleteLocalRef(aData);
       
   183     }
       
   184     SmsConnection* msgConn = reinterpret_cast<SmsConnection*>(aHandle);
       
   185     try
       
   186     {
       
   187         numberOfSegments = msgConn->numberOfDataSegments(aType,length,hostPort,
       
   188                            hostaddress,(const char*)nativeString);
       
   189     }
       
   190     catch (ExceptionBase ex)
       
   191     {
       
   192         JniUtils::throwNewException(aJni, "java/lang/RuntimeException" ,
       
   193                                     "Calculating number of segments failed");
       
   194     }
       
   195     if (numberOfSegments < 0)
       
   196     {
       
   197         JniUtils::throwNewException(aJni, "java/lang/RuntimeException" ,
       
   198                                     "Calculating number of message segments failed: " +
       
   199                                     JavaCommonUtils::intToString(numberOfSegments));
       
   200 
       
   201     }
       
   202     aJni->ReleaseStringUTFChars(aHostAddress,hostaddress);
       
   203     delete[] nativeString;
       
   204     return numberOfSegments;
       
   205 
       
   206 }
       
   207 
       
   208 /*
       
   209  * Class:     com_nokia_mj_impl_sms_SMSConnectionImpl
       
   210  * Method:    _sendMessage
       
   211  * Signature: (J)I
       
   212  */
       
   213 JNIEXPORT jint JNICALL Java_com_nokia_mj_impl_sms_SMSConnectionImpl__1sendMessage
       
   214 (JNIEnv* , jobject , jint aHandle)
       
   215 {
       
   216     JELOG2(EWMA);
       
   217     SmsConnection* msgConn = reinterpret_cast<SmsConnection*>(aHandle);
       
   218     return msgConn->send();
       
   219 }
       
   220 
       
   221 /*
       
   222  * Class:     com_nokia_mj_impl_sms_SMSConnectionImpl
       
   223  * Method:    _getMessageType
       
   224  * Signature: (J)I
       
   225  */
       
   226 JNIEXPORT jint JNICALL Java_com_nokia_mj_impl_sms_SMSConnectionImpl__1getMessageType
       
   227 (JNIEnv *, jobject,jint aHandle)
       
   228 {
       
   229     JELOG2(EWMA);
       
   230     SmsConnection* msgConn = reinterpret_cast<SmsConnection*>(aHandle);
       
   231     return msgConn->getMessageType();
       
   232 }
       
   233 
       
   234 /*
       
   235  * Class:     com_nokia_mj_impl_sms_SMSConnectionImpl
       
   236  * Method:    _getHostAddress
       
   237  * Signature: (J)Ljava/lang/String;
       
   238  */
       
   239 JNIEXPORT jstring JNICALL Java_com_nokia_mj_impl_sms_SMSConnectionImpl__1getHostAddress
       
   240 (JNIEnv *aJni, jobject,jint aHandle)
       
   241 {
       
   242     JELOG2(EWMA);
       
   243     SmsConnection* msgConn = reinterpret_cast<SmsConnection*>(aHandle);
       
   244     return msgConn->getRemoteHostAddress(*aJni);
       
   245 }
       
   246 
       
   247 /*
       
   248  * Class:     com_nokia_mj_impl_sms_SMSConnectionImpl
       
   249  * Method:    _getTimestamp
       
   250  * Signature: (J)J
       
   251  */
       
   252 JNIEXPORT jlong JNICALL Java_com_nokia_mj_impl_sms_SMSConnectionImpl__1getTimestamp
       
   253 (JNIEnv *, jobject,jint aHandle)
       
   254 {
       
   255     JELOG2(EWMA);
       
   256     SmsConnection* msgConn = reinterpret_cast<SmsConnection*>(aHandle);
       
   257     return msgConn->getTimestamp();
       
   258 }
       
   259 
       
   260 /*
       
   261  * Class:     com_nokia_mj_impl_sms_SMSConnectionImpl
       
   262  * Method:    _getReceivedMessageData
       
   263  * Signature: (JI)Ljava/lang/Object;
       
   264  */
       
   265 JNIEXPORT jobject JNICALL Java_com_nokia_mj_impl_sms_SMSConnectionImpl__1getReceivedMessageData
       
   266 (JNIEnv *aJni, jobject,jint aHandle,jint aMessageType)
       
   267 {
       
   268     JELOG2(EWMA);
       
   269     SmsConnection* msgConn = reinterpret_cast<SmsConnection*>(aHandle);
       
   270     return msgConn->getData(*aJni,aMessageType);
       
   271 }
       
   272 
       
   273 /*
       
   274  * Class:     com_nokia_mj_impl_sms_SMSConnectionImpl
       
   275  * Method:    _closeConnection
       
   276  * Signature: (J)V
       
   277  */
       
   278 JNIEXPORT void JNICALL Java_com_nokia_mj_impl_sms_SMSConnectionImpl__1closeConnection
       
   279 (JNIEnv *, jobject,jint aHandle)
       
   280 {
       
   281     JELOG2(EWMA);
       
   282     SmsConnection* msgConn = reinterpret_cast<SmsConnection*>(aHandle);
       
   283     msgConn->close();
       
   284 }
       
   285 
       
   286 /*
       
   287  * Class:     com_nokia_mj_impl_sms_SMSConnectionImpl
       
   288  * Method:    _dispose
       
   289  * Signature: (J)V
       
   290  */
       
   291 JNIEXPORT void JNICALL Java_com_nokia_mj_impl_sms_SMSConnectionImpl__1dispose
       
   292 (JNIEnv *, jobject,jint aHandle)
       
   293 {
       
   294     JELOG2(EWMA);
       
   295     SmsConnection* msgConn = reinterpret_cast<SmsConnection*>(aHandle);
       
   296     delete msgConn;
       
   297 }
       
   298 
       
   299 /*
       
   300  * Class:     com_nokia_mj_impl_sms_SMSConnectionImpl
       
   301  * Method:    _checkError
       
   302  * Signature: (I)V
       
   303  */
       
   304 JNIEXPORT void JNICALL Java_com_nokia_mj_impl_sms_SMSConnectionImpl__1checkError
       
   305 (JNIEnv *aJni, jobject , jint aError , jboolean aIsSendOperation)
       
   306 {
       
   307     JELOG2(EWMA);
       
   308 #ifdef __SYMBIAN32__
       
   309     if (aError == KErrTimedOut)
       
   310     {
       
   311         JniUtils::throwNewException(aJni, "java/io/InterruptedIOException" ,
       
   312                                     "Timeout occurred while sending message" +
       
   313                                     JavaCommonUtils::intToString(aError));
       
   314     }
       
   315     else
       
   316     {
       
   317         if (aIsSendOperation)
       
   318         {
       
   319             JniUtils::throwNewException(aJni, "java/io/IOException" ,
       
   320                                         "Sending message failed: "
       
   321                                         + JavaCommonUtils::intToString(aError));
       
   322         }
       
   323         else
       
   324         {
       
   325             JniUtils::throwNewException(aJni, "java/io/IOException" ,
       
   326                                         "Receiving message failed: "
       
   327                                         + JavaCommonUtils::intToString(aError));
       
   328         }
       
   329     }
       
   330 #endif // __SYMBIAN32__
       
   331 }