Adding callback for C++ (still has bug for return values); adding struct as method...
[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, string *type);
17                 int                                             printInt();
18                 void                                    setInt(int _i);
19
20                 const static int                size = 2;
21                 const static string     methodSignatures[size];
22
23         private:
24                 CallBackInterface       *cb;
25                 int                                     objectId = 0;
26 };
27
28
29 const string CallBack_CBSkeleton::methodSignatures[CallBack_CBSkeleton::size] = {
30
31         "intprintInt()",
32         "voidsetInt(int)"
33 };
34
35
36 // Constructor
37 CallBack_CBSkeleton::CallBack_CBSkeleton(CallBackInterface* _cb, int _objectId) {
38
39         cb = _cb;
40         objectId = _objectId;
41         cout << "Creating CallBack_Skeleton and waiting!" << endl;
42 }
43
44
45 CallBack_CBSkeleton::~CallBack_CBSkeleton() {
46
47 }
48
49
50 void* CallBack_CBSkeleton::invokeMethod(IoTRMIObject* rmiObj, string *type) {
51
52         // Loop continuously waiting for incoming bytes
53         void* retObj = NULL;
54         cout << "Get inside invoke method!" << endl;
55
56         //rmiObj->getMethodBytes();
57         cout << "Get inside invoke 2!" << endl;
58         string methodSign = rmiObj->getSignature();
59         cout << "Get inside invoke 3!" << endl;
60         cout << "Method sign: " << methodSign << endl;
61         
62         if (methodSign.compare("intprintInt()") == 0) {
63                 string paramCls[] = { };
64                 int numParam = 0;
65                 void* paramObj[] = { };
66                 rmiObj->getMethodParams(paramCls, numParam, paramObj);
67                 int retVal = printInt();
68                 retObj = &retVal;
69                 *type = "int";
70                 rmiObj->sendReturnObj(retObj, "int");
71         } else if (methodSign.compare("voidsetInt(int)") == 0) {
72                 string paramCls[] = { "int" };
73                 int numParam = 1;
74                 int param1 = 1;
75                 void* paramObj[] = { &param1 };
76                 rmiObj->getMethodParams(paramCls, numParam, paramObj);
77                 setInt(param1);
78                 *type = "void";
79         } else {
80                 string error = "Signature not recognized: " + string(methodSign);
81                 throw error;
82         }
83
84         return retObj;
85 }
86
87
88 int CallBack_CBSkeleton::printInt() {
89
90         return cb->printInt();
91 }
92
93
94 void CallBack_CBSkeleton::setInt(int _i) {
95
96         cb->setInt(_i);
97 }
98
99 #endif
100