Synchronous Active Objects Provide Robust EventDriven Applications - PowerPoint PPT Presentation

1 / 33
About This Presentation
Title:

Synchronous Active Objects Provide Robust EventDriven Applications

Description:

We want to get a username and then a password in the same field. ... hod (char [ ] x) obj1.m(x); actObj.op(i); select { case obj1.m(x); case accept method; ... – PowerPoint PPT presentation

Number of Views:48
Avg rating:3.0/5.0
Slides: 34
Provided by: ltie4
Category:

less

Transcript and Presenter's Notes

Title: Synchronous Active Objects Provide Robust EventDriven Applications


1
Synchronous Active Objects Provide Robust
Event-Driven Applications
Claude Petitpierre, Switzerland Anton Eliëns,
The Netherlands
2
1. Event driven programming provides awful
programs !2. There is at least one (good)
alternative to that style !
3
Content
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
4
The 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)

5
A 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
6
Use of the Dialog Box
main ADialog aDialog new ADialog (
) names aDialog.getNames (
) System.out.println ( names 0 names 1
)
7
Implementation with Listeners
listeners
GUI elements
Application program
Instantiated from the library
Coded by the developer
8
Implementation 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 )
9
Implementation With Events ( Timer listener )
final javax.swing.Timer timer timer new
Timer (5000, new ActionListener ( ) public
void actionPerformed (ActionEvent e)
dm.continuation (null) )
10
Text 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) )
11
Between the Specification and the
Implementationthe Program Control has been
Inverted
12
Parallel Blocking Calls
GUI elements
the calls trigger the transitions
stub
Application program
Instantiated from the library
Coded by the developer
13
Synchronous Call
main's thread
GUI thread
Listener getEvent()
actionPerformed() while ( ! trigger )
trigger true wait () 
notify ( ) trigger false
event
lisner.getEvent ( )
14
Waiting for Several Events
Common listener while
(!maskguards) mask X wait () 
notify ( )
mask 0
cl.getEvent(guards)
event
event
event
15
With 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
16
Parallel 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
17
Generalization 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 !
18
Active 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.

19
Object O1
Object O2
run () . . . select
case O2.send (msg) case
O3.m(x) . . .
void send (String msg) .......... run ()
accept send
20
Object 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
21
A 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)
22
Multitask Kernel
kernel
List of calls and accepts
23
Synchronous 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
24
Lines 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 !
25
Other 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
26
Impact upon Software Engineering
27
An example ! How to use this UML diagram ?
P. Kruchten, The Rational Unified Process An
Introduction, Addison-Wesley
28
Implementation 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 ?"
29
Synchronous 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.
30
Philosophers' 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
31
Verifying the philosophers' problem
32
Conclusion
  • 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 )

33
Thank you !
Write a Comment
User Comments (0)
About PowerShow.com