]> git.evergreen-ils.org Git - Evergreen.git/blob - OpenSRF/src/libstack/osrf_message.c
added some more api wrappers
[Evergreen.git] / OpenSRF / src / libstack / osrf_message.c
1 #include "osrf_message.h"
2
3 osrf_message* osrf_message_init( enum M_TYPE type, int thread_trace, int protocol ) {
4
5         osrf_message* msg                       = (osrf_message*) safe_malloc(sizeof(osrf_message));
6         msg->m_type                                     = type;
7         msg->thread_trace                       = thread_trace;
8         msg->protocol                           = protocol;
9         msg->next                                       = NULL;
10         msg->is_exception                       = 0;
11         msg->_params                            = NULL;
12         msg->_result_content            = NULL;
13
14         return msg;
15 }
16
17
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 );
21 }
22
23
24 void osrf_message_add_object_param( osrf_message* msg, jsonObject* o ) {
25         if(!msg|| !o) return;
26         if(!msg->_params)
27                 msg->_params = jsonParseString("[]");
28         char* j = jsonObjectToJSON(o);
29         jsonObjectPush(msg->_params, jsonParseString(j));
30         free(j);
31 }
32
33 void osrf_message_set_params( osrf_message* msg, jsonObject* o ) {
34         if(!msg || !o) return;
35
36         if(!o->type == JSON_ARRAY) {
37                 warning_handler("passing non-array to osrf_message_set_params()");
38                 return;
39         }
40
41         if(msg->_params) jsonObjectFree(msg->_params);
42
43         char* j = jsonObjectToJSON(o);
44         msg->_params = jsonParseString(j);
45         free(j);
46 }
47
48
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));
54 }
55
56
57 void osrf_message_set_status_info( 
58                 osrf_message* msg, char* status_name, char* status_text, int status_code ) {
59
60         if( msg == NULL )
61                 fatal_handler( "Bad params to osrf_message_set_status_info()" );
62
63         if( status_name != NULL ) 
64                 msg->status_name = strdup( status_name );
65
66         if( status_text != NULL )
67                 msg->status_text = strdup( status_text );
68
69         msg->status_code = status_code;
70 }
71
72
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()" );
76
77         msg->result_string =    strdup(json_string);
78         if(json_string) msg->_result_content = jsonParseString(json_string);
79 }
80
81
82
83 void osrfMessageFree( osrfMessage* msg ) {
84         osrf_message_free( msg );
85 }
86
87 void osrf_message_free( osrf_message* msg ) {
88         if( msg == NULL )
89                 return;
90
91         if( msg->status_name != NULL )
92                 free(msg->status_name);
93
94         if( msg->status_text != NULL )
95                 free(msg->status_text);
96
97         if( msg->_result_content != NULL )
98                 jsonObjectFree( msg->_result_content );
99
100         if( msg->result_string != NULL )
101                 free( msg->result_string);
102
103         if( msg->method_name != NULL )
104                 free(msg->method_name);
105
106         if( msg->_params != NULL )
107                 jsonObjectFree(msg->_params);
108
109         free(msg);
110 }
111
112 char* osrf_message_serialize(osrf_message* msg) {
113         if( msg == NULL ) return NULL;
114         jsonObject* json = jsonNewObject(NULL);
115         jsonObjectSetClass(json, "osrfMessage");
116         jsonObject* payload;
117         char sc[64]; memset(sc,0,64);
118
119         char* str;
120
121         INT_TO_STRING(msg->thread_trace);
122         jsonObjectSetKey(json, "threadTrace", jsonNewObject(INTSTR));
123
124         switch(msg->m_type) {
125                 
126                 case CONNECT: 
127                         jsonObjectSetKey(json, "type", jsonNewObject("CONNECT"));
128                         break;
129
130                 case DISCONNECT: 
131                         jsonObjectSetKey(json, "type", jsonNewObject("DISCONNECT"));
132                         break;
133
134                 case STATUS:
135                         jsonObjectSetKey(json, "type", jsonNewObject("STATUS"));
136                         payload = jsonNewObject(NULL);
137                         jsonObjectSetClass(payload, msg->status_name);
138                         jsonObjectSetKey(payload, "status", jsonNewObject(msg->status_text));
139          sprintf(sc,"%d",msg->status_code);
140                         jsonObjectSetKey(payload, "statusCode", jsonNewObject(sc));
141                         jsonObjectSetKey(json, "payload", payload);
142                         break;
143
144                 case REQUEST:
145                         jsonObjectSetKey(json, "type", jsonNewObject("REQUEST"));
146                         payload = jsonNewObject(NULL);
147                         jsonObjectSetClass(payload, "osrfMethod");
148                         jsonObjectSetKey(payload, "method", jsonNewObject(msg->method_name));
149                         str = jsonObjectToJSON(msg->_params);
150                         jsonObjectSetKey(payload, "params", jsonParseString(str));
151                         free(str);
152                         jsonObjectSetKey(json, "payload", payload);
153
154                         break;
155
156                 case RESULT:
157                         jsonObjectSetKey(json, "type", jsonNewObject("RESULT"));
158                         payload = jsonNewObject(NULL);
159                         jsonObjectSetClass(payload,"osrfResult");
160                         jsonObjectSetKey(payload, "status", jsonNewObject(msg->status_text));
161          sprintf(sc,"%d",msg->status_code);
162                         jsonObjectSetKey(payload, "statusCode", jsonNewObject(sc));
163                         str = jsonObjectToJSON(msg->_result_content);
164                         jsonObjectSetKey(payload, "content", jsonParseString(str));
165                         free(str);
166                         jsonObjectSetKey(json, "payload", payload);
167                         break;
168         }
169         
170         jsonObject* wrapper = jsonNewObject(NULL);
171         jsonObjectPush(wrapper, json);
172         char* j = jsonObjectToJSON(wrapper);
173         jsonObjectFree(wrapper);
174         return j;
175 }
176
177
178 int osrf_message_deserialize(char* string, osrf_message* msgs[], int count) {
179
180         if(!string || !msgs || count <= 0) return 0;
181         int numparsed = 0;
182
183         jsonObject* json = jsonParseString(string);
184
185         if(!json) {
186                 warning_handler(
187                         "osrf_message_deserialize() unable to parse data: \n%s\n", string);
188                 return 0;
189         }
190
191         int x;
192
193         for( x = 0; x < json->size && x < count; x++ ) {
194
195                 jsonObject* message = jsonObjectGetIndex(json, x);
196
197                 char* j =  jsonObjectToJSON(message);
198                 debug_handler("deserialize parsed message \n%s\n", j );
199                 free(j);
200         
201
202                 if(message && message->type != JSON_NULL && 
203                         message->classname && !strcmp(message->classname, "osrfMessage")) {
204
205                         osrf_message* new_msg = safe_malloc(sizeof(osrf_message));
206
207                         jsonObject* tmp = jsonObjectGetKey(message, "type");
208
209                         char* t;
210                         if( ( t = jsonObjectGetString(tmp)) ) {
211
212                                 if(!strcmp(t, "CONNECT"))               new_msg->m_type = CONNECT;
213                                 if(!strcmp(t, "DISCONNECT"))    new_msg->m_type = DISCONNECT;
214                                 if(!strcmp(t, "STATUS"))                new_msg->m_type = STATUS;
215                                 if(!strcmp(t, "REQUEST"))               new_msg->m_type = REQUEST;
216                                 if(!strcmp(t, "RESULT"))                new_msg->m_type = RESULT;
217                         }
218
219                         tmp = jsonObjectGetKey(message, "threadTrace");
220                         if(tmp) {
221                                 if(tmp->type == JSON_NUMBER)
222                                         new_msg->thread_trace = (int) jsonObjectGetNumber(tmp);
223                                 if(tmp->type == JSON_STRING)
224                                         new_msg->thread_trace = atoi(jsonObjectGetString(tmp));
225                         }
226
227
228                         tmp = jsonObjectGetKey(message, "protocol");
229
230                         if(tmp) {
231                                 if(tmp->type == JSON_NUMBER)
232                                         new_msg->protocol = (int) jsonObjectGetNumber(tmp);
233                                 if(tmp->type == JSON_STRING)
234                                         new_msg->protocol = atoi(jsonObjectGetString(tmp));
235                         }
236
237                         tmp = jsonObjectGetKey(message, "payload");
238                         if(tmp) {
239                                 if(tmp->classname)
240                                         new_msg->status_name = strdup(tmp->classname);
241
242                                 jsonObject* tmp0 = jsonObjectGetKey(tmp,"method");
243                                 if(jsonObjectGetString(tmp0))
244                                         new_msg->method_name = strdup(jsonObjectGetString(tmp0));
245
246                                 tmp0 = jsonObjectGetKey(tmp,"params");
247                                 if(tmp0) {
248                                         char* s = jsonObjectToJSON(tmp0);
249                                         new_msg->_params = jsonParseString(s);
250                                         free(s);
251                                 }
252
253                                 tmp0 = jsonObjectGetKey(tmp,"status");
254                                 if(jsonObjectGetString(tmp0))
255                                         new_msg->status_text = strdup(jsonObjectGetString(tmp0));
256
257                                 tmp0 = jsonObjectGetKey(tmp,"statusCode");
258                                 if(tmp0) {
259                                         if(jsonObjectGetString(tmp0))
260                                                 new_msg->status_code = atoi(jsonObjectGetString(tmp0));
261                                         if(tmp0->type == JSON_NUMBER)
262                                                 new_msg->status_code = (int) jsonObjectGetNumber(tmp0);
263                                 }
264
265                                 tmp0 = jsonObjectGetKey(tmp,"content");
266                                 if(tmp0) {
267                                         char* s = jsonObjectToJSON(tmp0);
268                                         new_msg->_result_content = jsonParseString(s);
269                                         free(s);
270                                 }
271
272                         }
273                         msgs[numparsed++] = new_msg;
274                         debug_handler("deserialize has parsed %d messages", numparsed);
275                 }
276         }
277
278         jsonObjectFree(json);
279         return numparsed;
280 }
281
282
283
284 jsonObject* osrfMessageGetResult( osrfMessage* msg ) {
285         if(msg) return msg->_result_content;
286         return NULL;
287 }
288