Preparing 3rd benchmark for porting with capability-based RMI
[iot2.git] / benchmarks / SpeakerController / SpeakerSmart_Stub.java
1 package SpeakerController;
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 SpeakerSmart_Stub implements SpeakerSmart {
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<SpeakerCallback> listCallbackObj;
22         private int objIdCnt = 0;
23         private final static int object0Id = 0; //SpeakerSmartCallback
24         private static Integer[] object0Permission = { 0 };
25         private static List<Integer> set0Allowed;
26         
27
28         public SpeakerSmart_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<SpeakerCallback>();
34                 set0Allowed.add(-9999);
35                 ___initCallBack();
36         }
37
38         public int getPosition() {
39                 int methodId = 6;
40                 Class<?> retType = int.class;
41                 Class<?>[] paramCls = new Class<?>[] {  };
42                 Object[] paramObj = new Object[] {  };
43                 Object retObj = rmiCall.remoteCall(objectId, methodId, retType, null, paramCls, paramObj);
44                 return (int)retObj;
45         }
46
47         public boolean stopPlayback() {
48                 int methodId = 2;
49                 Class<?> retType = boolean.class;
50                 Class<?>[] paramCls = new Class<?>[] {  };
51                 Object[] paramObj = new Object[] {  };
52                 Object retObj = rmiCall.remoteCall(objectId, methodId, retType, null, paramCls, paramObj);
53                 return (boolean)retObj;
54         }
55
56         public void clearData() {
57                 int methodId = 9;
58                 Class<?> retType = void.class;
59                 Class<?>[] paramCls = new Class<?>[] {  };
60                 Object[] paramObj = new Object[] {  };
61                 rmiCall.remoteCall(objectId, methodId, retType, null, paramCls, paramObj);
62         }
63
64         public boolean startPlayback() {
65                 int methodId = 1;
66                 Class<?> retType = boolean.class;
67                 Class<?>[] paramCls = new Class<?>[] {  };
68                 Object[] paramObj = new Object[] {  };
69                 Object retObj = rmiCall.remoteCall(objectId, methodId, retType, null, paramCls, paramObj);
70                 return (boolean)retObj;
71         }
72
73         public boolean getPlaybackState() {
74                 int methodId = 3;
75                 Class<?> retType = boolean.class;
76                 Class<?>[] paramCls = new Class<?>[] {  };
77                 Object[] paramObj = new Object[] {  };
78                 Object retObj = rmiCall.remoteCall(objectId, methodId, retType, null, paramCls, paramObj);
79                 return (boolean)retObj;
80         }
81
82         public boolean setVolume(float _percent) {
83                 int methodId = 4;
84                 Class<?> retType = boolean.class;
85                 Class<?>[] paramCls = new Class<?>[] { float.class };
86                 Object[] paramObj = new Object[] { _percent };
87                 Object retObj = rmiCall.remoteCall(objectId, methodId, retType, null, paramCls, paramObj);
88                 return (boolean)retObj;
89         }
90
91         public float getVolume() {
92                 int methodId = 5;
93                 Class<?> retType = float.class;
94                 Class<?>[] paramCls = new Class<?>[] {  };
95                 Object[] paramObj = new Object[] {  };
96                 Object retObj = rmiCall.remoteCall(objectId, methodId, retType, null, paramCls, paramObj);
97                 return (float)retObj;
98         }
99
100         public void setPosition(int _mSec) {
101                 int methodId = 7;
102                 Class<?> retType = void.class;
103                 Class<?>[] paramCls = new Class<?>[] { int.class };
104                 Object[] paramObj = new Object[] { _mSec };
105                 rmiCall.remoteCall(objectId, methodId, retType, null, paramCls, paramObj);
106         }
107
108         public void loadData(short _samples[], int _offs, int _len) {
109                 int methodId = 8;
110                 Class<?> retType = void.class;
111                 Class<?>[] paramCls = new Class<?>[] { short[].class, int.class, int.class };
112                 Object[] paramObj = new Object[] { _samples, _offs, _len };
113                 rmiCall.remoteCall(objectId, methodId, retType, null, paramCls, paramObj);
114         }
115
116         public void init() {
117                 int methodId = 0;
118                 Class<?> retType = void.class;
119                 Class<?>[] paramCls = new Class<?>[] {  };
120                 Object[] paramObj = new Object[] {  };
121                 rmiCall.remoteCall(objectId, methodId, retType, null, paramCls, paramObj);
122         }
123
124         public void registerCallback(SpeakerCallback _cb) {
125                 try {
126                         SpeakerCallback_CallbackSkeleton skel0 = new SpeakerCallback_CallbackSkeleton(_cb, callbackAddress, objIdCnt++);
127                         listCallbackObj.add(skel0);
128                 } catch (Exception ex) {
129                         ex.printStackTrace();
130                         throw new Error("Exception when generating skeleton objects!");
131                 }
132
133                 int methodId = 10;
134                 Class<?> retType = void.class;
135                 Class<?>[] paramCls = new Class<?>[] { int.class };
136                 Object[] paramObj = new Object[] { new Integer(1) };
137                 rmiCall.remoteCall(objectId, methodId, retType, null, paramCls, paramObj);
138         }
139
140         public void ___initCallBack() {
141                 Thread thread = new Thread() {
142                         public void run() {
143                                 try {
144                                         rmiObj = new IoTRMIObject(ports[0]);
145                                         while (true) {
146                                                 byte[] method = rmiObj.getMethodBytes();
147                                                 int objId = IoTRMIObject.getObjectId(method);
148                                                 SpeakerCallback_CallbackSkeleton skel = (SpeakerCallback_CallbackSkeleton) listCallbackObj.get(objId);
149                                                 if (skel != null) {
150                                                         int methodId = IoTRMIObject.getMethodId(method);
151                                                         if (!set0Allowed.contains(methodId)) {
152                                                                 throw new Error("Callback object for SpeakerCallback is not allowed to access method: " + methodId);
153                                                         }
154                                                         skel.invokeMethod(rmiObj);
155                                                 } else {
156                                                         throw new Error("SpeakerCallback: Object with Id " + objId + " not found!");
157                                                 }
158                                         }
159                                 } catch (Exception ex) {
160                                         ex.printStackTrace();
161                                         throw new Error("Error instantiating class SpeakerCallback_CallbackSkeleton!");
162                                 }
163                         }
164                 };
165                 thread.start();
166
167                 int methodId = -9998;
168                 Class<?> retType = void.class;
169                 Class<?>[] paramCls = new Class<?>[] { int[].class, String.class, int.class };
170                 Object[] paramObj = new Object[] { ports, callbackAddress, 0 };
171                 rmiCall.remoteCall(objectId, methodId, retType, null, paramCls, paramObj);
172         }
173
174 }