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");
178 memset(sc, 0, sizeof(sc));
182 INT_TO_STRING(msg->thread_trace);
183 jsonObjectSetKey(json, "threadTrace", jsonNewObject(INTSTR));
185 if (msg->sender_locale != NULL) {
186 jsonObjectSetKey(json, "locale", jsonNewObject(msg->sender_locale));
187 } else if (current_locale != NULL) {
188 jsonObjectSetKey(json, "locale", jsonNewObject(current_locale));
190 jsonObjectSetKey(json, "locale", jsonNewObject(default_locale));
193 switch(msg->m_type) {
196 jsonObjectSetKey(json, "type", jsonNewObject("CONNECT"));
200 jsonObjectSetKey(json, "type", jsonNewObject("DISCONNECT"));
204 jsonObjectSetKey(json, "type", jsonNewObject("STATUS"));
205 payload = jsonNewObject(NULL);
206 jsonObjectSetClass(payload, msg->status_name);
207 jsonObjectSetKey(payload, "status", jsonNewObject(msg->status_text));
208 snprintf(sc, sizeof(sc), "%d", msg->status_code);
209 jsonObjectSetKey(payload, "statusCode", jsonNewObject(sc));
210 jsonObjectSetKey(json, "payload", payload);
214 jsonObjectSetKey(json, "type", jsonNewObject("REQUEST"));
215 payload = jsonNewObject(NULL);
216 jsonObjectSetClass(payload, "osrfMethod");
217 jsonObjectSetKey(payload, "method", jsonNewObject(msg->method_name));
218 str = jsonObjectToJSON(msg->_params);
219 jsonObjectSetKey(payload, "params", jsonParseString(str));
221 jsonObjectSetKey(json, "payload", payload);
226 jsonObjectSetKey(json, "type", jsonNewObject("RESULT"));
227 payload = jsonNewObject(NULL);
228 jsonObjectSetClass(payload,"osrfResult");
229 jsonObjectSetKey(payload, "status", jsonNewObject(msg->status_text));
230 snprintf(sc, sizeof(sc), "%d", msg->status_code);
231 jsonObjectSetKey(payload, "statusCode", jsonNewObject(sc));
232 str = jsonObjectToJSON(msg->_result_content);
233 jsonObjectSetKey(payload, "content", jsonParseString(str));
235 jsonObjectSetKey(json, "payload", payload);
243 int osrf_message_deserialize(char* string, osrf_message* msgs[], int count) {
245 if(!string || !msgs || count <= 0) return 0;
248 jsonObject* json = jsonParseString(string);
251 osrfLogWarning( OSRF_LOG_MARK,
252 "osrf_message_deserialize() unable to parse data: \n%s\n", string);
258 for( x = 0; x < json->size && x < count; x++ ) {
260 const jsonObject* message = jsonObjectGetIndex(json, x);
262 if(message && message->type != JSON_NULL &&
263 message->classname && !strcmp(message->classname, "osrfMessage")) {
265 osrf_message* new_msg = safe_malloc(sizeof(osrf_message));
267 const jsonObject* tmp = jsonObjectGetKeyConst(message, "type");
270 if( ( t = jsonObjectGetString(tmp)) ) {
272 if(!strcmp(t, "CONNECT")) new_msg->m_type = CONNECT;
273 if(!strcmp(t, "DISCONNECT")) new_msg->m_type = DISCONNECT;
274 if(!strcmp(t, "STATUS")) new_msg->m_type = STATUS;
275 if(!strcmp(t, "REQUEST")) new_msg->m_type = REQUEST;
276 if(!strcmp(t, "RESULT")) new_msg->m_type = RESULT;
279 tmp = jsonObjectGetKeyConst(message, "threadTrace");
281 char* tt = jsonObjectToSimpleString(tmp);
283 new_msg->thread_trace = atoi(tt);
288 /* use the sender's locale, or the global default */
290 free( current_locale );
292 tmp = jsonObjectGetKeyConst(message, "locale");
294 new_msg->sender_locale = jsonObjectToSimpleString(tmp);
295 current_locale = strdup( new_msg->sender_locale );
297 current_locale = NULL;
300 tmp = jsonObjectGetKeyConst(message, "protocol");
303 char* proto = jsonObjectToSimpleString(tmp);
305 new_msg->protocol = atoi(proto);
310 tmp = jsonObjectGetKeyConst(message, "payload");
313 new_msg->status_name = strdup(tmp->classname);
315 const jsonObject* tmp0 = jsonObjectGetKeyConst(tmp,"method");
316 if(jsonObjectGetString(tmp0))
317 new_msg->method_name = strdup(jsonObjectGetString(tmp0));
319 tmp0 = jsonObjectGetKeyConst(tmp,"params");
321 char* s = jsonObjectToJSON(tmp0);
322 new_msg->_params = jsonParseString(s);
323 if(new_msg->_params && new_msg->_params->type == JSON_NULL)
324 new_msg->_params->type = JSON_ARRAY;
328 tmp0 = jsonObjectGetKeyConst(tmp,"status");
329 if(jsonObjectGetString(tmp0))
330 new_msg->status_text = strdup(jsonObjectGetString(tmp0));
332 tmp0 = jsonObjectGetKeyConst(tmp,"statusCode");
334 if(jsonObjectGetString(tmp0))
335 new_msg->status_code = atoi(jsonObjectGetString(tmp0));
336 if(tmp0->type == JSON_NUMBER)
337 new_msg->status_code = (int) jsonObjectGetNumber(tmp0);
340 tmp0 = jsonObjectGetKeyConst(tmp,"content");
342 char* s = jsonObjectToJSON(tmp0);
343 new_msg->_result_content = jsonParseString(s);
348 msgs[numparsed++] = new_msg;
352 jsonObjectFree(json);
358 jsonObject* osrfMessageGetResult( osrfMessage* msg ) {
359 if(msg) return msg->_result_content;