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) > sizeof(default_locale) - 1 ) return NULL;
35 strcpy( default_locale, locale );
36 return (const char*) default_locale;
39 void osrf_message_set_method( osrf_message* msg, const char* method_name ) {
40 if( msg == NULL || method_name == NULL ) return;
41 msg->method_name = strdup( method_name );
45 void osrf_message_add_object_param( osrf_message* msg, const jsonObject* o ) {
48 msg->_params = jsonParseString("[]");
49 char* j = jsonObjectToJSON(o);
50 jsonObjectPush(msg->_params, jsonParseString(j));
54 void osrf_message_set_params( osrf_message* msg, const jsonObject* o ) {
55 if(!msg || !o) return;
57 if(o->type != JSON_ARRAY) {
58 osrfLogDebug( OSRF_LOG_MARK, "passing non-array to osrf_message_set_params(), fixing...");
59 if(msg->_params) jsonObjectFree(msg->_params);
60 jsonObject* clone = jsonObjectClone(o);
61 msg->_params = jsonNewObject(NULL);
62 jsonObjectPush(msg->_params, clone);
66 if(msg->_params) jsonObjectFree(msg->_params);
67 msg->_params = jsonObjectClone(o);
71 /* only works if parse_json_params is false */
72 void osrf_message_add_param( osrf_message* msg, const char* param_string ) {
73 if(msg == NULL || param_string == NULL) return;
74 if(!msg->_params) msg->_params = jsonParseString("[]");
75 jsonObjectPush(msg->_params, jsonParseString(param_string));
79 void osrf_message_set_status_info( osrf_message* msg,
80 const char* status_name, const char* status_text, int status_code ) {
83 if( status_name != NULL )
84 msg->status_name = strdup( status_name );
86 if( status_text != NULL )
87 msg->status_text = strdup( status_text );
89 msg->status_code = status_code;
93 void osrf_message_set_result_content( osrf_message* msg, const char* json_string ) {
94 if( msg == NULL || json_string == NULL) return;
95 msg->result_string = strdup(json_string);
96 if(json_string) msg->_result_content = jsonParseString(json_string);
101 void osrfMessageFree( osrfMessage* msg ) {
102 osrf_message_free( msg );
105 void osrf_message_free( osrf_message* msg ) {
109 if( msg->status_name != NULL )
110 free(msg->status_name);
112 if( msg->status_text != NULL )
113 free(msg->status_text);
115 if( msg->_result_content != NULL )
116 jsonObjectFree( msg->_result_content );
118 if( msg->result_string != NULL )
119 free( msg->result_string);
121 if( msg->method_name != NULL )
122 free(msg->method_name);
124 if( msg->sender_locale != NULL )
125 free(msg->sender_locale);
127 if( msg->_params != NULL )
128 jsonObjectFree(msg->_params);
134 char* osrfMessageSerializeBatch( osrfMessage* msgs [], int count ) {
135 if( !msgs ) return NULL;
139 const osrfMessage* msg = NULL;
140 jsonObject* wrapper = jsonNewObject(NULL);
142 while( ((msg = msgs[i]) && (i++ < count)) )
143 jsonObjectPush(wrapper, osrfMessageToJSON( msg ));
145 j = jsonObjectToJSON(wrapper);
146 jsonObjectFree(wrapper);
152 char* osrf_message_serialize(const osrf_message* msg) {
154 if( msg == NULL ) return NULL;
157 jsonObject* json = osrfMessageToJSON( msg );
160 jsonObject* wrapper = jsonNewObject(NULL);
161 jsonObjectPush(wrapper, json);
162 j = jsonObjectToJSON(wrapper);
163 jsonObjectFree(wrapper);
170 jsonObject* osrfMessageToJSON( const osrfMessage* msg ) {
172 jsonObject* json = jsonNewObject(NULL);
173 jsonObjectSetClass(json, "osrfMessage");
176 osrf_clearbuf(sc, sizeof(sc));
180 INT_TO_STRING(msg->thread_trace);
181 jsonObjectSetKey(json, "threadTrace", jsonNewObject(INTSTR));
183 if (msg->sender_locale != NULL) {
184 jsonObjectSetKey(json, "locale", jsonNewObject(msg->sender_locale));
185 } else if (current_locale != NULL) {
186 jsonObjectSetKey(json, "locale", jsonNewObject(current_locale));
188 jsonObjectSetKey(json, "locale", jsonNewObject(default_locale));
191 switch(msg->m_type) {
194 jsonObjectSetKey(json, "type", jsonNewObject("CONNECT"));
198 jsonObjectSetKey(json, "type", jsonNewObject("DISCONNECT"));
202 jsonObjectSetKey(json, "type", jsonNewObject("STATUS"));
203 payload = jsonNewObject(NULL);
204 jsonObjectSetClass(payload, msg->status_name);
205 jsonObjectSetKey(payload, "status", jsonNewObject(msg->status_text));
206 snprintf(sc, sizeof(sc), "%d", msg->status_code);
207 jsonObjectSetKey(payload, "statusCode", jsonNewObject(sc));
208 jsonObjectSetKey(json, "payload", payload);
212 jsonObjectSetKey(json, "type", jsonNewObject("REQUEST"));
213 payload = jsonNewObject(NULL);
214 jsonObjectSetClass(payload, "osrfMethod");
215 jsonObjectSetKey(payload, "method", jsonNewObject(msg->method_name));
216 str = jsonObjectToJSON(msg->_params);
217 jsonObjectSetKey(payload, "params", jsonParseString(str));
219 jsonObjectSetKey(json, "payload", payload);
224 jsonObjectSetKey(json, "type", jsonNewObject("RESULT"));
225 payload = jsonNewObject(NULL);
226 jsonObjectSetClass(payload,"osrfResult");
227 jsonObjectSetKey(payload, "status", jsonNewObject(msg->status_text));
228 snprintf(sc, sizeof(sc), "%d", msg->status_code);
229 jsonObjectSetKey(payload, "statusCode", jsonNewObject(sc));
230 str = jsonObjectToJSON(msg->_result_content);
231 jsonObjectSetKey(payload, "content", jsonParseString(str));
233 jsonObjectSetKey(json, "payload", payload);
241 int osrf_message_deserialize(const char* string, osrf_message* msgs[], int count) {
243 if(!string || !msgs || count <= 0) return 0;
246 jsonObject* json = jsonParseString(string);
249 osrfLogWarning( OSRF_LOG_MARK,
250 "osrf_message_deserialize() unable to parse data: \n%s\n", string);
256 for( x = 0; x < json->size && x < count; x++ ) {
258 const jsonObject* message = jsonObjectGetIndex(json, x);
260 if(message && message->type != JSON_NULL &&
261 message->classname && !strcmp(message->classname, "osrfMessage")) {
263 osrf_message* new_msg = safe_malloc(sizeof(osrf_message));
265 const jsonObject* tmp = jsonObjectGetKeyConst(message, "type");
268 if( ( t = jsonObjectGetString(tmp)) ) {
270 if(!strcmp(t, "CONNECT")) new_msg->m_type = CONNECT;
271 if(!strcmp(t, "DISCONNECT")) new_msg->m_type = DISCONNECT;
272 if(!strcmp(t, "STATUS")) new_msg->m_type = STATUS;
273 if(!strcmp(t, "REQUEST")) new_msg->m_type = REQUEST;
274 if(!strcmp(t, "RESULT")) new_msg->m_type = RESULT;
277 tmp = jsonObjectGetKeyConst(message, "threadTrace");
279 char* tt = jsonObjectToSimpleString(tmp);
281 new_msg->thread_trace = atoi(tt);
286 /* use the sender's locale, or the global default */
288 free( current_locale );
290 tmp = jsonObjectGetKeyConst(message, "locale");
292 new_msg->sender_locale = jsonObjectToSimpleString(tmp);
293 current_locale = strdup( new_msg->sender_locale );
295 current_locale = NULL;
298 tmp = jsonObjectGetKeyConst(message, "protocol");
301 char* proto = jsonObjectToSimpleString(tmp);
303 new_msg->protocol = atoi(proto);
308 tmp = jsonObjectGetKeyConst(message, "payload");
311 new_msg->status_name = strdup(tmp->classname);
313 const jsonObject* tmp0 = jsonObjectGetKeyConst(tmp,"method");
314 if(jsonObjectGetString(tmp0))
315 new_msg->method_name = strdup(jsonObjectGetString(tmp0));
317 tmp0 = jsonObjectGetKeyConst(tmp,"params");
319 char* s = jsonObjectToJSON(tmp0);
320 new_msg->_params = jsonParseString(s);
321 if(new_msg->_params && new_msg->_params->type == JSON_NULL)
322 new_msg->_params->type = JSON_ARRAY;
326 tmp0 = jsonObjectGetKeyConst(tmp,"status");
327 if(jsonObjectGetString(tmp0))
328 new_msg->status_text = strdup(jsonObjectGetString(tmp0));
330 tmp0 = jsonObjectGetKeyConst(tmp,"statusCode");
332 if(jsonObjectGetString(tmp0))
333 new_msg->status_code = atoi(jsonObjectGetString(tmp0));
334 if(tmp0->type == JSON_NUMBER)
335 new_msg->status_code = (int) jsonObjectGetNumber(tmp0);
338 tmp0 = jsonObjectGetKeyConst(tmp,"content");
340 char* s = jsonObjectToJSON(tmp0);
341 new_msg->_result_content = jsonParseString(s);
346 msgs[numparsed++] = new_msg;
350 jsonObjectFree(json);
356 jsonObject* osrfMessageGetResult( osrfMessage* msg ) {
357 if(msg) return msg->_result_content;