• No results found

Design Patterns And Frameworks For Object Oriented Communication Systems pdf

N/A
N/A
Protected

Academic year: 2020

Share "Design Patterns And Frameworks For Object Oriented Communication Systems pdf"

Copied!
18
0
0

Loading.... (view fulltext now)

Full text

(1)

Design Patterns and Frameworks

for Object-oriented

Communication Systems

Douglas C. Schmidt

http://www.cs.wustl.edu/

schmidt/

[email protected]

Washington University, St. Louis

1

Motivation

Developing ecient, robust, extensible, and

reusable communication software is hard

It is essential to understand successful

tech-niques that have proven eective to solve

common development challenges

Design patterns and frameworks help to

cap-ture, articulate, and instantiate these

suc-cessful techniques

2

Observations

Developers of communication software

con-front recurring challenges that are largely

application-independent

{ e.g., service initialization and distribution, error

handling, ow control, event demultiplexing, con-currency control

Successful developers resolve these challenges

by applying appropriate design patterns

However, these patterns have traditionally

been either:

1. Locked inside heads of expert developers 2. Buried in source code

Design Patterns

Design patterns represent solutions to

prob-lems that arise when developing software

within a particular context

{ i.e., \Patterns==problem/solution pairs in a

con-text"

Patterns capture the static and dynamic

struc-ture and collaboration among key

partici-pants in software designs

{ They are particularly useful for articulating how

and why to resolve non-functional forces

Patterns facilitate reuse of successful

(2)

Proxy Pattern

NETWORK

CLIENT

SERVER : BROKER

1: METHOD CALL

4: METHOD RETURN

: QUOTER

2: FORWARD REQUEST

3: RESPONSE

: QUOTER PROXY

Intent: provide a surrogate for another

ob-ject that controls access to it

5

Graphical Notation

PROCESS

THREAD

OBJECT

: CLASS

CLASS

CLASS CATEGORY

CLASS UTILITY

INHERITS

CONTAINS

INSTANTIATES

A

ABSTRACT CLASS

USES TEMPLATE

CLASS

6

More Observations

Reuse of patterns alone is not sucient

{ Patterns enable reuse of architecture and design

knowledge, but not code (directly)

To be productive, developers must also reuse

detailed designs, algorithms, interfaces,

im-plementations, etc.

Application frameworks are an eective way

to achieve broad reuse of software

Frameworks

A framework is:

{ \An integrated collection of components that

col-laborate to produce a reusable architecture for a family of related applications"

Frameworks dier from conventional class

libraries:

1. Frameworks are \semi-complete" applications 2. Frameworks address a particular application do-main 3. Frameworks provide \inversion of control"

Typically, applications are developed by

(3)

Dierences Between Class

Libraries and Frameworks

APPLICATION SPECIFIC

LOGIC

USER INTERFACE

CLASS LIBRARIES

NETWORKING

MATH ADTS

DATA BASE

APPLICATION SPECIFIC

LOGIC MATH

OBJECT-ORIENTED

FRAMEWORK

ADTS

INVOKES

CALL BACKS

NETWORKING USER

INTERFACE

DATABASE INVOKES

EVENT LOOP

EVENT LOOP

9

Tutorial Outline

Outline key challenges for developing

com-munication software

Present the key reusable design patterns in

a distributed medical imaging system

{ Both single-threaded and multi-threaded solutions

are presented

Discuss lessons learned from using patterns

on production software systems

10

Stand-alone vs. Distributed

Application Architectures

PRINTER

FILE SYSTEM

PRINTER FILE SYSTEM

COMPUTER

(1)

(1) STAND-ALONESTAND-ALONE APPLICATIONAPPLICATION ARCHITECTUREARCHITECTURE

(2)

(2) DISTRIBUTEDDISTRIBUTED APPLICATIONAPPLICATION ARCHITECTUREARCHITECTURE CD ROM

CD ROM NETWORK DISPLAY

SERVICE

FI LE SERVICE PRINT

SERVICE

CYCLE SERVICES

Concurrency vs. Parallelism

CONCURRENT SERVER

maxfdp1 read_fds WORK

REQUEST

SERVER

CLIENT

WORK

REQUEST REQUESTWORK REQUESTWORK

CLIENT

CLIENT CLIENT

SERVER

CPU1 CPU2 CPU3 CPU4

WORK REQUEST

WORK

REQUEST REQUESTWORK REQUESTWORK

CLIENT

CLIENT

CLIENT CLIENT

(4)

Sources of Complexity

Distributed application development exhibits

both inherent and accidental complexity

Inherent complexity results from

fundamen-tal challenges, e.g.,

{ Distributed systems . Latency

. Error handling

. Service partitioning and load balancing { Concurrent systems

. Race conditions . Deadlock avoidance . Fair scheduling

. Performance optimization and tuning 13

Sources of Complexity (cont'd)

Accidental complexity results from

limita-tions with tools and techniques, e.g.,

{ Low-level tools

. e.g., Lack of type-secure, portable, re-entrant,

and extensible system call interfaces and com-ponent libraries

{ Inadequate debugging support

{ Widespread use of algorithmic decomposition . Fine for explaining network programming

con-cepts and algorithms but inadequate for devel-oping large-scale distributed applications

{ Continuous rediscovery and reinvention of core

con-cepts and components

14

OO Contributions

Concurrent and distributed programming has

traditionally been performed using low-level

OS mechanisms, e.g.,

{ fork/exec { Shared memory { Signals

{ Sockets and select

{ POSIX pthreads, Solaris threads, Win32 threads

OO design patterns and frameworks elevate

development to focus on application

con-cerns, e.g.,

{ Service functionality and policies { Service conguration

{ Concurrent event demultiplexing and event

han-dler dispatching

{ Service concurrency and synchronization

Distributed Medical Imaging

Example

This example illustrates the reusable design

patterns and framework components used in

an OO architecture for a distributed medical

imaging system

Application clients uses Blob Servers to store

and retrieve medical images

Clients and Servers communicate via a

connection-oriented transport protocol

(5)

Distributed Electronic Medical

Imaging Architecture

DIAGNOSTIC DIAGNOSTIC STATIONS STATIONS

ATM ATM MAN MAN

ATM ATM LAN LAN

ATM ATM LAN LAN

MODALITIES MODALITIES

((CTCT,, MR MR,, CR CR))

LOCAL LOCAL STORE STORE MODALITIES

MODALITIES

((CTCT,, MR MR,, CR CR))

NAME NAME SERVER SERVER

ROUTING ROUTING SERVICE SERVICE

CENTRAL CENTRAL STORE STORE

CLUSTER CLUSTER STORE STORE TIME

TIME SERVER SERVER

L LOCATIONOCATION

SER SERVICEVICE

17

Architecture of the Blob Server

: Reactor BLOB

BLOB SERVERSERVER

: Blob : Blob Handler Handler

svc_run svc_run svc_run

svc_run

: Blob : Blob Handler Handler : Blob

: Blob Handler Handler

: Blob : Blob Acceptor Acceptor : Options : Options svc_run

svc_run svc_run

svc_run

: Blob : Blob Processor Processor QueueQueue: Msg: Msg

* Manage short-term and long-term blob persistence

* Respond to queries from Blob Locators

18

Design Patterns in the Blob

Server

Half-Sync/ Half-Sync/ Half-Async Half-Async

Strategy

Strategy AdapterAdapter

Proxy Proxy TACTICAL

PATTERNS STRATEGIC

PATTERNS Double CheckedDouble CheckedLockingLocking Connector Connector

Singleton Singleton Acceptor Acceptor

Service Service Configurator Configurator

Reactor Reactor Thread-per

Thread-per Request Request

Thread-per Thread-per

Session Session

Thread Thread Pool Pool

Active Object Active Object

Tactical Patterns

Proxy

{ \Provide a surrogate or placeholder for another

object to control access to it"

Strategy

{ \Dene a family of algorithms, encapsulate each

one, and make them interchangeable"

Adapter

{ \Convert the interface of a class into another

in-terface client expects"

Singleton

{ \Ensure a class only has one instance and provide

(6)

Concurrency Patterns

Reactor

{ \Decouples event demultiplexing and event

han-dler dispatching from application services performed in response to events"

Active Object

{ \Decouples method execution from method

invo-cation and simplies synchronized access to shared resources by concurrent threads"

Half-Sync/Half-Async

{ \Decouples synchronous I/O from asynchronous

I/O in a system to simplify concurrent program-ming eort without degrading execution eciency"

Double-Checked Locking Pattern

{ \Ensures atomic initialization of objects and

elim-inates unnecessary locking overhead on each ac-cess"

21

Concurrency Architecture

Patterns

Thread-per-Request

{ \Allows each client request to run concurrently"

Thread-Pool

{ \Allows up to N requests to execute concurrently"

Thread-per-Session

{ \Allows each client session to run concurrently"

22

Service Initialization Patterns

Connector

{ \Decouples active connection establishment from

the service performed once the connection is es-tablished"

Acceptor

{ \Decouples passive connection establishment from

the service performed once the connection is es-tablished"

Service Congurator

{ \Decouples the behavior of network services from

point in time at which services are congured into an application"

Concurrency Patterns in the Blob

Server

The following example illustrates the design

patterns and framework components in an

OO implementation of a concurrent Blob

Server

There are various architectural patterns for

structuring concurrency in a Blob Server

1. Reactive

(7)

Reactive Blob Server Architecture

: Reactor : Reactor

BLOB SERVER BLOB SERVER

: Blob : Blob Processor Processor : Blob : Blob Acceptor Acceptor SERVER CLIENT CLIENT CLIENT 1:

1: CONNECT CONNECT

2:

2: HANDLE INPUT HANDLE INPUT

3:

3: CREATE PROCESSOR CREATE PROCESSOR

4:

4: ACCEPT CONNECTION ACCEPT CONNECTION

5:

5: ACTIVATE PROCESSOR ACTIVATE PROCESSOR

6:

6: PROCESS BLOB REQUEST PROCESS BLOB REQUEST

: Blob : Blob Processor Processor

25

Thread-per-Request Blob Server

Architecture

SERVER SERVER CLIENT CLIENT CLIENT

CLIENT CLIENTCLIENT

: Reactor : Reactor

BLOB SERVER

: Blob : Blob Acceptor Acceptor

1:

1: CONNECT CONNECT

2:

2: HANDLE INPUT HANDLE INPUT

3:

3: CREATE PROCESSOR CREATE PROCESSOR

4:

4: ACCEPT CONNECTION ACCEPT CONNECTION

5:

5: SPAWN THREAD SPAWN THREAD : Blob : Blob Processor Processor : Blob : Blob Processor Processor : Blob : Blob Processor Processor 6:

6: PROCESS BLOB REQUEST PROCESS BLOB REQUEST

26

Thread-per-Session Blob Server

Architecture

SERVER SERVER CLIENT CLIENT CLIENT CLIENT 2:

2: CREATE CREATE,, ACCEPT ACCEPT,, AND ACTIVATE AND ACTIVATE BLOB

BLOB__PROCESSORPROCESSOR BLOB SERVER

1: 1: BIND BIND

CLIENT CLIENT : Blob : Blob Processor Processor : Blob : Blob Processor Processor : Blob : Blob Processor Processor 4:

4: PROCESS BLOB REQUEST PROCESS BLOB REQUEST

3:

3: SPAWN THREAD SPAWN THREAD

: Reactor : Reactor : Blob : Blob Acceptor Acceptor

Thread-Pool Blob Server

Architecture

: Reactor : Reactor

BLOB SERVER BLOB SERVER

1: 1: BLOB BLOB REQUEST REQUEST

2:

2: HANDLE INPUT HANDLE INPUT

3:

3: ENQUEUE REQUEST ENQUEUE REQUEST

: Blob : Blob Handler Handler

6:

6: PROCESS BLOB REQUEST PROCESS BLOB REQUEST

SERVER SERVER CLIENT

CLIENT

CLIENT

CLIENT CLIENTCLIENT

: Blob : Blob Handler Handler : Blob : Blob Handler Handler 5:

5: DEQUEUE DEQUEUE && PROCESS PROCESS REQUEST REQUEST worker worker thread thread worker worker thread thread worker worker thread

thread workerworkerthreadthread

(8)

The ADAPTIVE Communication

Environment (ACE)

THREAD THREAD LIBRARY LIBRARY DYNAMIC DYNAMIC LINKING LINKING MEMORY MEMORY MAPPING MAPPING SELECT SELECT// POLL POLL SYSTEM SYSTEM V V IPCIPC

STREAM STREAM PIPES PIPES NAMED NAMED PIPES PIPES C

APISS SOCKETSSOCKETSTLITLI // COMMUNICATION COMMUNICATION SUBSYSTEM SUBSYSTEM

VIRTUAL MEMORY VIRTUAL MEMORY SUBSYSTEM SUBSYSTEM GENERAL UNIX AND WIN32 SERVICES PROCESS

PROCESS//THREADTHREAD SUBSYSTEM SUBSYSTEM SYNCH SYNCH WRAPPERS WRAPPERS FRAMEWORKS

AND CLASS CATEGORIES

ACCEPTOR

ACCEPTOR CONNECTORCONNECTOR DISTRIBUTED SERVICES NAME NAME SERVER SERVER TOKEN TOKEN SERVER SERVER LOGGING LOGGING SERVER SERVER GATEWAY GATEWAY SERVER SERVER SOCK SOCK__SAPSAP//

TLI TLI__SAPSAP

FIFO FIFO SAP SAP REACTOR REACTOR LOG LOG MSG MSG SERVICE SERVICE HANDLER HANDLER TIME TIME SERVER SERVER

OS ADAPTATION LAYER SERVICE SERVICE CONFIG CONFIG --URATOR URATOR ADAPTIVE SERVICE EXECUTIVE

ADAPTIVE SERVICE EXECUTIVE (ASX)(ASX)

C++ WRAPPERS THREAD THREAD MANAGER MANAGER SPIPE SPIPE SAP SAP CORBA CORBA HANDLER HANDLER SYSV SYSV WRAPPERS WRAPPERS MEM MEM MAP MAP SHARED SHARED MALLOC MALLOC

A set of C++ wrappers and frameworks

based on common design patterns

29

The Reactor Pattern

Intent

{ \Decouples event demultiplexing and event

han-dler dispatching from the services performed in re-sponse to events"

This pattern resolves the following forces

for event-driven software:

{ How to demultiplex multiple types of events from

multiple sources of events eciently within a single thread of control

{ How to extend application behavior without

requir-ing changes to the event dispatchrequir-ing framework

30

Structure of the Reactor Pattern

Reactor Reactor handle_events() register_handler(h) remove_handler(h) expire_timers() 11 11 11 Event_Handler Event_Handler handle_input() handle_output() handle_signal() handle_timeout() get_handle() A 11 nn nn Concrete Concrete Event_Handler Event_Handler Timer_Queue Timer_Queue schedule_timer(h) cancel_timer(h) expire_timer(h) 11 11 select (handles); select (handles); foreach h in handles { foreach h in handles { if (h is output handler) if (h is output handler) table[h]->handle_output () ; table[h]->handle_output () ; if (h is input handler) if (h is input handler) table[h]->handle_input (); table[h]->handle_input (); if (h is signal handler) if (h is signal handler) table[h]->handle_signal (); table[h]->handle_signal (); }}

this->expire_timers (); this->expire_timers ();

nn HandlesHandles11

APPLICATION APPLICATION --DEPENDENT DEPENDENT APPLICATION -INDEPENDENT n

Participants in the Reactor pattern

Collaboration in the Reactor

Pattern

main main program program INITIALIZE INITIALIZE REGISTER HANDLER REGISTER HANDLER

callback : callback : Concrete Concrete Event_Handler Event_Handler

START EVENT LOOP START EVENT LOOP

DATA ARRIVES DATA ARRIVES OK TO SEND OK TO SEND

reactor reactor : Reactor : Reactor

handle_events()

FOREACH EVENT DO FOREACH EVENT DO

handle_input() select() Reactor() register_handler(callback)

handle_output()

SIGNAL ARRIVES SIGNAL ARRIVES TIMER EXPIRES TIMER EXPIRES

handle_signal() handle_timeout() get_handle()

EXTRACT HANDLE EXTRACT HANDLE

REMOVE HANDLER

REMOVE HANDLER remove_handler(callback)

INITIALIZATIONINITIALIZATION

MODEMODE

EVENT HANDLINGEVENT HANDLING

MODEMODE

handle_close()

(9)

Using the Reactor in the Blob

Server

:: Reactor Reactor

REGISTERED OBJECTS

: Handle : Handle Table Table

FRAMEWORKFRAMEWORK

LEVELLEVEL

KERNELKERNEL LEVELLEVEL

APPLICATIONAPPLICATION

LEVELLEVEL

OS EVENT DEMULTIPLEXING INTERFACE OS EVENT DEMULTIPLEXING INTERFACE 1: handle_input()

1: handle_input()

svc_run

svc_run svc_runsvc_run svc_run svc_run

: Blob : Blob Processor Processor : Message

: Message Queue Queue 4: getq(msg) 4: getq(msg) 5:svc(msg) 5:svc(msg)

: Event : Event Handler Handler : Blob : Blob Handler Handler

: Event : Event Handler Handler : Blob : Blob Handler Handler

: Event : Event Handler Handler : Blob : Blob Handler Handler

2: recv_request(msg) 2: recv_request(msg) 3: putq(msg) 3: putq(msg)

33

The Blob Handler Interface

The

Blob Handler

is the Proxy for

commu-nicating with clients

{ Together with Reactor, it implements the

asyn-chronous task portion of the Half-Sync/Half-Async pattern

// Reusable Svc Handler.

class Blob_Handler : public Event_Handler {

public:

// Entry point into Blob Handler. virtual int open (void) {

// Register with Reactor to handle client input. Reactor::instance ()->register_handler

(this, READ_MASK); }

protected:

// Notified by Reactor when client requests arrive. virtual int handle_input (void);

// Receive and frame client requests. int recv_request (Message_Block &*);

SOCK_Stream peer_stream_; // IPC endpoint. };

34

The Active Object Pattern

Intent

{ \Decouples method execution from method

invo-cation and simplies synchronized access to shared resources by concurrent threads"

This pattern resolves the following forces

for concurrent communication software:

{ How to allow blocking read and write operations

on one endpoint that do not detract from the qual-ity of service of other endpoints

{ How to simplify concurrent access to shared state { How to simplify composition of independent

ser-vices

Structure of the Active Object

Pattern

Client Client Interface Interface

ResultHandle m1() ResultHandle m2() ResultHandle m3()

Activation Activation Queue Queue

insert() remove()

Scheduler Scheduler

dispatch() m1'() m2'() m3'()

Resource Resource Representation

Representation MethodMethodObjectsObjects

loop {

m = actQueue.remove() dispatch (m)

}

INVISIBLE INVISIBLE

TO TO CLIENTS CLIENTS VISIBLE VISIBLE

TO TO CLIENTS CLIENTS

nn 11

11 11

11

11

The

Scheduler

determines the sequence that

(10)

Collaboration in the Active

Object Pattern

INVOKE INVOKE

INSERT IN INSERT IN PRIORITY QUEUE PRIORITY QUEUE

cons(m1')

remove(m1')

DEQUEUE NEXT DEQUEUE NEXT METHOD OBJECT METHOD OBJECT

RETURN RESULT RETURN RESULT

EXECUTE EXECUTE

client

client InterfaceInterface: Client: Client : Activation: ActivationQueueQueue

insert(m1')

dispatch(m1') METHOD OBJECTMETHOD OBJECT CONSTRUCTIONCONSTRUCTION

SCHEDULING

/

EXECUTION

COMPLETION

m1()

: :

Represent-ation ation : Scheduler

: Scheduler

CREATE METHOD OBJECT

reply_to_future() future()

RETURN RESULT RETURN RESULT

HANDLE HANDLE

37

Using the Active Object Pattern

in the Blob Server

:: Reactor Reactor : Handle : Handle Table Table

FRAMEWORKFRAMEWORK

LEVELLEVEL

KERNELKERNEL LEVELLEVEL

APPLICATIONAPPLICATION

LEVELLEVEL

1: handle_input() 1: handle_input() REGISTERED

OBJECTS

: Event : Event Handler Handler : Blob : Blob Handler Handler

svc_run

svc_run svc_runsvc_run

svc_run svc_run 4: getq(msg)

4: getq(msg) 5:svc(msg) 5:svc(msg)

: Event : Event Handler Handler : Blob : Blob Handler Handler

: Event : Event Handler Handler : Blob : Blob Handler Handler

2: recv_request(msg) 2: recv_request(msg) 3: putq(msg) 3: putq(msg)

: Blob : Blob Processor Processor : Message

: Message Queue Queue

OS EVENT DEMULTIPLEXING INTERFACE OS EVENT DEMULTIPLEXING INTERFACE

38

The Blob Processor Class

Processes Blob requests using the

\Thread-Pool" concurrency model

{ Implement the synchronous task portion of the

Half-Sync/Half-Async pattern

class Blob_Processor : public Task { public:

// Singleton access point.

static Blob_Processor *instance (void);

// Pass a request to the thread pool. virtual put (Message_Block *);

// Event loop for the pool thread virtual int svc (int) {

Message_Block *mb = 0; // Message buffer.

// Wait for messages to arrive. for (;;) {

getq (mb); // Inherited from class Task; // Identify and perform Blob Server // request processing here...

protected:

Blob_Processor (void); // Constructor.

Using the Singleton Pattern

The

Blob Processor

is implemented as a

Sin-gleton that is created \on demand"

Blob_Processor *

Blob_Processor::instance (void) { // Beware race conditions! if (instance_ == 0) {

instance_ = new Blob_Processor; }

return instance_; }

Constructor creates the thread pool

Blob_Processor::Blob_Processor (void) { Thread_Manager::instance ()->spawn_n

(11)

The Double-Checked Locking

Pattern

Intent

{ \Ensures atomic initialization of objects and

elim-inates unnecessary locking overhead on each ac-cess"

This pattern resolves the following forces:

1. Ensures atomic initialization or access to objects,regardless of thread scheduling order 2. Keeps locking overhead to a minimum

{ e.g., only lock on rst access

Note, this pattern assumes atomic memory

access

:::

41

Using the Double-Checked

Locking Pattern for the Blob

Server

Blob Processor

static instance() static instance() static instance_ static instance_

if (instance_ == NULL) { if (instance_ == NULL) { mutex_.acquire (); mutex_.acquire (); if (instance_ == NULL) if (instance_ == NULL) instance_ = new Blob_Processor; instance_ = new Blob_Processor; mutex_.release ();

mutex_.release (); }}

return instance_; return instance_;

Mutex Mutex

42

Half-Sync/Half-Async Pattern

Intent

{ \Decouples synchronous I/O from asynchronous

I/O in a system to simplify programming eort without degrading execution eciency"

This pattern resolves the following forces

for concurrent communication systems:

{ How to simplify programming for higher-level

com-munication tasks

. These are performed synchronously

{ How to ensure ecient lower-level I/O

communi-cation tasks

. These are performed asynchronously

Structure of the

Half-Sync/Half-Async Pattern

QUEUEINGQUEUEING LAYERLAYER

ASYNCHRONOUSASYNCHRONOUS TASK LAYER TASK LAYER SYNCHRONOUSSYNCHRONOUS TASK LAYER TASK LAYER

SSYNCYNC TASK TASK 11

SSYNCYNC TASK TASK 33

SSYNCYNC TASK TASK 22

1, 4: read(data) 1, 4: read(data)

3: enqueue(data) 3: enqueue(data)

2: interrupt 2: interrupt

ASYNC ASYNC TASK TASK

(12)

Collaborations in the

Half-Sync/Half-Async Pattern

EXTERNAL EVENT EXTERNAL EVENT

PROCESS MSG PROCESS MSG

read(msg)

EXECUTE TASK EXECUTE TASK ENQUEUE MSG ENQUEUE MSG

External External Event Source

Event Source AsyncAsyncTaskTask MessageMessageQueueQueue SyncSyncTaskTask

work()

DEQUEUE MSG DEQUEUE MSG

ASYNCASYNC PHASEPHASE

QUEUEINGQUEUEING PHASEPHASE

SYNCSYNC PHASEPHASE

RECV MSG RECV MSG

notification()

read(msg)

work() enqueue(msg)

This illustrates input processing (output

pro-cessing is similar)

45

Using the Half-Sync/Half-Async

Pattern in the Blob Server

ASYNC TASKASYNC TASK

LEVELLEVEL

SYNCH TASKSYNCH TASK

LEVELLEVEL

1: handle_input()

1: handle_input() :: Reactor Reactor

QUEUEINGQUEUEING LEVELLEVEL

svc_run

svc_run svc_runsvc_run svc_run

: Blob Processor

: Message Queue 4: getq(msg)

5:svc(msg)

: Blob : Blob Handler Handler

: Event : Event Handler Handler

: Event : Event Handler Handler : Blob : Blob Handler Handler

: Event : Event Handler Handler : Blob : Blob Handler

Handler 2: recv_request(msg)2: recv_request(msg) 3: putq(msg) 3: putq(msg)

46

Joining Async and Sync Tasks in

the Blob Server

The following methods form the boundary

between the Async and Sync layers

int

Blob_Handler::handle_input (void) {

Message_Block *mb = 0;

// Receive and frame message // (uses peer_stream_). recv_request (mb);

// Insert message into the Queue. Blob_Processor::instance ()->put (mb); }

// Task entry point.

Blob_Processor::put (Message_Block *msg) {

// Insert the message on the Message_Queue // (inherited from class Task).

putq (msg); }

The Acceptor Pattern

Intent

{ \Decouples passive initialization of a service from

the tasks performed once the service is initialized"

This pattern resolves the following forces

for network servers using interfaces like

sock-ets or TLI:

1. How to reuse passive connection establishment codefor each new service 2. How to make the connection establishment codeportable across platforms that may contain

sock-ets but not TLI, or vice versa

(13)

Structure of the Acceptor Pattern

ACTIVATES

Svc Handler Svc Handler

peer_stream_ open()

Acceptor Acceptor

peer_acceptor_ handle_input()

Svc Handler Svc Handler

Reactor Reactor

handle_input()

49

Collaboration in the Acceptor

Pattern

Server Server

REGISTER HANDLER REGISTER HANDLER

START EVENT LOOP START EVENT LOOP

CONNECTION EVENT CONNECTION EVENT

REGISTER HANDLER REGISTER HANDLER

FOR CLIENT FOR CLIENT I/OI/O FOREACH EVENT DO FOREACH EVENT DO EXTRACT HANDLE EXTRACT HANDLE INITIALIZE PASSIVE INITIALIZE PASSIVE

ENDPOINT ENDPOINT

acc : acc : Acceptor Acceptor

handle_input()

handle_close() reactor : reactor : Reactor Reactor

select() sh: sh: Svc_Handler Svc_Handler

handle_input()

get_handle()

EXTRACT HANDLE EXTRACT HANDLE

DATA EVENT DATA EVENT

CLIENT SHUTDOWN CLIENT SHUTDOWN

svc()

PROCESS MSG PROCESS MSG

open()

CREATE CREATE,, ACCEPT ACCEPT,, AND ACTIVATE OBJECT AND ACTIVATE OBJECT

SERVER SHUTDOWN

SERVER SHUTDOWN handle_close()

ENDPOINT

INITIALIZATION

PHASE

SERVICE

INITIALIZATION

PHASE

SERVICE

PROCESSING

PHASE

peer_acceptor_ peer_acceptor_

: SOCK : SOCK Acceptor Acceptor

handle_events() get_handle() register_handler(acc)

sh = make_svc_handler() accept_svc_handler (sh) activate_svc_handler (sh)

open()

register_handler(sh)

Acceptor

is a factory that creates, connects,

and activates a

Svc Handler

50

Using the Acceptor Pattern in the

Blob Server

PASSIVE LISTENER

ACTIVE CONNECTIONS

: Svc Handler

: Blob Handler

: Svc Handler : Blob Handler

: Svc Handler

: Blob Handler

: Svc Handler : Blob Handler : Acceptor

: Reactor

: Blob Acceptor

1: handle_input()

2: sh = make_svc_handler() 3: accept_svc_handler(sh) 4: activate_svc_handler(sh)

The Acceptor Class

The

Acceptor

class implements the Acceptor

pattern

// Reusable Factor

template <class SVC_HANDLER> class Acceptor :

public Service_Object // Subclass of Event_Handler. {

public:

// Notified by Reactor when clients connect. virtual int handle_input (void)

{

// The strategy for initializing a SVC_HANDLER. SVC_HANDLER *sh = new SVC_HANDLER;

peer_acceptor_.accept (*sh); sh->open ();

} // ...

protected:

(14)

The Blob Acceptor Class

Interface

The

Blob Acceptor

class accepts connections

and initializes

Blob Handlers class Blob_Acceptor

: public Acceptor<Blob_Handler>

// Inherits handle_input() strategy from Acceptor. {

public:

// Called when Blob_Acceptor is dynamically linked. virtual int init (int argc, char *argv);

// Called when Blob_Acceptor is dynamically unlinked. virtual int fini (void);

53

The Service Congurator Pattern

Intent

{ \Decouples the behavior of communication

ser-vices from the point in time at which these serser-vices are congured into an application or system"

This pattern resolves the following forces

for highly exible communication software:

{ How to defer the selection of a particular type, or

a particular implementation, of a service until very late in the design cycle

. i.e., at installation-time or run-time

{ How to build complete applications by composing

multiple independently developed services

{ How to optimize, recongure, and control the

be-havior of the service at run-time

54

Structure of the Service

Congurator Pattern

Reactor Reactor 11

nn Event Event Handler Handler

Concrete Concrete Service Object Service Object

REACTIVEREACTIVE LAYERLAYER

CONFIGURATIONCONFIGURATION

LAYERLAYER

APPLICATIONAPPLICATION

LAYERLAYER

11

11 Service Service Config Config

nn Service Service Object Object

A

suspend() suspend() resume() resume() init() init() fini() fini() info() info()

11 RepositoryRepositoryServiceService 11

Collaboration in the Service

Congurator Pattern

: Service : Service Config Config main()

main()

REGISTER SERVICE REGISTER SERVICE

START EVENT LOOP START EVENT LOOP

INCOMING EVENT INCOMING EVENT FOREACH EVENT DO FOREACH EVENT DO

STORE IN REPOSITORY STORE IN REPOSITORY CONFIGURE

CONFIGURE

FOREACH SVC ENTRY DO FOREACH SVC ENTRY DO

svc : svc : Service_Object Service_Object : Reactor: Reactor

run_event_loop()

handle_events() handle_input()

Service_Config()

: Service : Service Repository Repository

insert()

EXTRACT HANDLE EXTRACT HANDLE INITIALIZE SERVICE

INITIALIZE SERVICE init(argc, argv)

fini()

DYNAMICALLY LINK DYNAMICALLY LINK

SERVICE

SERVICE link_service()

unlink_service()

SHUTDOWN EVENT

SHUTDOWN EVENT handle_close()

UNLINK SERVICE UNLINK SERVICE

remove() register_handler(svc)

get_handle()

remove_handler(svc)

CONFIGURATIONCONFIGURATION

MODEMODE

EVENT HANDLING

MODE

process_directives()

(15)

Using the Service Congurator

Pattern in the Blob Server

: Service : Service Config Config

SERVICE SERVICE CONFIGURATOR CONFIGURATOR

RUNTIME RUNTIME

: Service : Service Repository Repository

: Reactor : Reactor : Service : Service Object Object : TP : TP Blob Server Blob Server

: Service : Service Object Object : TPR : TPR Blob Server Blob Server

SHARED SHARED OBJECTS OBJECTS

: Service : Service Object Object : Reactive : Reactive Blob Server Blob Server

Existing service is based on

Half-Sync/Half-Async pattern

Other versions could be single-threaded or

use other concurrency strategies

:::

57

The Blob Acceptor Class

Implementation

// Initialize service when dynamically linked.

int Blob_Acceptor::init (int argc, char *argv[]) {

Options::instance ()->parse_args (argc, argv);

// Set the endpoint into listener mode. Acceptor::open (local_addr);

// Initialize the communication endpoint. Reactor::instance ()->register_handler

(this, READ_MASK) }

// Terminate service when dynamically unlinked.

int Blob_Acceptor::fini (void) {

// Unblock threads in the pool so they will // shutdown correctly.

Blob_Processor::instance ()->close ();

// Wait for all threads to exit. Thread_Manager::instance ()->wait (); }

58

Conguring the Blob Server with

the Service Congurator

The concurrent Blob Server is congured

and initialized via a conguration script

% cat ./svc.conf

dynamic TP_Blob_Server Service_Object * blob_server.dll:make_TP_Blob_Server() "-p $PORT -t $THREADS"

Factory function that dynamically allocates

a Half-Sync/Half-Async Blob Server object

extern "C" Service_Object *make_TP_Blob_Server (void);

Service_Object *make_TP_Blob_Server (void) {

return new Blob_Acceptor;

// ACE dynamically unlinks and deallocates this object. }

Main Program for Blob Server

Dynamically congure and execute the Blob

Server

{ Note that this is totally generic!

int main (int argc, char *argv[]) {

Service_Config daemon;

// Initialize the daemon and dynamically // configure the service.

daemon.open (argc, argv);

// Loop forever, running services and handling // reconfigurations.

daemon.run_event_loop ();

(16)

The Connector Pattern

Intent

{ \Decouples active initialization of a service from

the task performed once a service is initialized"

This pattern resolves the following forces

for network clients that use interfaces like

sockets or TLI:

1. How to reuse active connection establishment codefor each new service 2. How to make the connection establishment codeportable across platforms that may contain

sock-ets but not TLI, or vice versa

3. How to enable exible service concurrency policies 4. How to actively establish connections with largenumber of peers eciently

61

Structure of the Connector

Pattern

Connector Connector

connect(sh, addr) complete()

ACTIVATES

HANDLE ASYNC CONNECTION COMPLETION

Service Service Handler Handler

peer_stream_ open()

Service Handler Service Handler

Reactor Reactor

11

nn

62

Collaboration in the Connector

Pattern

Client Client FOREACH CONNECTION FOREACH CONNECTION INITIATE CONNECTION INITIATE CONNECTION SYNC CONNECT SYNC CONNECT

INSERT IN REACTOR INSERT IN REACTOR

con : con : Connector Connector

handle_input() reactor : reactor : Reactor Reactor sh:

sh: Svc_Handler Svc_Handler

register_handler(sh) get_handle() EXTRACT HANDLE

EXTRACT HANDLE

DATA ARRIVES DATA ARRIVES

svc() PROCESS DATA

PROCESS DATA

connect(sh, addr)

connect() ACTIVATE OBJECT

ACTIVATE OBJECT

peer_stream_ peer_stream_ : SOCK : SOCK Connector Connector

SERVICESERVICE

PROCESSINGPROCESSING

PHASEPHASE

activate_svc_handler(sh) connect_svc_handler(sh, addr)

CONNECTIONCONNECTION

INITIATIONINITIATION

//

SEVICESEVICE

INITIALIZATIONINITIALIZATION PHASEPHASE

START EVENT LOOP START EVENT LOOP FOREACH EVENT DO FOREACH EVENT DO

handle_events() select() open()

Synchronous mode

Collaboration in the Connector

Pattern

Client Client

FOREACH CONNECTION FOREACH CONNECTION INITIATE CONNECTION INITIATE CONNECTION ASYNC CONNECT ASYNC CONNECT INSERT IN REACTOR INSERT IN REACTOR

START EVENT LOOP START EVENT LOOP

FOREACH EVENT DO FOREACH EVENT DO

handle_events() select()

CONNECTION COMPLETE CONNECTION COMPLETE

INSERT IN REACTOR INSERT IN REACTOR

con : con : Connector Connector

handle_input() reactor : reactor : Reactor Reactor sh:

sh: Svc_Handler Svc_Handler

handle_output()

register_handler(sh) get_handle()

EXTRACT HANDLE EXTRACT HANDLE

DATA ARRIVES DATA ARRIVES

svc()

PROCESS DATA PROCESS DATA

connect(sh, addr)

connect()

ACTIVATE OBJECT ACTIVATE OBJECT

register_handler(con) peer_stream_

peer_stream_ : SOCK : SOCK Connector Connector

CONNECTIONCONNECTION INITIATIONINITIATION

PHASEPHASE

SERVICESERVICE

INITIALIZATIONINITIALIZATION

PHASEPHASE

SERVICESERVICE

PROCESSINGPROCESSING

PHASEPHASE

activate_svc_handler(sh) connect_svc_handler(sh, addr)

open()

(17)

Using the Connector in the Blob

Clients

:: Reactor Reactor ACTIVE CONNECTIONS

: Svc : Svc Handler Handler : Blob : Blob Handler Handler

: Connector : Connector : Blob : Blob Connector Connector : Svc : Svc Handler Handler : Blob : Blob Handler Handler

: Svc : Svc Handler Handler : Blob : Blob Handler Handler

PENDING CONNECTIONS

: Svc : Svc Handler Handler : Blob : Blob Handler Handler

: Svc : Svc Handler Handler : Blob : Blob Handler Handler

: Svc : Svc Handler Handler : Blob : Blob Handler Handler

: Svc : Svc Handler Handler : Blob : Blob Handler Handler

65

Benets of Design Patterns

Design patterns enable large-scale reuse of

software architectures

Patterns explicitly capture expert knowledge

and design tradeos

Patterns help improve developer

communi-cation

Patterns help ease the transition to

object-oriented technology

66

Drawbacks to Design Patterns

Patterns do not lead to direct code reuse

Patterns are deceptively simple

Teams may suer from pattern overload

Patterns are validated by experience and

dis-cussion rather than by automated testing

Integrating patterns into a software

devel-opment process is a human-intensive

activ-ity

Suggestions for Using Patterns

Eectively

Do not recast everything as a pattern

{ Instead, develop strategic domain patterns and reuse

existing tactical patterns

Institutionalize rewards for developing

pat-terns

Directly involve pattern authors with

appli-cation developers and domain experts

Clearly document when patterns apply and

do not apply

(18)

Books and Magazines on Patterns

Books

{ Gamma et al., \Design Patterns: Elements of

Reusable Object-Oriented Software" Addison-Wesley, Reading, MA, 1994.

{ \Pattern Languages of Program Design," editors

James O. Coplien and Douglas C. Schmidt, Addison-Wesley, Reading, MA, 1995

Special Issues in Journals

{ \Theory and Practice of Object Systems" (guest

editor: Stephen P. Berczuk)

{ \Communications of the ACM" (guest editors:

Dou-glas C. Schmidt, Ralph Johnson, and Mohamed Fayad)

Magazines

{ C++ Report and Journal of Object-Oriented

Pro-gramming, columns by Coplien, Vlissides, and De Souza

69

Conferences and Workshops on

Patterns

1st EuroPLoP

{ July 10,14, 1996, Kloster Irsee, Germany

3rd Pattern Languages of Programs

Con-ference

{ September 4,6, 1996, Monticello, Illinois, USA

Relevant WWW URLs

http://www.cs.wustl.edu/~schmidt/jointPLoP,96.html/

http://st-www.cs.uiuc.edu/users/patterns/patterns.html

70

Obtaining ACE

The ADAPTIVE Communication

Environ-ment (ACE) is an OO toolkit designed

ac-cording to key network programming

pat-terns

All source code for ACE is freely available

{ Anonymously ftp towuarchive.wustl.edu { Transfer the les/languages/c++/ACE/*.gzand

gnu/ACE-documentation/*.gz

Mailing lists

* [email protected]

* [email protected] * [email protected]

* [email protected]

WWW URL

References

Related documents

calcium is removed by SERCA ten times more effectively than by NCX [198]. Of note, NCX function is dependent not only on its expression, but also on local Na + and Ca

The firm on its part is to provide employment opportunities, patronize the community’s human and physical resources, create enabling conditions for contingent enterprises to

In this passage it is precisely the term ‘fetish’ that Marx uses to indicate an eco- nomic process being considered ‘natural’, as if it were inscribed into the very structure

ALL patients with MTHFR C677T gene polymorphism were 4 times more likely to be resistant to MTX chemotherapy compared to patients who did not have the MTHFR C677T gene polymorphism

In a menu based access control, the user interface given to the user controls the operations that can be performed on the object, i.e., If A and B operations can be performed on

Its cre- ation involved solving many issues along the way: understanding the biological concepts fundamental to the understanding of the work; sampling the field of protein docking

Reason For Enquiry Continuing Credit Contract Association Principal's Account. Co Borrower

Enhanced Coalbed Meth recov &lt;1000 m Enhanced Coalbed Meth recov &gt;1000 m Enhanced Oil Recovery (offshore) Enhanced Oil Recovery (onshore).. The role of CCS in the fight