1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */ |
|
2 /* |
|
3 * Copyright (C) 2003 Imendio AB |
|
4 * |
|
5 * This program is free software; you can redistribute it and/or |
|
6 * modify it under the terms of the GNU Lesser General Public License as |
|
7 * published by the Free Software Foundation; either version 2 of the |
|
8 * License, or (at your option) any later version. |
|
9 * |
|
10 * This program is distributed in the hope that it will be useful, |
|
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
13 * Lesser General Public License for more details. |
|
14 * |
|
15 * You should have received a copy of the GNU Lesser General Public |
|
16 * License along with this program; if not, write to the |
|
17 * Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
|
18 * Boston, MA 02111-1307, USA. |
|
19 */ |
|
20 |
|
21 #include <config.h> |
|
22 |
|
23 #include "lm-internals.h" |
|
24 #include "lm-message-handler.h" |
|
25 #include "lm-debug.h" |
|
26 struct LmMessageHandler { |
|
27 gboolean valid; |
|
28 gint ref_count; |
|
29 LmHandleMessageFunction function; |
|
30 gpointer user_data; |
|
31 GDestroyNotify notify; |
|
32 }; |
|
33 |
|
34 LmHandlerResult |
|
35 _lm_message_handler_handle_message (LmMessageHandler *handler, |
|
36 LmConnection *connection, |
|
37 LmMessage *message) |
|
38 { |
|
39 g_return_val_if_fail (handler != NULL, |
|
40 LM_HANDLER_RESULT_ALLOW_MORE_HANDLERS); |
|
41 lm_verbose("\nCalling handler in handler_handle_message\n"); |
|
42 if (!handler->valid) { |
|
43 return LM_HANDLER_RESULT_ALLOW_MORE_HANDLERS; |
|
44 } |
|
45 |
|
46 if (handler->function) { |
|
47 lm_verbose("In handler msg Node name: %s", message->node->name); |
|
48 return (* handler->function) (handler, connection, |
|
49 message, handler->user_data); |
|
50 } |
|
51 lm_verbose("could not call handler: go for more handlers\n"); |
|
52 return LM_HANDLER_RESULT_ALLOW_MORE_HANDLERS; |
|
53 } |
|
54 |
|
55 /** |
|
56 * lm_message_handler_new: |
|
57 * @function: a callback |
|
58 * @user_data: user data passed to function |
|
59 * @notify: function called when the message handler is freed |
|
60 * |
|
61 * Creates a new message handler. This can be set to handle incoming messages |
|
62 * and when a message of the type the handler is registered to handle is |
|
63 * received @function will be called and @user_data will be passed to it. |
|
64 * @notify is called when the message handler is freed, that way any memory |
|
65 * allocated by @user_data can be freed. |
|
66 * |
|
67 * Return value: a newly created message handler |
|
68 **/ |
|
69 EXPORT_C LmMessageHandler * |
|
70 lm_message_handler_new (LmHandleMessageFunction function, |
|
71 gpointer user_data, |
|
72 GDestroyNotify notify) |
|
73 { |
|
74 LmMessageHandler *handler; |
|
75 |
|
76 g_return_val_if_fail (function != NULL, NULL); |
|
77 |
|
78 handler = g_new0 (LmMessageHandler, 1); |
|
79 |
|
80 if (handler == NULL) { |
|
81 return NULL; |
|
82 } |
|
83 |
|
84 handler->valid = TRUE; |
|
85 handler->ref_count = 1; |
|
86 handler->function = function; |
|
87 handler->user_data = user_data; |
|
88 handler->notify = notify; |
|
89 |
|
90 return handler; |
|
91 } |
|
92 |
|
93 /** |
|
94 * lm_message_handler_invalidate |
|
95 * @handler: an #LmMessageHandler |
|
96 * |
|
97 * Invalidates the handler. Useful if you need to cancel a reply |
|
98 **/ |
|
99 EXPORT_C void |
|
100 lm_message_handler_invalidate (LmMessageHandler *handler) |
|
101 { |
|
102 handler->valid = FALSE; |
|
103 } |
|
104 |
|
105 /** |
|
106 * lm_message_handler_is_valid |
|
107 * @handler: an #LmMessageHandler |
|
108 * |
|
109 * Fetches whether the handler is valid or not. |
|
110 * |
|
111 * Return value: #TRUE if @handler is valid, otherwise #FALSE |
|
112 **/ |
|
113 EXPORT_C gboolean |
|
114 lm_message_handler_is_valid (LmMessageHandler *handler) |
|
115 { |
|
116 g_return_val_if_fail (handler != NULL, FALSE); |
|
117 |
|
118 return handler->valid; |
|
119 } |
|
120 |
|
121 /** |
|
122 * lm_message_handler_ref: |
|
123 * @handler: an #LmMessageHandler |
|
124 * |
|
125 * Adds a reference to @handler. |
|
126 * |
|
127 * Return value: the message handler |
|
128 **/ |
|
129 EXPORT_C LmMessageHandler * |
|
130 lm_message_handler_ref (LmMessageHandler *handler) |
|
131 { |
|
132 g_return_val_if_fail (handler != NULL, NULL); |
|
133 |
|
134 handler->ref_count++; |
|
135 |
|
136 return handler; |
|
137 } |
|
138 |
|
139 /** |
|
140 * lm_message_handler_unref: |
|
141 * @handler: an #LmMessagHandler |
|
142 * |
|
143 * Removes a reference from @handler. When no more references are present the |
|
144 * handler is freed. |
|
145 **/ |
|
146 EXPORT_C void |
|
147 lm_message_handler_unref (LmMessageHandler *handler) |
|
148 { |
|
149 g_return_if_fail (handler != NULL); |
|
150 |
|
151 handler->ref_count --; |
|
152 |
|
153 if (handler->ref_count == 0) { |
|
154 if (handler->notify) { |
|
155 (* handler->notify) (handler->user_data); |
|
156 } |
|
157 g_free (handler); |
|
158 } |
|
159 } |
|
160 |
|