KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > proactive > examples > cruisecontrol > Interface


1 /*
2 * ################################################################
3 *
4 * ProActive: The Java(TM) library for Parallel, Distributed,
5 * Concurrent computing with Security and Mobility
6 *
7 * Copyright (C) 1997-2002 INRIA/University of Nice-Sophia Antipolis
8 * Contact: proactive-support@inria.fr
9 *
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or any later version.
14 *
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
23 * USA
24 *
25 * Initial developer(s): The ProActive Team
26 * http://www.inria.fr/oasis/ProActive/contacts.html
27 * Contributor(s):
28 *
29 * ################################################################
30 */

31 package org.objectweb.proactive.examples.cruisecontrol;
32
33 /** This class is the main dispatcher for the active objects.
34  * it acts as link between the distributed active objects
35  * and the Applet.
36  * The interface is turn active by the ProActive primitives turnActive.
37  * All the communications between active objects, between an active object and the Applet
38  * pass in transit through it
39  *
40  */

41 public class Interface {
42
43   // Fields
44

45   /**
46    * the active state of the car when the controller is off
47    */

48   final static int ACTIVE = 1;
49   /** the off state of the car when the engine is off */
50   final static int INACTIVE = 0;
51   /**
52    * the active state of the car when the Cruise Control is on
53    */

54   final static int CRUISING = 2;
55   /** the state of the car */
56   int state = INACTIVE;
57   /** the desired speed of the controller */
58   double desiredSpeed = 0;
59   /** the acceleration to apply to the car when the cruise control is off */
60   double acc = 0;
61   /** Used to brake */
62   double brake = 0;
63   /** the incline of the road */
64   double alpha = 0;
65   /** the active object Car */
66   CarModel activeSpeed = null;
67   /** the active object cruise control */
68   CruiseControl activeCruise = null;
69
70   ///** The active object road */
71
//public RoadModel road = null;
72

73   /** The applet of the interface which is necessary for painting purposes */
74   CruiseControlApplet applet;
75
76
77   /** A no-arg construtor for ProActive pruposes */
78   public Interface() {
79
80   }
81
82
83   /** constructor wich initializes the applet field */
84   public Interface(CruiseControlApplet m_applet) {
85     this.applet = m_applet;
86
87   }
88
89
90   /** Method which creates the differents active objects */
91   public void initialize() {
92     Object JavaDoc[] arg;
93     arg = new Object JavaDoc[1];
94     arg[0] = org.objectweb.proactive.ProActive.getStubOnThis();
95     
96     //System.out.println("initialize Method");
97
try {
98       activeSpeed = (CarModel)org.objectweb.proactive.ProActive.newActive(CarModel.class.getName(), arg);
99
100     } catch (Exception JavaDoc e) {
101       e.printStackTrace();
102       System.exit(0);
103     }
104
105     arg[0] = org.objectweb.proactive.ProActive.getStubOnThis();
106
107     try {
108
109       activeCruise = (CruiseControl)org.objectweb.proactive.ProActive.newActive(CruiseControl.class.getName(), arg);
110     } catch (Exception JavaDoc e) {
111       e.printStackTrace();
112       System.exit(0);
113     }
114
115
116     /** try
117      * {
118      *road = (RoadModel)org.objectweb.proactive.ProActive.newActiveObject ("org.objectweb.cruisecontrol.RoadModel", arg, null);
119      *
120      * }
121      *catch (org.objectweb.proactive.ActiveObjectCreationException e)
122      * {
123      *e.printStackTrace();
124      *System.exit(0);
125      * }
126      */

127
128   }
129
130
131   ///////////////////////////////////////////////////////////////////
132

133   public void displayMessage(String JavaDoc msg) {
134     applet.receiveMessage(msg);
135   }
136
137
138   /** Turns on the active objects activeSpeed and the road */
139   public void engineOn() {
140     // //System.out.println("Speed Changed");
141
//System.out.println("EngineOn : Interface : STATE >> "+ state);
142
if (this.state == INACTIVE) {
143       this.state = ACTIVE;
144       //System.out.println("Interface : EngineOn : STATE after >> "+ state);
145
activeSpeed.engineOn();
146       applet.engineOn();
147     }
148   }
149
150
151   /** Turns off the active objects activeSpeed, activeCruise, calls the applet method engineOff */
152   public void engineOff() {
153     activeSpeed.engineOff();
154     activeCruise.engineOff();
155
156     this.state = INACTIVE;
157     this.acc = 0;
158     this.desiredSpeed = 0;
159     setAcceleration(0);
160     applet.engineOff();
161     
162     //System.out.println("Speed Null");
163
}
164
165
166   ///////////////////////////////////////////////////////////////////
167
/** Actives the controller,
168    * get the current speed from the object activeSpeed
169    */

170   public void controlOn() {
171     if (this.state == ACTIVE) {
172       this.state = CRUISING;
173       desiredSpeed = getSpeed().doubleValue();
174       //System.out.println("control On : Interface");
175
// Acceleration Null from the driver
176
//System.out.println("control On : Interface : Interface Speed"+acc);
177
activeCruise.controlOn(desiredSpeed, this.acc);
178       //System.out.println("Cruise Acc : "+activeCruise.acc);
179
//System.out.println("state :"+activeCruise.state);
180
}
181
182   }
183
184
185   /** Deactives the controller, set the current acceleration to null,
186    * asks the applet to repaint the controller
187    */

188   public void controlOff() {
189     if (this.state == CRUISING) {
190       this.state = ACTIVE;
191       acc = 0;
192       setAcceleration(0);
193
194       activeCruise.controlOff();
195     }
196   }
197
198
199
200   ///////////////////////////////////////////////////////////////
201
/** Deactives the controller when the speed is too low to be held
202    * asks the applet to repaint the controller
203    */

204   public void deactiveControl() {
205     if (this.state == CRUISING) {
206       this.state = ACTIVE;
207       acc = 0;
208       activeSpeed.brake();
209       applet.controlPaneOff();
210     }
211   }
212
213
214   ///////////////////////////////////////////////////////////////////
215
/** When the user pressed the "+" button, increases the current acceleration with one,
216    * changes the current acceleration of the car
217    * and asks the applet to repaint the controller
218    */

219   public void accelerate() {
220
221     if (this.state == ACTIVE) {
222       if (this.acc < 50) {
223         this.acc += 1;
224         //System.out.println("Interface : Accelerate >> THIS.ACC : "+this.acc);
225
activeSpeed.setAcceleration(this.acc);
226         applet.setAcceleration(this.acc);
227
228       }
229     }
230
231   }
232
233
234   /** When the user pressed the "-" button, decreases the current acceleration with one,
235    * changes the current acceleration of the car
236    * and asks the applet to repaint the controller
237    */

238   public void decelerate() {
239     if (this.state == ACTIVE) {
240       if (this.acc > 0) {
241         this.acc -= 1;
242         activeSpeed.setAcceleration(this.acc);
243         applet.setAcceleration(this.acc);
244       }
245
246     }
247
248   }
249
250
251
252   ////////////////////////////////////////////////////////////////////////
253
/** When the user pressed the "++" button, increases the current desired speed with one,
254    * changes the current desired speed of the controller
255    * changes the desired speed of the applet
256    */

257   public void accelerateCruise() {
258     if (this.state == CRUISING) {
259       desiredSpeed += 1;
260       activeCruise.setDesiredSpeed(desiredSpeed);
261       applet.setDesiredSpeed(desiredSpeed);
262       //System.out.println("desiredSpeed Interface");
263
}
264
265   }
266
267
268   /** When the user pressed the "--" button, decreases the current desired speed with one,
269    * changes the current desired speed of the controller
270    * changes the desired speed of the applet
271    */

272   public void decelerateCruise() {
273     if (this.state == CRUISING) {
274       desiredSpeed -= 1;
275       activeCruise.setDesiredSpeed(desiredSpeed);
276       applet.setDesiredSpeed(desiredSpeed);
277       //System.out.println("desiredSpeed Interface");
278
}
279
280   }
281
282   ///////////////////////////////////////////////////////////////////////
283
/**
284    * Increases the current value of the incline road
285    */

286   public void incAlpha() {
287     //System.out.println("incAlpha : Interface");
288
//System.out.println("incAlpha : Interface : state >> "+ this.state);
289
if ((this.state == ACTIVE) || (this.state == CRUISING)) {
290       //System.out.println("incAlpha : Interface");
291
if (alpha < 0.6)
292         alpha += 0.09;
293       else
294         alpha = 0.6;
295       setAlpha(alpha);
296       //System.out.println("Interface : IncAlpha : Alpha >> "+ alpha);
297
}
298   }
299
300
301   /**
302    * Decreases the current value of the incline road
303    */

304   public void decAlpha() {
305     if ((this.state == ACTIVE) || (this.state == CRUISING)) {
306       if (alpha > -0.6)
307         alpha -= 0.09;
308       else
309         alpha = -0.6;
310       setAlpha(alpha);
311       //System.out.println("Interface : IncAlpha : Alpha >> "+ alpha);
312
}
313   }
314
315
316
317   ////////////////////////////////////////////////////////////////////////
318

319   /** Sets the current acceleration to null
320    * sets the controlller off if it is active
321    * calls the activeSpeed method brake
322    * asks the applet to repaint it
323    */

324   public void brake() {
325     //System.out.println("Interface : Brake : state >> "+ this.state);
326
if ((this.state == ACTIVE) || (this.state == CRUISING)) {
327       activeCruise.controlOff();
328       activeSpeed.brake();
329       applet.brake();
330       acc = 0;
331       this.state = ACTIVE;
332     }
333
334   }
335
336
337   /** Changes the speed that the applet displays */
338   public void setSpeed(double speed) {
339     applet.setSpeed(speed);
340   }
341
342
343   /** Changes the distance that the applet displays */
344   public void setDistance(double distance) {
345     applet.setDistance(distance);
346   }
347
348
349   /** Changes the applet desired Speed */
350   public void setDesiredSpeed(double m_speed) {
351     applet.setDesiredSpeed(m_speed);
352     //System.out.println("desiredSpeed Interface");
353
}
354
355
356   /** Gets the current speed of the active object activeSpeed */
357   public Double JavaDoc getSpeed() {
358     //System.out.println("Interface : getSpeed Interface");
359
return activeSpeed.getSpeed();
360   }
361
362
363   /** Changes the acceleration of the active object activeSpeed
364    * and asks the applet to repaint it
365    */

366   public void setAcceleration(double m_acc) {
367     //System.out.println("Interface : setAcceleration : M_ACC >> "+ m_acc);
368
this.acc = m_acc;
369     activeSpeed.setAcceleration(m_acc);
370     applet.setAcceleration(m_acc);
371   }
372
373
374   /** Returns the current acceleration of the user */
375   public Double JavaDoc getAcceleration() {
376     //System.out.println("Interface : getAcceleration : THIS.ACC >> "+ acc);
377
return new Double JavaDoc(this.acc);
378   }
379
380
381   ///////////////////////////////////////////////////////////////
382

383   /** Changes the incline of the car : active Speed
384    * and asks the applet to display the new value
385    */

386   public void setAlpha(double m_alpha) {
387     if ((this.state == ACTIVE) || (this.state == CRUISING)) {
388       activeSpeed.setAlpha(m_alpha);
389       applet.setAlpha(m_alpha);
390       //System.out.println("Interface : setAlpha >> "+ m_alpha);
391
}
392
393   }
394 }
395
396
397
Popular Tags