1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
|
/*
* libosengine - A synchronization engine for the opensync framework
* Copyright (C) 2004-2005 Armin Bauer <armin.bauer@opensync.org>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
#include "opensync.h"
#include "opensync_internals.h"
/**
* @ingroup OSEngineMessage
* @brief A Message used by the inter thread messaging library
*
*/
/*@{*/
/*! @brief Creates a new message of the given type
*
* This function will create a new message of the given type, with
* the given parent and signal name. The parent will be passed to the OSyncMessageHandler
*
* @param parent Who send this message. Can be any pointer.
* @param msgname The name of the message
* @param type The type of this message
* @returns Pointer to a newly allocated message
*
*/
OSyncMessage *osync_message_new(OSyncMessageCommand cmd, int size, OSyncError **error)
{
OSyncMessage *message = osync_try_malloc0(sizeof(OSyncMessage), error);
if (!message)
return NULL;
message->cmd = cmd;
message->refCount = 1;
if (size > 0)
message->buffer = g_byte_array_sized_new( size );
else
message->buffer = g_byte_array_new();
message->buffer_read_pos = 0;
return message;
}
void osync_message_ref(OSyncMessage *message)
{
g_atomic_int_inc(&(message->refCount));
}
void osync_message_unref(OSyncMessage *message)
{
if (g_atomic_int_dec_and_test(&(message->refCount))) {
g_byte_array_free(message->buffer, TRUE);
g_free(message);
}
}
/*! @brief Sets the handler that will receive the reply
*
* @param message The message to work on
* @param replyqueue Which queue should receive the reply
* @param handler Which handler should be called when the reply is received
* @param user_data Which user data should be passed to the handler
*
*/
void osync_message_set_handler(OSyncMessage *message, OSyncMessageHandler handler, gpointer user_data)
{
osync_trace(TRACE_INTERNAL, "%p handler to %p", message, user_data);
message->user_data = user_data;
message->callback = handler;
}
/*! @brief Creates a new reply
*
* @param parent Who send this message. Can be any pointer.
* @param message The message to which you wish to reply
* @returns Pointer to a newly allocated message
*
*/
OSyncMessage *osync_message_new_reply(OSyncMessage *message, OSyncError **error)
{
OSyncMessage *reply = osync_message_new(OSYNC_MESSAGE_REPLY, 0, error);
if (!reply)
return NULL;
reply->id1 = message->id1;
reply->id2 = message->id2;
return reply;
}
/*! @brief Creates a new error reply
*
* @param parent Who send this message. Can be any pointer.
* @param message The message to which you wish to reply
* @returns Pointer to a newly allocated message
*/
OSyncMessage *osync_message_new_errorreply(OSyncMessage *message, OSyncError **error)
{
OSyncMessage *reply = osync_message_new(OSYNC_MESSAGE_ERRORREPLY, 0, error);
if (!reply)
return NULL;
reply->id1 = message->id1;
reply->id2 = message->id2;
return reply;
}
OSyncMessage *osync_message_new_error(OSyncError *error, OSyncError **loc_error)
{
OSyncMessage *message = osync_message_new(OSYNC_MESSAGE_ERROR, 0, loc_error);
if (!message)
return NULL;
osync_marshal_error(message, error);
return message;
}
/*! @brief Checks if the message is a error
*
* @param message The message to check
* @return #TRUE if the message is a error, #FALSE otherwise
*
*/
gboolean osync_message_is_error(OSyncMessage *message)
{
if (message->cmd == OSYNC_MESSAGE_ERRORREPLY)
return TRUE;
return FALSE;
}
osync_bool osync_message_is_answered(OSyncMessage *message)
{
return message->is_answered;
}
void osync_message_set_answered(OSyncMessage *message)
{
message->is_answered = TRUE;
}
/*! @brief Gets the command from a message
*
* This function will return the command of a message
*
* @param message The message
* @returns the command
*/
OSyncMessageCommand osync_message_get_command(OSyncMessage *message)
{
g_assert(message);
return message->cmd;
}
/*@}*/
void osync_message_write_int(OSyncMessage *message, int value)
{
g_byte_array_append( message->buffer, (unsigned char*)&value, sizeof( int ) );
}
void osync_message_write_long_long_int(OSyncMessage *message, long long int value)
{
g_byte_array_append( message->buffer, (unsigned char*)&value, sizeof( long long int ) );
}
void osync_message_write_string(OSyncMessage *message, const char *value)
{
int length = 0;
if (value == NULL) {
length = -1;
g_byte_array_append( message->buffer, (unsigned char*)&length, sizeof( int ) );
} else {
int length = strlen( value ) + 1;
g_byte_array_append( message->buffer, (unsigned char*)&length, sizeof( int ) );
g_byte_array_append( message->buffer, (unsigned char*)value, length );
}
}
void osync_message_write_data(OSyncMessage *message, const void *value, int size)
{
g_byte_array_append( message->buffer, value, size );
}
void osync_message_read_int(OSyncMessage *message, int *value)
{
memcpy(value, &(message->buffer->data[ message->buffer_read_pos ]), sizeof(int));
message->buffer_read_pos += sizeof(int);
}
void osync_message_read_long_long_int(OSyncMessage *message, long long int *value)
{
memcpy(value, &(message->buffer->data[ message->buffer_read_pos ]), sizeof(long long int));
message->buffer_read_pos += sizeof(long long int);
}
void osync_message_read_const_string(OSyncMessage *message, char **value)
{
int length = 0;
memcpy(&length, &(message->buffer->data[ message->buffer_read_pos ]), sizeof(int));
message->buffer_read_pos += sizeof(int);
if (length == -1) {
*value = NULL;
return;
}
*value = (char *)&(message->buffer->data[message->buffer_read_pos]);
message->buffer_read_pos += length;
}
void osync_message_read_string(OSyncMessage *message, char **value)
{
int length = 0;
memcpy(&length, &(message->buffer->data[ message->buffer_read_pos ]), sizeof(int));
message->buffer_read_pos += sizeof(int);
if (length == -1) {
*value = NULL;
return;
}
*value = (char*)malloc(length);
memcpy(*value, &(message->buffer->data[ message->buffer_read_pos ]), length );
message->buffer_read_pos += length;
}
void osync_message_read_const_data(OSyncMessage *message, void **value, int size)
{
*value = &(message->buffer->data[message->buffer_read_pos]);
message->buffer_read_pos += size;
}
void osync_message_read_data(OSyncMessage *message, void *value, int size)
{
memcpy(value, &(message->buffer->data[ message->buffer_read_pos ]), size );
message->buffer_read_pos += size;
}
|