reading c2-style applications built in c2.fw

38
Reading C2-style Applications Built in c2.fw Eric M. Dashofy [email protected] For ICS 52 October 20, 2004

Upload: hisa

Post on 06-Feb-2016

56 views

Category:

Documents


0 download

DESCRIPTION

Reading C2-style Applications Built in c2.fw. Eric M. Dashofy [email protected] For ICS 52 October 20, 2004. Overview. Software architectures The C2 architectural style With live example The Klax application. Software Architecture: Basic Elements. Interfaces. Clock. Component. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Reading C2-style Applications Built in c2.fw

Reading C2-style Applications Built in c2.fw

Eric M. [email protected]

For ICS 52October 20, 2004

Page 2: Reading C2-style Applications Built in c2.fw

Overview Software architectures The C2 architectural style

With live example The Klax application

Page 3: Reading C2-style Applications Built in c2.fw

Software Architecture: Basic Elements

Clock Component

Interfaces

Bus1

Connector

Interfaces

Page 4: Reading C2-style Applications Built in c2.fw

Software Architecture: Basic Elements (cont).

Clock

Bus1

LCDDriver

Links

Configuration

Page 5: Reading C2-style Applications Built in c2.fw

An Architectural Style One definition of an architectural style: An architectural style is a named set of

constraints (e.g., rules) you put on your development Constraints may be topological, behavioral,

communication-oriented, you name it This can complicate development because these

constraints may be inconvenient or unfamiliar BUT architectural styles elicit beneficial system

properties that would be really hard to get otherwise

Page 6: Reading C2-style Applications Built in c2.fw

Examples from building architecture:

1. Name some constraints in each style

2. Name some benefits (-ilities) elicited by each style.

Page 7: Reading C2-style Applications Built in c2.fw

The C2 Architectural Style Topological Constraints

All components, connectors have two interfaces, “top” and “bottom”

Components can be connected to 0-1 connector on each interface

Connectors can be connected to 0+ components or connectors on each interface

Page 8: Reading C2-style Applications Built in c2.fw

The C2 Architectural Style Communication Constraints

Components & connectors communicate using only independent events or messages

• Requests go up (“requests rise”)• Notifications go down• No passing pointers

Components & connectors communicate asynchronously

• Can send/receive events at any time• No blocking!

Page 9: Reading C2-style Applications Built in c2.fw

The C2 Architectural Style Dependency Constraints

Components may make assumptions about services provided above them

• But not who is providing them Components may NOT make assumptions

about services provided below them Concurrency Constraints

Each component & connector assumes it’s running in its own thread of control

Page 10: Reading C2-style Applications Built in c2.fw

Live ExampleDataStore

GUI

GUIInterpreter

Alarm

Page 11: Reading C2-style Applications Built in c2.fw

Live ExampleDataStore

GUI

GUIInterpreter

Alarm

Stores data; emits

notifications whenever data

changes.

Page 12: Reading C2-style Applications Built in c2.fw

Live ExampleDataStore

GUI

GUIInterpreter

Alarm

Interprets basic GUI actions &

translates them into data store

operations

Page 13: Reading C2-style Applications Built in c2.fw

Live ExampleDataStore

GUI

GUIInterpreter

Alarm

Rings a bell whenever value

of X changes

Page 14: Reading C2-style Applications Built in c2.fw

Live ExampleDataStore

GUI

GUIInterpreter

AlarmAccept & process

user actions

Page 15: Reading C2-style Applications Built in c2.fw

Live ExampleDataStore

GUI

GUIInterpreter

AlarmBus connector – routes messages

Page 16: Reading C2-style Applications Built in c2.fw

Live ExampleDataStore

GUI

GUIInterpreter

Alarm

Page 17: Reading C2-style Applications Built in c2.fw

What –ilities does C2 buy us? What do you think?

Page 18: Reading C2-style Applications Built in c2.fw

-ilities bought by C2 Flexibility

Loose coupling lets you swap out components, interpose components

Flexible connectors allow run-time changes Distributability

Since no assumption of shared address space, distributing an app across machines is easy

Visibility Since all messages go through connectors, they are easy

to catch and log Parallelizability

One-thread-per-brick means multiprocessor machines are effectively utilized

Page 19: Reading C2-style Applications Built in c2.fw

Event-based vs. OO Event-based

Communication by events

Mostly asynchronous Requests emitted to

unknown parties Responses and state

changes go to many parties

No shared pointers

Object Oriented Communication by

procedure calls Mostly synchronous Requests emitted to

named party only Responses to caller

only, state changes to many

Lots of shared pointers

Page 20: Reading C2-style Applications Built in c2.fw

Implementing a C2 architecture

C2 architecture

Application (implemented)

Java/JVM

Native OS

Implemented by…

Uses services provided by…

??? How do we bridge this gap?

Event-basedWorld

Object-orientedWorld

Page 21: Reading C2-style Applications Built in c2.fw

Implementing a C2 architecture

C2 architecture

Application (implemented)

Java/JVM

Native OS

Implemented by…

Uses services provided by…

Architecture Framework

Uses services provided by…

Uses services provided by…

Event-basedWorld

Object-orientedWorld

Page 22: Reading C2-style Applications Built in c2.fw

What’s an architecture framework? An architecture framework is

software that helps to bridge the gaps between an architectural style and a particular implementation platform.

Page 23: Reading C2-style Applications Built in c2.fw

Example C2 World

Components Connectors Events Links Many threads Asynchronous

communication

Java World Objects Method calls Parameters References Few threads Synchronous

communication

Page 24: Reading C2-style Applications Built in c2.fw

Enter c2.fw c2.fw is an architecture framework for the

C2 style built in Java, providing: Abstract base classes for components,

connectors Reusable connectors (local & network) (Pluggable) topology management (Pluggable) message queuing policies (Pluggable) threading policies

Essentially, c2.fw does the hard work, components and connectors simply implement behaviors.

Page 25: Reading C2-style Applications Built in c2.fw

What does a message look like? In c2.fw, a message can be any

serializable (e.g. no pointers) object NamedPropertyMessages (or

subclasses) are popular though Have a String name Plus a set of name-value pairs

• e.g. {“description”, “Rectangle 1”}• e.g. {“width”, 123}• e.g. {“color”, java.awt.Color.BLACK}

Page 26: Reading C2-style Applications Built in c2.fw

“Reading” a c2.fw application Basic format Lifecycle methods:

init(), begin(), end(), destroy() start(), stop(), suspend(), resume()

Handling messages void handle(Message m)

Sending messages sendToAll(Message m, Interface i)

Page 27: Reading C2-style Applications Built in c2.fw

Your basic c2.fw component…package edu.uci.ics.mypackage;

import c2.fw.*; //import c2.fw packageimport c2.legacy.*; //import support for 2-interface //C2 components

Page 28: Reading C2-style Applications Built in c2.fw

Your basic c2.fw component…package edu.uci.ics.mypackage;

import c2.fw.*; //import c2.fw packageimport c2.legacy.*; //import support for 2-interface //C2 components

public class MyC2Component extends AbstractC2Brick{

}

Implements lots of boilerplate functionality from the base c2.fw.Brick

interface; declares a top interface topIface and a bottom interface

bottomIface.

Page 29: Reading C2-style Applications Built in c2.fw

A boilerplate c2.fw component…package edu.uci.ics.mypackage;

import c2.fw.*; //import c2.fw packageimport c2.legacy.*; //import support for 2-interface //C2 components

public class MyC2Component extends AbstractC2Brick{

public MyC2Component(Identifier id){ super(id); }

}

Each component or connector has a unique identifier.

Page 30: Reading C2-style Applications Built in c2.fw

A boilerplate c2.fw component…package edu.uci.ics.mypackage;

import c2.fw.*; //import c2.fw packageimport c2.legacy.*; //import support for 2-interface //C2 components

public class MyC2Component extends AbstractC2Brick{

public MyC2Component(Identifier id){ super(id); }

public void begin(){ //called automatically by fw. //send out initial events }}

Page 31: Reading C2-style Applications Built in c2.fw

Lifecycle Methodspublic void init(){ //called when component/connector is created //but component not guaranteed to be hooked up}

public void begin(){ //called when component/connector is hooked up //in the architecture, should send initial messages}

public void end(){ //called when component/connector is about to be //unhooked, should send final messages}

public void destroy(){ //called when component/connector is about to be //destroyed}

Page 32: Reading C2-style Applications Built in c2.fw

A boilerplate c2.fw component…package edu.uci.ics.mypackage;

import c2.fw.*; //import c2.fw packageimport c2.legacy.*; //import support for 2-interface //C2 components

public class MyC2Component extends AbstractC2Brick{

public MyC2Component(Identifier id){ super(id); }

public void begin(){ //called automatically by fw. //send out initial events }

public void handle(Message m){ //handle message }}

Page 33: Reading C2-style Applications Built in c2.fw

Implementing handle() //handle() method for our hypothetical alarm component //Called automatically when component receives a message //Should ring bell whenever value of ‘X’ changes

public void handle(Message m){ //handle message

}

Page 34: Reading C2-style Applications Built in c2.fw

Implementing handle() //handle() method for our hypothetical alarm component

public void handle(Message m){ //handle message

if(m instanceof NamedPropertyMessage){ NamedPropertyMessage npm = (NamedPropertyMessage)m; }

}

Page 35: Reading C2-style Applications Built in c2.fw

Implementing handle() //handle() method for our hypothetical alarm component

public void handle(Message m){ //handle message

if(m instanceof NamedPropertyMessage){ NamedPropertyMessage npm = (NamedPropertyMessage)m; String name = npm.getName(); if((name != null) && (name.equals(“valueChanged”)){

} } }

Page 36: Reading C2-style Applications Built in c2.fw

Implementing handle() //handle() method for our hypothetical alarm component

public void handle(Message m){ //handle message

if(m instanceof NamedPropertyMessage){ NamedPropertyMessage npm = (NamedPropertyMessage)m; String name = npm.getName(); if((name != null) && (name.equals(“valueChanged”)){ String varName = (String)npm.getParameter(“varName”); if((varName != null) && (varName.equals(“x”)){ } } } }

Page 37: Reading C2-style Applications Built in c2.fw

Implementing handle() //handle() method for our hypothetical alarm component

public void handle(Message m){ //handle message

if(m instanceof NamedPropertyMessage){ NamedPropertyMessage npm = (NamedPropertyMessage)m; String name = npm.getName(); if((name != null) && (name.equals(“valueChanged”)){ String varName = (String)npm.getParameter(“varName”); if((varName != null) && (varName.equals(“x”)){ ringBell(); NamedPropertyMessage rnpm = new NamedPropertyMessage(“AlarmFired”); rnpm.addProperty(“time”, System.currentTimeMillis()); sendToAll(rnpm, bottomIface); } } } }

Page 38: Reading C2-style Applications Built in c2.fw

Implementing handle()If we care, we could have checked what interface that message came in on:

if(m.getDestination().getInterfaceIdentifier().equals( AbstractC2Brick.TOP_INTERFACE_ID)){

//it’s a notification

}