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 osrfLogDebug( 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);
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 = jsonParseString("[]");
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 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) return;
75 msg->result_string = strdup(json_string);
76 if(json_string) msg->_result_content = jsonParseString(json_string);
81 void osrfMessageFree( osrfMessage* msg ) {
82 osrf_message_free( msg );
85 void osrf_message_free( osrf_message* msg ) {
89 if( msg->status_name != NULL )
90 free(msg->status_name);
92 if( msg->status_text != NULL )
93 free(msg->status_text);
95 if( msg->_result_content != NULL )
96 jsonObjectFree( msg->_result_content );
98 if( msg->result_string != NULL )
99 free( msg->result_string);
101 if( msg->method_name != NULL )
102 free(msg->method_name);
104 if( msg->_params != NULL )
105 jsonObjectFree(msg->_params);
111 char* osrfMessageSerializeBatch( osrfMessage* msgs [], int count ) {
112 if( !msgs ) return NULL;
116 osrfMessage* msg = NULL;
117 jsonObject* wrapper = jsonNewObject(NULL);
119 while( ((msg = msgs[i]) && (i++ < count)) )
120 jsonObjectPush(wrapper, osrfMessageToJSON( msg ));
122 j = jsonObjectToJSON(wrapper);
123 jsonObjectFree(wrapper);
129 char* osrf_message_serialize(osrf_message* msg) {
131 if( msg == NULL ) return NULL;
134 jsonObject* json = osrfMessageToJSON( msg );
137 jsonObject* wrapper = jsonNewObject(NULL);
138 jsonObjectPush(wrapper, json);
139 j = jsonObjectToJSON(wrapper);
140 jsonObjectFree(wrapper);
147 jsonObject* osrfMessageToJSON( osrfMessage* msg ) {
149 jsonObject* json = jsonNewObject(NULL);
150 jsonObjectSetClass(json, "osrfMessage");
152 char sc[64]; memset(sc,0,64);
156 INT_TO_STRING(msg->thread_trace);
157 jsonObjectSetKey(json, "threadTrace", jsonNewObject(INTSTR));
159 switch(msg->m_type) {
162 jsonObjectSetKey(json, "type", jsonNewObject("CONNECT"));
166 jsonObjectSetKey(json, "type", jsonNewObject("DISCONNECT"));
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);
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));
187 jsonObjectSetKey(json, "payload", payload);
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));
201 jsonObjectSetKey(json, "payload", payload);
209 int osrf_message_deserialize(char* string, osrf_message* msgs[], int count) {
211 if(!string || !msgs || count <= 0) return 0;
214 jsonObject* json = jsonParseString(string);
217 osrfLogWarning( OSRF_LOG_MARK,
218 "osrf_message_deserialize() unable to parse data: \n%s\n", string);
224 for( x = 0; x < json->size && x < count; x++ ) {
226 jsonObject* message = jsonObjectGetIndex(json, x);
228 if(message && message->type != JSON_NULL &&
229 message->classname && !strcmp(message->classname, "osrfMessage")) {
231 osrf_message* new_msg = safe_malloc(sizeof(osrf_message));
233 jsonObject* tmp = jsonObjectGetKey(message, "type");
236 if( ( t = jsonObjectGetString(tmp)) ) {
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;
245 tmp = jsonObjectGetKey(message, "threadTrace");
247 char* tt = jsonObjectToSimpleString(tmp);
249 new_msg->thread_trace = atoi(tt);
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));
261 tmp = jsonObjectGetKey(message, "protocol");
264 char* proto = jsonObjectToSimpleString(tmp);
266 new_msg->protocol = atoi(proto);
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));
278 tmp = jsonObjectGetKey(message, "payload");
281 new_msg->status_name = strdup(tmp->classname);
283 jsonObject* tmp0 = jsonObjectGetKey(tmp,"method");
284 if(jsonObjectGetString(tmp0))
285 new_msg->method_name = strdup(jsonObjectGetString(tmp0));
287 tmp0 = jsonObjectGetKey(tmp,"params");
289 char* s = jsonObjectToJSON(tmp0);
290 new_msg->_params = jsonParseString(s);
291 if(new_msg->_params && new_msg->_params->type == JSON_NULL)
292 new_msg->_params->type = JSON_ARRAY;
296 tmp0 = jsonObjectGetKey(tmp,"status");
297 if(jsonObjectGetString(tmp0))
298 new_msg->status_text = strdup(jsonObjectGetString(tmp0));
300 tmp0 = jsonObjectGetKey(tmp,"statusCode");
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);
308 tmp0 = jsonObjectGetKey(tmp,"content");
310 char* s = jsonObjectToJSON(tmp0);
311 new_msg->_result_content = jsonParseString(s);
316 msgs[numparsed++] = new_msg;
320 jsonObjectFree(json);
326 jsonObject* osrfMessageGetResult( osrfMessage* msg ) {
327 if(msg) return msg->_result_content;