b087046db8291101847f62bb1ca63f991fc21824
[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                 const static int size = 2;
20                 const static string methodSignatures[size];
21
22         private:                
23                 CallBackInterface       *cb;
24                 IoTRMIObject            *rmiObj;
25 };
26
27
28 const string CallBack_Skeleton::methodSignatures[CallBack_Skeleton::size] = {
29
30         "intprintInt()",
31         "voidsetInt(int)"
32 };
33
34
35 // Constructor
36 CallBack_Skeleton::CallBack_Skeleton(CallBackInterface* _cb, int _port) {
37
38         bool _bResult = false;
39         cb = _cb;
40         rmiObj = new IoTRMIObject(_port, &_bResult, methodSignatures, size);
41         waitRequestInvokeMethod();
42 }
43
44
45 CallBack_Skeleton::~CallBack_Skeleton() {
46
47         if (rmiObj != NULL) {
48                 delete rmiObj;
49                 rmiObj = NULL;
50         }
51 }
52
53
54 void CallBack_Skeleton::waitRequestInvokeMethod() {
55
56         // Loop continuously waiting for incoming bytes
57         while (true) {
58
59                 rmiObj->getMethodBytes();
60                 string methodSign = rmiObj->getSignature();
61                 cout << "Method sign: " << methodSign << endl;
62                 
63                 if (methodSign.compare("intprintInt()") == 0) {
64                         string paramCls[] = { };
65                         int numParam = 0;
66                         void* paramObj[] = { };
67                         rmiObj->getMethodParams(paramCls, numParam, paramObj);
68                         int retVal = printInt();
69                         void* retObj = &retVal;
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                 } else {
79                         string error = "Signature not recognized: " + string(methodSign);
80                         throw error;
81                 }
82         }
83 }
84
85
86 int CallBack_Skeleton::printInt() {
87
88         return cb->printInt();
89 }
90
91
92 void CallBack_Skeleton::setInt(int _i) {
93
94         cb->setInt(_i);
95 }
96
97 #endif
98