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.
     1 /*
     2 * Copyright (c) 2009 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<stdio.h>  
    20 #include <dbus/dbus.h>
    21 #include <stdlib.h>
    22 #include <string.h>
    23 #include <fcntl.h>
    24 #include <pthread.h>
    25 #include <unistd.h>
    26 
    27 #define MAX_THREAD 25
    28 
    29 typedef struct{
    30 pthread_mutex_t mutex;
    31 pthread_cond_t cond;
    32 int ret; 
    33 }threadData1;
    34 
    35 #define LOG_FILE "c:\\logs\\send_messages1_log1.txt"
    36 #include "std_log_result.h"
    37 #define LOG_FILENAME_LINE __FILE__, __LINE__
    38 
    39 void create_xml(int result)
    40 	{
    41 	if(result)
    42 		assert_failed = 1;
    43 	
    44 	testResultXml("send_messages1");
    45     close_log_file();
    46 	}
    47 
    48 int handle_error(DBusError* error)
    49 	{
    50 	std_log(LOG_FILENAME_LINE,"%s", error->name);
    51 	std_log(LOG_FILENAME_LINE,"%s", error->message);
    52 	dbus_error_free(error);
    53 	create_xml(1);
    54 	return 1; 
    55 	} 
    56 
    57 static void* send_msg1(void* data)
    58 {
    59 	DBusConnection* connection;
    60 	DBusError error;
    61 	static int cnt = 1;
    62 	dbus_int32_t no = 5;
    63 	DBusPendingCall* pending;
    64 	DBusMessage* msg1;
    65 	DBusMessage* msg;
    66 	int data_slot = *(int*)data;
    67 	threadData1* thrData;
    68 
    69 	dbus_error_init(&error);
    70 	connection = dbus_bus_get(DBUS_BUS_SESSION, &error);
    71 	if(!connection)
    72 		{
    73 		handle_error(&error);
    74 		return NULL;
    75 		}
    76 	
    77 	thrData = (threadData1*)dbus_connection_get_data(connection, data_slot);
    78 	if(!thrData)
    79 		return NULL;
    80 	
    81 	pthread_mutex_lock(&thrData->mutex);
    82 	
    83 	msg = dbus_message_new_method_call("Test.Method.Call", "/Test/Method/Object", "test.Method.Call", "simple");
    84 	
    85 	dbus_message_append_args(msg, DBUS_TYPE_INT32, &no, DBUS_TYPE_INVALID);
    86 	 
    87 	pthread_cond_wait(&thrData->cond,  &thrData->mutex);
    88 	 
    89 	// send message and get a handle for a reply
    90 	   if (!dbus_connection_send_with_reply (connection, msg, &pending, -1)) { // -1 is default timeout
    91 	   thrData->ret = 2;
    92 	   }   
    93 	   if (NULL == pending) {
    94 	   thrData->ret = 2;
    95 	   } 
    96 	   dbus_connection_flush(connection);
    97 	   
    98 		// free message
    99 	   dbus_message_unref(msg);   
   100 	  
   101 	   // block until we recieve a reply
   102 	   dbus_pending_call_block(pending);
   103 	
   104 	   // get the reply message
   105 	   msg1 = dbus_pending_call_steal_reply(pending);
   106 	   if (NULL == msg1) {
   107 	   thrData->ret = 2;
   108 	
   109 	   }  
   110 	   // free the pending message handle
   111 	   dbus_pending_call_unref(pending);
   112 		 
   113 	  
   114 	   dbus_message_get_args(msg1, &error, DBUS_TYPE_INT32, &no, DBUS_TYPE_INVALID);
   115 	   
   116 	   std_log(LOG_FILENAME_LINE, "%d\n", no);
   117 	   
   118 	   if(no == 9090)
   119 		   {
   120 		   thrData->ret++;
   121 		   }
   122 	   
   123 	 	 
   124 	   // free reply and close connection
   125 	   dbus_message_unref(msg1); 
   126 	   dbus_connection_unref(connection);
   127 	   pthread_mutex_unlock(&thrData->mutex); 
   128 	   return NULL;
   129 }
   130  
   131 int main()
   132 {
   133 	DBusConnection* connection;
   134 	DBusError error;
   135 	int cnt;
   136 	int data_slot = -1;
   137 	
   138 	pthread_t thread[MAX_THREAD];
   139 	int thrVal[MAX_THREAD]={0};
   140 	void* thrValPtr[MAX_THREAD];
   141 	threadData1 thrData;
   142 	
   143 	for(cnt=0; cnt<MAX_THREAD; cnt++)
   144 		thrValPtr[cnt] = (void*)&thrVal[cnt];	
   145 	 
   146 	dbus_error_init(&error);
   147 	connection = dbus_bus_get(DBUS_BUS_SESSION, &error);
   148 	if(!connection || dbus_error_is_set(&error))
   149 		return handle_error(&error);
   150 	
   151 	pthread_mutex_init(&thrData.mutex, NULL);
   152 	pthread_cond_init(&thrData.cond, NULL);
   153 		thrData.ret = 0;
   154 		
   155 		dbus_connection_allocate_data_slot(&data_slot);
   156 	dbus_connection_set_data(connection, data_slot, &thrData, NULL);
   157 		
   158 		dbus_threads_init_default();
   159 	
   160 		for(cnt=0; cnt<MAX_THREAD; cnt++)
   161 			pthread_create(&thread[cnt], NULL, &send_msg1, &data_slot);
   162 	 
   163 	sleep(1);
   164 	
   165 	pthread_cond_broadcast(&thrData.cond);
   166 	
   167 	for(cnt=0; cnt<MAX_THREAD; cnt++)
   168 		pthread_join(thread[cnt], &thrValPtr[cnt]); 
   169 	 
   170 	if(thrData.ret != MAX_THREAD)
   171 	{ 
   172 		std_log(LOG_FILENAME_LINE, "No. of threads crashed %d", (MAX_THREAD - thrData.ret));
   173 		create_xml(1);
   174 		return 1;
   175 	}
   176 	 
   177 	dbus_connection_unref(connection);
   178 	
   179 	std_log(LOG_FILENAME_LINE, "Test Successful"); 
   180 	create_xml(0);
   181 	return 0;
   182 }