36477ac1cbf55ae968240e72173ca85a89c24dc8
[iot2.git] / iotjava / iotrmi / C++ / sample / CallBack_Skeleton.hpp
1 #include <iostream>
2 #include "../IoTRMIObject.hpp"
3 #include "CallBack.hpp"
4
5 using namespace std;
6
7 class CallBack_Skeleton : public CallBackInterface {
8         public:
9                 CallBack_Skeleton(CallBackInterface* _cb, int _port);
10                 ~CallBack_Skeleton();
11
12                 void                    waitRequestInvokeMethod();
13                 int                             printInt();
14                 void                    setInt(int _i);
15
16                 const static int size = 2;
17                 const static string methodSignatures[size];
18
19         private:                
20                 CallBackInterface       *cb;
21                 IoTRMIObject            *rmiObj;
22 };
23
24
25 const string CallBack_Skeleton::methodSignatures[CallBack_Skeleton::size] = {
26
27         "intprintInt()",
28         "voidsetInt(int)"
29 };
30
31
32 // Constructor
33 CallBack_Skeleton::CallBack_Skeleton(CallBackInterface* _cb, int _port) {
34
35         bool _bResult = false;
36         cb = _cb;
37         rmiObj = new IoTRMIObject(_port, &_bResult, methodSignatures, size);
38         waitRequestInvokeMethod();
39 }
40
41
42 CallBack_Skeleton::~CallBack_Skeleton() {
43
44         if (rmiObj != NULL) {
45                 delete rmiObj;
46                 rmiObj = NULL;
47         }
48 }
49
50
51 void CallBack_Skeleton::waitRequestInvokeMethod() {
52
53         // Loop continuously waiting for incoming bytes
54         while (true) {
55
56                 rmiObj->getMethodBytes();
57                 string methodSign = rmiObj->getSignature();
58                 cout << "Method sign: " << methodSign << endl;
59                 
60                 if (methodSign.compare("intprintInt()") == 0) {
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                 } else if (methodSign.compare("voidsetInt(int)") == 0) {
69                         string paramCls[] = { "int" };
70                         int numParam = 1;
71                         int param1 = 1;
72                         void* paramObj[] = { &param1 };
73                         rmiObj->getMethodParams(paramCls, numParam, paramObj);
74                         setInt(param1);
75                 } else {
76                         string error = "Signature not recognized: " + string(methodSign);
77                         throw error;
78                 }
79         }
80 }
81
82
83 int CallBack_Skeleton::printInt() {
84
85         return cb->printInt();
86 }
87
88
89 void CallBack_Skeleton::setInt(int _i) {
90
91         cb->setInt(_i);
92 }
93
94