ofdbus/dbus/tsrc/testapps/dbus_test_cases/test-shell-service.c
changeset 31 ce057bb09d0b
child 34 5fae379060a7
equal deleted inserted replaced
30:e20de85af2ee 31:ce057bb09d0b
       
     1 /*
       
     2 * Copyright (c) 2008 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 #include "test-utils.h"
       
    19 
       
    20 static DBusLoop *loop;
       
    21 static dbus_bool_t already_quit = FALSE;
       
    22 static const char* echo_path = "/org/freedesktop/TestSuite";
       
    23 
       
    24 typedef struct
       
    25 {
       
    26   int argc;
       
    27   char **argv;
       
    28 } EchoData;
       
    29 
       
    30 static void
       
    31 quit (void)
       
    32 {
       
    33   if (!already_quit)
       
    34     {
       
    35       _dbus_loop_quit (loop);
       
    36       already_quit = TRUE;
       
    37     }
       
    38 }
       
    39 
       
    40 static void
       
    41 die (const char *message)
       
    42 {
       
    43   fprintf (stderr, "*** test-service: %s", message);
       
    44   exit (1);
       
    45 }
       
    46 
       
    47 static DBusHandlerResult
       
    48 handle_echo (DBusConnection     *connection,
       
    49              DBusMessage        *message)
       
    50 {
       
    51   DBusError error;
       
    52   DBusMessage *reply;
       
    53   DBusMessageIter iter;
       
    54   int i;
       
    55   EchoData *d;
       
    56 
       
    57   _dbus_verbose ("sending reply to Echo method\n");
       
    58 
       
    59   if (!dbus_connection_get_object_path_data (connection, echo_path, (void **)&d))
       
    60       die ("No memory");
       
    61 
       
    62 
       
    63   dbus_error_init (&error);
       
    64 
       
    65   reply = dbus_message_new_method_return (message);
       
    66   if (reply == NULL)
       
    67     die ("No memory\n");
       
    68   
       
    69   dbus_message_iter_init_append (reply, &iter);
       
    70   for (i = 0; i < d->argc; ++i)
       
    71     if (!dbus_message_iter_append_basic (&iter, DBUS_TYPE_STRING, &(d->argv[i])))
       
    72       die ("No memory\n");
       
    73 
       
    74   if (!dbus_connection_send (connection, reply, NULL))
       
    75     die ("No memory\n");
       
    76 
       
    77   fprintf (stderr, "Shell echo service echoed the command line\n");
       
    78   
       
    79   dbus_message_unref (reply);
       
    80     
       
    81   return DBUS_HANDLER_RESULT_HANDLED;
       
    82 }
       
    83 
       
    84 static void
       
    85 path_unregistered_func (DBusConnection  *connection,
       
    86                         void            *user_data)
       
    87 {
       
    88   /* connection was finalized */
       
    89 }
       
    90 
       
    91 static DBusHandlerResult
       
    92 path_message_func (DBusConnection  *connection,
       
    93                    DBusMessage     *message,
       
    94                    void            *user_data)
       
    95 {
       
    96   if (dbus_message_is_method_call (message,
       
    97                                    "org.freedesktop.TestSuite",
       
    98                                    "Echo"))
       
    99     return handle_echo (connection, message);
       
   100   else if (dbus_message_is_method_call (message,
       
   101                                         "org.freedesktop.TestSuite",
       
   102                                         "Exit"))
       
   103     {
       
   104       quit ();
       
   105       return DBUS_HANDLER_RESULT_HANDLED;
       
   106     }
       
   107   else
       
   108     return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
       
   109 }
       
   110 
       
   111 static DBusObjectPathVTable
       
   112 echo_vtable = {
       
   113   path_unregistered_func,
       
   114   path_message_func,
       
   115   NULL,
       
   116 };
       
   117 
       
   118 static DBusHandlerResult
       
   119 filter_func (DBusConnection     *connection,
       
   120              DBusMessage        *message,
       
   121              void               *user_data)
       
   122 {
       
   123   if (dbus_message_is_signal (message,
       
   124                               DBUS_INTERFACE_LOCAL,
       
   125                               "Disconnected"))
       
   126     {
       
   127       quit ();
       
   128       return DBUS_HANDLER_RESULT_HANDLED;
       
   129     }
       
   130   else
       
   131     {
       
   132       return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
       
   133     }
       
   134 }
       
   135 
       
   136 int
       
   137 main (int    argc,
       
   138       char **argv)
       
   139 {
       
   140   DBusConnection *connection;
       
   141   DBusError error;
       
   142   EchoData echo_data;
       
   143   int result;
       
   144   
       
   145   echo_data.argc = argc;
       
   146   echo_data.argv = argv;
       
   147   
       
   148   dbus_error_init (&error);
       
   149   connection = dbus_bus_get (DBUS_BUS_STARTER, &error);
       
   150   if (connection == NULL)
       
   151     {
       
   152       fprintf (stderr, "*** Failed to open connection to activating message bus: %s\n",
       
   153                error.message);
       
   154       dbus_error_free (&error);
       
   155       return 1;
       
   156     }
       
   157 
       
   158   loop = _dbus_loop_new ();
       
   159   if (loop == NULL)
       
   160     die ("No memory\n");
       
   161   
       
   162   if (!test_connection_setup (loop, connection))
       
   163     die ("No memory\n");
       
   164 
       
   165   if (!dbus_connection_add_filter (connection,
       
   166                                    filter_func, NULL, NULL))
       
   167     die ("No memory");
       
   168 
       
   169   if (!dbus_connection_register_object_path (connection,
       
   170                                              echo_path,
       
   171                                              &echo_vtable,
       
   172                                              (void*) &echo_data))
       
   173     die ("No memory");
       
   174 
       
   175   {
       
   176     void *d;
       
   177     if (!dbus_connection_get_object_path_data (connection, echo_path, &d))
       
   178       die ("No memory");
       
   179     if (d != (void*) &echo_data)
       
   180       die ("dbus_connection_get_object_path_data() doesn't seem to work right\n");
       
   181   }
       
   182   
       
   183   result = dbus_bus_request_name (connection, "org.freedesktop.DBus.TestSuiteShellEchoServiceSuccess",
       
   184                                   0, &error);
       
   185   if (dbus_error_is_set (&error))
       
   186     {
       
   187       fprintf (stderr, "Error %s\n", error.message);
       
   188       _dbus_verbose ("*** Failed to acquire service: %s\n",
       
   189                      error.message);
       
   190       dbus_error_free (&error);
       
   191       exit (1);
       
   192     }
       
   193 
       
   194   _dbus_verbose ("*** Test service entering main loop\n");
       
   195   _dbus_loop_run (loop);
       
   196 
       
   197   test_connection_shutdown (loop, connection);
       
   198 
       
   199   dbus_connection_remove_filter (connection, filter_func, NULL);
       
   200   
       
   201   dbus_connection_unref (connection);
       
   202 
       
   203   _dbus_loop_unref (loop);
       
   204   loop = NULL;
       
   205   
       
   206   dbus_shutdown ();
       
   207 
       
   208   _dbus_verbose ("*** Test service exiting\n");
       
   209   
       
   210   return 0;
       
   211 }