Preparing files, stubs, and skeletons for 2nd benchmark
[iot2.git] / benchmarks / drivers / SpruceSensor / MoistureSensor_Skeleton.java
1 package iotcode.SpruceSensor;
2
3 import java.io.IOException;
4 import java.util.List;
5 import java.util.ArrayList;
6 import java.util.Arrays;
7 import iotrmi.Java.IoTRMICall;
8 import iotrmi.Java.IoTRMIObject;
9
10 import iotcode.interfaces.*;
11
12 public class MoistureSensor_Skeleton implements MoistureSensor {
13
14         private MoistureSensor mainObj;
15         private IoTRMIObject rmiObj;
16
17         private String callbackAddress;
18         private int objIdCnt = 0;
19         private IoTRMICall rmiCall;
20         private int[] ports;
21
22         private final static int object0Id = 0; //MoistureSensorSmart
23         private static Integer[] object0Permission = { 2, 4, 5, 1, 3, 0 };
24         private static List<Integer> set0Allowed;
25         
26
27         public MoistureSensor_Skeleton(MoistureSensor _mainObj, String _callbackAddress, int _port) throws Exception {
28                 mainObj = _mainObj;
29                 callbackAddress = _callbackAddress;
30                 rmiObj = new IoTRMIObject(_port);
31                 set0Allowed = new ArrayList<Integer>(Arrays.asList(object0Permission));
32                 set0Allowed.add(-9998);
33                 ___waitRequestInvokeMethod();
34         }
35
36         public void init() {
37                 mainObj.init();
38         }
39
40         public float getMoisture() {
41                 return mainObj.getMoisture();
42         }
43
44         public long getTimestampOfLastReading() {
45                 return mainObj.getTimestampOfLastReading();
46         }
47
48         public void setId(int id) {
49                 mainObj.setId(id);
50         }
51
52         public int getId() {
53                 return mainObj.getId();
54         }
55
56         public void registerCallback(MoistureSensorSmartCallback _callbackTo) {
57                 mainObj.registerCallback(_callbackTo);
58         }
59
60         public void ___regCB() throws IOException {
61                 Object[] paramObj = rmiObj.getMethodParams(new Class<?>[] { int[].class, String.class, int.class },new Class<?>[] { null, null, null });
62                 ports = (int[]) paramObj[0];
63                 rmiCall = new IoTRMICall(ports[0], (String) paramObj[1], (int) paramObj[2]);
64         }
65
66         public void ___init() {
67                 Object[] paramObj = rmiObj.getMethodParams(new Class<?>[] {  }, 
68                 new Class<?>[] {  });
69                 init();
70         }
71
72         public void ___getMoisture() throws IOException {
73                 Object[] paramObj = rmiObj.getMethodParams(new Class<?>[] {  }, 
74                 new Class<?>[] {  });
75                 Object retObj = getMoisture();
76                 rmiObj.sendReturnObj(retObj);
77         }
78
79         public void ___getTimestampOfLastReading() throws IOException {
80                 Object[] paramObj = rmiObj.getMethodParams(new Class<?>[] {  }, 
81                 new Class<?>[] {  });
82                 Object retObj = getTimestampOfLastReading();
83                 rmiObj.sendReturnObj(retObj);
84         }
85
86         public void ___setId() {
87                 Object[] paramObj = rmiObj.getMethodParams(new Class<?>[] { int.class }, 
88                 new Class<?>[] { null });
89                 setId((int) paramObj[0]);
90         }
91
92         public void ___getId() throws IOException {
93                 Object[] paramObj = rmiObj.getMethodParams(new Class<?>[] {  }, 
94                 new Class<?>[] {  });
95                 Object retObj = getId();
96                 rmiObj.sendReturnObj(retObj);
97         }
98
99         public void ___registerCallback() {
100                 Object[] paramObj = rmiObj.getMethodParams(new Class<?>[] { int.class }, 
101                 new Class<?>[] { null });
102                 try {
103                         MoistureSensorSmartCallback stub0 = new MoistureSensorSmartCallback_CallbackStub(rmiCall, callbackAddress, objIdCnt, ports);
104                         objIdCnt++;
105                         registerCallback(stub0);
106                 } catch(Exception ex) {
107                         ex.printStackTrace();
108                         throw new Error("Exception from callback object instantiation!");
109                 }
110         }
111
112         private void ___waitRequestInvokeMethod() throws IOException {
113                 while (true) {
114                         rmiObj.getMethodBytes();
115                         int _objectId = rmiObj.getObjectId();
116                         int methodId = rmiObj.getMethodId();
117                         if (_objectId == object0Id) {
118                                 if (!set0Allowed.contains(methodId)) {
119                                         throw new Error("Object with object Id: " + _objectId + "  is not allowed to access method: " + methodId);
120                                 }
121                         }
122                         else {
123                                 throw new Error("Object Id: " + _objectId + " not recognized!");
124                         }
125                         switch (methodId) {
126                                 case 0: ___init(); break;
127                                 case 1: ___getMoisture(); break;
128                                 case 2: ___getTimestampOfLastReading(); break;
129                                 case 3: ___setId(); break;
130                                 case 4: ___getId(); break;
131                                 case 5: ___registerCallback(); break;
132                                 case -9998: ___regCB(); break;
133                                 default: 
134                                 throw new Error("Method Id " + methodId + " not recognized!");
135                         }
136                 }
137         }
138
139 }