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 osrf_message_free( osrf_message* msg ) {
87 if( msg->status_name != NULL )
88 free(msg->status_name);
90 if( msg->status_text != NULL )
91 free(msg->status_text);
93 if( msg->_result_content != NULL )
94 jsonObjectFree( msg->_result_content );
96 if( msg->result_string != NULL )
97 free( msg->result_string);
99 if( msg->method_name != NULL )
100 free(msg->method_name);
102 if( msg->_params != NULL )
103 jsonObjectFree(msg->_params);
108 char* osrf_message_serialize(osrf_message* msg) {
109 if( msg == NULL ) return NULL;
110 jsonObject* json = jsonNewObject(NULL);
111 jsonObjectSetClass(json, "osrfMessage");
113 char sc[64]; memset(sc,0,64);
117 INT_TO_STRING(msg->thread_trace);
118 jsonObjectSetKey(json, "threadTrace", jsonNewObject(INTSTR));
120 switch(msg->m_type) {
123 jsonObjectSetKey(json, "type", jsonNewObject("CONNECT"));
127 jsonObjectSetKey(json, "type", jsonNewObject("DISCONNECT"));
131 jsonObjectSetKey(json, "type", jsonNewObject("STATUS"));
132 payload = jsonNewObject(NULL);
133 jsonObjectSetClass(payload, msg->status_name);
134 jsonObjectSetKey(payload, "status", jsonNewObject(msg->status_text));
135 sprintf(sc,"%d",msg->status_code);
136 jsonObjectSetKey(payload, "statusCode", jsonNewObject(sc));
137 jsonObjectSetKey(json, "payload", payload);
141 jsonObjectSetKey(json, "type", jsonNewObject("REQUEST"));
142 payload = jsonNewObject(NULL);
143 jsonObjectSetClass(payload, "osrfMethod");
144 jsonObjectSetKey(payload, "method", jsonNewObject(msg->method_name));
145 str = jsonObjectToJSON(msg->_params);
146 jsonObjectSetKey(payload, "params", jsonParseString(str));
148 jsonObjectSetKey(json, "payload", payload);
153 jsonObjectSetKey(json, "type", jsonNewObject("RESULT"));
154 payload = jsonNewObject(NULL);
155 jsonObjectSetClass(payload,"osrfResult");
156 jsonObjectSetKey(payload, "status", jsonNewObject(msg->status_text));
157 sprintf(sc,"%d",msg->status_code);
158 jsonObjectSetKey(payload, "statusCode", jsonNewObject(sc));
159 str = jsonObjectToJSON(msg->_result_content);
160 jsonObjectSetKey(payload, "content", jsonParseString(str));
162 jsonObjectSetKey(json, "payload", payload);
166 jsonObject* wrapper = jsonNewObject(NULL);
167 jsonObjectPush(wrapper, json);
168 char* j = jsonObjectToJSON(wrapper);
169 jsonObjectFree(wrapper);
174 int osrf_message_deserialize(char* string, osrf_message* msgs[], int count) {
176 if(!string || !msgs || count <= 0) return 0;
179 jsonObject* json = jsonParseString(string);
183 "osrf_message_deserialize() unable to parse data: \n%s\n", string);
189 for( x = 0; x < json->size && x < count; x++ ) {
191 jsonObject* message = jsonObjectGetIndex(json, x);
193 char* j = jsonObjectToJSON(message);
194 debug_handler("deserialize parsed message \n%s\n", j );
198 if(message && message->type != JSON_NULL &&
199 message->classname && !strcmp(message->classname, "osrfMessage")) {
201 osrf_message* new_msg = safe_malloc(sizeof(osrf_message));
203 jsonObject* tmp = jsonObjectGetKey(message, "type");
206 if( ( t = jsonObjectGetString(tmp)) ) {
208 if(!strcmp(t, "CONNECT")) new_msg->m_type = CONNECT;
209 if(!strcmp(t, "DISCONNECT")) new_msg->m_type = DISCONNECT;
210 if(!strcmp(t, "STATUS")) new_msg->m_type = STATUS;
211 if(!strcmp(t, "REQUEST")) new_msg->m_type = REQUEST;
212 if(!strcmp(t, "RESULT")) new_msg->m_type = RESULT;
215 tmp = jsonObjectGetKey(message, "threadTrace");
217 if(tmp->type == JSON_NUMBER)
218 new_msg->thread_trace = (int) jsonObjectGetNumber(tmp);
219 if(tmp->type == JSON_STRING)
220 new_msg->thread_trace = atoi(jsonObjectGetString(tmp));
224 tmp = jsonObjectGetKey(message, "protocol");
227 if(tmp->type == JSON_NUMBER)
228 new_msg->protocol = (int) jsonObjectGetNumber(tmp);
229 if(tmp->type == JSON_STRING)
230 new_msg->protocol = atoi(jsonObjectGetString(tmp));
233 tmp = jsonObjectGetKey(message, "payload");
236 new_msg->status_name = strdup(tmp->classname);
238 jsonObject* tmp0 = jsonObjectGetKey(tmp,"method");
239 if(jsonObjectGetString(tmp0))
240 new_msg->method_name = strdup(jsonObjectGetString(tmp0));
242 tmp0 = jsonObjectGetKey(tmp,"params");
244 char* s = jsonObjectToJSON(tmp0);
245 new_msg->_params = jsonParseString(s);
249 tmp0 = jsonObjectGetKey(tmp,"status");
250 if(jsonObjectGetString(tmp0))
251 new_msg->status_text = strdup(jsonObjectGetString(tmp0));
253 tmp0 = jsonObjectGetKey(tmp,"statusCode");
255 if(jsonObjectGetString(tmp0))
256 new_msg->status_code = atoi(jsonObjectGetString(tmp0));
257 if(tmp0->type == JSON_NUMBER)
258 new_msg->status_code = (int) jsonObjectGetNumber(tmp0);
261 tmp0 = jsonObjectGetKey(tmp,"content");
263 char* s = jsonObjectToJSON(tmp0);
264 new_msg->_result_content = jsonParseString(s);
269 msgs[numparsed++] = new_msg;
270 debug_handler("deserialize has parsed %d messages", numparsed);
274 jsonObjectFree(json);