1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/ossrv/ofdbus/tsrc/nft/send_messages/src/send_messages.c Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,162 @@
1.4 +/*
1.5 +* Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
1.6 +* All rights reserved.
1.7 +* This component and the accompanying materials are made available
1.8 +* under the terms of "Eclipse Public License v1.0"
1.9 +* which accompanies this distribution, and is available
1.10 +* at the URL "http://www.eclipse.org/legal/epl-v10.html".
1.11 +*
1.12 +* Initial Contributors:
1.13 +* Nokia Corporation - initial contribution.
1.14 +*
1.15 +* Contributors:
1.16 +*
1.17 +* Description:
1.18 +*
1.19 +*/
1.20 +
1.21 +
1.22 +#include<stdio.h>
1.23 +#include <dbus/dbus.h>
1.24 +#include <stdlib.h>
1.25 +#include <string.h>
1.26 +#include <fcntl.h>
1.27 +#include <pthread.h>
1.28 +#include <unistd.h>
1.29 +
1.30 +#define MAX_THREAD 25
1.31 +
1.32 +typedef struct{
1.33 + pthread_mutex_t mutex;
1.34 + pthread_cond_t cond;
1.35 + DBusConnection* connection;
1.36 + int ret;
1.37 +}threadData;
1.38 +
1.39 +#define LOG_FILE "c:\\logs\\send_messages_log1.txt"
1.40 +#include "std_log_result.h"
1.41 +#define LOG_FILENAME_LINE __FILE__, __LINE__
1.42 +
1.43 +void create_xml(int result)
1.44 + {
1.45 + if(result)
1.46 + assert_failed = 1;
1.47 +
1.48 + testResultXml("send_messages");
1.49 + close_log_file();
1.50 + }
1.51 +
1.52 +int handle_error(DBusError* error)
1.53 + {
1.54 + std_log(LOG_FILENAME_LINE,"%s", error->name);
1.55 + std_log(LOG_FILENAME_LINE,"%s", error->message);
1.56 + dbus_error_free(error);
1.57 + create_xml(1);
1.58 + return 1;
1.59 + }
1.60 +
1.61 +static void* send_msg(void* data)
1.62 +{
1.63 + static int cnt = 1;
1.64 + dbus_int32_t no = 5;
1.65 + DBusPendingCall* pending;
1.66 + DBusMessage* msg1;
1.67 + DBusMessage* msg;
1.68 + DBusError error;
1.69 +
1.70 + threadData* thrData = (threadData*)data;
1.71 +
1.72 + pthread_mutex_lock(&thrData->mutex);
1.73 +
1.74 + msg = dbus_message_new_method_call("Test.Method.Call", "/Test/Method/Object", "test.Method.Call", "simple");
1.75 +
1.76 + dbus_message_append_args(msg, DBUS_TYPE_INT32, &no, DBUS_TYPE_INVALID);
1.77 +
1.78 + pthread_cond_wait(&thrData->cond, &thrData->mutex);
1.79 +
1.80 + // send message and get a handle for a reply
1.81 + if (!dbus_connection_send_with_reply (thrData->connection, msg, &pending, -1)) { // -1 is default timeout
1.82 + thrData->ret = 2;
1.83 + }
1.84 + if (NULL == pending) {
1.85 + thrData->ret = 2;
1.86 + }
1.87 + dbus_connection_flush(thrData->connection);
1.88 +
1.89 + // free message
1.90 + dbus_message_unref(msg);
1.91 +
1.92 + // block until we recieve a reply
1.93 + dbus_pending_call_block(pending);
1.94 +
1.95 + // get the reply message
1.96 + msg1 = dbus_pending_call_steal_reply(pending);
1.97 + if (NULL == msg1) {
1.98 + thrData->ret = 2;
1.99 +
1.100 + }
1.101 + // free the pending message handle
1.102 + dbus_pending_call_unref(pending);
1.103 +
1.104 + dbus_error_init(&error);
1.105 + dbus_message_get_args(msg1, &error, DBUS_TYPE_INT32, &no, DBUS_TYPE_INVALID);
1.106 +
1.107 + std_log(LOG_FILENAME_LINE, "%d\n", no);
1.108 +
1.109 + if(no == 9090)
1.110 + {
1.111 + thrData->ret++;
1.112 + }
1.113 +
1.114 +
1.115 + // free reply and close connection
1.116 + dbus_message_unref(msg1);
1.117 + pthread_mutex_unlock(&thrData->mutex);
1.118 + return NULL;
1.119 +}
1.120 +
1.121 +int main()
1.122 +{
1.123 + DBusError error;
1.124 + int cnt;
1.125 +
1.126 + threadData thrData;
1.127 + unsigned int thread[MAX_THREAD];
1.128 + int thrVal[MAX_THREAD]={0};
1.129 + void* thrValPtr[MAX_THREAD];
1.130 +
1.131 + for(cnt=0; cnt<MAX_THREAD; cnt++)
1.132 + thrValPtr[cnt] = (void*)&thrVal[cnt];
1.133 +
1.134 + dbus_error_init(&error);
1.135 + thrData.connection = dbus_bus_get(DBUS_BUS_SESSION, &error);
1.136 + if(!thrData.connection || dbus_error_is_set(&error))
1.137 + return handle_error(&error);
1.138 +
1.139 + pthread_mutex_init(&thrData.mutex, NULL);
1.140 + pthread_cond_init(&thrData.cond, NULL);
1.141 + thrData.ret = 0;
1.142 +
1.143 + for(cnt=0; cnt<MAX_THREAD; cnt++)
1.144 + pthread_create(&thread[cnt], NULL, &send_msg, &thrData);
1.145 +
1.146 + sleep(1);
1.147 +
1.148 + pthread_cond_broadcast(&thrData.cond);
1.149 +
1.150 + for(cnt=0; cnt<MAX_THREAD; cnt++)
1.151 + pthread_join(thread[cnt], &thrValPtr[cnt]);
1.152 +
1.153 + if(thrData.ret != MAX_THREAD)
1.154 + {
1.155 + std_log(LOG_FILENAME_LINE,"No. of threads crashed %d", (MAX_THREAD - thrData.ret));
1.156 + create_xml(1);
1.157 + return 1;
1.158 + }
1.159 +
1.160 + dbus_connection_unref(thrData.connection);
1.161 +
1.162 + std_log(LOG_FILENAME_LINE, "Test Successful");
1.163 + create_xml(0);
1.164 + return 0;
1.165 +}