os/ossrv/ofdbus/tsrc/nft/send_messages/src/send_messages.c
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
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
	DBusConnection* connection;
sl@0
    33
	int ret; 
sl@0
    34
}threadData;
sl@0
    35
sl@0
    36
#define LOG_FILE "c:\\logs\\send_messages_log1.txt"
sl@0
    37
#include "std_log_result.h"
sl@0
    38
#define LOG_FILENAME_LINE __FILE__, __LINE__
sl@0
    39
sl@0
    40
void create_xml(int result)
sl@0
    41
	{
sl@0
    42
	if(result)
sl@0
    43
		assert_failed = 1;
sl@0
    44
	
sl@0
    45
	testResultXml("send_messages");
sl@0
    46
    close_log_file();
sl@0
    47
	}
sl@0
    48
sl@0
    49
int handle_error(DBusError* error)
sl@0
    50
	{
sl@0
    51
	std_log(LOG_FILENAME_LINE,"%s", error->name);
sl@0
    52
	std_log(LOG_FILENAME_LINE,"%s", error->message);
sl@0
    53
	dbus_error_free(error);
sl@0
    54
	create_xml(1);
sl@0
    55
	return 1; 
sl@0
    56
	} 
sl@0
    57
sl@0
    58
static void* send_msg(void* data)
sl@0
    59
{
sl@0
    60
	static int cnt = 1;
sl@0
    61
	dbus_int32_t no = 5;
sl@0
    62
	DBusPendingCall* pending;
sl@0
    63
	DBusMessage* msg1;
sl@0
    64
	DBusMessage* msg;
sl@0
    65
	DBusError error; 
sl@0
    66
	
sl@0
    67
	threadData* thrData = (threadData*)data;
sl@0
    68
		
sl@0
    69
	pthread_mutex_lock(&thrData->mutex);
sl@0
    70
	
sl@0
    71
	msg = dbus_message_new_method_call("Test.Method.Call", "/Test/Method/Object", "test.Method.Call", "simple");
sl@0
    72
	
sl@0
    73
	dbus_message_append_args(msg, DBUS_TYPE_INT32, &no, DBUS_TYPE_INVALID);
sl@0
    74
	 
sl@0
    75
	pthread_cond_wait(&thrData->cond,  &thrData->mutex);
sl@0
    76
	 
sl@0
    77
	// send message and get a handle for a reply
sl@0
    78
	   if (!dbus_connection_send_with_reply (thrData->connection, msg, &pending, -1)) { // -1 is default timeout
sl@0
    79
	   thrData->ret = 2;
sl@0
    80
	   }   
sl@0
    81
	   if (NULL == pending) {
sl@0
    82
	   thrData->ret = 2;
sl@0
    83
	   } 
sl@0
    84
	   dbus_connection_flush(thrData->connection);
sl@0
    85
	   
sl@0
    86
		// free message
sl@0
    87
	   dbus_message_unref(msg);   
sl@0
    88
	  
sl@0
    89
	   // block until we recieve a reply
sl@0
    90
	   dbus_pending_call_block(pending);
sl@0
    91
	
sl@0
    92
	   // get the reply message
sl@0
    93
	   msg1 = dbus_pending_call_steal_reply(pending);
sl@0
    94
	   if (NULL == msg1) {
sl@0
    95
	   thrData->ret = 2;
sl@0
    96
	
sl@0
    97
	   }  
sl@0
    98
	   // free the pending message handle
sl@0
    99
	   dbus_pending_call_unref(pending);
sl@0
   100
		 
sl@0
   101
	   dbus_error_init(&error);
sl@0
   102
	   dbus_message_get_args(msg1, &error, DBUS_TYPE_INT32, &no, DBUS_TYPE_INVALID);
sl@0
   103
	  
sl@0
   104
	   std_log(LOG_FILENAME_LINE, "%d\n", no);
sl@0
   105
	   
sl@0
   106
	   if(no == 9090)
sl@0
   107
		   {
sl@0
   108
		   thrData->ret++;
sl@0
   109
		   }
sl@0
   110
	   
sl@0
   111
	 	 
sl@0
   112
	   // free reply and close connection
sl@0
   113
	   dbus_message_unref(msg1); 
sl@0
   114
	   pthread_mutex_unlock(&thrData->mutex); 
sl@0
   115
	   return NULL;
sl@0
   116
}
sl@0
   117
 
sl@0
   118
int main()
sl@0
   119
{
sl@0
   120
	DBusError error;
sl@0
   121
	int cnt;
sl@0
   122
	
sl@0
   123
	threadData thrData;
sl@0
   124
	unsigned int thread[MAX_THREAD];
sl@0
   125
	int thrVal[MAX_THREAD]={0};
sl@0
   126
	void* thrValPtr[MAX_THREAD];
sl@0
   127
	
sl@0
   128
	for(cnt=0; cnt<MAX_THREAD; cnt++)
sl@0
   129
		thrValPtr[cnt] = (void*)&thrVal[cnt];
sl@0
   130
	 
sl@0
   131
	dbus_error_init(&error);
sl@0
   132
	thrData.connection = dbus_bus_get(DBUS_BUS_SESSION, &error);
sl@0
   133
	if(!thrData.connection || dbus_error_is_set(&error))
sl@0
   134
		return handle_error(&error);
sl@0
   135
	
sl@0
   136
	pthread_mutex_init(&thrData.mutex, NULL);
sl@0
   137
	pthread_cond_init(&thrData.cond, NULL);
sl@0
   138
	thrData.ret = 0;
sl@0
   139
sl@0
   140
	for(cnt=0; cnt<MAX_THREAD; cnt++)
sl@0
   141
		pthread_create(&thread[cnt], NULL, &send_msg, &thrData);
sl@0
   142
 
sl@0
   143
	sleep(1);  
sl@0
   144
	
sl@0
   145
	pthread_cond_broadcast(&thrData.cond);
sl@0
   146
	
sl@0
   147
	for(cnt=0; cnt<MAX_THREAD; cnt++)
sl@0
   148
		pthread_join(thread[cnt], &thrValPtr[cnt]); 
sl@0
   149
	 
sl@0
   150
	if(thrData.ret != MAX_THREAD)
sl@0
   151
	{ 
sl@0
   152
		std_log(LOG_FILENAME_LINE,"No. of threads crashed %d", (MAX_THREAD - thrData.ret));
sl@0
   153
		create_xml(1);
sl@0
   154
		return 1;
sl@0
   155
	}
sl@0
   156
	 
sl@0
   157
	dbus_connection_unref(thrData.connection);
sl@0
   158
	
sl@0
   159
	std_log(LOG_FILENAME_LINE, "Test Successful"); 
sl@0
   160
	create_xml(0);
sl@0
   161
	return 0;
sl@0
   162
}