438563b35b9d39c9aa58fa91409efef8e76c8068
[iot2.git] / benchmarks / IrrigationController / MoistureSensorSmart_Stub.java
1 package IrrigationController;
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 MoistureSensorSmart_Stub implements MoistureSensorSmart {
13
14         private IoTRMICall rmiCall;
15         private String callbackAddress;
16         private int[] ports;
17
18         private final static int objectId = 0;
19         // Callback properties
20         private IoTRMIObject rmiObj;
21         List<MoistureSensorCallback> listCallbackObj;
22         private int objIdCnt = 0;
23         private final static int object0Id = 0; //MoistureSensorSmartCallback
24         private static Integer[] object0Permission = { 0 };
25         private static List<Integer> set0Allowed;
26         
27
28         public MoistureSensorSmart_Stub(int _port, String _skeletonAddress, String _callbackAddress, int _rev, int[] _ports) throws Exception {
29                 callbackAddress = _callbackAddress;
30                 ports = _ports;
31                 rmiCall = new IoTRMICall(_port, _skeletonAddress, _rev);
32                 set0Allowed = new ArrayList<Integer>(Arrays.asList(object0Permission));
33                 listCallbackObj = new ArrayList<MoistureSensorCallback>();
34                 set0Allowed.add(-9999);
35                 ___initCallBack();
36         }
37
38         public long getTimestampOfLastReading() {
39                 int methodId = 2;
40                 Class<?> retType = long.class;
41                 Class<?>[] paramCls = new Class<?>[] {  };
42                 Object[] paramObj = new Object[] {  };
43                 Object retObj = rmiCall.remoteCall(objectId, methodId, retType, null, paramCls, paramObj);
44                 return (long)retObj;
45         }
46
47         public int getId() {
48                 int methodId = 4;
49                 Class<?> retType = int.class;
50                 Class<?>[] paramCls = new Class<?>[] {  };
51                 Object[] paramObj = new Object[] {  };
52                 Object retObj = rmiCall.remoteCall(objectId, methodId, retType, null, paramCls, paramObj);
53                 return (int)retObj;
54         }
55
56         public void registerCallback(MoistureSensorCallback _callbackTo) {
57                 try {
58                         MoistureSensorCallback_CallbackSkeleton skel0 = new MoistureSensorCallback_CallbackSkeleton(_callbackTo, callbackAddress, objIdCnt++);
59                         listCallbackObj.add(skel0);
60                 } catch (Exception ex) {
61                         ex.printStackTrace();
62                         throw new Error("Exception when generating skeleton objects!");
63                 }
64
65                 int methodId = 5;
66                 Class<?> retType = void.class;
67                 Class<?>[] paramCls = new Class<?>[] { int.class };
68                 Object[] paramObj = new Object[] { new Integer(1) };
69                 rmiCall.remoteCall(objectId, methodId, retType, null, paramCls, paramObj);
70         }
71
72         public void ___initCallBack() {
73                 Thread thread = new Thread() {
74                         public void run() {
75                                 try {
76                                         rmiObj = new IoTRMIObject(ports[0]);
77                                         while (true) {
78                                                 byte[] method = rmiObj.getMethodBytes();
79                                                 int objId = IoTRMIObject.getObjectId(method);
80                                                 MoistureSensorCallback_CallbackSkeleton skel = (MoistureSensorCallback_CallbackSkeleton) listCallbackObj.get(objId);
81                                                 if (skel != null) {
82                                                         int methodId = IoTRMIObject.getMethodId(method);
83                                                         if (!set0Allowed.contains(methodId)) {
84                                                                 throw new Error("Callback object for MoistureSensorCallback is not allowed to access method: " + methodId);
85                                                         }
86                                                         skel.invokeMethod(rmiObj);
87                                                 } else {
88                                                         throw new Error("MoistureSensorCallback: Object with Id " + objId + " not found!");
89                                                 }
90                                         }
91                                 } catch (Exception ex) {
92                                         ex.printStackTrace();
93                                         throw new Error("Error instantiating class MoistureSensorCallback_CallbackSkeleton!");
94                                 }
95                         }
96                 };
97                 thread.start();
98
99                 int methodId = -9998;
100                 Class<?> retType = void.class;
101                 Class<?>[] paramCls = new Class<?>[] { int[].class, String.class, int.class };
102                 Object[] paramObj = new Object[] { ports, callbackAddress, 0 };
103                 rmiCall.remoteCall(objectId, methodId, retType, null, paramCls, paramObj);
104         }
105
106         public float getMoisture() {
107                 int methodId = 1;
108                 Class<?> retType = float.class;
109                 Class<?>[] paramCls = new Class<?>[] {  };
110                 Object[] paramObj = new Object[] {  };
111                 Object retObj = rmiCall.remoteCall(objectId, methodId, retType, null, paramCls, paramObj);
112                 return (float)retObj;
113         }
114
115         public void setId(int id) {
116                 int methodId = 3;
117                 Class<?> retType = void.class;
118                 Class<?>[] paramCls = new Class<?>[] { int.class };
119                 Object[] paramObj = new Object[] { id };
120                 rmiCall.remoteCall(objectId, methodId, retType, null, paramCls, paramObj);
121         }
122
123         public void init() {
124                 int methodId = 0;
125                 Class<?> retType = void.class;
126                 Class<?>[] paramCls = new Class<?>[] {  };
127                 Object[] paramObj = new Object[] {  };
128                 rmiCall.remoteCall(objectId, methodId, retType, null, paramCls, paramObj);
129         }
130
131 }