bde7e42627f9a9d1862d701bc2c9eb455e2d9e9c
[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         private:                
23                 CallBackInterface       *cb;
24                 IoTRMIObject            *rmiObj;
25 };
26
27
28 // Constructor
29 CallBack_Skeleton::CallBack_Skeleton(CallBackInterface* _cb, int _port) {
30
31         bool _bResult = false;
32         cb = _cb;
33         rmiObj = new IoTRMIObject(_port, &_bResult);
34         ___waitRequestInvokeMethod();
35 }
36
37
38 CallBack_Skeleton::~CallBack_Skeleton() {
39
40         if (rmiObj != NULL) {
41                 delete rmiObj;
42                 rmiObj = NULL;
43         }
44 }
45
46
47 int CallBack_Skeleton::printInt() {
48
49         return cb->printInt();
50 }
51
52
53 void CallBack_Skeleton::___printInt() {
54
55         string paramCls[] = { };
56         int numParam = 0;
57         void* paramObj[] = { };
58         rmiObj->getMethodParams(paramCls, numParam, paramObj);
59         int retVal = printInt();
60         void* retObj = &retVal;
61         rmiObj->sendReturnObj(retObj, "int");
62 }
63
64
65 void CallBack_Skeleton::setInt(int _i) {
66
67         cb->setInt(_i);
68 }
69
70
71 void CallBack_Skeleton::___setInt() {
72
73         string paramCls[] = { "int" };
74         int numParam = 1;
75         int param1 = 1;
76         void* paramObj[] = { &param1 };
77         rmiObj->getMethodParams(paramCls, numParam, paramObj);
78         setInt(param1);
79 }
80
81
82 void CallBack_Skeleton::___waitRequestInvokeMethod() {
83
84         // Loop continuously waiting for incoming bytes
85         while (true) {
86
87                 rmiObj->getMethodBytes();
88                 int methodId = rmiObj->getMethodId();
89                 
90                 switch (methodId) {
91                         case 0 : ___printInt(); break;
92                         case 1 : ___setInt(); break;
93                         default:
94                                 string error = "Method Id not recognized!";
95                                 throw error;
96                 }
97         }
98 }
99
100
101 #endif
102