1 #include <opensrf/osrf_message.h>
3 static char default_locale[17] = "en-US\0\0\0\0\0\0\0\0\0\0\0\0";
4 static char* current_locale = NULL;
6 osrf_message* osrf_message_init( enum M_TYPE type, int thread_trace, int protocol ) {
8 osrf_message* msg = (osrf_message*) safe_malloc(sizeof(osrf_message));
10 msg->thread_trace = thread_trace;
11 msg->protocol = protocol;
13 msg->is_exception = 0;
15 msg->_result_content = NULL;
16 msg->sender_locale = NULL;
22 const char* osrf_message_get_last_locale() {
23 return current_locale;
26 char* osrf_message_set_locale( osrf_message* msg, const char* locale ) {
27 if( msg == NULL || locale == NULL ) return NULL;
28 return msg->sender_locale = strdup( locale );
31 const char* osrf_message_set_default_locale( const char* locale ) {
32 if( locale == NULL ) return NULL;
33 if( strlen(locale) > 16 ) return NULL;
35 memcpy( default_locale, locale, strlen(locale) );
36 default_locale[strlen(locale)] = '\0';
37 return (const char*) default_locale;
40 void osrf_message_set_method( osrf_message* msg, char* method_name ) {
41 if( msg == NULL || method_name == NULL ) return;
42 msg->method_name = strdup( method_name );
46 void osrf_message_add_object_param( osrf_message* msg, jsonObject* o ) {
49 msg->_params = jsonParseString("[]");
50 char* j = jsonObjectToJSON(o);
51 jsonObjectPush(msg->_params, jsonParseString(j));
55 void osrf_message_set_params( osrf_message* msg, jsonObject* o ) {
56 if(!msg || !o) return;
58 if(o->type != JSON_ARRAY) {
59 osrfLogDebug( OSRF_LOG_MARK, "passing non-array to osrf_message_set_params(), fixing...");
60 jsonObject* clone = jsonObjectClone(o);
61 o = jsonNewObject(NULL);
62 jsonObjectPush(o, clone);
63 if(msg->_params) jsonObjectFree(msg->_params);
68 if(msg->_params) jsonObjectFree(msg->_params);
69 msg->_params = jsonObjectClone(o);
73 /* only works if parse_json_params is false */
74 void osrf_message_add_param( osrf_message* msg, char* param_string ) {
75 if(msg == NULL || param_string == NULL) return;
76 if(!msg->_params) msg->_params = jsonParseString("[]");
77 jsonObjectPush(msg->_params, jsonParseString(param_string));
81 void osrf_message_set_status_info(
82 osrf_message* msg, char* status_name, char* status_text, int status_code ) {
85 if( status_name != NULL )
86 msg->status_name = strdup( status_name );
88 if( status_text != NULL )
89 msg->status_text = strdup( status_text );
91 msg->status_code = status_code;
95 void osrf_message_set_result_content( osrf_message* msg, char* json_string ) {
96 if( msg == NULL || json_string == NULL) return;
97 msg->result_string = strdup(json_string);
98 if(json_string) msg->_result_content = jsonParseString(json_string);
103 void osrfMessageFree( osrfMessage* msg ) {
104 osrf_message_free( msg );
107 void osrf_message_free( osrf_message* msg ) {
111 if( msg->status_name != NULL )
112 free(msg->status_name);
114 if( msg->status_text != NULL )
115 free(msg->status_text);
117 if( msg->_result_content != NULL )
118 jsonObjectFree( msg->_result_content );
120 if( msg->result_string != NULL )
121 free( msg->result_string);
123 if( msg->method_name != NULL )
124 free(msg->method_name);
126 if( msg->sender_locale != NULL )
127 free(msg->sender_locale);
129 if( msg->_params != NULL )
130 jsonObjectFree(msg->_params);
136 char* osrfMessageSerializeBatch( osrfMessage* msgs [], int count ) {
137 if( !msgs ) return NULL;
141 osrfMessage* msg = NULL;
142 jsonObject* wrapper = jsonNewObject(NULL);
144 while( ((msg = msgs[i]) && (i++ < count)) )
145 jsonObjectPush(wrapper, osrfMessageToJSON( msg ));
147 j = jsonObjectToJSON(wrapper);
148 jsonObjectFree(wrapper);
154 char* osrf_message_serialize(osrf_message* msg) {
156 if( msg == NULL ) return NULL;
159 jsonObject* json = osrfMessageToJSON( msg );
162 jsonObject* wrapper = jsonNewObject(NULL);
163 jsonObjectPush(wrapper, json);
164 j = jsonObjectToJSON(wrapper);
165 jsonObjectFree(wrapper);
172 jsonObject* osrfMessageToJSON( osrfMessage* msg ) {
174 jsonObject* json = jsonNewObject(NULL);
175 jsonObjectSetClass(json, "osrfMessage");
177 char sc[64]; memset(sc,0,64);
181 INT_TO_STRING(msg->thread_trace);
182 jsonObjectSetKey(json, "threadTrace", jsonNewObject(INTSTR));
184 if (msg->sender_locale != NULL) {
185 jsonObjectSetKey(json, "locale", jsonNewObject(msg->sender_locale));
186 } else if (current_locale != NULL) {
187 jsonObjectSetKey(json, "locale", jsonNewObject(current_locale));
189 jsonObjectSetKey(json, "locale", jsonNewObject(default_locale));
192 switch(msg->m_type) {
195 jsonObjectSetKey(json, "type", jsonNewObject("CONNECT"));
199 jsonObjectSetKey(json, "type", jsonNewObject("DISCONNECT"));
203 jsonObjectSetKey(json, "type", jsonNewObject("STATUS"));
204 payload = jsonNewObject(NULL);
205 jsonObjectSetClass(payload, msg->status_name);
206 jsonObjectSetKey(payload, "status", jsonNewObject(msg->status_text));
207 sprintf(sc,"%d",msg->status_code);
208 jsonObjectSetKey(payload, "statusCode", jsonNewObject(sc));
209 jsonObjectSetKey(json, "payload", payload);
213 jsonObjectSetKey(json, "type", jsonNewObject("REQUEST"));
214 payload = jsonNewObject(NULL);
215 jsonObjectSetClass(payload, "osrfMethod");
216 jsonObjectSetKey(payload, "method", jsonNewObject(msg->method_name));
217 str = jsonObjectToJSON(msg->_params);
218 jsonObjectSetKey(payload, "params", jsonParseString(str));
220 jsonObjectSetKey(json, "payload", payload);
225 jsonObjectSetKey(json, "type", jsonNewObject("RESULT"));
226 payload = jsonNewObject(NULL);
227 jsonObjectSetClass(payload,"osrfResult");
228 jsonObjectSetKey(payload, "status", jsonNewObject(msg->status_text));
229 sprintf(sc,"%d",msg->status_code);
230 jsonObjectSetKey(payload, "statusCode", jsonNewObject(sc));
231 str = jsonObjectToJSON(msg->_result_content);
232 jsonObjectSetKey(payload, "content", jsonParseString(str));
234 jsonObjectSetKey(json, "payload", payload);
242 int osrf_message_deserialize(char* string, osrf_message* msgs[], int count) {
244 if(!string || !msgs || count <= 0) return 0;
247 jsonObject* json = jsonParseString(string);
250 osrfLogWarning( OSRF_LOG_MARK,
251 "osrf_message_deserialize() unable to parse data: \n%s\n", string);
257 for( x = 0; x < json->size && x < count; x++ ) {
259 jsonObject* message = jsonObjectGetIndex(json, x);
261 if(message && message->type != JSON_NULL &&
262 message->classname && !strcmp(message->classname, "osrfMessage")) {
264 osrf_message* new_msg = safe_malloc(sizeof(osrf_message));
266 jsonObject* tmp = jsonObjectGetKey(message, "type");
269 if( ( t = jsonObjectGetString(tmp)) ) {
271 if(!strcmp(t, "CONNECT")) new_msg->m_type = CONNECT;
272 if(!strcmp(t, "DISCONNECT")) new_msg->m_type = DISCONNECT;
273 if(!strcmp(t, "STATUS")) new_msg->m_type = STATUS;
274 if(!strcmp(t, "REQUEST")) new_msg->m_type = REQUEST;
275 if(!strcmp(t, "RESULT")) new_msg->m_type = RESULT;
278 tmp = jsonObjectGetKey(message, "threadTrace");
280 char* tt = jsonObjectToSimpleString(tmp);
282 new_msg->thread_trace = atoi(tt);
287 /* use the sender's locale, or the global default */
289 free( current_locale );
291 tmp = jsonObjectGetKey(message, "locale");
293 new_msg->sender_locale = jsonObjectToSimpleString(tmp);
294 current_locale = strdup( new_msg->sender_locale );
296 current_locale = NULL;
299 tmp = jsonObjectGetKey(message, "protocol");
302 char* proto = jsonObjectToSimpleString(tmp);
304 new_msg->protocol = atoi(proto);
309 tmp = jsonObjectGetKey(message, "payload");
312 new_msg->status_name = strdup(tmp->classname);
314 jsonObject* tmp0 = jsonObjectGetKey(tmp,"method");
315 if(jsonObjectGetString(tmp0))
316 new_msg->method_name = strdup(jsonObjectGetString(tmp0));
318 tmp0 = jsonObjectGetKey(tmp,"params");
320 char* s = jsonObjectToJSON(tmp0);
321 new_msg->_params = jsonParseString(s);
322 if(new_msg->_params && new_msg->_params->type == JSON_NULL)
323 new_msg->_params->type = JSON_ARRAY;
327 tmp0 = jsonObjectGetKey(tmp,"status");
328 if(jsonObjectGetString(tmp0))
329 new_msg->status_text = strdup(jsonObjectGetString(tmp0));
331 tmp0 = jsonObjectGetKey(tmp,"statusCode");
333 if(jsonObjectGetString(tmp0))
334 new_msg->status_code = atoi(jsonObjectGetString(tmp0));
335 if(tmp0->type == JSON_NUMBER)
336 new_msg->status_code = (int) jsonObjectGetNumber(tmp0);
339 tmp0 = jsonObjectGetKey(tmp,"content");
341 char* s = jsonObjectToJSON(tmp0);
342 new_msg->_result_content = jsonParseString(s);
347 msgs[numparsed++] = new_msg;
351 jsonObjectFree(json);
357 jsonObject* osrfMessageGetResult( osrfMessage* msg ) {
358 if(msg) return msg->_result_content;