1 #include "osrf_message.h"
3 osrf_message* osrf_message_init( enum M_TYPE type, int thread_trace, int protocol ) {
5 osrf_message* msg = (osrf_message*) safe_malloc(sizeof(osrf_message));
7 msg->thread_trace = thread_trace;
8 msg->protocol = protocol;
10 msg->is_exception = 0;
12 msg->_result_content = NULL;
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 );
24 void osrf_message_add_object_param( osrf_message* msg, jsonObject* o ) {
27 msg->_params = jsonParseString("[]");
28 char* j = jsonObjectToJSON(o);
29 jsonObjectPush(msg->_params, jsonParseString(j));
33 void osrf_message_set_params( osrf_message* msg, jsonObject* o ) {
34 if(!msg || !o) return;
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);
46 if(msg->_params) jsonObjectFree(msg->_params);
47 msg->_params = jsonObjectClone(o);
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));
59 void osrf_message_set_status_info(
60 osrf_message* msg, char* status_name, char* status_text, int status_code ) {
63 fatal_handler( "Bad params to osrf_message_set_status_info()" );
65 if( status_name != NULL )
66 msg->status_name = strdup( status_name );
68 if( status_text != NULL )
69 msg->status_text = strdup( status_text );
71 msg->status_code = status_code;
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()" );
79 msg->result_string = strdup(json_string);
80 if(json_string) msg->_result_content = jsonParseString(json_string);
85 void osrfMessageFree( osrfMessage* msg ) {
86 osrf_message_free( msg );
89 void osrf_message_free( osrf_message* msg ) {
93 if( msg->status_name != NULL )
94 free(msg->status_name);
96 if( msg->status_text != NULL )
97 free(msg->status_text);
99 if( msg->_result_content != NULL )
100 jsonObjectFree( msg->_result_content );
102 if( msg->result_string != NULL )
103 free( msg->result_string);
105 if( msg->method_name != NULL )
106 free(msg->method_name);
108 if( msg->_params != NULL )
109 jsonObjectFree(msg->_params);
115 char* osrfMessageSerializeBatch( osrfMessage* msgs [], int count ) {
116 if( !msgs ) return NULL;
120 osrfMessage* msg = NULL;
121 jsonObject* wrapper = jsonNewObject(NULL);
123 while( ((msg = msgs[i]) && (i++ < count)) )
124 jsonObjectPush(wrapper, osrfMessageToJSON( msg ));
126 j = jsonObjectToJSON(wrapper);
127 jsonObjectFree(wrapper);
133 char* osrf_message_serialize(osrf_message* msg) {
135 if( msg == NULL ) return NULL;
138 jsonObject* json = osrfMessageToJSON( msg );
141 jsonObject* wrapper = jsonNewObject(NULL);
142 jsonObjectPush(wrapper, json);
143 j = jsonObjectToJSON(wrapper);
144 jsonObjectFree(wrapper);
151 jsonObject* osrfMessageToJSON( osrfMessage* msg ) {
153 jsonObject* json = jsonNewObject(NULL);
154 jsonObjectSetClass(json, "osrfMessage");
156 char sc[64]; memset(sc,0,64);
160 INT_TO_STRING(msg->thread_trace);
161 jsonObjectSetKey(json, "threadTrace", jsonNewObject(INTSTR));
163 switch(msg->m_type) {
166 jsonObjectSetKey(json, "type", jsonNewObject("CONNECT"));
170 jsonObjectSetKey(json, "type", jsonNewObject("DISCONNECT"));
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);
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));
191 jsonObjectSetKey(json, "payload", payload);
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));
205 jsonObjectSetKey(json, "payload", payload);
213 int osrf_message_deserialize(char* string, osrf_message* msgs[], int count) {
215 if(!string || !msgs || count <= 0) return 0;
218 jsonObject* json = jsonParseString(string);
222 "osrf_message_deserialize() unable to parse data: \n%s\n", string);
228 for( x = 0; x < json->size && x < count; x++ ) {
230 jsonObject* message = jsonObjectGetIndex(json, x);
232 if(message && message->type != JSON_NULL &&
233 message->classname && !strcmp(message->classname, "osrfMessage")) {
235 osrf_message* new_msg = safe_malloc(sizeof(osrf_message));
237 jsonObject* tmp = jsonObjectGetKey(message, "type");
240 if( ( t = jsonObjectGetString(tmp)) ) {
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;
249 tmp = jsonObjectGetKey(message, "threadTrace");
251 char* tt = jsonObjectToSimpleString(tmp);
253 new_msg->thread_trace = atoi(tt);
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));
265 tmp = jsonObjectGetKey(message, "protocol");
268 char* proto = jsonObjectToSimpleString(tmp);
270 new_msg->protocol = atoi(proto);
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));
282 tmp = jsonObjectGetKey(message, "payload");
285 new_msg->status_name = strdup(tmp->classname);
287 jsonObject* tmp0 = jsonObjectGetKey(tmp,"method");
288 if(jsonObjectGetString(tmp0))
289 new_msg->method_name = strdup(jsonObjectGetString(tmp0));
291 tmp0 = jsonObjectGetKey(tmp,"params");
293 char* s = jsonObjectToJSON(tmp0);
294 new_msg->_params = jsonParseString(s);
298 tmp0 = jsonObjectGetKey(tmp,"status");
299 if(jsonObjectGetString(tmp0))
300 new_msg->status_text = strdup(jsonObjectGetString(tmp0));
302 tmp0 = jsonObjectGetKey(tmp,"statusCode");
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);
310 tmp0 = jsonObjectGetKey(tmp,"content");
312 char* s = jsonObjectToJSON(tmp0);
313 new_msg->_result_content = jsonParseString(s);
318 msgs[numparsed++] = new_msg;
322 jsonObjectFree(json);
328 jsonObject* osrfMessageGetResult( osrfMessage* msg ) {
329 if(msg) return msg->_result_content;