~jamesodhunt/ubuntu/raring/upstart/1.6

« back to all changes in this revision

Viewing changes to nih-dbus-tool/tests/expected/test_method_object_function_standard.c

  • Committer: Scott James Remnant
  • Date: 2010-02-04 23:39:59 UTC
  • mfrom: (1182.1.45 upstart)
  • mto: This revision was merged to the branch mainline in revision 1250.
  • Revision ID: scott@netsplit.com-20100204233959-7kajqjnaoh7208ob
Tags: upstream-0.6.5
ImportĀ upstreamĀ versionĀ 0.6.5

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
DBusHandlerResult
2
 
my_com_netsplit_Nih_Test_Method_method (NihDBusObject * object,
3
 
                                        NihDBusMessage *message)
4
 
{
5
 
        DBusMessageIter iter;
6
 
        DBusMessage *   reply;
7
 
        char *          str;
8
 
        const char *    str_dbus;
9
 
        int32_t         flags;
10
 
        char **         output;
11
 
        DBusMessageIter output_iter;
12
 
 
13
 
        nih_assert (object != NULL);
14
 
        nih_assert (message != NULL);
15
 
 
16
 
        /* Iterate the arguments to the message and demarshal into arguments
17
 
         * for our own function call.
18
 
         */
19
 
        dbus_message_iter_init (message->message, &iter);
20
 
 
21
 
        /* Demarshal a char * from the message */
22
 
        if (dbus_message_iter_get_arg_type (&iter) != DBUS_TYPE_STRING) {
23
 
                reply = dbus_message_new_error (message->message, DBUS_ERROR_INVALID_ARGS,
24
 
                                                "Invalid arguments to Method method");
25
 
                if (! reply)
26
 
                        return DBUS_HANDLER_RESULT_NEED_MEMORY;
27
 
 
28
 
                if (! dbus_connection_send (message->connection, reply, NULL)) {
29
 
                        dbus_message_unref (reply);
30
 
                        return DBUS_HANDLER_RESULT_NEED_MEMORY;
31
 
                }
32
 
 
33
 
                dbus_message_unref (reply);
34
 
                return DBUS_HANDLER_RESULT_HANDLED;
35
 
        }
36
 
 
37
 
        dbus_message_iter_get_basic (&iter, &str_dbus);
38
 
 
39
 
        str = nih_strdup (message, str_dbus);
40
 
        if (! str) {
41
 
                return DBUS_HANDLER_RESULT_NEED_MEMORY;
42
 
        }
43
 
 
44
 
        dbus_message_iter_next (&iter);
45
 
 
46
 
        /* Demarshal a int32_t from the message */
47
 
        if (dbus_message_iter_get_arg_type (&iter) != DBUS_TYPE_INT32) {
48
 
                reply = dbus_message_new_error (message->message, DBUS_ERROR_INVALID_ARGS,
49
 
                                                "Invalid arguments to Method method");
50
 
                if (! reply)
51
 
                        return DBUS_HANDLER_RESULT_NEED_MEMORY;
52
 
 
53
 
                if (! dbus_connection_send (message->connection, reply, NULL)) {
54
 
                        dbus_message_unref (reply);
55
 
                        return DBUS_HANDLER_RESULT_NEED_MEMORY;
56
 
                }
57
 
 
58
 
                dbus_message_unref (reply);
59
 
                return DBUS_HANDLER_RESULT_HANDLED;
60
 
        }
61
 
 
62
 
        dbus_message_iter_get_basic (&iter, &flags);
63
 
 
64
 
        dbus_message_iter_next (&iter);
65
 
 
66
 
        if (dbus_message_iter_get_arg_type (&iter) != DBUS_TYPE_INVALID) {
67
 
                reply = dbus_message_new_error (message->message, DBUS_ERROR_INVALID_ARGS,
68
 
                                                "Invalid arguments to Method method");
69
 
                if (! reply)
70
 
                        return DBUS_HANDLER_RESULT_NEED_MEMORY;
71
 
 
72
 
                if (! dbus_connection_send (message->connection, reply, NULL)) {
73
 
                        dbus_message_unref (reply);
74
 
                        return DBUS_HANDLER_RESULT_NEED_MEMORY;
75
 
                }
76
 
 
77
 
                dbus_message_unref (reply);
78
 
                return DBUS_HANDLER_RESULT_HANDLED;
79
 
        }
80
 
 
81
 
        /* Call the handler function */
82
 
        nih_error_push_context ();
83
 
        if (my_method (object->data, message, str, flags, &output) < 0) {
84
 
                NihError *err;
85
 
 
86
 
                err = nih_error_get ();
87
 
                if (err->number == ENOMEM) {
88
 
                        nih_free (err);
89
 
                        nih_error_pop_context ();
90
 
 
91
 
                        return DBUS_HANDLER_RESULT_NEED_MEMORY;
92
 
                } else if (err->number == NIH_DBUS_ERROR) {
93
 
                        NihDBusError *dbus_err = (NihDBusError *)err;
94
 
 
95
 
                        reply = NIH_MUST (dbus_message_new_error (message->message, dbus_err->name, err->message));
96
 
                        nih_free (err);
97
 
                        nih_error_pop_context ();
98
 
 
99
 
                        NIH_MUST (dbus_connection_send (message->connection, reply, NULL));
100
 
 
101
 
                        dbus_message_unref (reply);
102
 
                        return DBUS_HANDLER_RESULT_HANDLED;
103
 
                } else {
104
 
                        reply = NIH_MUST (dbus_message_new_error (message->message, DBUS_ERROR_FAILED, err->message));
105
 
                        nih_free (err);
106
 
                        nih_error_pop_context ();
107
 
 
108
 
                        NIH_MUST (dbus_connection_send (message->connection, reply, NULL));
109
 
 
110
 
                        dbus_message_unref (reply);
111
 
                        return DBUS_HANDLER_RESULT_HANDLED;
112
 
                }
113
 
        }
114
 
        nih_error_pop_context ();
115
 
 
116
 
        /* If the sender doesn't care about a reply, don't bother wasting
117
 
         * effort constructing and sending one.
118
 
         */
119
 
        if (dbus_message_get_no_reply (message->message))
120
 
                return DBUS_HANDLER_RESULT_HANDLED;
121
 
 
122
 
        do {
123
 
                __label__ enomem;
124
 
 
125
 
                /* Construct the reply message. */
126
 
                reply = dbus_message_new_method_return (message->message);
127
 
                if (! reply)
128
 
                        goto enomem;
129
 
 
130
 
                dbus_message_iter_init_append (reply, &iter);
131
 
 
132
 
                /* Marshal an array onto the message */
133
 
                if (! dbus_message_iter_open_container (&iter, DBUS_TYPE_ARRAY, "s", &output_iter)) {
134
 
                        dbus_message_unref (reply);
135
 
                        reply = NULL;
136
 
                        goto enomem;
137
 
                }
138
 
 
139
 
                for (size_t output_i = 0; output[output_i]; output_i++) {
140
 
                        const char *output_element;
141
 
 
142
 
                        output_element = output[output_i];
143
 
 
144
 
                        /* Marshal a char * onto the message */
145
 
                        if (! dbus_message_iter_append_basic (&output_iter, DBUS_TYPE_STRING, &output_element)) {
146
 
                                dbus_message_iter_abandon_container (&iter, &output_iter);
147
 
                                dbus_message_unref (reply);
148
 
                                reply = NULL;
149
 
                                goto enomem;
150
 
                        }
151
 
                }
152
 
 
153
 
                if (! dbus_message_iter_close_container (&iter, &output_iter)) {
154
 
                        dbus_message_unref (reply);
155
 
                        reply = NULL;
156
 
                        goto enomem;
157
 
                }
158
 
        enomem: __attribute__ ((unused));
159
 
        } while (! reply);
160
 
 
161
 
        /* Send the reply, appending it to the outgoing queue. */
162
 
        NIH_MUST (dbus_connection_send (message->connection, reply, NULL));
163
 
 
164
 
        dbus_message_unref (reply);
165
 
 
166
 
        return DBUS_HANDLER_RESULT_HANDLED;
167
 
}