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