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()");
41 if(msg->_params) jsonObjectFree(msg->_params);
43 char* j = jsonObjectToJSON(o);
44 msg->_params = jsonParseString(j);
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));
57 void osrf_message_set_status_info(
58 osrf_message* msg, char* status_name, char* status_text, int status_code ) {
61 fatal_handler( "Bad params to osrf_message_set_status_info()" );
63 if( status_name != NULL )
64 msg->status_name = strdup( status_name );
66 if( status_text != NULL )
67 msg->status_text = strdup( status_text );
69 msg->status_code = status_code;
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()" );
77 msg->result_string = strdup(json_string);
78 if(json_string) msg->_result_content = jsonParseString(json_string);
83 void osrfMessageFree( osrfMessage* msg ) {
84 osrf_message_free( msg );
87 void osrf_message_free( osrf_message* msg ) {
91 if( msg->status_name != NULL )
92 free(msg->status_name);
94 if( msg->status_text != NULL )
95 free(msg->status_text);
97 if( msg->_result_content != NULL )
98 jsonObjectFree( msg->_result_content );
100 if( msg->result_string != NULL )
101 free( msg->result_string);
103 if( msg->method_name != NULL )
104 free(msg->method_name);
106 if( msg->_params != NULL )
107 jsonObjectFree(msg->_params);
112 char* osrf_message_serialize(osrf_message* msg) {
113 if( msg == NULL ) return NULL;
114 jsonObject* json = jsonNewObject(NULL);
115 jsonObjectSetClass(json, "osrfMessage");
117 char sc[64]; memset(sc,0,64);
121 INT_TO_STRING(msg->thread_trace);
122 jsonObjectSetKey(json, "threadTrace", jsonNewObject(INTSTR));
124 switch(msg->m_type) {
127 jsonObjectSetKey(json, "type", jsonNewObject("CONNECT"));
131 jsonObjectSetKey(json, "type", jsonNewObject("DISCONNECT"));
135 jsonObjectSetKey(json, "type", jsonNewObject("STATUS"));
136 payload = jsonNewObject(NULL);
137 jsonObjectSetClass(payload, msg->status_name);
138 jsonObjectSetKey(payload, "status", jsonNewObject(msg->status_text));
139 sprintf(sc,"%d",msg->status_code);
140 jsonObjectSetKey(payload, "statusCode", jsonNewObject(sc));
141 jsonObjectSetKey(json, "payload", payload);
145 jsonObjectSetKey(json, "type", jsonNewObject("REQUEST"));
146 payload = jsonNewObject(NULL);
147 jsonObjectSetClass(payload, "osrfMethod");
148 jsonObjectSetKey(payload, "method", jsonNewObject(msg->method_name));
149 str = jsonObjectToJSON(msg->_params);
150 jsonObjectSetKey(payload, "params", jsonParseString(str));
152 jsonObjectSetKey(json, "payload", payload);
157 jsonObjectSetKey(json, "type", jsonNewObject("RESULT"));
158 payload = jsonNewObject(NULL);
159 jsonObjectSetClass(payload,"osrfResult");
160 jsonObjectSetKey(payload, "status", jsonNewObject(msg->status_text));
161 sprintf(sc,"%d",msg->status_code);
162 jsonObjectSetKey(payload, "statusCode", jsonNewObject(sc));
163 str = jsonObjectToJSON(msg->_result_content);
164 jsonObjectSetKey(payload, "content", jsonParseString(str));
166 jsonObjectSetKey(json, "payload", payload);
170 jsonObject* wrapper = jsonNewObject(NULL);
171 jsonObjectPush(wrapper, json);
172 char* j = jsonObjectToJSON(wrapper);
173 jsonObjectFree(wrapper);
178 int osrf_message_deserialize(char* string, osrf_message* msgs[], int count) {
180 if(!string || !msgs || count <= 0) return 0;
183 jsonObject* json = jsonParseString(string);
187 "osrf_message_deserialize() unable to parse data: \n%s\n", string);
193 for( x = 0; x < json->size && x < count; x++ ) {
195 jsonObject* message = jsonObjectGetIndex(json, x);
197 char* j = jsonObjectToJSON(message);
198 debug_handler("deserialize parsed message \n%s\n", j );
202 if(message && message->type != JSON_NULL &&
203 message->classname && !strcmp(message->classname, "osrfMessage")) {
205 osrf_message* new_msg = safe_malloc(sizeof(osrf_message));
207 jsonObject* tmp = jsonObjectGetKey(message, "type");
210 if( ( t = jsonObjectGetString(tmp)) ) {
212 if(!strcmp(t, "CONNECT")) new_msg->m_type = CONNECT;
213 if(!strcmp(t, "DISCONNECT")) new_msg->m_type = DISCONNECT;
214 if(!strcmp(t, "STATUS")) new_msg->m_type = STATUS;
215 if(!strcmp(t, "REQUEST")) new_msg->m_type = REQUEST;
216 if(!strcmp(t, "RESULT")) new_msg->m_type = RESULT;
219 tmp = jsonObjectGetKey(message, "threadTrace");
221 if(tmp->type == JSON_NUMBER)
222 new_msg->thread_trace = (int) jsonObjectGetNumber(tmp);
223 if(tmp->type == JSON_STRING)
224 new_msg->thread_trace = atoi(jsonObjectGetString(tmp));
228 tmp = jsonObjectGetKey(message, "protocol");
231 if(tmp->type == JSON_NUMBER)
232 new_msg->protocol = (int) jsonObjectGetNumber(tmp);
233 if(tmp->type == JSON_STRING)
234 new_msg->protocol = atoi(jsonObjectGetString(tmp));
237 tmp = jsonObjectGetKey(message, "payload");
240 new_msg->status_name = strdup(tmp->classname);
242 jsonObject* tmp0 = jsonObjectGetKey(tmp,"method");
243 if(jsonObjectGetString(tmp0))
244 new_msg->method_name = strdup(jsonObjectGetString(tmp0));
246 tmp0 = jsonObjectGetKey(tmp,"params");
248 char* s = jsonObjectToJSON(tmp0);
249 new_msg->_params = jsonParseString(s);
253 tmp0 = jsonObjectGetKey(tmp,"status");
254 if(jsonObjectGetString(tmp0))
255 new_msg->status_text = strdup(jsonObjectGetString(tmp0));
257 tmp0 = jsonObjectGetKey(tmp,"statusCode");
259 if(jsonObjectGetString(tmp0))
260 new_msg->status_code = atoi(jsonObjectGetString(tmp0));
261 if(tmp0->type == JSON_NUMBER)
262 new_msg->status_code = (int) jsonObjectGetNumber(tmp0);
265 tmp0 = jsonObjectGetKey(tmp,"content");
267 char* s = jsonObjectToJSON(tmp0);
268 new_msg->_result_content = jsonParseString(s);
273 msgs[numparsed++] = new_msg;
274 debug_handler("deserialize has parsed %d messages", numparsed);
278 jsonObjectFree(json);
284 jsonObject* osrfMessageGetResult( osrfMessage* msg ) {
285 if(msg) return msg->_result_content;