Fixing bug for return value from callback in C++ (sendReturnObj is called twice)...
[iot2.git] / iotjava / iotrmi / C++ / sample / CallBack_CBSkeleton.hpp
1 #ifndef _CALLBACK_CBSKELETON_HPP__
2 #define _CALLBACK_CBSKELETON_HPP__
3
4 #include <iostream>
5 #include "CallBackInterface.hpp"
6 #include "../IoTRMIObject.hpp"
7
8
9 using namespace std;
10
11 class CallBack_CBSkeleton : public CallBackInterface {
12         public:
13                 CallBack_CBSkeleton(CallBackInterface* _cb, int _objectId);
14                 ~CallBack_CBSkeleton();
15
16                 void                    invokeMethod(IoTRMIObject* rmiObj);
17                 int                             printInt();
18                 void                    setInt(int _i);
19
20                 void                    ___printInt(IoTRMIObject* rmiObj);
21                 void                    ___setInt(IoTRMIObject* rmiObj);
22
23                 const static int                size = 2;
24                 const static string     methodSignatures[size];
25
26         private:
27                 CallBackInterface       *cb;
28                 int                                     objectId = 0;
29 };
30
31
32 const string CallBack_CBSkeleton::methodSignatures[CallBack_CBSkeleton::size] = {
33
34         "intprintInt()",
35         "voidsetInt(int)"
36 };
37
38
39 // Constructor
40 CallBack_CBSkeleton::CallBack_CBSkeleton(CallBackInterface* _cb, int _objectId) {
41
42         cb = _cb;
43         objectId = _objectId;
44         cout << "Creating CallBack_Skeleton and waiting!" << endl;
45 }
46
47
48 CallBack_CBSkeleton::~CallBack_CBSkeleton() {
49
50 }
51
52
53 int CallBack_CBSkeleton::printInt() {
54
55         return cb->printInt();
56 }
57
58
59 void CallBack_CBSkeleton::___printInt(IoTRMIObject* rmiObj) {
60
61         string paramCls[] = { };
62         int numParam = 0;
63         void* paramObj[] = { };
64         rmiObj->getMethodParams(paramCls, numParam, paramObj);
65         int retVal = printInt();
66         void* retObj = &retVal;
67         rmiObj->sendReturnObj(retObj, "int");
68 }
69
70
71 void CallBack_CBSkeleton::setInt(int _i) {
72
73         cb->setInt(_i);
74 }
75
76
77 void CallBack_CBSkeleton::___setInt(IoTRMIObject* rmiObj) {
78
79         string paramCls[] = { "int" };
80         int numParam = 1;
81         int param1 = 1;
82         void* paramObj[] = { &param1 };
83         rmiObj->getMethodParams(paramCls, numParam, paramObj);
84         setInt(param1);
85 }
86
87
88 void CallBack_CBSkeleton::invokeMethod(IoTRMIObject* rmiObj) {
89
90         string methodSign = rmiObj->getSignature();
91         
92         if (methodSign.compare("intprintInt()") == 0) {
93                 ___printInt(rmiObj);
94         } else if (methodSign.compare("voidsetInt(int)") == 0) {
95                 ___setInt(rmiObj);
96         } else {
97                 string error = "Signature not recognized: " + string(methodSign);
98                 throw error;
99         }
100 }
101
102
103 #endif
104