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, object* o ) {
27 msg->_params = json_parse_string("[]");
28 char* j = o->to_json(o);
29 msg->_params->push(msg->_params, json_parse_string(j));
33 void osrf_message_set_params( osrf_message* msg, object* o ) {
34 if(!msg || !o) return;
37 warning_handler("passing non-array to osrf_message_set_params()");
41 if(msg->_params) free_object(msg->_params);
43 char* j = o->to_json(o);
44 msg->_params = json_parse_string(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 = new_object(NULL);
53 msg->_params->push(msg->_params, json_parse_string(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 = json_parse_string(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 free_object( 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 free_object(msg->_params);
108 char* osrf_message_serialize(osrf_message* msg) {
109 if( msg == NULL ) return NULL;
110 object* json = new_object(NULL);
111 json->set_class(json, "osrfMessage");
113 char sc[64]; memset(sc,0,64);
119 sprintf(tt,"%d",msg->thread_trace);
120 json->add_key(json, "threadTrace", new_object(tt));
122 switch(msg->m_type) {
125 json->add_key(json, "type", new_object("CONNECT"));
129 json->add_key(json, "type", new_object("DISCONNECT"));
133 json->add_key(json, "type", new_object("STATUS"));
134 payload = new_object(NULL);
135 payload->set_class(payload, msg->status_name);
136 payload->add_key(payload, "status", new_object(msg->status_text));
137 sprintf(sc,"%d",msg->status_code);
138 payload->add_key(payload, "statusCode", new_object(sc));
139 json->add_key(json, "payload", payload);
143 json->add_key(json, "type", new_object("REQUEST"));
144 payload = new_object(NULL);
145 payload->set_class(payload, "osrfMethod");
146 payload->add_key(payload, "method", new_object(msg->method_name));
147 str = object_to_json(msg->_params);
148 payload->add_key(payload, "params", json_parse_string(str));
150 json->add_key(json, "payload", payload);
155 json->add_key(json, "type", new_object("RESULT"));
156 payload = new_object(NULL);
157 payload->set_class(payload,"osrfResult");
158 payload->add_key(payload, "status", new_object(msg->status_text));
159 sprintf(sc,"%d",msg->status_code);
160 payload->add_key(payload, "statusCode", new_object(sc));
161 str = object_to_json(msg->_result_content);
162 payload->add_key(payload, "content", json_parse_string(str));
164 json->add_key(json, "payload", payload);
168 object* wrapper = new_object(NULL);
169 wrapper->push(wrapper, json);
170 char* j = wrapper->to_json(wrapper);
171 free_object(wrapper);
176 int osrf_message_deserialize(char* string, osrf_message* msgs[], int count) {
177 if(!string || !msgs || count <= 0) return 0;
179 object* json = json_parse_string(string);
180 if(json == NULL) return 0;
184 for( x = 0; x < json->size && x < count; x++ ) {
186 object* message = json->get_index(json, x);
188 if(message && !message->is_null &&
189 message->classname && !strcmp(message->classname, "osrfMessage")) {
191 osrf_message* new_msg = safe_malloc(sizeof(osrf_message));
193 object* tmp = message->get_key(message, "type");
195 if(tmp && tmp->string_data) {
196 char* t = tmp->string_data;
198 if(!strcmp(t, "CONNECT")) new_msg->m_type = CONNECT;
199 if(!strcmp(t, "DISCONNECT")) new_msg->m_type = DISCONNECT;
200 if(!strcmp(t, "STATUS")) new_msg->m_type = STATUS;
201 if(!strcmp(t, "REQUEST")) new_msg->m_type = REQUEST;
202 if(!strcmp(t, "RESULT")) new_msg->m_type = RESULT;
205 tmp = message->get_key(message, "threadTrace");
208 new_msg->thread_trace = tmp->num_value;
210 new_msg->thread_trace = atoi(tmp->string_data);
214 tmp = message->get_key(message, "protocol");
217 new_msg->protocol = tmp->num_value;
219 new_msg->protocol = atoi(tmp->string_data);
222 tmp = message->get_key(message, "payload");
225 new_msg->status_name = strdup(tmp->classname);
227 object* tmp0 = tmp->get_key(tmp,"method");
228 if(tmp0 && tmp0->string_data)
229 new_msg->method_name = strdup(tmp0->string_data);
231 tmp0 = tmp->get_key(tmp,"params");
233 char* s = tmp0->to_json(tmp0);
234 new_msg->_params = json_parse_string(s);
238 tmp0 = tmp->get_key(tmp,"status");
239 if(tmp0 && tmp0->string_data)
240 new_msg->status_text = strdup(tmp0->string_data);
242 tmp0 = tmp->get_key(tmp,"statusCode");
244 if(tmp0->is_string && tmp0->string_data)
245 new_msg->status_code = atoi(tmp0->string_data);
247 new_msg->status_code = tmp0->num_value;
250 tmp0 = tmp->get_key(tmp,"content");
252 char* s = tmp0->to_json(tmp0);
253 new_msg->_result_content = json_parse_string(s);
258 msgs[numparsed++] = new_msg;