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