ofdbus/dbus/tsrc/testapps/dbus_test_cases/test-service.c
changeset 0 e4d67989cc36
child 18 47c74d1534e1
equal deleted inserted replaced
-1:000000000000 0:e4d67989cc36
       
     1 /* Portion Copyright © 2008 Nokia Corporation and/or its subsidiary(-ies). All rights reserved.*/
       
     2 #include "test-utils.h"
       
     3 
       
     4 static DBusLoop *loop;
       
     5 static dbus_bool_t already_quit = FALSE;
       
     6 static dbus_bool_t hello_from_self_reply_received = FALSE;
       
     7 
       
     8 static void
       
     9 quit (void)
       
    10 {
       
    11   if (!already_quit)
       
    12     {
       
    13       _dbus_loop_quit (loop);
       
    14       already_quit = TRUE;
       
    15     }
       
    16 }
       
    17 
       
    18 static void
       
    19 die (const char *message)
       
    20 {
       
    21   fprintf (stderr, "*** test-service: %s", message);
       
    22   exit (1);
       
    23 }
       
    24 
       
    25 static void
       
    26 check_hello_from_self_reply (DBusPendingCall *pcall, 
       
    27                              void *user_data)
       
    28 {
       
    29   DBusMessage *reply;
       
    30   DBusMessage *echo_message, *echo_reply = NULL;
       
    31   DBusError error;
       
    32   DBusConnection *connection;
       
    33   
       
    34   int type;
       
    35   
       
    36   dbus_error_init (&error);
       
    37  
       
    38   connection = dbus_bus_get (DBUS_BUS_STARTER, &error);
       
    39   if (connection == NULL)
       
    40     {
       
    41       fprintf (stderr, "*** Failed to open connection to activating message bus: %s\n",
       
    42                error.message);
       
    43       dbus_error_free (&error);
       
    44       die("no memory");
       
    45     }
       
    46 
       
    47   
       
    48   echo_message = (DBusMessage *)user_data;
       
    49     
       
    50   reply = dbus_pending_call_steal_reply (pcall);
       
    51     
       
    52   type = dbus_message_get_type (reply);
       
    53     
       
    54   if (type == DBUS_MESSAGE_TYPE_METHOD_RETURN)
       
    55     {
       
    56       const char *s;
       
    57       printf ("Reply from HelloFromSelf received\n");
       
    58      
       
    59       if (!dbus_message_get_args (echo_message,
       
    60                               &error,
       
    61                               DBUS_TYPE_STRING, &s,
       
    62                               DBUS_TYPE_INVALID))
       
    63         {
       
    64             echo_reply = dbus_message_new_error (echo_message,
       
    65                                       error.name,
       
    66                                       error.message);
       
    67 
       
    68             if (echo_reply == NULL)
       
    69               die ("No memory\n");
       
    70 
       
    71         } 
       
    72       else
       
    73         {  
       
    74           echo_reply = dbus_message_new_method_return (echo_message);
       
    75           if (echo_reply == NULL)
       
    76             die ("No memory\n");
       
    77   
       
    78           if (!dbus_message_append_args (echo_reply,
       
    79                                  DBUS_TYPE_STRING, &s,
       
    80                                  DBUS_TYPE_INVALID))
       
    81             die ("No memory");
       
    82         }
       
    83         
       
    84       if (!dbus_connection_send (connection, echo_reply, NULL))
       
    85         die ("No memory\n");
       
    86       
       
    87       dbus_message_unref (echo_reply);
       
    88     }
       
    89   else if (type == DBUS_MESSAGE_TYPE_ERROR)
       
    90     {
       
    91       dbus_set_error_from_message (&error, reply);
       
    92       printf ("Error type in reply: %s\n", error.message);
       
    93 
       
    94       if (strcmp (error.name, DBUS_ERROR_NO_MEMORY) != 0)
       
    95         {
       
    96             echo_reply = dbus_message_new_error (echo_reply,
       
    97                                       error.name,
       
    98                                       error.message);
       
    99 
       
   100             if (echo_reply == NULL)
       
   101               die ("No memory\n");
       
   102 
       
   103             if (!dbus_connection_send (connection, echo_reply, NULL))
       
   104               die ("No memory\n");
       
   105 
       
   106             dbus_message_unref (echo_reply);
       
   107         }
       
   108       dbus_error_free (&error);
       
   109     }
       
   110   else
       
   111      _dbus_assert_not_reached ("Unexpected message received\n");
       
   112 
       
   113   hello_from_self_reply_received = TRUE;
       
   114   
       
   115   dbus_message_unref (reply);
       
   116   dbus_message_unref (echo_message);
       
   117   dbus_pending_call_unref (pcall);
       
   118   dbus_connection_unref (connection);
       
   119 }
       
   120 
       
   121 static DBusHandlerResult
       
   122 handle_run_hello_from_self (DBusConnection     *connection,
       
   123                                                DBusMessage        *message)
       
   124 {
       
   125   DBusError error;
       
   126   DBusMessage *reply, *self_message;
       
   127   DBusPendingCall *pcall;
       
   128   char *s;
       
   129 
       
   130   _dbus_verbose ("sending reply to Echo method\n");
       
   131   
       
   132   dbus_error_init (&error);
       
   133   
       
   134   if (!dbus_message_get_args (message,
       
   135                               &error,
       
   136                               DBUS_TYPE_STRING, &s,
       
   137                               DBUS_TYPE_INVALID))
       
   138     {
       
   139       reply = dbus_message_new_error (message,
       
   140                                       error.name,
       
   141                                       error.message);
       
   142 
       
   143       if (reply == NULL)
       
   144         die ("No memory\n");
       
   145 
       
   146       if (!dbus_connection_send (connection, reply, NULL))
       
   147         die ("No memory\n");
       
   148 
       
   149       dbus_message_unref (reply);
       
   150 
       
   151       return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
       
   152     }
       
   153     printf ("Sending HelloFromSelf\n");
       
   154 
       
   155  _dbus_verbose ("*** Sending message to self\n");
       
   156  self_message = dbus_message_new_method_call ("org.freedesktop.DBus.TestSuiteEchoService",
       
   157                                           "/org/freedesktop/TestSuite",
       
   158                                           "org.freedesktop.TestSuite",
       
   159                                           "HelloFromSelf");
       
   160   
       
   161   if (self_message == NULL)
       
   162     die ("No memory");
       
   163   
       
   164   if (!dbus_connection_send_with_reply (connection, self_message, &pcall, -1))
       
   165     die("No memory");
       
   166   
       
   167   dbus_message_ref (message);
       
   168   if (!dbus_pending_call_set_notify (pcall, check_hello_from_self_reply, (void *)message, NULL))
       
   169     die("No memory");
       
   170     
       
   171   printf ("Sent HelloFromSelf\n");
       
   172   return DBUS_HANDLER_RESULT_HANDLED;
       
   173 }
       
   174 
       
   175 static DBusHandlerResult
       
   176 handle_echo (DBusConnection     *connection,
       
   177              DBusMessage        *message)
       
   178 {
       
   179   DBusError error;
       
   180   DBusMessage *reply;
       
   181   char *s;
       
   182 
       
   183   _dbus_verbose ("sending reply to Echo method\n");
       
   184   
       
   185   dbus_error_init (&error);
       
   186   
       
   187   if (!dbus_message_get_args (message,
       
   188                               &error,
       
   189                               DBUS_TYPE_STRING, &s,
       
   190                               DBUS_TYPE_INVALID))
       
   191     {
       
   192       reply = dbus_message_new_error (message,
       
   193                                       error.name,
       
   194                                       error.message);
       
   195 
       
   196       if (reply == NULL)
       
   197         die ("No memory\n");
       
   198 
       
   199       if (!dbus_connection_send (connection, reply, NULL))
       
   200         die ("No memory\n");
       
   201 
       
   202       dbus_message_unref (reply);
       
   203 
       
   204       return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
       
   205     }
       
   206 
       
   207   reply = dbus_message_new_method_return (message);
       
   208   if (reply == NULL)
       
   209     die ("No memory\n");
       
   210   
       
   211   if (!dbus_message_append_args (reply,
       
   212                                  DBUS_TYPE_STRING, &s,
       
   213                                  DBUS_TYPE_INVALID))
       
   214     die ("No memory");
       
   215   
       
   216   if (!dbus_connection_send (connection, reply, NULL))
       
   217     die ("No memory\n");
       
   218 
       
   219   fprintf (stderr, "Echo service echoed string: \"%s\"\n", s);
       
   220   
       
   221   dbus_message_unref (reply);
       
   222     
       
   223   return DBUS_HANDLER_RESULT_HANDLED;
       
   224 }
       
   225 
       
   226 static void
       
   227 path_unregistered_func (DBusConnection  *connection,
       
   228                         void            *user_data)
       
   229 {
       
   230   /* connection was finalized */
       
   231 }
       
   232 
       
   233 static DBusHandlerResult
       
   234 path_message_func (DBusConnection  *connection,
       
   235                    DBusMessage     *message,
       
   236                    void            *user_data)
       
   237 {
       
   238   if (dbus_message_is_method_call (message,
       
   239                                    "org.freedesktop.TestSuite",
       
   240                                    "Echo"))
       
   241     return handle_echo (connection, message);
       
   242   else if (dbus_message_is_method_call (message,
       
   243                                         "org.freedesktop.TestSuite",
       
   244                                         "Exit"))
       
   245     {
       
   246       quit ();
       
   247       return DBUS_HANDLER_RESULT_HANDLED;
       
   248     }
       
   249   else if (dbus_message_is_method_call (message,
       
   250                                         "org.freedesktop.TestSuite",
       
   251                                         "EmitFoo"))
       
   252     {
       
   253       /* Emit the Foo signal */
       
   254       DBusMessage *signal;
       
   255       double v_DOUBLE;
       
   256 
       
   257       _dbus_verbose ("emitting signal Foo\n");
       
   258       
       
   259       signal = dbus_message_new_signal ("/org/freedesktop/TestSuite",
       
   260                                         "org.freedesktop.TestSuite",
       
   261                                         "Foo");
       
   262       if (signal == NULL)
       
   263         die ("No memory\n");
       
   264 
       
   265       v_DOUBLE = 42.6;
       
   266       if (!dbus_message_append_args (signal,
       
   267                                      DBUS_TYPE_DOUBLE, &v_DOUBLE,
       
   268                                      DBUS_TYPE_INVALID))
       
   269         die ("No memory");
       
   270   
       
   271       if (!dbus_connection_send (connection, signal, NULL))
       
   272         die ("No memory\n");
       
   273       
       
   274       return DBUS_HANDLER_RESULT_HANDLED;
       
   275     }
       
   276     
       
   277   else if (dbus_message_is_method_call (message,
       
   278                                    "org.freedesktop.TestSuite",
       
   279                                    "RunHelloFromSelf"))
       
   280     {
       
   281       return handle_run_hello_from_self (connection, message);
       
   282     }
       
   283   else if (dbus_message_is_method_call (message,
       
   284                                         "org.freedesktop.TestSuite",
       
   285                                         "HelloFromSelf"))
       
   286     {
       
   287         DBusMessage *reply;
       
   288         printf ("Received the HelloFromSelf message\n");
       
   289         
       
   290         reply = dbus_message_new_method_return (message);
       
   291         if (reply == NULL)
       
   292           die ("No memory");
       
   293         
       
   294         if (!dbus_connection_send (connection, reply, NULL))
       
   295           die ("No memory");
       
   296 
       
   297         return DBUS_HANDLER_RESULT_HANDLED;
       
   298     }
       
   299   else
       
   300     return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
       
   301 }
       
   302 
       
   303 static DBusObjectPathVTable
       
   304 echo_vtable = {
       
   305   path_unregistered_func,
       
   306   path_message_func,
       
   307   NULL,
       
   308 };
       
   309 
       
   310 
       
   311 static const char* echo_path = "/org/freedesktop/TestSuite" ;
       
   312 
       
   313 static DBusHandlerResult
       
   314 filter_func (DBusConnection     *connection,
       
   315              DBusMessage        *message,
       
   316              void               *user_data)
       
   317 {
       
   318   if (dbus_message_is_signal (message,
       
   319                               DBUS_INTERFACE_LOCAL,
       
   320                               "Disconnected"))
       
   321     {
       
   322       quit ();
       
   323       return DBUS_HANDLER_RESULT_HANDLED;
       
   324     }
       
   325   else
       
   326     {
       
   327       return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
       
   328     }
       
   329 }
       
   330 
       
   331 int
       
   332 main ()
       
   333 {
       
   334   DBusError error;
       
   335   int result,bus_get = 1;
       
   336   DBusConnection *connection;
       
   337   
       
   338   dbus_error_init (&error);
       
   339   connection = dbus_bus_get(DBUS_BUS_SESSION, &error);
       
   340  if(connection == NULL)
       
   341  {
       
   342  		
       
   343  		dbus_error_free(&error);
       
   344  		/*test-service.exe spawned by dbus_daemon_tets_main was returning NULL for dbus_bus_get(), work around for this is to use below API */
       
   345 		connection =  dbus_connection_open_private ("tcp:host=localhost,port=12436", &error);
       
   346  		bus_get = 0;
       
   347  }
       
   348   if (connection == NULL)
       
   349     {
       
   350       fprintf (stderr, "*** Failed to open connection to activating message bus: %s\n",
       
   351                error.message);
       
   352       dbus_error_free (&error);
       
   353       return 1;
       
   354     }
       
   355 
       
   356 if( !bus_get)
       
   357 {
       
   358 
       
   359 	if (!dbus_bus_register (connection, &error))
       
   360     {
       
   361      
       
   362       //_dbus_connection_close_possibly_shared (connection);
       
   363       dbus_connection_unref (connection);  
       
   364       return -1;
       
   365     }
       
   366 }
       
   367   loop = _dbus_loop_new ();
       
   368   if (loop == NULL)
       
   369     die ("No memory\n");
       
   370   
       
   371   if (!test_connection_setup (loop, connection))
       
   372     die ("No memory\n");
       
   373 
       
   374   if (!dbus_connection_add_filter (connection,
       
   375                                    filter_func, NULL, NULL))
       
   376     die ("No memory");
       
   377 
       
   378   if (!dbus_connection_register_object_path (connection,
       
   379                                              echo_path,
       
   380                                              &echo_vtable,
       
   381                                              (void*) 0xdeadbeef))
       
   382     die ("No memory");
       
   383 
       
   384   {
       
   385     void *d;
       
   386     if (!dbus_connection_get_object_path_data (connection, echo_path, &d))
       
   387       die ("No memory");
       
   388     if (d != (void*) 0xdeadbeef)
       
   389       die ("dbus_connection_get_object_path_data() doesn't seem to work right\n");
       
   390   }
       
   391   
       
   392   result = dbus_bus_request_name (connection, "org.freedesktop.DBus.TestSuiteEchoService",
       
   393                                   0, &error);
       
   394   if (dbus_error_is_set (&error))
       
   395     {
       
   396       fprintf (stderr, "Error %s\n", error.message);
       
   397       _dbus_verbose ("*** Failed to acquire service: %s\n",
       
   398                      error.message);
       
   399       dbus_error_free (&error);
       
   400       exit (1);
       
   401     }
       
   402   
       
   403   _dbus_verbose ("*** Test service entering main loop\n");
       
   404   _dbus_loop_run (loop);
       
   405   
       
   406   test_connection_shutdown (loop, connection);
       
   407 
       
   408   dbus_connection_remove_filter (connection, filter_func, NULL);
       
   409   
       
   410   if( !bus_get)
       
   411   		dbus_connection_close(connection);
       
   412   			
       
   413   dbus_connection_unref (connection);
       
   414 
       
   415   _dbus_loop_unref (loop);
       
   416   loop = NULL;
       
   417   
       
   418   dbus_free(loop);
       
   419   dbus_shutdown ();
       
   420 
       
   421   _dbus_verbose ("*** Test service exiting\n");
       
   422   
       
   423   return 0;
       
   424 }