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