27.03.2014 Views

Programming Model and Protocols for Reconfigurable Distributed ...

Programming Model and Protocols for Reconfigurable Distributed ...

Programming Model and Protocols for Reconfigurable Distributed ...

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

20 CHAPTER 2. COMPONENT MODEL<br />

its execution, a h<strong>and</strong>ler may trigger new events <strong>and</strong> mutate the component’s<br />

local state. The Kompics execution model guarantees that the event<br />

h<strong>and</strong>lers of one component instance are mutually exclusive, i.e., they are<br />

executed sequentially. This alleviates the need <strong>for</strong> synchronization between<br />

different event h<strong>and</strong>lers of the same component accessing the component’s<br />

mutable state, which greatly simplifies their programming.<br />

Code 2.5 illustrates an example event h<strong>and</strong>ler definition in Java. Upon<br />

receiving a Message event, the h<strong>and</strong>leMsg event h<strong>and</strong>ler increments a local<br />

message counter <strong>and</strong> prints a message to the st<strong>and</strong>ard output console.<br />

Code 2.5 A simple event h<strong>and</strong>ler<br />

1 H<strong>and</strong>ler h<strong>and</strong>leMsg = new H<strong>and</strong>ler() {<br />

2 public void h<strong>and</strong>le(Message message) {<br />

3 messages++; // ← component-local state update<br />

4 System.out.println("Received from " + message.source);<br />

5 }<br />

6 };<br />

In diagrams, we use the hEvent graphical notation to represent an<br />

event h<strong>and</strong>ler, where h is the h<strong>and</strong>ler’s name <strong>and</strong> Event is the type of events<br />

accepted by the h<strong>and</strong>ler, e.g., Message.<br />

2.1.5 Subscriptions<br />

A subscription binds an event h<strong>and</strong>ler to a component port, enabling the<br />

event h<strong>and</strong>ler to h<strong>and</strong>le events that arrive at the component on that port. A<br />

subscription is allowed only if the type of events accepted by the h<strong>and</strong>ler,<br />

say E, is allowed to pass by the port’s type definition. In other words, if E<br />

is the set of event types that the port allows to pass in the direction of the<br />

event h<strong>and</strong>ler, then either E ∈ E, or E must be a subtype of a member of E.<br />

Figure 2.3 illustrates the h<strong>and</strong>leMsg event h<strong>and</strong>ler from our previous<br />

example, being subscribed to the component’s required Network port. In<br />

diagrams, we represent a subscription using the graphical notation.<br />

In this example, the subscription of h<strong>and</strong>leMsg to the Network port is<br />

allowed because Message is in the positive set of Network. As a result of this<br />

subscription, h<strong>and</strong>leMsg will h<strong>and</strong>le all events of type Message or of any<br />

subtype of Message, received on this Network port.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!