some api changes
[Evergreen.git] / OpenSRF / src / libstack / osrf_message.c
1 #include "osrf_message.h"
2
3 osrf_message* osrf_message_init( enum M_TYPE type, int thread_trace, int protocol ) {
4
5         osrf_message* msg                       = (osrf_message*) safe_malloc(sizeof(osrf_message));
6         msg->m_type                                     = type;
7         msg->thread_trace                       = thread_trace;
8         msg->protocol                           = protocol;
9         msg->next                                       = NULL;
10         msg->is_exception                       = 0;
11         msg->_params                            = NULL;
12         msg->_result_content            = NULL;
13
14         return msg;
15 }
16
17
18 void osrf_message_set_method( osrf_message* msg, char* method_name ) {
19         if( msg == NULL || method_name == NULL ) return;
20         msg->method_name = strdup( method_name );
21 }
22
23
24 void osrf_message_add_object_param( osrf_message* msg, jsonObject* o ) {
25         if(!msg|| !o) return;
26         if(!msg->_params)
27                 msg->_params = jsonParseString("[]");
28         char* j = jsonObjectToJSON(o);
29         jsonObjectPush(msg->_params, jsonParseString(j));
30         free(j);
31 }
32
33 void osrf_message_set_params( osrf_message* msg, jsonObject* o ) {
34         if(!msg || !o) return;
35
36         if(o->type != JSON_ARRAY) {
37                 warning_handler("passing non-array to osrf_message_set_params(), fixing...");
38                 jsonObject* clone = jsonObjectClone(o);
39                 o = jsonNewObject(NULL);
40                 jsonObjectPush(o, clone);
41                 if(msg->_params) jsonObjectFree(msg->_params);
42                 msg->_params = o;
43                 return;
44         }
45
46         if(msg->_params) jsonObjectFree(msg->_params);
47         msg->_params = jsonObjectClone(o);
48 }
49
50
51 /* only works if parse_json_params is false */
52 void osrf_message_add_param( osrf_message* msg, char* param_string ) {
53         if(msg == NULL || param_string == NULL) return;
54         if(!msg->_params) msg->_params = jsonNewObject(NULL);
55         jsonObjectPush(msg->_params, jsonParseString(param_string));
56 }
57
58
59 void osrf_message_set_status_info( 
60                 osrf_message* msg, char* status_name, char* status_text, int status_code ) {
61
62         if( msg == NULL )
63                 fatal_handler( "Bad params to osrf_message_set_status_info()" );
64
65         if( status_name != NULL ) 
66                 msg->status_name = strdup( status_name );
67
68         if( status_text != NULL )
69                 msg->status_text = strdup( status_text );
70
71         msg->status_code = status_code;
72 }
73
74
75 void osrf_message_set_result_content( osrf_message* msg, char* json_string ) {
76         if( msg == NULL || json_string == NULL)
77                 warning_handler( "Bad params to osrf_message_set_result_content()" );
78
79         msg->result_string =    strdup(json_string);
80         if(json_string) msg->_result_content = jsonParseString(json_string);
81 }
82
83
84
85 void osrfMessageFree( osrfMessage* msg ) {
86         osrf_message_free( msg );
87 }
88
89 void osrf_message_free( osrf_message* msg ) {
90         if( msg == NULL )
91                 return;
92
93         if( msg->status_name != NULL )
94                 free(msg->status_name);
95
96         if( msg->status_text != NULL )
97                 free(msg->status_text);
98
99         if( msg->_result_content != NULL )
100                 jsonObjectFree( msg->_result_content );
101
102         if( msg->result_string != NULL )
103                 free( msg->result_string);
104
105         if( msg->method_name != NULL )
106                 free(msg->method_name);
107
108         if( msg->_params != NULL )
109                 jsonObjectFree(msg->_params);
110
111         free(msg);
112 }
113
114
115 char* osrfMessageSerializeBatch( osrfMessage* msgs [], int count ) {
116         if( !msgs ) return NULL;
117
118         char* j;
119         int i = 0;
120         osrfMessage* msg = NULL;
121         jsonObject* wrapper = jsonNewObject(NULL);
122
123         while( ((msg = msgs[i]) && (i++ < count)) ) 
124                 jsonObjectPush(wrapper, osrfMessageToJSON( msg ));
125
126         j = jsonObjectToJSON(wrapper);
127         jsonObjectFree(wrapper);
128
129         return j;       
130 }
131
132
133 char* osrf_message_serialize(osrf_message* msg) {
134
135         if( msg == NULL ) return NULL;
136         char* j = NULL;
137
138         jsonObject* json = osrfMessageToJSON( msg );
139
140         if(json) {
141                 jsonObject* wrapper = jsonNewObject(NULL);
142                 jsonObjectPush(wrapper, json);
143                 j = jsonObjectToJSON(wrapper);
144                 jsonObjectFree(wrapper);
145         }
146
147         return j;
148 }
149
150
151 jsonObject* osrfMessageToJSON( osrfMessage* msg ) {
152
153         jsonObject* json = jsonNewObject(NULL);
154         jsonObjectSetClass(json, "osrfMessage");
155         jsonObject* payload;
156         char sc[64]; memset(sc,0,64);
157
158         char* str;
159
160         INT_TO_STRING(msg->thread_trace);
161         jsonObjectSetKey(json, "threadTrace", jsonNewObject(INTSTR));
162
163         switch(msg->m_type) {
164                 
165                 case CONNECT: 
166                         jsonObjectSetKey(json, "type", jsonNewObject("CONNECT"));
167                         break;
168
169                 case DISCONNECT: 
170                         jsonObjectSetKey(json, "type", jsonNewObject("DISCONNECT"));
171                         break;
172
173                 case STATUS:
174                         jsonObjectSetKey(json, "type", jsonNewObject("STATUS"));
175                         payload = jsonNewObject(NULL);
176                         jsonObjectSetClass(payload, msg->status_name);
177                         jsonObjectSetKey(payload, "status", jsonNewObject(msg->status_text));
178          sprintf(sc,"%d",msg->status_code);
179                         jsonObjectSetKey(payload, "statusCode", jsonNewObject(sc));
180                         jsonObjectSetKey(json, "payload", payload);
181                         break;
182
183                 case REQUEST:
184                         jsonObjectSetKey(json, "type", jsonNewObject("REQUEST"));
185                         payload = jsonNewObject(NULL);
186                         jsonObjectSetClass(payload, "osrfMethod");
187                         jsonObjectSetKey(payload, "method", jsonNewObject(msg->method_name));
188                         str = jsonObjectToJSON(msg->_params);
189                         jsonObjectSetKey(payload, "params", jsonParseString(str));
190                         free(str);
191                         jsonObjectSetKey(json, "payload", payload);
192
193                         break;
194
195                 case RESULT:
196                         jsonObjectSetKey(json, "type", jsonNewObject("RESULT"));
197                         payload = jsonNewObject(NULL);
198                         jsonObjectSetClass(payload,"osrfResult");
199                         jsonObjectSetKey(payload, "status", jsonNewObject(msg->status_text));
200          sprintf(sc,"%d",msg->status_code);
201                         jsonObjectSetKey(payload, "statusCode", jsonNewObject(sc));
202                         str = jsonObjectToJSON(msg->_result_content);
203                         jsonObjectSetKey(payload, "content", jsonParseString(str));
204                         free(str);
205                         jsonObjectSetKey(json, "payload", payload);
206                         break;
207         }
208
209         return json;
210 }
211
212
213 int osrf_message_deserialize(char* string, osrf_message* msgs[], int count) {
214
215         if(!string || !msgs || count <= 0) return 0;
216         int numparsed = 0;
217
218         jsonObject* json = jsonParseString(string);
219
220         if(!json) {
221                 warning_handler(
222                         "osrf_message_deserialize() unable to parse data: \n%s\n", string);
223                 return 0;
224         }
225
226         int x;
227
228         for( x = 0; x < json->size && x < count; x++ ) {
229
230                 jsonObject* message = jsonObjectGetIndex(json, x);
231
232                 if(message && message->type != JSON_NULL && 
233                         message->classname && !strcmp(message->classname, "osrfMessage")) {
234
235                         osrf_message* new_msg = safe_malloc(sizeof(osrf_message));
236
237                         jsonObject* tmp = jsonObjectGetKey(message, "type");
238
239                         char* t;
240                         if( ( t = jsonObjectGetString(tmp)) ) {
241
242                                 if(!strcmp(t, "CONNECT"))               new_msg->m_type = CONNECT;
243                                 if(!strcmp(t, "DISCONNECT"))    new_msg->m_type = DISCONNECT;
244                                 if(!strcmp(t, "STATUS"))                new_msg->m_type = STATUS;
245                                 if(!strcmp(t, "REQUEST"))               new_msg->m_type = REQUEST;
246                                 if(!strcmp(t, "RESULT"))                new_msg->m_type = RESULT;
247                         }
248
249                         tmp = jsonObjectGetKey(message, "threadTrace");
250                         if(tmp) {
251                                 char* tt = jsonObjectToSimpleString(tmp);
252                                 if(tt) {
253                                         new_msg->thread_trace = atoi(tt);
254                                         free(tt);
255                                 }
256                                 /*
257                                 if(tmp->type == JSON_NUMBER)
258                                         new_msg->thread_trace = (int) jsonObjectGetNumber(tmp);
259                                 if(tmp->type == JSON_STRING)
260                                         new_msg->thread_trace = atoi(jsonObjectGetString(tmp));
261                                         */
262                         }
263
264
265                         tmp = jsonObjectGetKey(message, "protocol");
266
267                         if(tmp) {
268                                 char* proto = jsonObjectToSimpleString(tmp);
269                                 if(proto) {
270                                         new_msg->protocol = atoi(proto);
271                                         free(proto);
272                                 }
273
274                                 /*
275                                 if(tmp->type == JSON_NUMBER)
276                                         new_msg->protocol = (int) jsonObjectGetNumber(tmp);
277                                 if(tmp->type == JSON_STRING)
278                                         new_msg->protocol = atoi(jsonObjectGetString(tmp));
279                                         */
280                         }
281
282                         tmp = jsonObjectGetKey(message, "payload");
283                         if(tmp) {
284                                 if(tmp->classname)
285                                         new_msg->status_name = strdup(tmp->classname);
286
287                                 jsonObject* tmp0 = jsonObjectGetKey(tmp,"method");
288                                 if(jsonObjectGetString(tmp0))
289                                         new_msg->method_name = strdup(jsonObjectGetString(tmp0));
290
291                                 tmp0 = jsonObjectGetKey(tmp,"params");
292                                 if(tmp0) {
293                                         char* s = jsonObjectToJSON(tmp0);
294                                         new_msg->_params = jsonParseString(s);
295                                         free(s);
296                                 }
297
298                                 tmp0 = jsonObjectGetKey(tmp,"status");
299                                 if(jsonObjectGetString(tmp0))
300                                         new_msg->status_text = strdup(jsonObjectGetString(tmp0));
301
302                                 tmp0 = jsonObjectGetKey(tmp,"statusCode");
303                                 if(tmp0) {
304                                         if(jsonObjectGetString(tmp0))
305                                                 new_msg->status_code = atoi(jsonObjectGetString(tmp0));
306                                         if(tmp0->type == JSON_NUMBER)
307                                                 new_msg->status_code = (int) jsonObjectGetNumber(tmp0);
308                                 }
309
310                                 tmp0 = jsonObjectGetKey(tmp,"content");
311                                 if(tmp0) {
312                                         char* s = jsonObjectToJSON(tmp0);
313                                         new_msg->_result_content = jsonParseString(s);
314                                         free(s);
315                                 }
316
317                         }
318                         msgs[numparsed++] = new_msg;
319                 }
320         }
321
322         jsonObjectFree(json);
323         return numparsed;
324 }
325
326
327
328 jsonObject* osrfMessageGetResult( osrfMessage* msg ) {
329         if(msg) return msg->_result_content;
330         return NULL;
331 }
332