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 ) {
112 if( msg->status_name != NULL )
113 free(msg->status_name);
115 if( msg->status_text != NULL )
116 free(msg->status_text);
118 if( msg->_result_content != NULL )
119 jsonObjectFree( msg->_result_content );
121 if( msg->result_string != NULL )
122 free( msg->result_string);
124 if( msg->method_name != NULL )
125 free(msg->method_name);
127 if( msg->sender_locale != NULL )
128 free(msg->sender_locale);
130 if( msg->_params != NULL )
131 jsonObjectFree(msg->_params);
137 char* osrfMessageSerializeBatch( osrfMessage* msgs [], int count ) {
138 if( !msgs ) return NULL;
142 const osrfMessage* msg = NULL;
143 jsonObject* wrapper = jsonNewObject(NULL);
145 while( ((msg = msgs[i]) && (i++ < count)) )
146 jsonObjectPush(wrapper, osrfMessageToJSON( msg ));
148 j = jsonObjectToJSON(wrapper);
149 jsonObjectFree(wrapper);
155 char* osrf_message_serialize(const osrfMessage* msg) {
157 if( msg == NULL ) return NULL;
160 jsonObject* json = osrfMessageToJSON( msg );
163 jsonObject* wrapper = jsonNewObject(NULL);
164 jsonObjectPush(wrapper, json);
165 j = jsonObjectToJSON(wrapper);
166 jsonObjectFree(wrapper);
173 jsonObject* osrfMessageToJSON( const osrfMessage* msg ) {
175 jsonObject* json = jsonNewObject(NULL);
176 jsonObjectSetClass(json, "osrfMessage");
179 osrf_clearbuf(sc, sizeof(sc));
183 INT_TO_STRING(msg->thread_trace);
184 jsonObjectSetKey(json, "threadTrace", jsonNewObject(INTSTR));
186 if (msg->sender_locale != NULL) {
187 jsonObjectSetKey(json, "locale", jsonNewObject(msg->sender_locale));
188 } else if (current_locale != NULL) {
189 jsonObjectSetKey(json, "locale", jsonNewObject(current_locale));
191 jsonObjectSetKey(json, "locale", jsonNewObject(default_locale));
194 switch(msg->m_type) {
197 jsonObjectSetKey(json, "type", jsonNewObject("CONNECT"));
201 jsonObjectSetKey(json, "type", jsonNewObject("DISCONNECT"));
205 jsonObjectSetKey(json, "type", jsonNewObject("STATUS"));
206 payload = jsonNewObject(NULL);
207 jsonObjectSetClass(payload, msg->status_name);
208 jsonObjectSetKey(payload, "status", jsonNewObject(msg->status_text));
209 snprintf(sc, sizeof(sc), "%d", msg->status_code);
210 jsonObjectSetKey(payload, "statusCode", jsonNewObject(sc));
211 jsonObjectSetKey(json, "payload", payload);
215 jsonObjectSetKey(json, "type", jsonNewObject("REQUEST"));
216 payload = jsonNewObject(NULL);
217 jsonObjectSetClass(payload, "osrfMethod");
218 jsonObjectSetKey(payload, "method", jsonNewObject(msg->method_name));
219 str = jsonObjectToJSON(msg->_params);
220 jsonObjectSetKey(payload, "params", jsonParseString(str));
222 jsonObjectSetKey(json, "payload", payload);
227 jsonObjectSetKey(json, "type", jsonNewObject("RESULT"));
228 payload = jsonNewObject(NULL);
229 jsonObjectSetClass(payload,"osrfResult");
230 jsonObjectSetKey(payload, "status", jsonNewObject(msg->status_text));
231 snprintf(sc, sizeof(sc), "%d", msg->status_code);
232 jsonObjectSetKey(payload, "statusCode", jsonNewObject(sc));
233 str = jsonObjectToJSON(msg->_result_content);
234 jsonObjectSetKey(payload, "content", jsonParseString(str));
236 jsonObjectSetKey(json, "payload", payload);
244 int osrf_message_deserialize(const char* string, osrfMessage* msgs[], int count) {
246 if(!string || !msgs || count <= 0) return 0;
249 jsonObject* json = jsonParseString(string);
252 osrfLogWarning( OSRF_LOG_MARK,
253 "osrf_message_deserialize() unable to parse data: \n%s\n", string);
259 for( x = 0; x < json->size && x < count; x++ ) {
261 const jsonObject* message = jsonObjectGetIndex(json, x);
263 if(message && message->type != JSON_NULL &&
264 message->classname && !strcmp(message->classname, "osrfMessage")) {
266 osrfMessage* new_msg = safe_malloc(sizeof(osrfMessage));
268 new_msg->thread_trace = 0;
269 new_msg->protocol = 0;
270 new_msg->status_name = NULL;
271 new_msg->status_text = NULL;
272 new_msg->status_code = 0;
273 new_msg->is_exception = 0;
274 new_msg->_result_content = NULL;
275 new_msg->result_string = NULL;
276 new_msg->method_name = NULL;
277 new_msg->_params = NULL;
278 new_msg->next = NULL;
279 new_msg->full_param_string = NULL;
280 new_msg->sender_locale = NULL;
281 new_msg->sender_tz_offset = 0;
283 const jsonObject* tmp = jsonObjectGetKeyConst(message, "type");
286 if( ( t = jsonObjectGetString(tmp)) ) {
288 if(!strcmp(t, "CONNECT")) new_msg->m_type = CONNECT;
289 else if(!strcmp(t, "DISCONNECT")) new_msg->m_type = DISCONNECT;
290 else if(!strcmp(t, "STATUS")) new_msg->m_type = STATUS;
291 else if(!strcmp(t, "REQUEST")) new_msg->m_type = REQUEST;
292 else if(!strcmp(t, "RESULT")) new_msg->m_type = RESULT;
295 tmp = jsonObjectGetKeyConst(message, "threadTrace");
297 char* tt = jsonObjectToSimpleString(tmp);
299 new_msg->thread_trace = atoi(tt);
304 /* use the sender's locale, or the global default */
306 free( current_locale );
308 tmp = jsonObjectGetKeyConst(message, "locale");
310 if(tmp && (new_msg->sender_locale = jsonObjectToSimpleString(tmp))) {
311 current_locale = strdup( new_msg->sender_locale );
313 current_locale = NULL;
316 tmp = jsonObjectGetKeyConst(message, "protocol");
319 char* proto = jsonObjectToSimpleString(tmp);
321 new_msg->protocol = atoi(proto);
326 tmp = jsonObjectGetKeyConst(message, "payload");
329 new_msg->status_name = strdup(tmp->classname);
331 const jsonObject* tmp0 = jsonObjectGetKeyConst(tmp,"method");
332 const char* tmp_str = jsonObjectGetString(tmp0);
334 new_msg->method_name = strdup(tmp_str);
336 tmp0 = jsonObjectGetKeyConst(tmp,"params");
338 char* s = jsonObjectToJSON(tmp0);
339 new_msg->_params = jsonParseString(s);
340 if(new_msg->_params && new_msg->_params->type == JSON_NULL)
341 new_msg->_params->type = JSON_ARRAY;
345 tmp0 = jsonObjectGetKeyConst(tmp,"status");
346 tmp_str = jsonObjectGetString(tmp0);
348 new_msg->status_text = strdup(tmp_str);
350 tmp0 = jsonObjectGetKeyConst(tmp,"statusCode");
352 tmp_str = jsonObjectGetString(tmp0);
354 new_msg->status_code = atoi(tmp_str);
355 if(tmp0->type == JSON_NUMBER)
356 new_msg->status_code = (int) jsonObjectGetNumber(tmp0);
359 tmp0 = jsonObjectGetKeyConst(tmp,"content");
361 char* s = jsonObjectToJSON(tmp0);
362 new_msg->_result_content = jsonParseString(s);
367 msgs[numparsed++] = new_msg;
371 jsonObjectFree(json);
377 jsonObject* osrfMessageGetResult( osrfMessage* msg ) {
378 if(msg) return msg->_result_content;