os/ossrv/ofdbus/tsrc/nft/send_messages1/src/send_messages1.c
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
sl@0
     1
/*
sl@0
     2
* Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
sl@0
     3
* All rights reserved.
sl@0
     4
* This component and the accompanying materials are made available
sl@0
     5
* under the terms of "Eclipse Public License v1.0"
sl@0
     6
* which accompanies this distribution, and is available
sl@0
     7
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
sl@0
     8
*
sl@0
     9
* Initial Contributors:
sl@0
    10
* Nokia Corporation - initial contribution.
sl@0
    11
*
sl@0
    12
* Contributors:
sl@0
    13
*
sl@0
    14
* Description: 
sl@0
    15
*
sl@0
    16
*/
sl@0
    17
sl@0
    18
sl@0
    19
#include<stdio.h>  
sl@0
    20
#include <dbus/dbus.h>
sl@0
    21
#include <stdlib.h>
sl@0
    22
#include <string.h>
sl@0
    23
#include <fcntl.h>
sl@0
    24
#include <pthread.h>
sl@0
    25
#include <unistd.h>
sl@0
    26
sl@0
    27
#define MAX_THREAD 25
sl@0
    28
sl@0
    29
typedef struct{
sl@0
    30
pthread_mutex_t mutex;
sl@0
    31
pthread_cond_t cond;
sl@0
    32
int ret; 
sl@0
    33
}threadData1;
sl@0
    34
sl@0
    35
#define LOG_FILE "c:\\logs\\send_messages1_log1.txt"
sl@0
    36
#include "std_log_result.h"
sl@0
    37
#define LOG_FILENAME_LINE __FILE__, __LINE__
sl@0
    38
sl@0
    39
void create_xml(int result)
sl@0
    40
	{
sl@0
    41
	if(result)
sl@0
    42
		assert_failed = 1;
sl@0
    43
	
sl@0
    44
	testResultXml("send_messages1");
sl@0
    45
    close_log_file();
sl@0
    46
	}
sl@0
    47
sl@0
    48
int handle_error(DBusError* error)
sl@0
    49
	{
sl@0
    50
	std_log(LOG_FILENAME_LINE,"%s", error->name);
sl@0
    51
	std_log(LOG_FILENAME_LINE,"%s", error->message);
sl@0
    52
	dbus_error_free(error);
sl@0
    53
	create_xml(1);
sl@0
    54
	return 1; 
sl@0
    55
	} 
sl@0
    56
sl@0
    57
static void* send_msg1(void* data)
sl@0
    58
{
sl@0
    59
	DBusConnection* connection;
sl@0
    60
	DBusError error;
sl@0
    61
	static int cnt = 1;
sl@0
    62
	dbus_int32_t no = 5;
sl@0
    63
	DBusPendingCall* pending;
sl@0
    64
	DBusMessage* msg1;
sl@0
    65
	DBusMessage* msg;
sl@0
    66
	int data_slot = *(int*)data;
sl@0
    67
	threadData1* thrData;
sl@0
    68
sl@0
    69
	dbus_error_init(&error);
sl@0
    70
	connection = dbus_bus_get(DBUS_BUS_SESSION, &error);
sl@0
    71
	if(!connection)
sl@0
    72
		{
sl@0
    73
		handle_error(&error);
sl@0
    74
		return NULL;
sl@0
    75
		}
sl@0
    76
	
sl@0
    77
	thrData = (threadData1*)dbus_connection_get_data(connection, data_slot);
sl@0
    78
	if(!thrData)
sl@0
    79
		return NULL;
sl@0
    80
	
sl@0
    81
	pthread_mutex_lock(&thrData->mutex);
sl@0
    82
	
sl@0
    83
	msg = dbus_message_new_method_call("Test.Method.Call", "/Test/Method/Object", "test.Method.Call", "simple");
sl@0
    84
	
sl@0
    85
	dbus_message_append_args(msg, DBUS_TYPE_INT32, &no, DBUS_TYPE_INVALID);
sl@0
    86
	 
sl@0
    87
	pthread_cond_wait(&thrData->cond,  &thrData->mutex);
sl@0
    88
	 
sl@0
    89
	// send message and get a handle for a reply
sl@0
    90
	   if (!dbus_connection_send_with_reply (connection, msg, &pending, -1)) { // -1 is default timeout
sl@0
    91
	   thrData->ret = 2;
sl@0
    92
	   }   
sl@0
    93
	   if (NULL == pending) {
sl@0
    94
	   thrData->ret = 2;
sl@0
    95
	   } 
sl@0
    96
	   dbus_connection_flush(connection);
sl@0
    97
	   
sl@0
    98
		// free message
sl@0
    99
	   dbus_message_unref(msg);   
sl@0
   100
	  
sl@0
   101
	   // block until we recieve a reply
sl@0
   102
	   dbus_pending_call_block(pending);
sl@0
   103
	
sl@0
   104
	   // get the reply message
sl@0
   105
	   msg1 = dbus_pending_call_steal_reply(pending);
sl@0
   106
	   if (NULL == msg1) {
sl@0
   107
	   thrData->ret = 2;
sl@0
   108
	
sl@0
   109
	   }  
sl@0
   110
	   // free the pending message handle
sl@0
   111
	   dbus_pending_call_unref(pending);
sl@0
   112
		 
sl@0
   113
	  
sl@0
   114
	   dbus_message_get_args(msg1, &error, DBUS_TYPE_INT32, &no, DBUS_TYPE_INVALID);
sl@0
   115
	   
sl@0
   116
	   std_log(LOG_FILENAME_LINE, "%d\n", no);
sl@0
   117
	   
sl@0
   118
	   if(no == 9090)
sl@0
   119
		   {
sl@0
   120
		   thrData->ret++;
sl@0
   121
		   }
sl@0
   122
	   
sl@0
   123
	 	 
sl@0
   124
	   // free reply and close connection
sl@0
   125
	   dbus_message_unref(msg1); 
sl@0
   126
	   dbus_connection_unref(connection);
sl@0
   127
	   pthread_mutex_unlock(&thrData->mutex); 
sl@0
   128
	   return NULL;
sl@0
   129
}
sl@0
   130
 
sl@0
   131
int main()
sl@0
   132
{
sl@0
   133
	DBusConnection* connection;
sl@0
   134
	DBusError error;
sl@0
   135
	int cnt;
sl@0
   136
	int data_slot = -1;
sl@0
   137
	
sl@0
   138
	pthread_t thread[MAX_THREAD];
sl@0
   139
	int thrVal[MAX_THREAD]={0};
sl@0
   140
	void* thrValPtr[MAX_THREAD];
sl@0
   141
	threadData1 thrData;
sl@0
   142
	
sl@0
   143
	for(cnt=0; cnt<MAX_THREAD; cnt++)
sl@0
   144
		thrValPtr[cnt] = (void*)&thrVal[cnt];	
sl@0
   145
	 
sl@0
   146
	dbus_error_init(&error);
sl@0
   147
	connection = dbus_bus_get(DBUS_BUS_SESSION, &error);
sl@0
   148
	if(!connection || dbus_error_is_set(&error))
sl@0
   149
		return handle_error(&error);
sl@0
   150
	
sl@0
   151
	pthread_mutex_init(&thrData.mutex, NULL);
sl@0
   152
	pthread_cond_init(&thrData.cond, NULL);
sl@0
   153
		thrData.ret = 0;
sl@0
   154
		
sl@0
   155
		dbus_connection_allocate_data_slot(&data_slot);
sl@0
   156
	dbus_connection_set_data(connection, data_slot, &thrData, NULL);
sl@0
   157
		
sl@0
   158
		dbus_threads_init_default();
sl@0
   159
	
sl@0
   160
		for(cnt=0; cnt<MAX_THREAD; cnt++)
sl@0
   161
			pthread_create(&thread[cnt], NULL, &send_msg1, &data_slot);
sl@0
   162
	 
sl@0
   163
	sleep(1);
sl@0
   164
	
sl@0
   165
	pthread_cond_broadcast(&thrData.cond);
sl@0
   166
	
sl@0
   167
	for(cnt=0; cnt<MAX_THREAD; cnt++)
sl@0
   168
		pthread_join(thread[cnt], &thrValPtr[cnt]); 
sl@0
   169
	 
sl@0
   170
	if(thrData.ret != MAX_THREAD)
sl@0
   171
	{ 
sl@0
   172
		std_log(LOG_FILENAME_LINE, "No. of threads crashed %d", (MAX_THREAD - thrData.ret));
sl@0
   173
		create_xml(1);
sl@0
   174
		return 1;
sl@0
   175
	}
sl@0
   176
	 
sl@0
   177
	dbus_connection_unref(connection);
sl@0
   178
	
sl@0
   179
	std_log(LOG_FILENAME_LINE, "Test Successful"); 
sl@0
   180
	create_xml(0);
sl@0
   181
	return 0;
sl@0
   182
}