]> git.evergreen-ils.org Git - Evergreen.git/blob - OpenSRF/src/libstack/osrf_message.c
14452bf99392fe4adc7dd4db468f46f73977341d
[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                 osrfLogWarning( OSRF_LOG_MARK, "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         if(!msg) return;
62
63         if( status_name != NULL ) 
64                 msg->status_name = strdup( status_name );
65
66         if( status_text != NULL )
67                 msg->status_text = strdup( status_text );
68
69         msg->status_code = status_code;
70 }
71
72
73 void osrf_message_set_result_content( osrf_message* msg, char* json_string ) {
74         if( msg == NULL || json_string == NULL) return;
75         msg->result_string =    strdup(json_string);
76         if(json_string) msg->_result_content = jsonParseString(json_string);
77 }
78
79
80
81 void osrfMessageFree( osrfMessage* msg ) {
82         osrf_message_free( msg );
83 }
84
85 void osrf_message_free( osrf_message* msg ) {
86         if( msg == NULL )
87                 return;
88
89         if( msg->status_name != NULL )
90                 free(msg->status_name);
91
92         if( msg->status_text != NULL )
93                 free(msg->status_text);
94
95         if( msg->_result_content != NULL )
96                 jsonObjectFree( msg->_result_content );
97
98         if( msg->result_string != NULL )
99                 free( msg->result_string);
100
101         if( msg->method_name != NULL )
102                 free(msg->method_name);
103
104         if( msg->_params != NULL )
105                 jsonObjectFree(msg->_params);
106
107         free(msg);
108 }
109
110
111 char* osrfMessageSerializeBatch( osrfMessage* msgs [], int count ) {
112         if( !msgs ) return NULL;
113
114         char* j;
115         int i = 0;
116         osrfMessage* msg = NULL;
117         jsonObject* wrapper = jsonNewObject(NULL);
118
119         while( ((msg = msgs[i]) && (i++ < count)) ) 
120                 jsonObjectPush(wrapper, osrfMessageToJSON( msg ));
121
122         j = jsonObjectToJSON(wrapper);
123         jsonObjectFree(wrapper);
124
125         return j;       
126 }
127
128
129 char* osrf_message_serialize(osrf_message* msg) {
130
131         if( msg == NULL ) return NULL;
132         char* j = NULL;
133
134         jsonObject* json = osrfMessageToJSON( msg );
135
136         if(json) {
137                 jsonObject* wrapper = jsonNewObject(NULL);
138                 jsonObjectPush(wrapper, json);
139                 j = jsonObjectToJSON(wrapper);
140                 jsonObjectFree(wrapper);
141         }
142
143         return j;
144 }
145
146
147 jsonObject* osrfMessageToJSON( osrfMessage* msg ) {
148
149         jsonObject* json = jsonNewObject(NULL);
150         jsonObjectSetClass(json, "osrfMessage");
151         jsonObject* payload;
152         char sc[64]; memset(sc,0,64);
153
154         char* str;
155
156         INT_TO_STRING(msg->thread_trace);
157         jsonObjectSetKey(json, "threadTrace", jsonNewObject(INTSTR));
158
159         switch(msg->m_type) {
160                 
161                 case CONNECT: 
162                         jsonObjectSetKey(json, "type", jsonNewObject("CONNECT"));
163                         break;
164
165                 case DISCONNECT: 
166                         jsonObjectSetKey(json, "type", jsonNewObject("DISCONNECT"));
167                         break;
168
169                 case STATUS:
170                         jsonObjectSetKey(json, "type", jsonNewObject("STATUS"));
171                         payload = jsonNewObject(NULL);
172                         jsonObjectSetClass(payload, msg->status_name);
173                         jsonObjectSetKey(payload, "status", jsonNewObject(msg->status_text));
174          sprintf(sc,"%d",msg->status_code);
175                         jsonObjectSetKey(payload, "statusCode", jsonNewObject(sc));
176                         jsonObjectSetKey(json, "payload", payload);
177                         break;
178
179                 case REQUEST:
180                         jsonObjectSetKey(json, "type", jsonNewObject("REQUEST"));
181                         payload = jsonNewObject(NULL);
182                         jsonObjectSetClass(payload, "osrfMethod");
183                         jsonObjectSetKey(payload, "method", jsonNewObject(msg->method_name));
184                         str = jsonObjectToJSON(msg->_params);
185                         jsonObjectSetKey(payload, "params", jsonParseString(str));
186                         free(str);
187                         jsonObjectSetKey(json, "payload", payload);
188
189                         break;
190
191                 case RESULT:
192                         jsonObjectSetKey(json, "type", jsonNewObject("RESULT"));
193                         payload = jsonNewObject(NULL);
194                         jsonObjectSetClass(payload,"osrfResult");
195                         jsonObjectSetKey(payload, "status", jsonNewObject(msg->status_text));
196          sprintf(sc,"%d",msg->status_code);
197                         jsonObjectSetKey(payload, "statusCode", jsonNewObject(sc));
198                         str = jsonObjectToJSON(msg->_result_content);
199                         jsonObjectSetKey(payload, "content", jsonParseString(str));
200                         free(str);
201                         jsonObjectSetKey(json, "payload", payload);
202                         break;
203         }
204
205         return json;
206 }
207
208
209 int osrf_message_deserialize(char* string, osrf_message* msgs[], int count) {
210
211         if(!string || !msgs || count <= 0) return 0;
212         int numparsed = 0;
213
214         jsonObject* json = jsonParseString(string);
215
216         if(!json) {
217                 osrfLogWarning( OSRF_LOG_MARK, 
218                         "osrf_message_deserialize() unable to parse data: \n%s\n", string);
219                 return 0;
220         }
221
222         int x;
223
224         for( x = 0; x < json->size && x < count; x++ ) {
225
226                 jsonObject* message = jsonObjectGetIndex(json, x);
227
228                 if(message && message->type != JSON_NULL && 
229                         message->classname && !strcmp(message->classname, "osrfMessage")) {
230
231                         osrf_message* new_msg = safe_malloc(sizeof(osrf_message));
232
233                         jsonObject* tmp = jsonObjectGetKey(message, "type");
234
235                         char* t;
236                         if( ( t = jsonObjectGetString(tmp)) ) {
237
238                                 if(!strcmp(t, "CONNECT"))               new_msg->m_type = CONNECT;
239                                 if(!strcmp(t, "DISCONNECT"))    new_msg->m_type = DISCONNECT;
240                                 if(!strcmp(t, "STATUS"))                new_msg->m_type = STATUS;
241                                 if(!strcmp(t, "REQUEST"))               new_msg->m_type = REQUEST;
242                                 if(!strcmp(t, "RESULT"))                new_msg->m_type = RESULT;
243                         }
244
245                         tmp = jsonObjectGetKey(message, "threadTrace");
246                         if(tmp) {
247                                 char* tt = jsonObjectToSimpleString(tmp);
248                                 if(tt) {
249                                         new_msg->thread_trace = atoi(tt);
250                                         free(tt);
251                                 }
252                                 /*
253                                 if(tmp->type == JSON_NUMBER)
254                                         new_msg->thread_trace = (int) jsonObjectGetNumber(tmp);
255                                 if(tmp->type == JSON_STRING)
256                                         new_msg->thread_trace = atoi(jsonObjectGetString(tmp));
257                                         */
258                         }
259
260
261                         tmp = jsonObjectGetKey(message, "protocol");
262
263                         if(tmp) {
264                                 char* proto = jsonObjectToSimpleString(tmp);
265                                 if(proto) {
266                                         new_msg->protocol = atoi(proto);
267                                         free(proto);
268                                 }
269
270                                 /*
271                                 if(tmp->type == JSON_NUMBER)
272                                         new_msg->protocol = (int) jsonObjectGetNumber(tmp);
273                                 if(tmp->type == JSON_STRING)
274                                         new_msg->protocol = atoi(jsonObjectGetString(tmp));
275                                         */
276                         }
277
278                         tmp = jsonObjectGetKey(message, "payload");
279                         if(tmp) {
280                                 if(tmp->classname)
281                                         new_msg->status_name = strdup(tmp->classname);
282
283                                 jsonObject* tmp0 = jsonObjectGetKey(tmp,"method");
284                                 if(jsonObjectGetString(tmp0))
285                                         new_msg->method_name = strdup(jsonObjectGetString(tmp0));
286
287                                 tmp0 = jsonObjectGetKey(tmp,"params");
288                                 if(tmp0) {
289                                         char* s = jsonObjectToJSON(tmp0);
290                                         new_msg->_params = jsonParseString(s);
291                                         free(s);
292                                 }
293
294                                 tmp0 = jsonObjectGetKey(tmp,"status");
295                                 if(jsonObjectGetString(tmp0))
296                                         new_msg->status_text = strdup(jsonObjectGetString(tmp0));
297
298                                 tmp0 = jsonObjectGetKey(tmp,"statusCode");
299                                 if(tmp0) {
300                                         if(jsonObjectGetString(tmp0))
301                                                 new_msg->status_code = atoi(jsonObjectGetString(tmp0));
302                                         if(tmp0->type == JSON_NUMBER)
303                                                 new_msg->status_code = (int) jsonObjectGetNumber(tmp0);
304                                 }
305
306                                 tmp0 = jsonObjectGetKey(tmp,"content");
307                                 if(tmp0) {
308                                         char* s = jsonObjectToJSON(tmp0);
309                                         new_msg->_result_content = jsonParseString(s);
310                                         free(s);
311                                 }
312
313                         }
314                         msgs[numparsed++] = new_msg;
315                 }
316         }
317
318         jsonObjectFree(json);
319         return numparsed;
320 }
321
322
323
324 jsonObject* osrfMessageGetResult( osrfMessage* msg ) {
325         if(msg) return msg->_result_content;
326         return NULL;
327 }
328