]> pilppa.org Git - libplpbus.git/blob - src/plpbus/BusMessage.cc
1fbc724064dbf6fcb33070c6ce87319788d1503c
[libplpbus.git] / src / plpbus / BusMessage.cc
1 /*
2  * BusMessage.cpp
3  *
4  *  Created on: Aug 17, 2010
5  *      Author: lamikr
6  */
7
8 #include <malloc.h>
9 #include <omniORB4/CORBA.h>
10
11 #include <plp/log.h>
12 #include <plp/retval.h>
13
14 #include "BusMessage.hh"
15 #include "RequestResponseBus.hh"
16
17 using namespace std;
18 using namespace plpbus_orb;
19 using namespace plpbus;
20 using namespace CORBA;
21
22 #define CONST_MSG_ID_KEY_NAME   "_msgtype"
23
24 BusMessage::BusMessage() {
25         _dataItemSeq    = new DataItemSequence(0);
26 }
27
28 BusMessage::BusMessage(long type_param) {
29         _dataItemSeq    = new DataItemSequence(0);
30
31         add_long_parameter(CONST_MSG_ID_KEY_NAME, type_param);
32 }
33
34 BusMessage::BusMessage(long type_param,
35                         const char *arg_name_param,
36                         const char *arg_string_value_param) {
37         _dataItemSeq    = new DataItemSequence(0);
38         add_long_parameter(CONST_MSG_ID_KEY_NAME, type_param);
39         add_string_parameter(arg_name_param, arg_string_value_param);
40 }
41
42 BusMessage::~BusMessage() {
43         DataItemSequence        *seq;
44
45         log_debug("\n");
46         seq     = (DataItemSequence *)_dataItemSeq;
47         if (seq != NULL) {
48                 delete(seq);
49         }
50 }
51
52 int BusMessage::add_cstring_parameter(const char *arg_name_param, const char *value_param) {
53         long                    ln;
54         int                     ret_val;
55         DataItemSequence        *seq;
56         DataItem                *item;
57
58         ret_val = 0;
59         seq     = (DataItemSequence *)_dataItemSeq;
60         if (arg_name_param != NULL) {
61                 ln                      = seq->length();
62                 seq->length(ln + 1);
63                 item                    = &((*seq)[ln]);
64                 item->name              = arg_name_param;
65                 item->str_value         = value_param;
66         }
67         else {
68                 ret_val = -1;
69         }
70         return ret_val;
71 }
72
73 int BusMessage::add_string_parameter(string arg_name_param, string value_param)
74 {
75         long                    ln;
76         int                     ret_val;
77         DataItemSequence        *seq;
78         DataItem                *item;
79
80         //str   = (const char)"hooo";
81         ret_val = 0;
82         seq     = (DataItemSequence *)_dataItemSeq;
83         if (arg_name_param.empty() == false) {
84                 ln              = seq->length();
85                 seq->length(ln + 1);
86                 item            = &((*seq)[ln]);
87                 item->name      = arg_name_param.c_str();
88                 item->str_value = value_param.c_str();
89         }
90         else {
91                 ret_val = -1;
92         }
93         return ret_val;
94 }
95
96 int BusMessage::add_long_parameter(string arg_name_param, long value_param)
97 {
98         long                    length;
99         int                     ret_val;
100         DataItemSequence        *seq;
101
102         ret_val = 0;
103         seq     = (DataItemSequence *)_dataItemSeq;
104         if (arg_name_param.empty() == false) {
105                 length          = seq->length();
106                 seq->length(length + 1);
107                 (*seq)[length].name     = arg_name_param.c_str();
108                 (*seq)[length].value    <<= value_param;
109                 (*seq)[length].str_value = "";
110         }
111         else {
112                 ret_val = -1;
113         }
114         return ret_val;
115 }
116
117 int BusMessage::add_int_parameter(string arg_name_param, int value_param)
118 {
119         long                    length;
120         int                     ret_val;
121         DataItemSequence        *seq;
122
123         ret_val = 0;
124         seq     = (DataItemSequence *)_dataItemSeq;
125         if (arg_name_param.empty() == false) {
126                 length          = seq->length();
127                 seq->length(length + 1);
128                 (*seq)[length].name     = arg_name_param.c_str();
129                 (*seq)[length].value    <<= (long)value_param;
130                 (*seq)[length].str_value = "";
131         }
132         else {
133                 ret_val = -1;
134         }
135         return ret_val;
136 }
137
138 int BusMessage::add_double_parameter(string arg_name_param, double value_param)
139 {
140         long                    length;
141         int                     ret_val;
142         DataItemSequence        *seq;
143
144         ret_val = 0;
145         log_debug("add_double_parameter() started\n");
146         seq     = (DataItemSequence *)_dataItemSeq;
147         if (arg_name_param.empty() == false) {
148                 length          = seq->length();
149                 seq->length(length + 1);
150                 (*seq)[length].name     = arg_name_param.c_str();
151                 (*seq)[length].value    <<= (double)value_param;
152                 (*seq)[length].str_value = "";
153         }
154         else {
155                 ret_val = -1;
156         }
157         log_debug("add_double_parameter() done\n");
158         return ret_val;
159 }
160
161 int BusMessage::add_float_parameter(string arg_name_param, float value_param)
162 {
163         long                    length;
164         int                     ret_val;
165         DataItemSequence        *seq;
166
167         ret_val = 0;
168         seq     = (DataItemSequence *)_dataItemSeq;
169         if (arg_name_param.empty() == false) {
170                 length          = seq->length();
171                 seq->length(length + 1);
172                 (*seq)[length].name     = arg_name_param.c_str();
173                 (*seq)[length].value    <<= (float)value_param;
174                 (*seq)[length].str_value = "";
175         }
176         else {
177                 ret_val = -1;
178         }
179         return ret_val;
180 }
181
182 static DataItem *get_dataitem_by_param_name(DataItemSequence *seq, string arg_name_param) {
183         DataItem        *ret_val;
184         long int        count;
185         DataItem        *item;
186         const char      *item_name;
187
188         ret_val = NULL;
189         count   = seq->length();
190         for (long ii = 0; ii < count; ii++) {
191                 item            = &(*seq)[ii];
192                 item_name       = item->name;
193                 if (strcmp(arg_name_param.c_str(), item_name) == 0) {
194                         ret_val = item;
195                         break;
196                 }
197         }
198         return ret_val;
199 }
200
201 long BusMessage::get_type(int *err_flg) {
202         long    ret_val;
203         long    res;
204
205         ret_val = -1;
206         res     = get_long_parameter(CONST_MSG_ID_KEY_NAME, err_flg);
207         if (*err_flg == PLP_OK) {
208                 ret_val = res;
209         }
210         return ret_val;
211 }
212
213 string BusMessage::get_string_parameter(string arg_name_param, int *err_flg) {
214         string                  ret_val;
215         DataItem                *item;
216         DataItemSequence        *seq;
217         const char              *ch;
218
219         *err_flg        = PLP_ERR_DATA_NOT_FOUND;
220         seq             = (DataItemSequence *)_dataItemSeq;
221         item            = get_dataitem_by_param_name(seq, arg_name_param);
222         if (item != NULL) {
223                 //item->value >>= CORBA::Any::to_string(ch, 0);
224                 ch              = item->str_value;
225                 ret_val         = ch;
226                 *err_flg        = PLP_OK;
227         }
228         return ret_val;
229 }
230
231 long BusMessage::get_long_parameter(string arg_name_param, int *err_flg) {
232         long                    ret_val;
233         DataItem                *item;
234         DataItemSequence        *seq;
235
236         ret_val         = 0;
237         *err_flg        = PLP_ERR_DATA_NOT_FOUND;
238         seq             = (DataItemSequence *)_dataItemSeq;
239         item            = get_dataitem_by_param_name(seq, arg_name_param);
240         if (item != NULL) {
241                 item->value >>= ret_val;
242                 *err_flg        = PLP_OK;
243         }
244         return ret_val;
245 }
246
247 int BusMessage::get_int_parameter(string arg_name_param, int *err_flg) {
248         int                     ret_val;
249         DataItem                *item;
250         DataItemSequence        *seq;
251         long                    tmp;
252
253         ret_val         = 0;
254         *err_flg        = PLP_ERR_DATA_NOT_FOUND;
255         seq             = (DataItemSequence *)_dataItemSeq;
256         item            = get_dataitem_by_param_name(seq, arg_name_param);
257         if (item != NULL) {
258                 item->value >>= tmp;
259                 ret_val =        (int)tmp;
260                 *err_flg        = PLP_OK;
261         }
262         return ret_val;
263 }
264
265 double BusMessage::get_double_parameter(string arg_name_param, int *err_flg) {
266         double          ret_val;
267         DataItem        *item;
268         DataItemSequence *seq;
269
270         ret_val         = 0;
271         *err_flg        = PLP_ERR_DATA_NOT_FOUND;
272         seq             = (DataItemSequence *)_dataItemSeq;
273         item            = get_dataitem_by_param_name(seq, arg_name_param);
274         if (item != NULL) {
275                 item->value >>= ret_val;
276                 *err_flg        = PLP_OK;
277         }
278         return ret_val;
279 }
280
281 float BusMessage::get_float_parameter(string arg_name_param, int *err_flg) {
282         float           ret_val;
283         DataItem        *item;
284         DataItemSequence *seq;
285
286         ret_val         = 0;
287         *err_flg        = PLP_ERR_DATA_NOT_FOUND;
288         seq             = (DataItemSequence *)_dataItemSeq;
289         item            = get_dataitem_by_param_name(seq, arg_name_param);
290         if (item != NULL) {
291                 item->value >>= ret_val;
292                 *err_flg        = PLP_OK;
293         }
294         return ret_val;
295 }
296
297 unsigned long BusMessage::get_parameter_count() {
298         int                     ret_val;
299         DataItemSequence        *seq;
300
301         seq     = (DataItemSequence *)_dataItemSeq;
302         ret_val = seq->length();
303         return ret_val;
304 }
305
306 void BusMessage::printout_dataitem(long index, void *item) {
307         CORBA::TCKind   kind;
308         const char      *name;
309         DataItem        *dataitem;
310
311         dataitem        = (DataItem *)item;
312         if (item != NULL) {
313                 if (dataitem->name != NULL) {
314                         name    = dataitem->name;
315                         kind    = dataitem->value.type()->kind();
316                         switch(kind) {
317                                 case tk_long:
318                                         {
319                                                 long    val;
320                                                 dataitem->value >>= val;
321                                                 log_debug("\t[%ld] %s: %ld\n", index, name, val);
322                                                 //val_str       = strdup("long");
323                                         }
324                                         break;
325                                 case tk_double:
326                                         {
327                                                 double val;
328                                                 dataitem->value >>= val;
329                                                 log_debug("\t[%ld] %s: %f\n", index, name, val);
330                                                 //val_str       = strdup("double");
331                                         }
332                                         break;
333                                 case tk_float:
334                                         {
335                                                 float val;
336                                                 dataitem->value >>= val;
337                                                 log_debug("\t[l%ld] %s: %f\n", index, name, val);
338                                                 //val_str       = strdup("float");
339                                         }
340                                         break;
341                                 default:
342                                         {
343                                                 const char *val;
344                                                 val     = dataitem->str_value;
345                                                 log_debug("\t[%ld] %s: %s\n", index, name, val);
346                                         }
347                                         break;
348                                         log_error("\t[%ld], name: %s, value unknown\n", index, name);
349                         }
350                 }
351                 else {
352                         log_error("\t[%ld] Invalid data item name name or value\n", index);
353                 }
354         }
355         else {
356                 log_error("\t[%ld] dataItem NULL.", index);
357         }
358 }
359
360 void BusMessage::printout() {
361         long int                count;
362         DataItem                item;
363         DataItemSequence        *seq;
364
365         seq     = (DataItemSequence *)_dataItemSeq;
366         count   = seq->length();
367         log_debug("BusMessage.printout(): parameter count: %ld\n", count);
368         for (long ii = 0; ii < count; ii++) {
369                 item    = (*seq)[ii];
370                 printout_dataitem(ii, &item);
371         }
372         log_debug("BusMessage.printout() done\n");;
373 }