]> git.evergreen-ils.org Git - Evergreen.git/blob - OpenSRF/src/libstack/osrf_message.c
49b6ea11cd6605f29cbb01260e42b4e3eead1300
[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 osrf_message_free( osrf_message* msg ) {
84         if( msg == NULL )
85                 return;
86
87         if( msg->status_name != NULL )
88                 free(msg->status_name);
89
90         if( msg->status_text != NULL )
91                 free(msg->status_text);
92
93         if( msg->_result_content != NULL )
94                 jsonObjectFree( msg->_result_content );
95
96         if( msg->result_string != NULL )
97                 free( msg->result_string);
98
99         if( msg->method_name != NULL )
100                 free(msg->method_name);
101
102         if( msg->_params != NULL )
103                 jsonObjectFree(msg->_params);
104
105         free(msg);
106 }
107
108 char* osrf_message_serialize(osrf_message* msg) {
109         if( msg == NULL ) return NULL;
110         jsonObject* json = jsonNewObject(NULL);
111         jsonObjectSetClass(json, "osrfMessage");
112         jsonObject* payload;
113         char sc[64]; memset(sc,0,64);
114
115         char* str;
116
117         INT_TO_STRING(msg->thread_trace);
118         jsonObjectSetKey(json, "threadTrace", jsonNewObject(INTSTR));
119
120         switch(msg->m_type) {
121                 
122                 case CONNECT: 
123                         jsonObjectSetKey(json, "type", jsonNewObject("CONNECT"));
124                         break;
125
126                 case DISCONNECT: 
127                         jsonObjectSetKey(json, "type", jsonNewObject("DISCONNECT"));
128                         break;
129
130                 case STATUS:
131                         jsonObjectSetKey(json, "type", jsonNewObject("STATUS"));
132                         payload = jsonNewObject(NULL);
133                         jsonObjectSetClass(payload, msg->status_name);
134                         jsonObjectSetKey(payload, "status", jsonNewObject(msg->status_text));
135          sprintf(sc,"%d",msg->status_code);
136                         jsonObjectSetKey(payload, "statusCode", jsonNewObject(sc));
137                         jsonObjectSetKey(json, "payload", payload);
138                         break;
139
140                 case REQUEST:
141                         jsonObjectSetKey(json, "type", jsonNewObject("REQUEST"));
142                         payload = jsonNewObject(NULL);
143                         jsonObjectSetClass(payload, "osrfMethod");
144                         jsonObjectSetKey(payload, "method", jsonNewObject(msg->method_name));
145                         str = jsonObjectToJSON(msg->_params);
146                         jsonObjectSetKey(payload, "params", jsonParseString(str));
147                         free(str);
148                         jsonObjectSetKey(json, "payload", payload);
149
150                         break;
151
152                 case RESULT:
153                         jsonObjectSetKey(json, "type", jsonNewObject("RESULT"));
154                         payload = jsonNewObject(NULL);
155                         jsonObjectSetClass(payload,"osrfResult");
156                         jsonObjectSetKey(payload, "status", jsonNewObject(msg->status_text));
157          sprintf(sc,"%d",msg->status_code);
158                         jsonObjectSetKey(payload, "statusCode", jsonNewObject(sc));
159                         str = jsonObjectToJSON(msg->_result_content);
160                         jsonObjectSetKey(payload, "content", jsonParseString(str));
161                         free(str);
162                         jsonObjectSetKey(json, "payload", payload);
163                         break;
164         }
165         
166         jsonObject* wrapper = jsonNewObject(NULL);
167         jsonObjectPush(wrapper, json);
168         char* j = jsonObjectToJSON(wrapper);
169         jsonObjectFree(wrapper);
170         return j;
171 }
172
173
174 int osrf_message_deserialize(char* string, osrf_message* msgs[], int count) {
175
176         if(!string || !msgs || count <= 0) return 0;
177         int numparsed = 0;
178
179         jsonObject* json = jsonParseString(string);
180
181         if(!json) {
182                 warning_handler(
183                         "osrf_message_deserialize() unable to parse data: \n%s\n", string);
184                 return 0;
185         }
186
187         int x;
188
189         for( x = 0; x < json->size && x < count; x++ ) {
190
191                 jsonObject* message = jsonObjectGetIndex(json, x);
192
193                 char* j =  jsonObjectToJSON(message);
194                 debug_handler("deserialize parsed message \n%s\n", j );
195                 free(j);
196         
197
198                 if(message && message->type != JSON_NULL && 
199                         message->classname && !strcmp(message->classname, "osrfMessage")) {
200
201                         osrf_message* new_msg = safe_malloc(sizeof(osrf_message));
202
203                         jsonObject* tmp = jsonObjectGetKey(message, "type");
204
205                         char* t;
206                         if( ( t = jsonObjectGetString(tmp)) ) {
207
208                                 if(!strcmp(t, "CONNECT"))               new_msg->m_type = CONNECT;
209                                 if(!strcmp(t, "DISCONNECT"))    new_msg->m_type = DISCONNECT;
210                                 if(!strcmp(t, "STATUS"))                new_msg->m_type = STATUS;
211                                 if(!strcmp(t, "REQUEST"))               new_msg->m_type = REQUEST;
212                                 if(!strcmp(t, "RESULT"))                new_msg->m_type = RESULT;
213                         }
214
215                         tmp = jsonObjectGetKey(message, "threadTrace");
216                         if(tmp) {
217                                 if(tmp->type == JSON_NUMBER)
218                                         new_msg->thread_trace = (int) jsonObjectGetNumber(tmp);
219                                 if(tmp->type == JSON_STRING)
220                                         new_msg->thread_trace = atoi(jsonObjectGetString(tmp));
221                         }
222
223
224                         tmp = jsonObjectGetKey(message, "protocol");
225
226                         if(tmp) {
227                                 if(tmp->type == JSON_NUMBER)
228                                         new_msg->protocol = (int) jsonObjectGetNumber(tmp);
229                                 if(tmp->type == JSON_STRING)
230                                         new_msg->protocol = atoi(jsonObjectGetString(tmp));
231                         }
232
233                         tmp = jsonObjectGetKey(message, "payload");
234                         if(tmp) {
235                                 if(tmp->classname)
236                                         new_msg->status_name = strdup(tmp->classname);
237
238                                 jsonObject* tmp0 = jsonObjectGetKey(tmp,"method");
239                                 if(jsonObjectGetString(tmp0))
240                                         new_msg->method_name = strdup(jsonObjectGetString(tmp0));
241
242                                 tmp0 = jsonObjectGetKey(tmp,"params");
243                                 if(tmp0) {
244                                         char* s = jsonObjectToJSON(tmp0);
245                                         new_msg->_params = jsonParseString(s);
246                                         free(s);
247                                 }
248
249                                 tmp0 = jsonObjectGetKey(tmp,"status");
250                                 if(jsonObjectGetString(tmp0))
251                                         new_msg->status_text = strdup(jsonObjectGetString(tmp0));
252
253                                 tmp0 = jsonObjectGetKey(tmp,"statusCode");
254                                 if(tmp0) {
255                                         if(jsonObjectGetString(tmp0))
256                                                 new_msg->status_code = atoi(jsonObjectGetString(tmp0));
257                                         if(tmp0->type == JSON_NUMBER)
258                                                 new_msg->status_code = (int) jsonObjectGetNumber(tmp0);
259                                 }
260
261                                 tmp0 = jsonObjectGetKey(tmp,"content");
262                                 if(tmp0) {
263                                         char* s = jsonObjectToJSON(tmp0);
264                                         new_msg->_result_content = jsonParseString(s);
265                                         free(s);
266                                 }
267
268                         }
269                         msgs[numparsed++] = new_msg;
270                         debug_handler("deserialize has parsed %d messages", numparsed);
271                 }
272         }
273
274         jsonObjectFree(json);
275         return numparsed;
276 }
277
278