Title: Synchronous Active Objects Provide Robust EventDriven Applications
1Synchronous Active Objects Provide Robust
Event-Driven Applications
Claude Petitpierre, Switzerland Anton Eliëns,
The Netherlands
21. Event driven programming provides awful
programs !2. There is at least one (good)
alternative to that style !
3Content
Assessment of event driven programming -
example (read a username and a password) Synchrono
us calls to the rescue - implementation Impact
on software engineering - design - modeling -
analysis - validation
4The Problem
- We want to get a username and then a password in
the same field. - The entry of the username may take any amount of
time. - Once the username has been typed in,
- either a password is typed in or
- the cancel button is pressed (and the demand is
cancelled) or - nothing happens for a predefined amount of time
(and the demand is cancelled)
5A Specification of the Dialog
class ADialog String getNames
instantiateDialogWindow ( ) names 0
textField.getText ( ) label.setText ("password
") CHOICE names 1 textField.getText (
) OR delay ( 5000 ) OR cancel.pressed (
) return names // null if no names
6Use of the Dialog Box
main ADialog aDialog new ADialog (
) names aDialog.getNames (
) System.out.println ( names 0 names 1
)
7Implementation with Listeners
listeners
GUI elements
Application program
Instantiated from the library
Coded by the developer
8Implementation With Events ( main )
public static void main (String args ) dm
new DialogMain ( ) aDialog new ADialog ( dm
) public void continuation ( String names
) cancelButton.setEnabled (false) //
? textField.setEnabled (false) //
? System.out.println ( names 0 names 1 )
9Implementation With Events ( Timer listener )
final javax.swing.Timer timer timer new
Timer (5000, new ActionListener ( ) public
void actionPerformed (ActionEvent e)
dm.continuation (null) )
10Text Field Listener
name.addActionListener (new ActionListener ()
boolean readUsername true public void
actionPerformed (ActionEvent e) if
(readUsername) names 0 name.getText
() label.setText ("password
") readUsername false cancelButton.addAc
tionListener (new ActionListener () public
void actionPerformed (ActionEvent e)
timer.stop () de.continuation
(names) ) timer.start () else
names 1 name.getText ( ) timer.stop
() de.continuation (names) )
name.addActionListener (new ActionListener ()
boolean readUsername true public void
actionPerformed (ActionEvent e) if
(readUsername) names 0 name.getText
() label.setText ("password
") readUsername false cancelButton.addAc
tionListener (new ActionListener () public
void actionPerformed (ActionEvent e)
timer.stop () de.continuation
(names) ) timer.start () // code
executed somewhere else else names 1
name.getText ( ) timer.stop
() de.continuation (names) )
11Between the Specification and the
Implementationthe Program Control has been
Inverted
12Parallel Blocking Calls
GUI elements
the calls trigger the transitions
stub
Application program
Instantiated from the library
Coded by the developer
13Synchronous Call
main's thread
GUI thread
Listener getEvent()
actionPerformed() while ( ! trigger )
trigger true wait ()Â
notify ( ) trigger false
event
lisner.getEvent ( )
14Waiting for Several Events
Common listener while
(!maskguards) mask X wait ()Â
notify ( )
mask 0
cl.getEvent(guards)
event
event
event
15With our Proposition(identical to the spec)
class ADialog String getNames
instantiateDialogWindow () names 0
textField.read ( ) label.setText ("password
") select case names 1 textField.read (
) case delay ( 5000 ) names
null case cancel.pressed ( ) names
null return names
16Parallel Pending Calls
CHOICE names 1 textField.getText (
) OR waituntil ( now ( ) 5000 ) OR
cancel.pressed ( )
cl new CommonListener (
textField_ID, timer_ID, cancel_ID ) switch (
wait ( cl ) ) case 0 names 1
textField.getText ( ) case 1 waituntil ( now (
) 5000 ) case 2 cancel.pressed ( )
compilation
17Generalization of the concept
Is it possible to use it across a whole program,
from the accesses to the devices to the
inter-thread communications ?
Yes !
18Active Objects with Synchronous Calls
- The instantiation, references, calls, structures
of active objects are identical to those of the
usual (passive) objects. - An active object has a thread executing its run
method.It can be considered as a process. - A synchronous active object can decide when an
incoming call can take place. Such calls realize
the inter-process communications.
19Object O1
Object O2
run () . . . select
case O2.send (msg) case
O3.m(x) . . .
void send (String msg) .......... run ()
accept send
20Object O1
Object O2
run () . . . select
case O2.send (msg) case
O3.m(x) . . .
void send (String msg) .......... run ()
accept send
void send (String msg) ......... run ()
accept send
21A Model for Synchronous Active Objects
method (char x, int y)
obj1.m(x)
met (int y)
hod (char x)
select case obj1.m(x) case accept
method case met
actObj.op(i)
22Multitask Kernel
kernel
List of calls and accepts
23Synchronous Calls for the GUI
public void run () kernel.await
(sButton,pressed_ID) sButton.pressed ()
kernel
class SButton extends JButton // listener
public void pressed () public SButton
( ) addActionListener (new
ActionListener() public void
actionPerformed (ActionEvent e)
accept pressed // No wait !
)
event
24Lines of Code for the Problem Above
With synchronous calls 51 LOCs
(listeners integrated in the library) With
listeners 74 LOCs With PAC pattern
(presentation-abstraction-control) 133
LOCs I found no way to provide a
good separation of the various aspects !
25Other Advantages of Synchronous Calls
The main program does not block the GUI while it
accesses remote data ! The sequence of actions
can be read directly in the code.
Disadvantages Two threads (main GUI) ?
invokeLater Performances ? enough for a GUI
26Impact upon Software Engineering
27An example ! How to use this UML diagram ?
P. Kruchten, The Rational Unified Process An
Introduction, Addison-Wesley
28Implementation with a main program
4. new session 8. PIN entered
Session manager
Card validator
2. read card
3. validate card
Card reader
1. insert card
5. "Enter PIN"
Display
9. send (validate_PIN)
Authenticator
6. get PIN
7. PIN_entered
11a. receive (PIN validity) ?
10. PIN valid ?
11b. cancel ?
Keypad
ATM Network
12. "Amount ?"
29Synchronous Objects Map onCSP, CCS, FSM, Promela
Java with synchronous objects is a specification
language as precise as Promela, for example, but
it is simultaneously an implementation
language. We are actually developing a framework
for the analysis of synchronous programs.
30Philosophers' Program
active class Phil int i //
philosopher number public Phil (int i)
this.i i run () for ()
think waituntil (now() random())
aforki.get()
afork(i1)5.get() eat waituntil
(now() random())
aforki.put()
afork(i1)5.put() Phil phil
new Phil 5
active class Fork public void get ()
public void put () run () for
() accept get accept
put Fork afork new Fork 5
31Verifying the philosophers' problem
32Conclusion
- Synchronous objects avoid the program control
inversion introduced by event driven programming - They make non deterministic programs as simple as
sequential ones - They are very close to the best known formal
languages (CSP) and are thus prone to analysis
and validation - They can be implemented with the current
libraries of Java - They are particularly well adapted to distributed
applications - We have a compiler and a framework with
synchronous libraries ( see our Web site
http//ltiwww.epfl.ch/sJava )
33Thank you !