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