Patch from Scott McKellar:
[OpenSRF.git] / src / libopensrf / osrf_message.c
1 #include <opensrf/osrf_message.h>
2
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;
5
6 osrfMessage* osrf_message_init( enum M_TYPE type, int thread_trace, int protocol ) {
7
8         osrfMessage* msg                        = (osrfMessage*) safe_malloc(sizeof(osrfMessage));
9         msg->m_type                                     = type;
10         msg->thread_trace                       = thread_trace;
11         msg->protocol                           = protocol;
12         msg->status_name                        = NULL;
13         msg->status_text                        = NULL;
14         msg->status_code                        = 0;
15         msg->next                                       = NULL;
16         msg->is_exception                       = 0;
17         msg->_params                            = NULL;
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;
24
25         return msg;
26 }
27
28
29 const char* osrf_message_get_last_locale() {
30         return current_locale;
31 }
32
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 );
36 }
37
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;
41
42         strcpy( default_locale, locale );
43         return (const char*) default_locale;
44 }
45
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 );
49 }
50
51
52 void osrf_message_add_object_param( osrfMessage* msg, const jsonObject* o ) {
53         if(!msg|| !o) return;
54         if(!msg->_params)
55                 msg->_params = jsonParseString("[]");
56         char* j = jsonObjectToJSON(o);
57         jsonObjectPush(msg->_params, jsonParseString(j));
58         free(j);
59 }
60
61 void osrf_message_set_params( osrfMessage* msg, const jsonObject* o ) {
62         if(!msg || !o) return;
63
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);
70                 return;
71         }
72
73         if(msg->_params) jsonObjectFree(msg->_params);
74         msg->_params = jsonObjectClone(o);
75 }
76
77
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));
83 }
84
85
86 void osrf_message_set_status_info( osrfMessage* msg,
87                 const char* status_name, const char* status_text, int status_code ) {
88         if(!msg) return;
89
90         if( status_name != NULL ) 
91                 msg->status_name = strdup( status_name );
92
93         if( status_text != NULL )
94                 msg->status_text = strdup( status_text );
95
96         msg->status_code = status_code;
97 }
98
99
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);
104 }
105
106
107
108 void osrfMessageFree( osrfMessage* msg ) {
109         osrf_message_free( msg );
110 }
111
112 void osrf_message_free( osrfMessage* msg ) {
113         if( msg == NULL )
114                 return;
115
116         if( msg->status_name != NULL )
117                 free(msg->status_name);
118
119         if( msg->status_text != NULL )
120                 free(msg->status_text);
121
122         if( msg->_result_content != NULL )
123                 jsonObjectFree( msg->_result_content );
124
125         if( msg->result_string != NULL )
126                 free( msg->result_string);
127
128         if( msg->method_name != NULL )
129                 free(msg->method_name);
130
131         if( msg->sender_locale != NULL )
132                 free(msg->sender_locale);
133
134         if( msg->_params != NULL )
135                 jsonObjectFree(msg->_params);
136
137         free(msg);
138 }
139
140
141 char* osrfMessageSerializeBatch( osrfMessage* msgs [], int count ) {
142         if( !msgs ) return NULL;
143
144         char* j;
145         int i = 0;
146         const osrfMessage* msg = NULL;
147         jsonObject* wrapper = jsonNewObject(NULL);
148
149         while( ((msg = msgs[i]) && (i++ < count)) ) 
150                 jsonObjectPush(wrapper, osrfMessageToJSON( msg ));
151
152         j = jsonObjectToJSON(wrapper);
153         jsonObjectFree(wrapper);
154
155         return j;       
156 }
157
158
159 char* osrf_message_serialize(const osrfMessage* msg) {
160
161         if( msg == NULL ) return NULL;
162         char* j = NULL;
163
164         jsonObject* json = osrfMessageToJSON( msg );
165
166         if(json) {
167                 jsonObject* wrapper = jsonNewObject(NULL);
168                 jsonObjectPush(wrapper, json);
169                 j = jsonObjectToJSON(wrapper);
170                 jsonObjectFree(wrapper);
171         }
172
173         return j;
174 }
175
176
177 jsonObject* osrfMessageToJSON( const osrfMessage* msg ) {
178
179         jsonObject* json = jsonNewObject(NULL);
180         jsonObjectSetClass(json, "osrfMessage");
181         jsonObject* payload;
182         char sc[64];
183         osrf_clearbuf(sc, sizeof(sc));
184
185         char* str;
186
187         INT_TO_STRING(msg->thread_trace);
188         jsonObjectSetKey(json, "threadTrace", jsonNewObject(INTSTR));
189
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));
194         } else {
195                 jsonObjectSetKey(json, "locale", jsonNewObject(default_locale));
196         }
197
198         switch(msg->m_type) {
199                 
200                 case CONNECT: 
201                         jsonObjectSetKey(json, "type", jsonNewObject("CONNECT"));
202                         break;
203
204                 case DISCONNECT: 
205                         jsonObjectSetKey(json, "type", jsonNewObject("DISCONNECT"));
206                         break;
207
208                 case STATUS:
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);
216                         break;
217
218                 case REQUEST:
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));
225                         free(str);
226                         jsonObjectSetKey(json, "payload", payload);
227
228                         break;
229
230                 case RESULT:
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));
239                         free(str);
240                         jsonObjectSetKey(json, "payload", payload);
241                         break;
242         }
243
244         return json;
245 }
246
247
248 int osrf_message_deserialize(const char* string, osrfMessage* msgs[], int count) {
249
250         if(!string || !msgs || count <= 0) return 0;
251         int numparsed = 0;
252
253         jsonObject* json = jsonParseString(string);
254
255         if(!json) {
256                 osrfLogWarning( OSRF_LOG_MARK, 
257                         "osrf_message_deserialize() unable to parse data: \n%s\n", string);
258                 return 0;
259         }
260
261         int x;
262
263         for( x = 0; x < json->size && x < count; x++ ) {
264
265                 const jsonObject* message = jsonObjectGetIndex(json, x);
266
267                 if(message && message->type != JSON_NULL && 
268                         message->classname && !strcmp(message->classname, "osrfMessage")) {
269
270                         osrfMessage* new_msg = safe_malloc(sizeof(osrfMessage));
271                         new_msg->m_type = 0;
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;
286
287                         const jsonObject* tmp = jsonObjectGetKeyConst(message, "type");
288
289                         const char* t;
290                         if( ( t = jsonObjectGetString(tmp)) ) {
291
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;
297                         }
298
299                         tmp = jsonObjectGetKeyConst(message, "threadTrace");
300                         if(tmp) {
301                                 char* tt = jsonObjectToSimpleString(tmp);
302                                 if(tt) {
303                                         new_msg->thread_trace = atoi(tt);
304                                         free(tt);
305                                 }
306                         }
307
308                         /* use the sender's locale, or the global default */
309                         if (current_locale)
310                                 free( current_locale );
311
312                         tmp = jsonObjectGetKeyConst(message, "locale");
313                         if(tmp) {
314                                 new_msg->sender_locale = jsonObjectToSimpleString(tmp);
315                                 current_locale = strdup( new_msg->sender_locale );
316                         } else {
317                                 current_locale = NULL;
318                         }
319
320                         tmp = jsonObjectGetKeyConst(message, "protocol");
321
322                         if(tmp) {
323                                 char* proto = jsonObjectToSimpleString(tmp);
324                                 if(proto) {
325                                         new_msg->protocol = atoi(proto);
326                                         free(proto);
327                                 }
328                         }
329
330                         tmp = jsonObjectGetKeyConst(message, "payload");
331                         if(tmp) {
332                                 if(tmp->classname)
333                                         new_msg->status_name = strdup(tmp->classname);
334
335                                 const jsonObject* tmp0 = jsonObjectGetKeyConst(tmp,"method");
336                                 const char* tmp_str = jsonObjectGetString(tmp0);
337                                 if(tmp_str)
338                                         new_msg->method_name = strdup(tmp_str);
339
340                                 tmp0 = jsonObjectGetKeyConst(tmp,"params");
341                                 if(tmp0) {
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;
346                                         free(s);
347                                 }
348
349                                 tmp0 = jsonObjectGetKeyConst(tmp,"status");
350                                 tmp_str = jsonObjectGetString(tmp0);
351                                 if(tmp_str)
352                                         new_msg->status_text = strdup(tmp_str);
353
354                                 tmp0 = jsonObjectGetKeyConst(tmp,"statusCode");
355                                 if(tmp0) {
356                                         tmp_str = jsonObjectGetString(tmp0);
357                                         if(tmp_str)
358                                                 new_msg->status_code = atoi(tmp_str);
359                                         if(tmp0->type == JSON_NUMBER)
360                                                 new_msg->status_code = (int) jsonObjectGetNumber(tmp0);
361                                 }
362
363                                 tmp0 = jsonObjectGetKeyConst(tmp,"content");
364                                 if(tmp0) {
365                                         char* s = jsonObjectToJSON(tmp0);
366                                         new_msg->_result_content = jsonParseString(s);
367                                         free(s);
368                                 }
369
370                         }
371                         msgs[numparsed++] = new_msg;
372                 }
373         }
374
375         jsonObjectFree(json);
376         return numparsed;
377 }
378
379
380
381 jsonObject* osrfMessageGetResult( osrfMessage* msg ) {
382         if(msg) return msg->_result_content;
383         return NULL;
384 }
385