1 #include <opensrf/osrf_message.h>
3 static char default_locale[17] = "en-US\0\0\0\0\0\0\0\0\0\0\0\0";
4 static char* current_locale = NULL;
6 osrfMessage* osrf_message_init( enum M_TYPE type, int thread_trace, int protocol ) {
8 osrfMessage* msg = (osrfMessage*) safe_malloc(sizeof(osrfMessage));
10 msg->thread_trace = thread_trace;
11 msg->protocol = protocol;
12 msg->status_name = NULL;
13 msg->status_text = NULL;
16 msg->is_exception = 0;
18 msg->_result_content = NULL;
19 msg->result_string = NULL;
20 msg->method_name = NULL;
21 msg->full_param_string = NULL;
22 msg->sender_locale = NULL;
23 msg->sender_tz_offset = 0;
29 const char* osrf_message_get_last_locale() {
30 return current_locale;
33 char* osrf_message_set_locale( osrfMessage* msg, const char* locale ) {
34 if( msg == NULL || locale == NULL ) return NULL;
35 return msg->sender_locale = strdup( locale );
38 const char* osrf_message_set_default_locale( const char* locale ) {
39 if( locale == NULL ) return NULL;
40 if( strlen(locale) > sizeof(default_locale) - 1 ) return NULL;
42 strcpy( default_locale, locale );
43 return (const char*) default_locale;
46 void osrf_message_set_method( osrfMessage* msg, const char* method_name ) {
47 if( msg == NULL || method_name == NULL ) return;
48 msg->method_name = strdup( method_name );
52 void osrf_message_add_object_param( osrfMessage* msg, const jsonObject* o ) {
55 msg->_params = jsonParseString("[]");
56 char* j = jsonObjectToJSON(o);
57 jsonObjectPush(msg->_params, jsonParseString(j));
61 void osrf_message_set_params( osrfMessage* msg, const jsonObject* o ) {
62 if(!msg || !o) return;
64 if(o->type != JSON_ARRAY) {
65 osrfLogDebug( OSRF_LOG_MARK, "passing non-array to osrf_message_set_params(), fixing...");
66 if(msg->_params) jsonObjectFree(msg->_params);
67 jsonObject* clone = jsonObjectClone(o);
68 msg->_params = jsonNewObject(NULL);
69 jsonObjectPush(msg->_params, clone);
73 if(msg->_params) jsonObjectFree(msg->_params);
74 msg->_params = jsonObjectClone(o);
78 /* only works if parse_json_params is false */
79 void osrf_message_add_param( osrfMessage* msg, const char* param_string ) {
80 if(msg == NULL || param_string == NULL) return;
81 if(!msg->_params) msg->_params = jsonParseString("[]");
82 jsonObjectPush(msg->_params, jsonParseString(param_string));
86 void osrf_message_set_status_info( osrfMessage* msg,
87 const char* status_name, const char* status_text, int status_code ) {
90 if( status_name != NULL )
91 msg->status_name = strdup( status_name );
93 if( status_text != NULL )
94 msg->status_text = strdup( status_text );
96 msg->status_code = status_code;
100 void osrf_message_set_result_content( osrfMessage* msg, const char* json_string ) {
101 if( msg == NULL || json_string == NULL) return;
102 msg->result_string = strdup(json_string);
103 if(json_string) msg->_result_content = jsonParseString(json_string);
108 void osrfMessageFree( osrfMessage* msg ) {
109 osrf_message_free( msg );
112 void osrf_message_free( osrfMessage* msg ) {
116 if( msg->status_name != NULL )
117 free(msg->status_name);
119 if( msg->status_text != NULL )
120 free(msg->status_text);
122 if( msg->_result_content != NULL )
123 jsonObjectFree( msg->_result_content );
125 if( msg->result_string != NULL )
126 free( msg->result_string);
128 if( msg->method_name != NULL )
129 free(msg->method_name);
131 if( msg->sender_locale != NULL )
132 free(msg->sender_locale);
134 if( msg->_params != NULL )
135 jsonObjectFree(msg->_params);
141 char* osrfMessageSerializeBatch( osrfMessage* msgs [], int count ) {
142 if( !msgs ) return NULL;
146 const osrfMessage* msg = NULL;
147 jsonObject* wrapper = jsonNewObject(NULL);
149 while( ((msg = msgs[i]) && (i++ < count)) )
150 jsonObjectPush(wrapper, osrfMessageToJSON( msg ));
152 j = jsonObjectToJSON(wrapper);
153 jsonObjectFree(wrapper);
159 char* osrf_message_serialize(const osrfMessage* msg) {
161 if( msg == NULL ) return NULL;
164 jsonObject* json = osrfMessageToJSON( msg );
167 jsonObject* wrapper = jsonNewObject(NULL);
168 jsonObjectPush(wrapper, json);
169 j = jsonObjectToJSON(wrapper);
170 jsonObjectFree(wrapper);
177 jsonObject* osrfMessageToJSON( const osrfMessage* msg ) {
179 jsonObject* json = jsonNewObject(NULL);
180 jsonObjectSetClass(json, "osrfMessage");
183 osrf_clearbuf(sc, sizeof(sc));
187 INT_TO_STRING(msg->thread_trace);
188 jsonObjectSetKey(json, "threadTrace", jsonNewObject(INTSTR));
190 if (msg->sender_locale != NULL) {
191 jsonObjectSetKey(json, "locale", jsonNewObject(msg->sender_locale));
192 } else if (current_locale != NULL) {
193 jsonObjectSetKey(json, "locale", jsonNewObject(current_locale));
195 jsonObjectSetKey(json, "locale", jsonNewObject(default_locale));
198 switch(msg->m_type) {
201 jsonObjectSetKey(json, "type", jsonNewObject("CONNECT"));
205 jsonObjectSetKey(json, "type", jsonNewObject("DISCONNECT"));
209 jsonObjectSetKey(json, "type", jsonNewObject("STATUS"));
210 payload = jsonNewObject(NULL);
211 jsonObjectSetClass(payload, msg->status_name);
212 jsonObjectSetKey(payload, "status", jsonNewObject(msg->status_text));
213 snprintf(sc, sizeof(sc), "%d", msg->status_code);
214 jsonObjectSetKey(payload, "statusCode", jsonNewObject(sc));
215 jsonObjectSetKey(json, "payload", payload);
219 jsonObjectSetKey(json, "type", jsonNewObject("REQUEST"));
220 payload = jsonNewObject(NULL);
221 jsonObjectSetClass(payload, "osrfMethod");
222 jsonObjectSetKey(payload, "method", jsonNewObject(msg->method_name));
223 str = jsonObjectToJSON(msg->_params);
224 jsonObjectSetKey(payload, "params", jsonParseString(str));
226 jsonObjectSetKey(json, "payload", payload);
231 jsonObjectSetKey(json, "type", jsonNewObject("RESULT"));
232 payload = jsonNewObject(NULL);
233 jsonObjectSetClass(payload,"osrfResult");
234 jsonObjectSetKey(payload, "status", jsonNewObject(msg->status_text));
235 snprintf(sc, sizeof(sc), "%d", msg->status_code);
236 jsonObjectSetKey(payload, "statusCode", jsonNewObject(sc));
237 str = jsonObjectToJSON(msg->_result_content);
238 jsonObjectSetKey(payload, "content", jsonParseString(str));
240 jsonObjectSetKey(json, "payload", payload);
248 int osrf_message_deserialize(const char* string, osrfMessage* msgs[], int count) {
250 if(!string || !msgs || count <= 0) return 0;
253 jsonObject* json = jsonParseString(string);
256 osrfLogWarning( OSRF_LOG_MARK,
257 "osrf_message_deserialize() unable to parse data: \n%s\n", string);
263 for( x = 0; x < json->size && x < count; x++ ) {
265 const jsonObject* message = jsonObjectGetIndex(json, x);
267 if(message && message->type != JSON_NULL &&
268 message->classname && !strcmp(message->classname, "osrfMessage")) {
270 osrfMessage* new_msg = safe_malloc(sizeof(osrfMessage));
272 new_msg->thread_trace = 0;
273 new_msg->protocol = 0;
274 new_msg->status_name = NULL;
275 new_msg->status_text = NULL;
276 new_msg->status_code = 0;
277 new_msg->is_exception = 0;
278 new_msg->_result_content = NULL;
279 new_msg->result_string = NULL;
280 new_msg->method_name = NULL;
281 new_msg->_params = NULL;
282 new_msg->next = NULL;
283 new_msg->full_param_string = NULL;
284 new_msg->sender_locale = NULL;
285 new_msg->sender_tz_offset = 0;
287 const jsonObject* tmp = jsonObjectGetKeyConst(message, "type");
290 if( ( t = jsonObjectGetString(tmp)) ) {
292 if(!strcmp(t, "CONNECT")) new_msg->m_type = CONNECT;
293 else if(!strcmp(t, "DISCONNECT")) new_msg->m_type = DISCONNECT;
294 else if(!strcmp(t, "STATUS")) new_msg->m_type = STATUS;
295 else if(!strcmp(t, "REQUEST")) new_msg->m_type = REQUEST;
296 else if(!strcmp(t, "RESULT")) new_msg->m_type = RESULT;
299 tmp = jsonObjectGetKeyConst(message, "threadTrace");
301 char* tt = jsonObjectToSimpleString(tmp);
303 new_msg->thread_trace = atoi(tt);
308 /* use the sender's locale, or the global default */
310 free( current_locale );
312 tmp = jsonObjectGetKeyConst(message, "locale");
314 if(tmp && (new_msg->sender_locale = jsonObjectToSimpleString(tmp))) {
315 current_locale = strdup( new_msg->sender_locale );
317 current_locale = NULL;
320 tmp = jsonObjectGetKeyConst(message, "protocol");
323 char* proto = jsonObjectToSimpleString(tmp);
325 new_msg->protocol = atoi(proto);
330 tmp = jsonObjectGetKeyConst(message, "payload");
333 new_msg->status_name = strdup(tmp->classname);
335 const jsonObject* tmp0 = jsonObjectGetKeyConst(tmp,"method");
336 const char* tmp_str = jsonObjectGetString(tmp0);
338 new_msg->method_name = strdup(tmp_str);
340 tmp0 = jsonObjectGetKeyConst(tmp,"params");
342 char* s = jsonObjectToJSON(tmp0);
343 new_msg->_params = jsonParseString(s);
344 if(new_msg->_params && new_msg->_params->type == JSON_NULL)
345 new_msg->_params->type = JSON_ARRAY;
349 tmp0 = jsonObjectGetKeyConst(tmp,"status");
350 tmp_str = jsonObjectGetString(tmp0);
352 new_msg->status_text = strdup(tmp_str);
354 tmp0 = jsonObjectGetKeyConst(tmp,"statusCode");
356 tmp_str = jsonObjectGetString(tmp0);
358 new_msg->status_code = atoi(tmp_str);
359 if(tmp0->type == JSON_NUMBER)
360 new_msg->status_code = (int) jsonObjectGetNumber(tmp0);
363 tmp0 = jsonObjectGetKeyConst(tmp,"content");
365 char* s = jsonObjectToJSON(tmp0);
366 new_msg->_result_content = jsonParseString(s);
371 msgs[numparsed++] = new_msg;
375 jsonObjectFree(json);
381 jsonObject* osrfMessageGetResult( osrfMessage* msg ) {
382 if(msg) return msg->_result_content;