ofdbus/dbus/tsrc/testapps/match_rule_server/src/match_rule_server.c
changeset 0 e4d67989cc36
child 50 79045913e4e9
equal deleted inserted replaced
-1:000000000000 0:e4d67989cc36
       
     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 "test-utils.h"
       
    20 #include <fcntl.h>
       
    21 #include <string.h>  
       
    22   
       
    23 void handle_error(char* str)
       
    24 {
       
    25 	FILE* fp = fopen("C:\\match_rule_data.txt", "a+");
       
    26 	if(fp)
       
    27 	{
       
    28 		fwrite(str, strlen(str), 1, fp);
       
    29 		fclose(fp);
       
    30 	} 
       
    31 }
       
    32 
       
    33 int check_signal(DBusMessage* msg, char* iface, char* member)
       
    34 {
       
    35 	DBusError error;
       
    36 	char buf[80]; 
       
    37 	char* str;
       
    38 	
       
    39 	dbus_error_init(&error);
       
    40 	if(dbus_message_is_signal(msg, iface, member))
       
    41 	{
       
    42 		printf("\n\nGot the new Signal.\n");
       
    43 		if(!dbus_message_get_args(msg, &error, DBUS_TYPE_STRING, &str, DBUS_TYPE_INVALID))
       
    44 		{
       
    45 			handle_error("\nError while retrieving arguments");
       
    46 			exit(1);
       
    47 		}
       
    48 		sprintf(buf, "\nSignal %s received is %s", member, str);
       
    49 		handle_error(buf);
       
    50 		return 1;
       
    51 	}
       
    52 	return 0;
       
    53 }
       
    54 
       
    55 int send_reply(DBusConnection* connection,int type, char* dest, char* path, char* iface, char* member)
       
    56 {
       
    57 	DBusMessage* reply;
       
    58 	reply = dbus_message_new(type);
       
    59 	if(!dbus_message_set_destination(reply, dest))
       
    60 		return 1;
       
    61 	if(!dbus_message_set_path(reply, path))
       
    62 		return 1;
       
    63 	if(!dbus_message_set_interface(reply, iface))
       
    64 		return 1;
       
    65 	if(!dbus_message_set_member(reply, member))
       
    66 		return 1;
       
    67 	if(type == DBUS_MESSAGE_TYPE_ERROR)
       
    68 		if(!dbus_message_set_error_name(reply, "Test.Signal.Send.RemoveMatchRuleError"))
       
    69 			return 1;
       
    70 	
       
    71 	dbus_connection_send(connection, reply, NULL);
       
    72 	dbus_connection_flush(connection);
       
    73 	return 0;
       
    74 }
       
    75 
       
    76 int main()
       
    77 {
       
    78 	DBusError error; 
       
    79 	DBusConnection* connection;
       
    80 	DBusMessage* msg;
       
    81 	DBusObjectPathVTable vtable =
       
    82 	{
       
    83 		NULL,NULL,NULL
       
    84 	};
       
    85 	
       
    86 	char* str;
       
    87 	char buf[80];
       
    88 	int cnt = 0;
       
    89 	//Note: fifo file used by tests: match_rule, dbus_match_rule
       
    90 	const char* fifopath = "C:\\mkfifo1.file";
       
    91 	int err;
       
    92 	int fd;
       
    93 	char* buf1;
       
    94 	
       
    95 	dbus_error_init(&error);
       
    96 		 
       
    97 	connection = dbus_bus_get(DBUS_BUS_SESSION, &error);
       
    98 	  
       
    99 	if(dbus_error_is_set(&error))  
       
   100 	{ 
       
   101 		sprintf(buf, "\nError Occured :: %s\n%s", error.name, error.message);
       
   102 		handle_error(buf);
       
   103 		return 1;
       
   104 	}
       
   105 	
       
   106 	dbus_bus_add_match(connection, "type='signal',interface='Test.Signal.Send2',member='first'",&error);
       
   107 	dbus_bus_add_match(connection, "type='signal',interface='Test.Signal.Send2',member='third'",&error);
       
   108 	
       
   109 	if(dbus_error_is_set(&error))
       
   110 	{
       
   111 		sprintf(buf, "\nError :: %s\n%s", error.name, error.message);
       
   112 		handle_error(buf);
       
   113 		return 1;
       
   114 	}
       
   115 	
       
   116 	if(!dbus_connection_register_object_path (connection, "/Test/Signal/Object", &vtable, NULL))
       
   117 	{
       
   118 		handle_error("Not able to register path.");
       
   119 		return 1;
       
   120 	}
       
   121 	
       
   122 	if(!dbus_bus_request_name (connection, "test.Signal.Send", DBUS_NAME_FLAG_ALLOW_REPLACEMENT, &error) == -1)
       
   123 	{
       
   124 		handle_error("Not able to Request name.");
       
   125 		return 1;
       
   126 	}
       
   127 	
       
   128 	fd = open(fifopath, O_WRONLY);
       
   129 	if(fd > 0)
       
   130 	{
       
   131 		buf1 = "done1"; //for checkpoint 1
       
   132 		err = write (fd, buf1, strlen(buf1)); 
       
   133 		close(fd);
       
   134 	}
       
   135 	while(TRUE)
       
   136 	{	
       
   137 		dbus_connection_read_write(connection, 0);
       
   138 		msg = dbus_connection_pop_message(connection);
       
   139 		if(msg == NULL)
       
   140 		{ 
       
   141 		//	sleep(1);
       
   142 			continue; 
       
   143 		}	
       
   144 		
       
   145 		if(check_signal(msg, "Test.Signal.Send2", "first"))
       
   146 			break;
       
   147 		if(check_signal(msg, "Test.Signal.Send2", "third"))
       
   148 			break;
       
   149 		
       
   150 		dbus_message_unref(msg);
       
   151 	} 
       
   152 	str = "server reply.";
       
   153 	msg = dbus_message_new_signal("/Test/Signal/Object1", "Test.Signal.Send3", "second");
       
   154 	 
       
   155 	if(!msg)   
       
   156 	{   
       
   157 		handle_error("\nmsg is NULL.");
       
   158 		return 1;   
       
   159 	}
       
   160 	 
       
   161 	if(!dbus_message_append_args(msg, DBUS_TYPE_STRING, &str, DBUS_TYPE_INVALID))
       
   162 	{
       
   163 		handle_error("\nAppending arg fail.");
       
   164 		return 1;    
       
   165 	} 
       
   166 	dbus_connection_send(connection, msg, NULL);
       
   167 	dbus_connection_flush(connection);
       
   168 	
       
   169 	dbus_bus_remove_match(connection, "type='signal',interface='Test.Signal.Send2',member='first'", &error);
       
   170 	if(	dbus_error_is_set(&error))
       
   171 	{
       
   172 		sprintf(buf, "Error :: %s\n%s", error.name, error.message);
       
   173 		handle_error(buf);
       
   174 	}
       
   175 	
       
   176 	fd = open(fifopath, O_WRONLY);
       
   177     if (fd > 0)
       
   178         {
       
   179         buf1 = "done2"; //for checkpoint 2
       
   180         err = write(fd, buf1, strlen(buf1));
       
   181         close(fd);
       
   182         }
       
   183 
       
   184 	while(TRUE)
       
   185 	{	
       
   186 		dbus_connection_read_write(connection, 0);		
       
   187 		msg = dbus_connection_pop_message(connection);
       
   188 			
       
   189 		if(msg == NULL)
       
   190 		{ 
       
   191 			sleep(1);
       
   192 			continue; 
       
   193 		}
       
   194 		
       
   195 		if(check_signal(msg, "Test.Signal.Send2", "first"))
       
   196 		{
       
   197 			if(send_reply(connection, DBUS_MESSAGE_TYPE_ERROR, "test.Signal.Send1", "/Test/Signal/Object1", NULL, "error"))
       
   198 				break; 
       
   199 		
       
   200 			break;//cnt++;
       
   201 		}
       
   202 		
       
   203 		if(check_signal(msg, "Test.Signal.Send2", "third"))
       
   204 		{ 
       
   205 			cnt++;
       
   206 			if(cnt==2)
       
   207 			{
       
   208 				if(send_reply(connection, DBUS_MESSAGE_TYPE_METHOD_CALL, "test.Signal.Send1", "/Test/Signal/Object1", NULL, "success"))
       
   209 					break;
       
   210 			
       
   211 				break; 
       
   212 			}
       
   213 		} 
       
   214 		dbus_message_unref(msg);
       
   215 	}
       
   216 	
       
   217 	dbus_connection_unref(connection);
       
   218 	return 0; 
       
   219 }