c++ network programming with patterns, frameworks, and …schmidt/pdf/ace-tutorial4.pdf · c++...
TRANSCRIPT
-
C++ Network Programming with Patterns,Frameworks, and ACE
Douglas C. SchmidtProfessor Department of [email protected] Vanderbilt Universitywww.cs.wustl.edu/schmidt/ (615) 343-8197
SponsorsNSF, DARPA, ATD, BBN, Boeing, Cisco, Comverse, GDIS, Experian, Global MT,
Hughes, Kodak, Krones, Lockheed, Lucent, Microsoft, Mitre, Motorola, NASA, Nokia,Nortel, OCI, Oresis, OTI, QNX, Raytheon, SAIC, Siemens SCR, Siemens MED,
Siemens ZT, Sprint, Telcordia, USENIX
Advanced ACE Tutorial Douglas C. Schmidt
Roadmap to Levels of Middleware
HHOOSSTT IINNFFRRAASSTTRRUUCCTTUURREE MMIIDDDDLLEEWWAARREE
DDIISSTTRRIIBBUUTTIIOONN MMIIDDDDLLEEWWAARREE
CCOOMMMMOONN MMIIDDDDLLEEWWAARREE SSEERRVVIICCEESS
AAPPPPLLIICCAATTIIOONNSS
HHAARRDDWWAARREE DDEEVVIICCEESS
WWTTSSHHUUDD
NNaavv
AAVVIIOONNIICCSSRREEPPLLIICCAATTIIOONN
SSEERRVVIICCEEDDOOMMAAIINN--SSPPEECCIIFFIICC MMIIDDDDLLEEWWAARREE SSEERRVVIICCEESS
OOPPEERRAATTIINNGG SSYYSSTTEEMMSS && PPRROOTTOOCCOOLLSS
EEVVEENNTTCCHHAANNNNEELL
CCoonnssCCoonnss
CCoonnss
www.cs.wustl.edu/schmidt/PDF/middleware-chapter.pdf
Observations
Historically, appsbuilt atop OS
Today, apps builtatop middleware
Middleware hasmultiple layers
Just like networkprotocol stacks
Vanderbilt University 1
Advanced ACE Tutorial Douglas C. Schmidt
Motivation for Concurrency
SERVER
WORK
REQUEST
WORK
REQUEST
WORK
REQUEST
WORK
REQUEST
CLIENT
CLIENT
CLIENT CLIENT
(2) CONCURRENT SERVER
maxfdp1
read_fds
WORK
REQUEST
SERVER
CLIENT
WORK
REQUEST
WORK
REQUEST
WORK
REQUESTCLIENT
CLIENT CLIENT
(1) ITERATIVE SERVER
Leverage hardware/software
e.g., multi-processors and OSthread support
Increase performance
e.g., overlap computation andcommunication
Improve response-time
e.g., GUIs and network servers
Simplify program structure
e.g., sync vs. async
Vanderbilt University 2
Advanced ACE Tutorial Douglas C. Schmidt
Motivation for DistributionPRINTER
FILESYSTEM
PRINTER FILE SYSTEM
COMPUTER
(1) STAND-ALONE APPLICATION ARCHITECTURE
(2) DISTRIBUTED APPLICATION ARCHITECTURE
CD ROM
CD ROM
NETWORKFI LE
SERVICE
CYCLE
SERVICE
DISPLAY
SERVICE
PRINT
SERVICE
NAME
SERVICE
TIME
SERVICE
Collaboration! connectivity andinterworking
Performance! multi-processingand locality
Reliability and availability!
replication
Scalability and portability!
modularity
Extensibility! dynamicconfiguration and reconfiguration
Cost effectiveness! opensystems and resource sharing
Vanderbilt University 3
-
Advanced ACE Tutorial Douglas C. Schmidt
Challenges and Solutions
Developing efficient, robust, and extensible concurrent networkingapplications is hard
e.g., must address complex topics that are less problematic or notrelevant for non-concurrent, stand-alone applications
OO techniques and OO language features help to enhance softwarequality factors
Key OO techniques include patterns and frameworks Key OO language features include classes, inheritance, dynamic
binding, and parameterized types Key software quality factors include modularity, extensibility,
portability, reusability, and correctness
Vanderbilt University 4
Advanced ACE Tutorial Douglas C. Schmidt
Caveats
OO is not a panacea
Though when used properly it helps minimize accidentalcomplexity and improve software quality factors
Its also essential to understand advanced OS features to enhancefunctionality and performance, e.g.,
Multi-threading Multi-processing Synchronization Shared memory Explicit dynamic linking Communication protocols and IPC mechanisms
Vanderbilt University 5
Advanced ACE Tutorial Douglas C. Schmidt
Tutorial Outline
Brief overview of key OO networking and concurrency concepts andOS platform mechanisms
Emphasis is on practical solutions
Examine a range of examples in detail
Networked Logging Service Concurrent Web Server Application-level Telecom Gateway Call Center Manager Event Server
Discuss general concurrent programming strategies
Provide URLs for further reading on the topic
Vanderbilt University 6
Advanced ACE Tutorial Douglas C. Schmidt
Software Development Environment The topics discussed here are largely independent of OS, network,
and programming language
Currently used successfully on UNIX/POSIX, Windows, andRTOS platforms, running on TCP/IP networks using C++
Examples are illustrated using freely available ADAPTIVECommunication Environment (ACE) OO framework components
Although ACE is written in C++, the principles covered in thistutorial apply to other OO languages
e.g., Java, Eiffel, Smalltalk, etc.
In addition, other networks and backplanes can be used, as well
Vanderbilt University 7
-
Advanced ACE Tutorial Douglas C. Schmidt
Sources of ComplexityPRINTER
FILESYSTEM
PRINTER FILE SYSTEM
COMPUTER
(1) STAND-ALONE APPLICATION ARCHITECTURE
(2) DISTRIBUTED APPLICATION ARCHITECTURE
CD ROM
CD ROM
NETWORKFI LE
SERVICE
CYCLE
SERVICE
DISPLAY
SERVICE
PRINT
SERVICE
NAME
SERVICE
TIME
SERVICE
Inherent complexity
Latency Reliability Synchronization Deadlock
Accidental Complexity
Low-level APIs Poor debugging tools Algorithmic
decomposition Continuous
re-invention
Vanderbilt University 8
Advanced ACE Tutorial Douglas C. Schmidt
Sources of Inherent Complexity
Inherent complexity results from fundamental domain challenges,e.g.:
Concurrent programming
Eliminating race conditions
Deadlock avoidance
Fair scheduling
Performance optimizationand tuning
Distributed programming
Addressing the impact of latency
Fault tolerance and high availability
Load balancing and servicepartitioning
Consistent ordering of distributedevents
Vanderbilt University 9
Advanced ACE Tutorial Douglas C. Schmidt
Sources of Accidental Complexity
Accidental complexity results from limitations with tools and techniquesused to develop concurrent applications, e.g.,
Lack of portable, reentrant, type-safe and extensible system callinterfaces and component libraries
Inadequate debugging support and lack of concurrent anddistributed program analysis tools
Widespread use of algorithmic decomposition
Fine for explaining concurrent programming concepts andalgorithms but inadequate for developing large-scale concurrentnetwork applications
Continuous rediscovery and reinvention of core concepts andcomponents
Vanderbilt University 10
Advanced ACE Tutorial Douglas C. Schmidt
OO Contributions to Concurrentand Distributed Applications
Concurrent network programming istraditionally performed usinglow-level OS mechanisms, e.g.,
fork/exec
Shared memory and semaphores
Memory-mapped files
Signals
sockets/select
Low-level thread APIs
Patterns and frameworks elevatedevelopment level to focus onapplication concerns, e.g.,
Service functionality andpolicies
Service configuration
Concurrent eventdemultiplexing and eventhandler dispatching
Service concurrency andsynchronization
Vanderbilt University 11
-
Advanced ACE Tutorial Douglas C. Schmidt
Overview of Patterns
Patterns represent solutions to problems that arise when developingsoftware within a particular context
i.e., Patterns == problem/solution pairs within a context
Patterns capture the static and dynamic structure and collaborationamong key participants in software designs
They are particularly useful for articulating how and why toresolve non-functional forces
Patterns facilitate reuse of successful software architectures anddesigns
Vanderbilt University 12 Adv
ance
dA
CE
Tuto
rial
Do
Exa
mpl
e:th
eP
roxy
Pat
tern
NE
TW
OR
KC
LIE
NT
SE
RV
ER
2: F
OR
WA
RD
RE
QU
ES
T3:
RE
SP
ON
SE
: QU
OTE
R
1: M
ET
HO
D C
AL
L
4: M
ET
HO
D R
ET
UR
N
: QU
OTE
R
PR
OX
Y
: BR
OK
ER
Inte
nt:
Pro
vide
asu
rrog
ate
for
anot
her
obje
ctth
atco
ntro
lsac
cess
toit
Van
derb
iltU
nive
rsity
Advanced ACE Tutorial Douglas C. Schmidt
Overview of Frameworks and Components
A framework is:
An integrated collection of components that collaborate toproduce a reusable architecture for a family of related applications
Frameworks differ from conventional class libraries:
1. Frameworks are semi-complete applications2. Frameworks address a particular application domain3. Frameworks provide inversion of control
Frameworks facilitate reuse of successful networked applicationsoftware designs and implementations
Applications inherit from and instantiate framework components
Vanderbilt University 14
Advanced ACE Tutorial Douglas C. Schmidt
Class Libraries versus Frameworks
NETWORKING
DATABASE
GUI
EVENT
LOOP
APPLICATION-SPECIFIC
FUNCTIONALITY
EVENT
LOOP
EVENT
LOOP
CALL
BACKSINVOKES
(A) CLASS LIBRARY ARCHITECTURE
(B) FRAMEWORK ARCHITECTURE
DATABASE
CLASSES
NETWORK
IPCCLASSES
MATH
CLASSES
ADTCLASSES
GUICLASSES
APPLICATION-SPECIFIC
FUNCTIONALITY
EVENT
LOOP
GLUE
CODE
LOCAL
INVOCATIONS
ADTCLASSES
MATH
CLASSES
Key distinctions
Class libraries
Reusable building blocks Domain-independent Limited in scope Passive
Frameworks
Reusable, semi-completeapplications
Domain-specific Broader in scope Active
Vanderbilt University 15
-
Advanced ACE Tutorial Douglas C. Schmidt
The ADAPTIVE Communication Environment (ACE)
PROCESSES/THREADS
DYNAMIC
LINKING
SHARED
MEMORYSELECT/IO COMP
FILE SYS
APIS
WIN32 NAMEDPIPES & UNIX
STREAM PIPES
UNIX
FIFOS
CAPIS
SOCKETS/TLI
COMMUNICATION
SUBSYSTEM
VIRTUAL MEMORY & FILESUBSYSTEM
GENERAL OPERATING SYSTEM SERVICES
PROCESS/THREADSUBSYSTEM
FRAMEWORK
LAYER
ACCEPTOR CONNECTOR
NETWORKED
SERVICE
COMPONENTS
LAYER
NAME
SERVER
TOKEN
SERVER
LOGGING
SERVER
GATEWAY
SERVER
SOCK SAP/TLI SAP
FIFO
SAP
LOG
MSG
SERVICE
HANDLER
TIME
SERVER
C++WRAPPER
FACADE
LAYER SPIPESAP
CORBA
HANDLER
FILE
SAP
SHARED
MALLOC
THE ACE ORB
(TAO)
JAWS ADAPTIVE
WEB SERVERSTANDARDS-BASED MIDDLEWARE
REACTOR/PROACTOR
PROCESS/THREAD
MANAGERS
STREAMS
SERVICE
CONFIG-URATOR
SYNCH
WRAPPERS
MEM
MAP
OS ADAPTATION LAYER
www.cs.wustl.edu/schmidt/ACE.html
Vanderbilt University 16
Advanced ACE Tutorial Douglas C. Schmidt
ACE Statistics
ACE library contains 250,000lines of C++
Over 40 person-years of effort
Ported to UNIX, Windows, MVS, andRT/embedded platforms
e.g., VxWorks, LynxOS, Chorus
Large user and open-sourcedeveloper community
schmidt/ACE-users.html
Currently used bydozens of companies
Bellcore, BBN,Boeing, Ericsson,Hughes, Kodak,Lockheed, Lucent,Motorola, Nokia,Nortel, Raytheon,SAIC, Siemens, etc.
Supported commerciallyby Riverace
www.riverace.com
Vanderbilt University 17
Advanced ACE Tutorial Douglas C. Schmidt
The Key Frameworks in ACE
Acceptor-Connector
Reactor Proactor
ServiceConfigurator
Streams Task
ACE contains a number of frameworks that can be used separatelyor together
This design permits fine-grained subsetting of ACE components
Subsetting helps minimize ACEs memory footprint $ACE_ROOT/doc/ACE-subsets.html
Vanderbilt University 18
Advanced ACE Tutorial Douglas C. Schmidt
Patterns for Communication Middleware
EventPatterns
ConcurrencyPatterns
ExternalPolymorphism
WrapperFacade
Connector
ThreadPool
Thread-perSession
Thread-perRequest
AsynchronousCompletion
Token
ThreadSpecificStorage
ActiveObject
Half-Sync/Half-Async
Leader/Followers
ComponentConfigurator
Object LifetimeManager
Reactor
Proactor
DoubleCheckedLocking
Thread-Safe
Interface
ScopedLocking
StrategizedLocking
InitializationPatterns
SynchronizationPatterns
AcceptorObservation
Failures rarely result fromunknown scientificprinciples, but from failingto apply provenengineering practices andpatterns
Benefits of Patterns
Facilitate design reuse
Preserve crucial designinformation
Guide design choices
Vanderbilt University 19
-
Advanced ACE Tutorial Douglas C. Schmidt
The ACE ORB (TAO)
NETWORK
ORB RUN-TIMESCHEDULER
operation()
IDLSTUBS
IDLSKELETON
in args
out args + returnvalue
CLIENT
OS KERNEL
HIGH-SPEEDNETWORK INTERFACE
REAL-TIME I/OSUBSYSTEM
OBJECT(SERVANT)
OS KERNEL
HIGH-SPEEDNETWORK INTERFACE
REAL-TIME I/OSUBSYSTEM
ACECOMPONENTS
OBJREF
REAL-TIME ORB COREIOP
PLUGGABLEORB & XPORTPROTOCOLS
IOPPLUGGABLE
ORB & XPORTPROTOCOLS
REAL-TIMEOBJECT
ADAPTER
www.cs.wustl.edu/schmidt/TAO.html
TAO Overview !
A real-time,high-performanceORB
Leverages ACE Runs on POSIX,
Windows,RTOSs
Related efforts !
QuO at BBN
MIC/GME atVanderbilt
XOTS
Vanderbilt University 20
Advanced ACE Tutorial Douglas C. Schmidt
TAO Statistics
TAO order of magnitude
Core ORB > 300,000 LOC IDL compiler > 200,000
LOC CORBA Object Services >
250,000 LOC Leverages ACE heavily
Ported to UNIX, Windows, &RT/embedded platforms
e.g., VxWorks, LynxOS,Chorus, WinCE
50 person-years of effort
Currently used by manycompanies
e.g., Boeing, BBN, Lockheed,Lucent, Motorola, Raytheon,SAIC, Siemens, etc.
Supported commercially by OCIand PrismTech
www.ociweb.com www.prismtechnologies.com
Vanderbilt University 21
Advanced ACE Tutorial Douglas C. Schmidt
JAWS Adaptive Web ServerWWWWWW
SERVERSERVER2: index.html2: index.html
1: GET ~schmidt1: GET ~schmidt
HTTP/1.0HTTP/1.0
COMMUNICATION PROTOCOLCOMMUNICATION PROTOCOL
((EE..GG.,., HTTP HTTP))
GUIGUI
HTMLHTMLPARSERPARSER
REQUESTERREQUESTER
GRAPHICSGRAPHICSADAPTERADAPTER
NETWORK
OS KERNEL
OS I/O SUBSYSTEM
NETWORK ADAPTERS
OS KERNEL
OS I/O SUBSYSTEM
NETWORK ADAPTERS
DISPATCHER
PROTOCOL
HANDLERS
WWW
CLIENTCLIENT
www.cs.wustl.edu/jxh/research/
JAWS Overview
A high-performanceWeb server
Flexible concurrencyand dispatchingmechanisms
Leverages the ACEframework
Ported to most OSplatforms
Used commercially byCacheFlow
www.cacheflow.com
Vanderbilt University 22
Advanced ACE Tutorial Douglas C. Schmidt
Java ACE
FRAMEWORKS
AND CLASS
CATEGORIES
DISTRIBUTED
SERVICES AND
COMPONENTSNAME
SERVER
TOKEN
SERVER
LOGGING
SERVER
TIME
SERVER
JAVAWRAPPERS
SYNCH
WRAPPERSSOCK_SAP THREAD
MANAGER
LOG
MSG
TIMER
QUEUE
SERVICE
CONFIGURATOR
ADAPTIVE SERVICE EXECUTIVE (ASX)
ACCEPTOR CONNECTORSERVICE
HANDLER
JAVA VIRTUAL MACHINE (JVM)
www.cs.wustl.edu/schmidt/JACE.htmlwww.cs.wustl.edu/schmidt/C++2java.
htmlwww.cs.wustl.edu/schmidt/PDF/
MedJava.pdf
Java ACEOverview
A Java versionof ACE
Used formedicalimagingprototype
Vanderbilt University 23
-
Adv
ance
dA
CE
Tuto
rial
Do
Net
wor
ked
Logg
ing
Ser
vice
P1
P2
P3
LOC
AL
IPC
CLI
EN
T
LOG
GIN
G
DA
EM
ON
P1
P2
P3
LOC
AL
IPC
CLI
EN
T
LOG
GIN
G
DA
EM
ON
NE
TW
OR
KS
TOR
AG
E
DE
VIC
E
HO
ST
AH
OS
T B
AB
SE
RV
ER
L
OG
GIN
G
DA
EM
ON
SE
RV
ER
CLI
EN
TH
OS
TA
RE
MO
TE I
PC
HO
ST
B
REMO
TE IPC
CLI
EN
T
CO
NS
OL
E
PR
INT
ER
Inte
nt:
Ser
ver
logg
ing
daem
onco
llect
s,fo
rmat
s,an
dou
tput
slo
ggin
gre
cord
sfo
rwar
ded
from
clie
ntlo
ggin
gda
emon
sre
sidi
ngth
roug
hout
ane
twor
kor
Inte
rnet
Van
derb
iltU
nive
rsity
Advanced ACE Tutorial Douglas C. Schmidt
Networked Logging Service Programming APIThe logging API is similar to printf() , e.g.:
ACE_ERROR ((LM_ERROR, "(%t) fork failed"));
Generates on logging server host:
Oct 31 14:50:13 [email protected]@2766@LM_ERROR@client::(4) fork failed
and
ACE_DEBUG ((LM_DEBUG,"(%t) sending to server %s", server_host));
generates on logging server host:
Oct 31 14:50:28 [email protected]@18352@LM_DEBUG@drwho::(6) sending to server bastille
Vanderbilt University 25
Advanced ACE Tutorial Douglas C. Schmidt
Conventional Logging Server Design
Typical algorithmic pseudo-code fornetworked logging server:
void logging_server (void) {initialize acceptor endpoint
loop forever {wait for eventshandle data eventshandle connection events
}}
The grand mistake:
Avoid the temptation tostep-wise refine thisalgorithmicallydecomposedpseudo-code directly intothe detailed design andimplementation of thelogging server!
Vanderbilt University 26 Adv
ance
dA
CE
Tuto
rial
Do
The
sele
ct()
-bas
edLo
ggin
gS
erve
rIm
plem
enta
tion
NE
TW
OR
K
SE
RV
ER
LO
GG
ING
D
AE
MO
N
maxh
andle
p1
rea
d_
ha
nd
les
CO
NN
EC
TIO
N
RE
QU
ES
T
LO
GG
ING
RE
CO
RD
SL
OG
GIN
G
RE
CO
RD
S
LO
GG
ING
RE
CO
RD
S
CL
IEN
T CL
IEN
TC
LIE
NT
CL
IEN
T
SE
RV
ER
acc
ep
tor
Ser
ializ
esse
rver
proc
essi
ngat
sele
ct()
dem
uxin
gle
vel
Van
derb
iltU
nive
rsity
-
Advanced ACE Tutorial Do
Conventional Logging ServerImplementation
Note the excessive amount of detail required toprogram at the socket level...
// Main programstatic const int PORT = 10000;
typedef u_long COUNTER;typedef int HANDLE;
// Counts the # of logging records processedstatic COUNTER request_count;
// Acceptor-mode socket handlestatic HANDLE acceptor;
// Highest active handle number, plus 1static HANDLE maxhp1;
// Set of currently active handlesstatic fd_set activity_handles;
// Scratch copy of activity_handlesstatic fd_set ready_handles;
Vanderbilt University
Advanced ACE Tutorial Do
Main Event Loop of Logging Server
int main (int argc, char *argv[]){
initialize_acceptor(argc > 1 ? atoi (argv[1]) : PORT);
// Loop forever performing logging// server processing.
for (;;) {// struct assignment.ready_handles = activity_handles;
// Wait for client I/O events.select (maxhp1, &ready_handles, 0, 0, 0);
// First receive pending logging records.handle_data ();
// Then accept pending connections.handle_connections ();
}}
Vanderbilt University
Advanced ACE Tutorial Do
Initialize Acceptor Socketstatic void initialize_acceptor (u_short port){
struct sockaddr_in saddr;
// Create a local endpoint of communication.acceptor = socket (PF_INET, SOCK_STREAM, 0);
// Set up the address info. to become server.memset ((void *) &saddr, 0, sizeof saddr);saddr.sin_family = AF_INET;saddr.sin_port = htons (port);saddr.sin_addr.s_addr = htonl (INADDR_ANY);
// Associate address with endpointbind (acceptor,
(struct sockaddr *) &saddr,sizeof saddr);
// Make endpoint listen for connection requests.listen (acceptor, 5);
// Initialize handle sets.FD_ZERO (&ready_handles);FD_ZERO (&activity_handles);FD_SET (acceptor, &activity_handles);maxhp1 = acceptor + 1;
}
Vanderbilt University
Advanced ACE Tutorial Do
Handle Data Processing
static void handle_data (void) {// acceptor + 1 is the lowest client handle
for (HANDLE h = acceptor + 1; h < maxhp1; h++)if (FD_ISSET (h, &ready_handles)) {
ssize_t n = handle_log_record (h, 1);
// Guaranteed not to block in this case!if (n > 0)
++request_count;// Count the # of logging records
else if (n == 0) {// Handle connection shutdown.FD_CLR (h, &activity_handles);close (h);if (h + 1 == maxhp1) {
// Skip past unused handleswhile (!FD_ISSET (--h,
&activity_handles))continue;
maxhp1 = h + 1;}
}}
}
Vanderbilt University
-
Advanced ACE Tutorial Do
Receive and Process Logging Records
static ssize_t handle_log_record (HANDLE in_h,HANDLE out_h) {
ssize_t n;size_t len;Log_Record lr;
// The first recv reads the length (stored as a// fixed-size integer) of adjacent logging record.
n = recv (in_h, (char *) &len, sizeof len, 0);if (n = maxhp1)
maxhp1 = h + 1;} while (select (acceptor + 1, &ready_handles,
0, 0, &poll_tv) == 1);}
Vanderbilt University
Advanced ACE Tutorial Do
Conventional Client LoggingDaemon Implementation
The main() method receives logging records fromclient applications and forwards them on to thelogging server
int main (int argc, char *argv[]){
HANDLE stream = initialize_stream_endpoint(argc > 1
? atoi (argv[1]): PORT);
Log_Record lr;
// Loop forever performing client// logging daemon processing.
for (;;) {// ... get logging records from client// application processes ...
size_t size = htonl (lr.size);send (stream, &size, sizeof size);encode_log_record (&lr);send (stream, ((char *) &lr), sizeof lr);
}}
Vanderbilt University
Advanced ACE Tutorial Do
Client Connection Establishment
static HANDLE initialize_stream_endpoint(const char *host, u_short port)
{struct sockaddr_in saddr;
// Create a local endpoint of communication.HANDLE stream = socket (PF_INET, SOCK_STREAM, 0);
// Set up the address info. to become client.memset ((void *) &saddr, 0, sizeof saddr);saddr.sin_family = AF_INET;saddr.sin_port = htons (port);hostent *hp = gethostbyname (host);memcpy ((void *) &saddr,
htonl (hp->h_addr),hp->h_length);
// Associate address with endpointconnect (stream,
(struct sockaddr *) &saddr,sizeof saddr);
return stream;}
Vanderbilt University
-
Advanced ACE Tutorial Douglas C. Schmidt
Limitations with Algorithmic DecompositionAlgorithmic decomposition tightly couples application-specificfunctionality and the following configuration-related characteristics:
Application Structure
The number of services per process Time when services are configured into a process
Communication and Demultiplexing Mechanisms
The underlying IPC mechanisms that communicate with otherparticipating clients and servers
Event demultiplexing and event handler dispatching mechanisms
Concurrency and Synchronization Model
The process and/or thread architecture that executes service(s) atrun-time
Vanderbilt University 36
Advanced ACE Tutorial Douglas C. Schmidt
Overcoming Limitations via OO
The algorithmic decomposition illustrated above specifies manylow-level details
Moreover, the excessive coupling impedes reusability,extensibility, and portability...
In contrast, OO focuses on application-specific behavior, e.g.,
int Logging_Handler::handle_input (void){
ssize_t n = handle_log_record (peer ().get_handle (),ACE_STDOUT);
if (n > 0)++request_count; // Count the # of logging records
return n
-
Advanced ACE Tutorial Douglas C. Schmidt
Summary of Pattern Intents
Wrapper Facade ! Encapsulates the functions and data providedby existing non-OO APIs within more concise, robust, portable,maintainable, and cohesive OO class interfaces
Reactor ! Demultiplexes and dispatches requests that aredelivered concurrently to an application by one or more clients
Acceptor ! Decouple the passive connection and initialization of apeer service in a distributed system from the processing performedonce the peer service is connected and initialized
Component Configurator ! Decouples the implementation ofservices from the time when they are configured
Active Object ! Decouples method execution from methodinvocation to enhance concurrency and simplify synchronizedaccess to an object that resides in its own thread of control
Vanderbilt University 40
Advanced ACE Tutorial Douglas C. Schmidt
Components in the OO Logging Server
Application-specific components
Process logging records received from clients
Connection-oriented application components
ACE_Svc_Handler (service handler)
Performs I/O-related tasks with clients ACE_Acceptor factory
Passively accepts connection requests
Dynamically creates a service handler for each client andactivates it
Application-independent ACE framework components
Perform IPC, explicit dynamic linking, event demultiplexing, eventhandler dispatching, multi-threading, etc.
Vanderbilt University 41
Adv
ance
dA
CE
Tuto
rial
Do
Cla
ssD
iagr
amfo
rO
OLo
ggin
gS
erve
r
Ser
vice
Con
figur
ator
Stre
amC
onne
ctio
n
Lo
ggin
gA
cce
pto
r
Logg
ing_
Han
dler
SO
CK
_Acc
epto
r
Logg
ing
Han
dlerS
OC
K_
Str
ea
mN
ull_
Syn
ch
SV
C_
HA
ND
LE
RP
EE
R_
AC
CE
PT
OR
PE
ER
_S
TR
EA
MS
YN
CH
_S
TR
AT
CONNECTION-ORIENTED
COMPONENTS
APPLICATION-SPECIFIC
COMPONENTS
ACE
FRAMEWORK
COMPONENTS
IPC
_SA
P
Rea
cto
r
>
1n
Con
curr
ency
PE
ER
AC
CE
PT
OR
PE
ER
ST
RE
AM
Svc
Ha
nd
ler
Acc
ep
tor
Van
derb
iltU
nive
rsity
Advanced ACE Tutorial Douglas C. Schmidt
Addressing Robustness, Portability,and Maintainability Challenges
Problem
Building distributed applications using low-level APIs is hard
Forces
Low-level APIs are verbose, tedious, and error-prone to program Low-level APIs are non-portable and non-maintainable
Solution
Apply the Wrapper Facade pattern to encapsulate low-levelfunctions and data structures
Vanderbilt University 43
-
Advanced ACE Tutorial Douglas C. Schmidt
The Wrapper Facade Pattern
Intent
Encapsulates the functionsand data provided byexisting lower-level,non-OO APIs within moreconcise, robust, portable,maintainable, and cohesivehigher-level OO classinterfaces
1: method_k()
2: function_k()
client
Functionsfunction_1()...function_n()
WrapperFacade
method_1()...method_m()
POSA2 (www.cs.wustl.edu/schmidt/POSA/ )
Forces Resolved
Avoid tedious, error-prone, and non-portable system APIs
Create cohesive abstractions
Vanderbilt University 44 Adv
ance
dA
CE
Tuto
rial
Do
Mot
ivat
ing
the
Wra
pper
Faca
deP
atte
rn:
the
Soc
ketA
PI
SE
RV
ER
CL
IEN
T
sock
et(
)bin
d()
(optio
nal)
connect
()se
nd
()/r
ecv
()
sock
et(
)b
ind
()lis
ten
()a
cce
pt(
)se
nd
()/r
ecv
()
2:
AC
TIV
E
RO
LE
3:
SE
RV
ICE
PR
OC
ES
SIN
Gcl
ose
()cl
ose
()
NE
TW
OR
K
1:
PA
SS
IVE
RO
LE
Soc
kets
are
the
mos
tcom
mon
netw
ork
prog
ram
min
gA
PIa
ndar
eav
aila
ble
onm
ostO
Spl
atfo
rms
Van
derb
iltU
nive
rsity
Advanced ACE Tutorial Douglas C. Schmidt
Problem with Sockets: Lack of Type-safetyint buggy_echo_server (u_short port_num){ // Error checking omitted.
sockaddr_in s_addr;int acceptor =
socket (PF_UNIX, SOCK_DGRAM, 0);s_addr.sin_family = AF_INET;s_addr.sin_port = port_num;s_addr.sin_addr.s_addr = INADDR_ANY;bind (acceptor, (sockaddr *) &s_addr,
sizeof s_addr);int handle = accept (acceptor, 0, 0);for (;;) {
char buf[BUFSIZ];ssize_t n = read (acceptor, buf, sizeof buf);if (n
-
Advanced ACE Tutorial Douglas C. Schmidt
Problem with Sockets: Portability
Having multiple standards, i.e., sockets and TLI, makes portabilitydifficult, e.g.,
May require conditional compilation In addition, related functions are not included in POSIX standards
e.g., select() , WaitForMultipleObjects() , and poll()
Portability between UNIX and Windows Sockets is problematic, e.g.:
Header files Error numbers Handle vs. descriptor types Shutdown semantics I/O controls and socket options
Vanderbilt University 48
Advanced ACE Tutorial Douglas C. Schmidt
Problem with Sockets: Poorly Structured
sock
et()
bin
d()
connec
t()
liste
n()
acce
pt(
)re
ad()
wri
te()
read
v()
wri
tev(
)re
cv()
send()
recv
from
()se
ndto
()re
cvm
sg()
sendm
sg()
sets
ock
opt(
)get
sock
opt(
)get
pee
rnam
e()
get
sock
nam
e()
get
host
byn
ame(
)get
serv
byn
ame(
)
Limitations
Socket API is linear rather than hierarchical
There is no consistency among names...
Non-portable
Vanderbilt University 49
Adv
ance
dA
CE
Tuto
rial
Do
Soc
ketT
axon
omy
XFE
RC
ON
NE
CTI
ON/
CO
MM
UN
ICA
TIO
N
RO
LE
LO
CA
LLO
CA
L/R
EM
OT
E
STREAMAC
TIV
E
PA
SS
IVE
DATAGRAM
CONNECTEDDATAGRAM
TYPE OF COMMUNICATION SERVICE
CO
MM
UN
ICA
TIO
N D
OM
AIN
se
nd
to()
/re
cvf
rom
()
so
ck
et(
PF
_U
NIX
)/b
ind
()
so
ck
et(
PF
_IN
ET
)/b
ind
()
sen
d()
/recv(
)s
oc
ke
t(P
F_
UN
IX)
bin
d()
/co
nn
ec
t()
so
ck
et(
PF
_U
NIX
)b
ind
()/l
iste
n()
/ac
ce
pt(
)
so
ck
et(
PF
_U
NIX
)b
ind
()/c
on
ne
ct(
)
sen
d()
/recv(
)
so
ck
et(
PF
_IN
ET
)b
ind
()/l
iste
n()
/ac
ce
pt(
)
so
ck
et(
PF
_IN
ET
)b
ind
()/c
on
ne
ct(
)
sen
d()
/recv(
)
so
ck
et(
PF
_U
NIX
)/b
ind
()
sen
d()
/recv(
)
se
nd
to()
/re
cvf
rom
()
so
ck
et(
PF
_IN
ET
)/b
ind
()
so
ck
et(
PF
_IN
ET
)b
ind
()/c
on
ne
ct(
)
so
ck
et(
PF
_U
NIX
)b
ind
()/c
on
ne
ct(
) s
oc
ke
t(P
F_
INE
T)
bin
d()
/co
nn
ec
t()
The
Soc
ketA
PIc
anbe
clas
sifie
dal
ong
thre
edi
men
sion
s
1.C
onne
ctio
nro
le
2.C
omm
unic
atio
ndo
mai
n
3.Ty
peof
serv
ice
Van
derb
iltU
nive
rsity
Adv
ance
dA
CE
Tuto
rial
Do
Sol
utio
n:A
CE
Soc
ketW
rapp
erFa
cade
s
XFE
R
LO
CA
LLO
CA
L/R
EM
OT
E
STREAMAC
TIV
E
PA
SS
IVE
DATAGRAM
CONNECTEDDATAGRAM
CO
MM
UN
ICA
TIO
N D
OM
AIN
LS
OC
K_
Co
nn
ec
tor
SO
CK
_C
on
ne
cto
r
SO
CK
_A
cc
ep
tor
LS
OC
K_
Ac
ce
pto
r
SO
CK
_D
gra
m_M
cast
LS
OC
K_
Str
ea
mS
OC
K_
Str
ea
m
LS
OC
K_
CO
Dg
ram
SO
CK
_C
OD
gra
m
SO
CK
_D
gra
m L
SO
CK
_D
gra
m
SO
CK
_Dgr
am
SO
CK
_D
gra
m_B
cast
TYPE OF COMMUNICATION SERVICE
CO
NNECTI
ON/
CO
MM
UNIC
ATI
ON
RO
LE
LS
OC
K_D
gram
The
AC
EC
++
wra
pper
faca
des
mor
eex
plic
itly
mod
elth
eke
yso
cket
com
pone
nts
usin
gO
Ocl
asse
s
Van
derb
iltU
nive
rsity
-
Adv
ance
dA
CE
Tuto
rial
Do
The
AC
EC
onne
ctio
n-O
rient
edS
ocke
tWra
pper
Faca
des
ACE_IPC_SAP
ACE_Addr
ACE_SOCK_IO
ACE_SOCK
ACE_SOCK_Acceptor
ACE_INET_Addr
ACE_SOCK_Stream
ACE_SOCK_Connector
Par
ticip
ants
Pas
sive
and
activ
eco
nnec
tion
fact
orie
s
AC
E_S
OC
K_A
ccepto
ran
dA
CE
_S
OC
K_C
onnect
or
Str
eam
ing
clas
ses
A
CE
_S
OC
K_S
tream
and
AC
E_S
OC
K_IO
Add
ress
ing
clas
ses
A
CE
_A
ddr
and
AC
E_IN
ET
_A
ddr
Van
derb
iltU
nive
rsity
Advanced ACE Tutorial Douglas C. Schmidt
The ACE Connection-Oriented SocketWrapper Facade Factories
class ACE_SOCK_Connector{public:
// Traitstypedef ACE_INET_Addr PEER_ADDR;typedef ACE_SOCK_Stream PEER_STREAM;
int connect(ACE_SOCK_Stream &new_sap,
const ACE_INET_Addr &raddr,ACE_Time_Value *timeout,const ACE_INET_Addr &laddr);
// ...};
class ACE_SOCK_Acceptor: public ACE_SOCK
{public:
// Traitstypedef ACE_INET_Addr PEER_ADDR;typedef ACE_SOCK_Stream PEER_STREAM;
ACE_SOCK_Acceptor (const ACE_INET_Addr &);int open (const ACE_INET_Addr &addr);int accept
(ACE_SOCK_Stream &new_sap,ACE_INET_Addr *,ACE_Time_Value *);
//...};
Vanderbilt University 53
Advanced ACE Tutorial Douglas C. Schmidt
ACE Connection-Oriented Socket Wrapper FacadeStreaming and Addressing Classes
class ACE_SOCK_Stream: public ACE_SOCK {
public:// Trait.typedef ACE_INET_Addr PEER_ADDR;ssize_t send (const void *buf,
int n);ssize_t recv (void *buf,
int n);ssize_t send_n (const void *buf,
int n);ssize_t sendv_n (const iovec *iov,
int n);ssize_t recv_n (void *buf, int n);int close (void);// ...
};
class ACE_INET_Addr: public ACE_Addr
{public:
ACE_INET_Addr (u_short port,const char host[]);
u_short get_port_number (void);ACE_UINT_32 get_ip_addr (void);// ...
};
Vanderbilt University 54
Advanced ACE Tutorial Douglas C. Schmidt
Design Interlude: Motivating theSocket Wrapper Facade Structure
Q: Why decouple the ACE_SOCK_Acceptor and theACE_SOCK_Connector from ACE_SOCK_Stream?
A: For the same reasons that ACE_Acceptor andACE_Connector are decoupled from ACE_Svc_Handler , e.g.,
An ACE_SOCK_Stream is only responsible for data transfer
Regardless of whether the connection is established passivelyor actively
This ensures that the ACE_SOCK*components arent usedincorrectly...
e.g., you cant accidentally read() or write() onACE_SOCK_Connectors or ACE_SOCK_Acceptors , etc.
Vanderbilt University 55
-
Advanced ACE Tutorial Do
An Echo Server Written usingACE C++ Socket Wrapper Facades
int echo_server (u_short port_num){
// Local server address.ACE_INET_Addr my_addr (port_num);
// Initialize the acceptor mode server.ACE_SOCK_Acceptor acceptor (my_addr);
// Data transfer object.ACE_SOCK_Stream new_stream;
// Accept a new connection.acceptor.accept (new_stream);
for (;;) {char buf[BUFSIZ];// Error caught at compile time!ssize_t n =
acceptor.recv (buf, sizeof buf);new_stream.send_n (buf, n);
}}
Vanderbilt University
Advanced ACE Tutorial Do
A Generic Version of the Echo Server
template int echo_server (u_short port){
// Local server address (note traits).typenameACCEPTOR::PEER_ADDR my_addr (port);
// Initialize the acceptor mode server.ACCEPTOR acceptor (my_addr);
// Data transfer object (note traits).typename ACCEPTOR::PEER_STREAM stream;
// Accept a new connection.acceptor.accept (stream);
for (;;) {char buf[BUFSIZ];ssize_t n =
stream.recv (buf, sizeof buf);stream.send_n (buf, n);
}}
Vanderbilt University
Advanced
AC
ETutorial
Douglas
C.S
chmidt
Scope
oftheA
CE
IPC
Wrapper
Facades
AACCEE
IIPPCC
SSAAPP
AA
SO
CK
ET
API
SSOOCCKK
SSAAPP
TLI
API
TTLLII
SSAAPP
STR
EA
M
PIP
E A
PI
SSPPIIPPEE
SSAAPP
NAM
ED
PIPE A
PI
FFIIFFOO
SSAAPP
SSSSLL
SSAAPP
SSL
API
MM
AP
API
MMEEMM
SSAAPP
SYSTE
M V
IPC A
PI
SSyyssVV
IIPPCC
C+
+N
Pv1
(ww
w.cs.w
ustl.e
du/sch
mid
t/AC
E/b
ook1
/)
VanderbiltU
niversity58
Advanced ACE Tutorial Do
Using the Wrapper Facade Patternfor the Logging Server
Note we havent improved the overall design (yet)
// ... Same as before ...
// Acceptor-mode socket handle.static ACE_SOCK_Acceptor acceptor;
// Set of currently active handlesstatic ACE_Handle_Set activity_handles;
// Scratch copy of activity_handlesstatic ACE_Handle_Set ready_handles;
static void initialize_acceptor (u_short port){
// Set up address info. to become server.ACE_INET_Addr saddr (port);
// Create a local endpoint of communication.acceptor.open (saddr);
// Set the into non-blocking mode.acceptor.enable (ACE_NONBLOCK);
activity_handles.set_bit (acceptor.get_handle ());}
Vanderbilt University
-
Advanced ACE Tutorial Do
Main Event Loop of Logging Server
int main (int argc, char *argv[]){
initialize_acceptor(argc > 1 ? atoi (argv[1]) : PORT);
// Loop forever performing logging// server processing.
for (;;) {// object assignment.ready_handles = activity_handles;
// Wait for client I/O events.ACE::select (int (maxhp1),
// calls operator fd_set *().ready_handles);
// First receive pending logging records.handle_data ();
// Then accept pending connections.handle_connections ();
}}
Vanderbilt University
Advanced ACE Tutorial Do
Handling Connections andData Processing
static void handle_connections (void) {if (ready_handles.is_set (acceptor.get_handle ()))
ACE_SOCK_Stream str;
// Handle all pending connection requests.while (acceptor.accept (str) != -1)
activity_handles.set_bit (str.get_handle ());}
}
static void handle_data (void) {ACE_HANDLE h;ACE_Handle_Set_Iterator iter (ready_handles);
while ((h = iter ()) != ACE_INVALID_HANDLE) {ACE_SOCK_Stream str (h);ssize_t n = handle_log_record (str, ACE_STDOUT);if (n > 0) // Count # of logging records.
++request_count;else if (n == 0) {
// Handle connection shutdown.activity_handles.clr_bit (h);s.close ();
}}
Vanderbilt University
Advanced ACE Tutorial Do
Receive and Process Logging Records
static ssize_t handle_log_record (ACE_SOCK_Stream s,ACE_HANDLE out_h)
ACE_UINT_32 len;ACE_Log_Record lr;
// The first recv reads the length (stored as a// fixed-size integer) of adjacent logging record.
ssize_t n = s.recv_n ((char *) &len, sizeof len);if (n sizeof (lr)) return -1;
// The second recv then reads bytes to// obtain the actual record.s.recv_n ((char *) &lr, sizeof lr);
// Decode and print record.decode_log_record (&lr);if (ACE_OS::write (out_h, lr.buf, lr.size) == -1)
return -1;else return 0;
}
Vanderbilt University
Advanced ACE Tutorial Do
OO Client LoggingDaemon Implementation
int main (int argc, char *argv[]){
ACE_SOCK_Stream stream;ACE_SOCK_Connector con; // Establish connection.con.connect (stream, ACE_INET_Addr (argc > 1
? atoi (argv[1]) : PORT));ACE_Log_Record lr;
// Loop forever performing client// logging daemon processing.for (;;) {
// ... get logging records from client// application processes ...ACE_UINT_32 size = lr.size;lr.size = htonl (lr.size);encode_log_record (&lr);iovec iov[2];iov[0].iov_len = sizeof (ACE_UINT_32);iov[0].iov_base = &lr.size;iov[1].iov_len = size;iov[1].iov_base = &lr;// Uses writev(2);stream.sendv_n (iov, 2);
}}
Vanderbilt University
-
Advanced ACE Tutorial Douglas C. Schmidt
Evaluating the Wrapper Facade Solution
Benefits
More concise
More robust
More portable
More maintainable
More efficient
Liabilities
Potentially more indirection
Additional learning curve
Still havent solved the overalldesign problem
i.e., the overall design isstill based on step-wiserefinement of functions
Vanderbilt University 64
Advanced ACE Tutorial Douglas C. Schmidt
ACE C++ Wrapper FacadeDesign Refactoring Principles
Enforce typesafety at compile-time
Allow controlled violations of typesafety
Simplify for the common case
Replace one-dimensional interfaces with hierarchical classcategories
Enhance portability with parameterized types
Inline performance critical methods
Define auxiliary classes to hide error-prone details
Vanderbilt University 65
Advanced ACE Tutorial Douglas C. Schmidt
Enforce Typesafety at Compile-Time
Sockets cannot detect certain errors at compile-time, e.g.,
int acceptor = socket (PF_INET, SOCK_STREAM, 0);// ...bind (acceptor, ...); // Bind address.listen (acceptor); // Make a acceptor-mode socket.HANDLE n_sd = accept (acceptor, 0, 0);// Error not detected until run-time.read (acceptor, buf, sizeof buf);
ACE enforces type-safety at compile-time via factories, e.g.:
ACE_SOCK_Acceptor acceptor (port);
// Error: recv() not a method of .acceptor.recv (buf, sizeof buf);
Vanderbilt University 66
Advanced ACE Tutorial Douglas C. Schmidt
Allow Controlled Violations of Typesafety
Make it easy to use the C++ Socket wrapper facades correctly, hard touse it incorrectly, but not impossible to use it in ways the classdesigners did not anticipate
e.g., it may be necessary to retrieve the underlying socket handle:
ACE_SOCK_Acceptor acceptor;
// ...
ACE_Handle_Set ready_handles;
// ...
if (ready_handles.is_set (acceptor.get_handle ())ACE::select (acceptor.get_handle () + 1, ready_handles);
Vanderbilt University 67
-
Advanced ACE Tutorial Douglas C. Schmidt
Supply Default Parameters
ACE_SOCK_Connector (ACE_SOCK_Stream &new_stream,const ACE_Addr &remote_sap,ACE_Time_Value *timeout = 0,const ACE_Addr &local_sap = ACE_Addr::sap_any,int protocol_family = PF_INET,int protocol = 0);
The result is extremely concise for the common case:
ACE_SOCK_Stream stream;
// Compiler supplies default values.ACE_SOCK_Connector con (stream, ACE_INET_Addr (port, host));
Vanderbilt University 68 Adv
ance
dA
CE
Tuto
rial
Do
Defi
neP
arsi
mon
ious
Inte
rfac
es
e.g.
,use
LS
OC
Kto
pass
sock
etha
ndle
s:
AC
E_LS
OC
K_S
tream
stre
am
;A
CE
_LS
OC
K_A
ccepto
racc
epto
r("
/tm
p/foo")
;
acc
epto
r.acc
ept
(str
eam
);st
ream
.send_handle
(str
eam
.get_
handle
());
vers
usth
ele
sspa
rsim
onio
usB
SD
4.3
sock
etco
de
AC
E_LS
OC
K::se
nd_handle
(const
AC
E_H
AN
DLE
sd)
const
{u_ch
ar
a[2
];io
vec
iov;
msg
hdr
send_m
sg;
a[0
]=
0xa
b,
a[1
]=
0xc
d;
iov.
iov_
base
=(c
har
*)a;
iov.
iov_
len
=si
zeof
a;
send_m
sg.m
sg_io
v=
&io
v;se
nd_m
sg.m
sg_io
vlen
=1;
send_m
sg.m
sg_nam
e=
(char
*)0;
send_m
sg.m
sg_nam
ele
n=
0;
send_m
sg.m
sg_acc
rights
=(c
har
*)&
sd;
send_m
sg.m
sg_acc
rights
len
=si
zeof
sd;
retu
rnse
ndm
sg(t
his
->get_
handle
(),
&se
nd_m
sg,
0);
Not
eth
atS
VR
4an
dB
SD
4.4
AP
Isar
edi
ffere
ntth
anB
SD
4.3!
Van
derb
iltU
nive
rsity
Advanced ACE Tutorial Douglas C. Schmidt
Combine Multiple Operations into One Operation
Creating a conventional acceptor-mode socket requires multiple calls:
int acceptor = socket (PF_INET, SOCK_STREAM, 0);sockaddr_in addr;memset (&addr, 0, sizeof addr);addr.sin_family = AF_INET;addr.sin_port = htons (port);addr.sin_addr.s_addr = INADDR_ANY;bind (acceptor, &addr, addr_len);listen (acceptor);// ...
ACE_SOCK_Acceptor combines this into a single operation:
ACE_SOCK_Acceptor acceptor ((ACE_INET_Addr) port);
Vanderbilt University 70
Advanced ACE Tutorial Douglas C. Schmidt
Create Hierarchical Class Categories
AACCEELLSSOOCCKK
AACCEEIIPPCCSSAAPP
GGRROOUUPP
CCOOMMMM
DDAATTAAGGRRAAMM
CCOOMMMM
SSTTRREEAAMM
CCOOMMMM
CCOONNNNEECCTTIIOONN
EESSTTAABBLLIISSHHMMEENNTT
AACCEESSOOCCKK
AACCEESSOOCCKK
CCOODDggrraamm
AACCEESSOOCCKKDDggrraamm
ACESOCKDgramBcast
AACCEESSOOCCKK
AAcccceeppttoorr
AACCEESSOOCCKK
CCoonnnneeccttoorr
AACCEESSOOCCKKSSttrreeaamm
AACCEELLSSOOCCKKCCOODDggrraamm
AACCEELLSSOOCCKKDDggrraamm
AACCEELLSSOOCCKK
CCoonnnneeccttoorr
AACCEELLSSOOCCKK
AAcccceeppttoorr
AACCEELLSSOOCCKKSSttrreeaamm
ACESOCKDgramMcast
Vanderbilt University 71
-
Advanced ACE Tutorial Douglas C. Schmidt
Enhance Portability with Parameterized Types
OS KERNELPROTOCOL MECHANISMS
(TCP/IP, OSI, ETC.)
USER
SPACE
DISTRIBUTED
APPLICATION 1APPLICATION1 DISTRIBUTED
APPLICATION 3APPLICATION3DISTRIBUTED
APPLICATION 2APPLICATION2
KERNEL
SPACE
BSD SOCKET
API
COMMON INTERFACE
(PARAMETERIZED TYPES)
SOCKET
API
SOCK_SAP
BSD SOCKET
API
SYSTEM VTLI API
TLI_SAP
NETWORK
INTERFACE
// Conditionally select IPC mechanism.#if defined (USE_SOCKETS)typedef ACE_SOCK_Acceptor PEER_ACCEPTOR;#elif defined (USE_TLI)typedef ACE_TLI_Acceptor PEER_ACCEPTOR;#endif // USE_SOCKETS.
int main (void){
// ...// Invoke with appropriate// network programming interface.echo_server (port);
}
Switching wholesale between sockets and TLI simply requiresinstantiating a different ACE C++ wrapper facade
Vanderbilt University 72
Advanced ACE Tutorial Douglas C. Schmidt
Inline Performance Critical Methods
Inlining is time and space efficient since key methods are very short:
class ACE_SOCK_Stream : public ACE_SOCK{public:
ssize_t send (const void *buf, size_t n){
return ACE_OS::send (this->get_handle (), buf, n);}
ssize_t recv (void *buf, size_t n){
return ACE_OS::recv (this->get_handle (), buf, n);}
};
Vanderbilt University 73
Advanced ACE Tutorial Douglas C. Schmidt
Define Auxiliary Classes to Hide Error-Prone Details
Standard C socket addressing is awkward and error-prone
e.g., easy to neglect to zero-out a sockaddr_in or convert portnumbers to network byte-order, etc.
ACE C++ Socket wrapper facades define classes to handle details
class ACE_INET_Addr : public ACE_Addr { public:ACE_INET_Addr (u_short port, long ip_addr = 0) {
memset (&this->inet_addr_, 0, sizeof this->inet_addr_);this->inet_addr_.sin_family = AF_INET;this->inet_addr_.sin_port = htons (port);memcpy (&this->inet_addr_.sin_addr, &ip_addr, sizeof ip_addr);
}// ...
private:sockaddr_in inet_addr_;
};
Vanderbilt University 74
Advanced ACE Tutorial Douglas C. Schmidt
Demultiplexing and Dispatching Events Problem
The logging server must process several different types of eventssimultaneously from different sources of events
Forces
Multi-threading is not always available Multi-threading is not always efficient Multi-threading can be error-prone Tightly coupling event demuxing with server-specific logic is
inflexible
Solution
Use the Reactor pattern to decouple event demuxing/dispatchingfrom server-specific processing
Vanderbilt University 75
-
Advanced ACE Tutorial Douglas C. Schmidt
The Reactor PatternReactor
handle_events()register_handler(h)remove_handler(h)
select (handles);foreach h in handles loop table[h].handle_event(type)end loop
Event Handlerhandle_event(type)get_handle()
handlers
Handle ownsuses
notifies
ConcreteEvent
Handler
Synchronous EventDemultiplexer
select()
1 N
www.cs.wustl.edu/schmidt/POSA/
Intent
Demuxes & dispatchesrequests that aredelievered concurrencyto an application by oneor more clients
Forces Resolved
Serially demux eventssynchronously &efficiently
Extend applicationswithout changingdemuxing code
Vanderbilt University 76
Advanced ACE Tutorial Douglas C. Schmidt
Collaboration in the Reactor Pattern
mainprogram
INITIALIZE
REGISTER HANDLER
callback :Concrete
Event_Handler
START EVENT LOOP
DATA ARRIVES
OK TO SEND
Reactor
handle_events()
FOREACH EVENT DO
handle_input()
select()
Reactor()
register_handler(callback)
handle_output()
SIGNAL ARRIVES
TIMER EXPIRES
handle_signal()
handle_timeout()
get_handle()EXTRACT HANDLE
REMOVE HANDLERremove_handler(callback)
INIT
IAL
IZA
TIO
N
MO
DE
EV
EN
T H
AN
DL
ING
MO
DE
handle_close()CLEANUP
Note inversion ofcontrol
Also note howlong-runningevent handlerscan degradequality of servicesince callbackssteal Reactorsthread ofcontrol...
Vanderbilt University 77
Adv
ance
dA
CE
Tuto
rial
Do
Str
uctu
rean
dIm
plem
enta
tions
ofth
eA
CE
Rea
ctor
Fra
mew
ork
Rea
ctor
fram
ewor
kpa
rtic
ipan
ts
AC
E_
Re
act
or
AC
E_
Eve
nt_
Ha
nd
ler
AC
E_
Tim
er_
Qu
eu
e
AC
E_
Tim
e_
Va
lue
Ap
plic
atio
n E
ven
tH
an
dle
r
0..1
Com
mon
Rea
ctor
impl
emen
tatio
nsin
AC
E
AC
E_
Re
act
or_
Imp
l
AC
E_
Se
lect
_R
ea
cto
r_Im
pl
AC
E_
TP
_R
ea
cto
r
AC
E_
WF
MO
_R
ea
cto
r
AC
E_
Se
lect
_R
ea
cto
r_T
TO
KE
N
AC
E_
Re
act
or
1
AC
E_
Se
lect
_R
ea
cto
r
b
ind
Van
derb
iltU
nive
rsity
Adv
ance
dA
CE
Tuto
rial
Do
Usi
ngth
eA
CE
Rea
ctor
Fra
mew
ork
inth
eLo
ggin
gS
erve
rR
EG
IST
ER
ED
OB
JEC
TS
FRAMEWORK
LEVEL
KERNEL
LEVEL
APPLICATION
LEVEL
1:
ha
nd
le_
inp
ut(
)
5:
ha
nd
le_
inp
ut(
)6
: re
cv(
ms
g)
7:p
roc
es
s(m
sg
)
2:
sh
= n
ew
Lo
gg
ing
_H
an
dle
r3
: a
cc
ep
t (s
h->
pe
er(
))4:
sh
->o
pen
()
OS
EV
EN
T D
EM
ULT
IPLE
XIN
G I
NT
ER
FA
CE
: R
ea
cto
r:T
imer
Qu
eue
: H
and
leTa
ble
: E
ven
tH
and
ler
: L
og
gin
gH
an
dle
r
: E
ven
tH
and
ler
: L
og
gin
gH
an
dle
r
: E
ven
tH
and
ler
: L
og
gin
gA
ccep
tor
Ben
efits
Str
aigh
tforw
ard
topr
ogra
m
Con
curr
ency
cont
rol
isea
sy
Liab
ilitie
s
Cal
lbac
ksar
eb
rittle
Can
tle
vera
gem
ulti-
proc
esso
rs
Van
derb
iltU
nive
rsity
-
Advanced
AC
ETutorial
Douglas
C.S
chmidt
Addressing
Acceptor
EndpointC
onnectionand
InitializationC
hallenges
Problem
T
hecom
munication
protocolusedbetw
eenapplications
isoften
orthogonaltoits
connectionestablishm
entandservice
handlerinitialization
protocols
Forces
Low
-levelconnectionA
PIs
areerror-prone
andnon-portable
S
eparatinginitialization
fromprocessing
increasessoftw
arereuse
Solution
U
sethe
Acceptor
patternto
decouplepassive
connectionestablishm
entandconnection
handlerinitialization
fromthe
subsequentloggingprotocol
VanderbiltU
niversity80
Advanced ACE Tutorial Do
The Acceptor-Connector Pattern(Acceptor Role)
SvcHandler
peer_stream_open()
Acceptorpeer_acceptor_accept()
Svc Handler
Reactor
APPLICATION-DEFINED
APPLICATION-INDEPENDENT
ACTIVATES
www.cs.wustl.edu/schmidt/POSA/
Intent of Acceptor Role
Decouple the passiveconnection andinitialization of a peerservice in a distributedsystem from theprocessing performedonce the peer service isconnected andinitialized
Forces resolved
Reuse passiveconnection setupand serviceinitialization code
Ensure thatacceptor-modehandles arent usedto read/write data
Vanderbilt University
Advanced ACE Tutorial Do
Structure of the ACEAcceptor-Connector Framework
ACE_Svc_Handler
PEER_STREAM,SYNCH_STRATEGY
ACE_Acceptor
SVC_HANDLER,PEER_ACCEPTOR
ACE_Connector
SVC_HANDLER,PEER_CONNECTOR
ApplicationService
bind
ACE_Event_Handler
ACE_Task
SYNCH_STRATEGY
Framework characteristics
Uses C++ parameterized types to strategize IPCand service aspects
Uses Template Method pattern to strategizecreation, connection establishment, andconcurrency policies
Vanderbilt University
Advanced ACE Tutorial Do
Using the ACE_Acceptorin the Logging Server
PASSIVE
LISTENER
ACTIVE
CONNECTIONS
1: handle_input()2: sh = make_svc_handler()3: accept_svc_handler(sh)4: activate_svc_handler(sh)
: Reactor
: Acceptor
: LoggingAcceptor
: SvcHandler
: LoggingHandler
: SvcHandler
: LoggingHandler
: SvcHandler
: LoggingHandler
: SvcHandler
: LoggingHandler
The ACE_Acceptor is a factory
i.e., it creates, connects, and activates anACE_Svc_Handler
Theres often one ACE_Acceptorper-service/per-port
Vanderbilt University
-
Advanced ACE Tutorial Do
ACE_Acceptor Class Public Interface
template // IPC aspect
class ACE_Acceptor : public ACE_Service_Object{
// Inherits indirectly from public:
// Initialization.virtual int open
(typename const PEER_ACCEPTOR::PEER_ADDR &,ACE_Reactor * = ACE_Reactor::instance ());
// Template Method.virtual int handle_input (ACE_HANDLE);
protected:// Factory method creates a service handler.
virtual SVC_HANDLER *make_svc_handler (void);// Accept a new connection.
virtual int accept_svc_handler (SVC_HANDLER *);// Activate a service handler.
virtual int activate_svc_handler (SVC_HANDLER *);
private:// Acceptor IPC connection strategy.
PEER_ACCEPTOR peer_acceptor_;};
Vanderbilt University
Advanced ACE Tutorial Do
ACE_Acceptor Class Implementation
// Shorthand names.#define SH SVC_HANDLER#define PA PEER_ACCEPTOR
// Template Method that creates, connects,// and activates service handlers.
template intACE_Acceptor::handle_input (ACE_HANDLE){
// Factory method that makes a service handler.
SH *svc_handler = make_svc_handler ();
// Accept the connection.
accept_svc_handler (svc_handler);
// Delegate control to the service handler.
activate_svc_handler (svc_handler);}
Vanderbilt University
Advanced
AC
ETutorial
Douglas
C.S
chmidt
The
Template
Method
Pattern
Co
ncrete
Class
prim
itive_op
eration
1()p
rimitive_o
peratio
n2()
...primitive
_opera
tion1()
...primitive
_opera
tion2()
...
Ab
stra
ct
Cla
ss
tem
pla
te_
me
tho
d()
prim
itive
_o
pe
ratio
n1
()p
rimitiv
e_
op
era
tion
2()
Intent
Define
theskeleton
ofan
algorithmin
anoperation,deferringsom
esteps
tosubclasses
Gam
ma
etal.,Design
Patterns:
Elem
entsof
Reusable
Object-O
rientedS
oftware
AW
,94
VanderbiltU
niversity86
Advanced
AC
ETutorial
Douglas
C.S
chmidt
Using
theTem
plateM
ethodP
atternin
theA
CE
Acceptor
Implem
entation
Ac
ce
pto
rhandle
_in
put()
make
_svc_
handle
r()acce
pt_
svc_handle
r()activa
te_svc_
handle
r()
...m
ake
_svc_
handle
r()...acce
pt_
svc_handle
r()...activa
te_svc_
handle
r()
My
Accep
tor
make_svc_h
and
ler()activate_svc_h
and
ler()
Benefits
Straightforw
ardto
programvia
inheritanceand
dynamic
binding
Liabilities
Design
isbrittle
andcan
causeexplosion
ofsubclasses
dueto
whitebox
design
VanderbiltU
niversity87
-
Advanced
AC
ETutorial
Douglas
C.S
chmidt
The
Strategy
Pattern
Strateg
yalg
orith
m_in
terface()
Co
ncrete
Strateg
y A
algo
rithm
_interface() ST
RA
TE
GY
Co
ncrete
Strateg
y B
algo
rithm
_interface() C
on
creteS
trategy C
algo
rithm
_interface()
Co
ntext
context_interface()
Intent
Define
afam
ilyof
algorithms,encapsulate
eachone,and
make
theminterchangeable
Gam
ma
etal.,Design
Patterns:
Elem
entsof
Reusable
Object-O
rientedS
oftware
AW
,94
VanderbiltU
niversity88
Advanced
AC
ETutorial
Douglas
C.S
chmidt
Using
theS
trategyP
atternin
theA
CE
Acceptor
Implem
entation
Th
readS
trategy
Pro
cessS
trategy
Reactive
Strateg
y
Ac
ce
pto
rh
an
dle
_in
pu
t()
sh
= c
rea
te_
svc
_h
an
dle
r ()...a
cc
ep
t_s
vc_
ha
nd
ler (s
h)
...1
: ac
tivate
_s
vc_
ha
nd
ler(s
h)
...
2: a
ctiva
te_
svc
_h
an
dle
r(sh
)
activate_svc_han
dler()
Co
nc
urre
nc
yS
trate
gy
Benefits
More
extensibledue
toblackbox
design
Liabilities
More
complex
andharder
todevelop
initially
VanderbiltU
niversity89
Advanced ACE Tutorial Do
ACE_Acceptor Template MethodHook Implementations
Template method hooks can be overridden
// Factory method for creating a service handler.template SH *ACE_Acceptor::make_svc_handler (ACE_HANDLE)
return new SH; // Default behavior.}
// Accept connections from clients.template intACE_Acceptor::accept_svc_handler (SH *sh){
peer_acceptor_.accept (sh->peer ());}
// Activate the service handler.template intACE_Acceptor::activate_svc_handler (SH *sh){
if (sh->open () == -1)sh->close ();
}
Vanderbilt University
Advanced ACE Tutorial Do
ACE_Acceptor InitializationImplementation
Note how the PEER_ACCEPTORs open() methodhides all the details associated with passivelyinitializing communication endpoints
// Initialization.
template intACE_Acceptor::open
(typename const PA::PEER_ADDR &addr,ACE_Reactor *reactor)
{// Forward initialization to the concrete// peer acceptor.peer_acceptor_.open (addr);
// Register with Reactor.reactor->register_handler
(this, ACE_Event_Handler::ACCEPT_MASK);}
Vanderbilt University
-
Advanced ACE Tutorial Do
ACE_Svc_Handler Class PublicInterface
Note how IPC and synchronization aspects arestrategized
template // Synch aspect
class ACE_Svc_Handler: public ACE_Task
// Task is-a Service_Object,// which is-an Event_Handler{public:
// Constructor.ACE_Svc_Handler (Reactor * =
ACE_Reactor::instance ());// Activate the handler (called by the
. // or ).virtual int open (void *);
// Return underlying IPC mechanism.PEER_STREAM &peer (void);
// ...private:
PEER_STREAM peer_; // IPC mechanism.virtual ACE_Svc_Handler (void);
};
Vanderbilt University
Advanced
AC
ETutorial
Douglas
C.S
chmidt
AC
E_S
vc_H
andle
rIm
plementation
#defin
eP
SP
EE
R_S
TR
EA
M#defin
eS
SS
YN
CH
_S
TR
AT
tem
pla
te
AC
E_S
vc_H
andle
r::A
CE
_S
vc_H
andle
r(A
CE
_R
eacto
r*r):
AC
E_S
ervice
_O
bje
ct(r)
{}te
mpla
te
int
AC
E_S
vc_H
andle
r::o
pen
(void
*){
//E
nable
non-b
lockin
gI/O
.peer
().enable
(AC
E_N
ON
BLO
CK
);
//R
egiste
rhandle
rw
ithth
eR
eacto
r.re
acto
r()->
registe
r_handle
r(th
is,A
CE
_E
vent_
Handle
r::RE
AD
_M
AS
K);
}
By
default,aA
CE
_S
vc_H
andle
robjectis
registeredw
iththe
singletonA
CE
_R
eacto
r
T
hism
akesthe
servicereactive
sothatno
othersynchronizationm
echanisms
arenecessary
VanderbiltU
niversity93
Advanced ACE Tutorial Do
Object Diagram for OO Logging Server
ServiceConfig
SERVER
SERVERLOGGINGDAEMON
CONNECTIONREQUEST
REMOTECONTROL
OPERATIONS
CLIENT
LOGGINGRECORDS
CLIENT CLIENTCLIENT
LoggingHandler
LoggingHandler
LoggingAcceptor
ServiceManager
ServiceRepository
Reactor
Vanderbilt University
Advanced ACE Tutorial Do
The Logging_Handler andLogging_Acceptor Classes
// Performs I/O with client logging daemons.
class Logging_Handler : publicACE_Svc_Handler
{public:
// Recv and process remote logging records.virtual int handle_input (ACE_HANDLE);
};
// Logging_Handler factory.
class Logging_Acceptor : publicACE_Acceptor
{public:
// Dynamic linking hooks.virtual int init (int argc, char *argv[]);virtual int fini (void);
};
Vanderbilt University
-
Advanced ACE Tutorial Douglas C. Schmidt
Design Interlude: Parameterizing IPC Mechanisms
Q: How can you switch between different IPC mechanisms?
A: By parameterizing IPC Mechanisms with C++ Templates, e.g.:
#if defined (ACE_USE_SOCKETS)typedef ACE_SOCK_Acceptor PEER_ACCEPTOR;#elif defined (ACE_USE_TLI)typedef ACE_TLI_Acceptor PEER_ACCEPTOR;#endif /* ACE_USE_SOCKETS */
class Logging_Handler : publicACE_Svc_Handler{ /* ... /* };
class Logging_Acceptor : publicACE_Acceptor
{ /* ... */ };
Vanderbilt University 96
Advanced ACE Tutorial Douglas C. Schmidt
Logging_Handler Input Method
Callback routine that receives logging records
intLogging_Handler::handle_input (ACE_HANDLE){
// Call existing function to recv// logging record and print to stdout.ssize_t n =
handle_log_record (peer ().get_handle (),ACE_STDOUT);
if (n > 0)// Count the # of logging records++request_count;
return n < = 0 ? -1 : 0;}
Implementation ofapplication-specificlogging method
This is the main codesupplied by adeveloper!
Vanderbilt University 97
Adv
ance
dA
CE
Tuto
rial
Do
Loggin
g_A
ccepto
rIn
itial
izat
ion
and
Term
inat
ion
//A
uto
matic
ally
calle
dw
hen
aLoggin
g_A
ccepto
r//
obje
ctis
linke
ddyn
am
ically
.
Loggin
g_A
ccepto
r::in
it(int
arg
c,ch
ar
*arg
v[])
{A
CE
_G
et_
Opt
get_
opt
(arg
c,arg
v,"p
:",
0);
AC
E_IN
ET
_A
ddr
addr
(DE
FA
ULT
_P
OR
T);
for
(int
c;(c
=get_
opt
())
!=-1
;)
switc
h(c
){
case
p:
addr.
set
(ato
i(g
eto
pt.opta
rg))
;bre
ak;
defa
ult:
bre
ak;
}//
Initi
aliz
eendpoin
tand
regis
ter
//w
ithth
e
.open
(addr,
AC
E_R
eact
or:
:inst
ance
());
} //A
uto
matic
ally
calle
dw
hen
obje
ctis
unlin
ked.
Loggin
g_A
ccepto
r::fin
i(v
oid
){
handle
_cl
ose
();
}
Van
derb
iltU
nive
rsity
Advanced ACE Tutorial Douglas C. Schmidt
Putting the Pieces Together at Run-time Problem
Prematurely committing ourselves to a particular logging serverconfiguration is inflexible and inefficient
Forces
It is useful to build systems by scripting components Certain design decisions cant be made efficiently until run-time It is a bad idea to force users to pay for components they do not
use
Solution
Use the Component Configurator pattern to assemble the desiredlogging server components dynamically
Vanderbilt University 99
-
Advanced ACE Tutorial Douglas C. Schmidt
The Component Configurator Pattern
Reactor1n
ConcreteComponent
RE
AC
TIV
EL
AY
ER
CO
NF
IGU
RA
TIO
NL
AY
ER
AP
PL
ICA
TIO
NL
AY
ER
1 1
ComponentConfig
n
Component
A
suspend()resume()init()fini()info()
1
ComponentRepository
1
EventHandler
Intent
Decouples theimplementation of servicesfrom the time when they areconfigured
Forces Resolved
Reduce resource utilization
Support dynamic(re)configuration
www.cs.wustl.edu/schmidt/POSA/
Vanderbilt University 100 Adv
ance
dA
CE
Tuto
rial
Do
Str
uctu
reof
the
AC
ES
ervi
ceC
onfig
urat
orF
ram
ewor
k
AC
E_
Se
rvic
e_
Ob
ject
AC
E_
Se
rvic
e_
Co
nfig
AC
E_
Se
rvic
e_
Re
po
sito
ry
AC
E_
Se
rvic
e_
Re
po
sito
ry_
Ite
rato
r
AC
E_
Eve
nt_
Ha
nd
ler
Ap
plic
atio
n S
erv
ice
Fra
mew
ork
char
acte
ristic
s
AC
E_S
erv
ice_C
onfig
uses
ava
riant
ofth
eM
onos
tate
patte
rn
Can
beac
cess
edei
ther
via
asc
ripto
rpr
ogra
mm
atic
ally
Van
derb
iltU
nive
rsity
Adv
ance
dA
CE
Tuto
rial
Do
Usi
ngth
eA
CE
Ser
vice
Con
figur
ator
Fra
mew
ork
for
the
Logg
ing
Ser
ver
SE
RV
ICE
CO
NF
IGU
RA
TO
R
RU
NT
IME
Se
rvic
eR
ep
os
ito
ry
Se
rvic
eO
bje
ct
Th
rea
dP
oo
lL
og
ge
r
DL
LS
Se
rvic
eO
bje
ct
Th
rea
dL
og
ge
r
dyn
am
ic L
ogge
r S
erv
ice
_O
bje
ct *
lo
gge
r:m
ake
_L
ogge
r()
"-p
20
01
"sv
c.co
nf
FIL
E
Se
rvic
eO
bje
ct
Re
ac
tive
Lo
gg
er
Re
ac
tor
Se
rvic
eC
on
fig
The
exis
ting
Logg
ing
Ser
ver
serv
ice
issi
ngle
-thr
eade
d
Oth
erve
rsio
nsco
uld
bem
ulti-
thre
aded
Not
eho
ww
eca
nsc
riptt
his
via
the
svc.
conf
file
Van
derb
iltU
nive
rsity
Advanced ACE Tutorial Douglas C. Schmidt
Dynamically Linking a Service
Dynamically linked factory functionthat allocates a newLogging_Acceptor
extern "C"ACE_Service_Object *make_Logger (void);
ACE_Service_Object *make_Logger (void){
return new Logging_Acceptor;// Framework automatically// deletes memory.
}
Application-specific factoryfunction used to dynamicallycreate a service
The make_Logger() functionprovides a hook between anapplication-specific serviceand theapplication-independent ACEmechanisms
ACE handles all memoryallocation and deallocation
Vanderbilt University 103
-
Advanced ACE Tutorial Douglas C. Schmidt
Service Configuration
The logging service is configuredvia scripting in a svc.conf file:
% cat ./svc.conf# Dynamically configure# the logging servicedynamic LoggerService_Object *logger:_make_Logger() "-p 2001"# Note, .dll or .so suffix# added to the logger# automatically
Generic event-loop todynamically configure servicedaemons
int main (int argc, char *argv[]){
// Initialize the daemon and// configure servicesACE_Service_Config::open (argc,
argv);// Run forever, performing the// configured servicesACE_Reactor::instance ()->
run_reactor_event_loop ();/* NOTREACHED */
}
Vanderbilt University 104
Advanced ACE Tutorial Douglas C. Schmidt
State Chart for the Service Configurator Framework
INITIALIZED
CONFIGURE/Service_Config::process_directives()
NETWORK EVENT/Reactor::dispatch()
RECONFIGURE/Service_Config::process_directives()
SHUTDOWN/Service_Config::close() AWAITING
EVENTS
CALL HANDLER/Event_Handler::handle_input()
IDLE
PERFORMCALLBACK
START EVENT LOOP/Reactor::run_event_loop()
Vanderbilt University 105
Advanced ACE Tutorial Douglas C. Schmidt
Advantages of OO Logging Server
The OO architecture illustrated thus far decouplesapplication-specific service functionality from:
Time when a service is configured into a process The number of services per-process The type of IPC mechanism used The type of event demultiplexing mechanism used
We can use the techniques discussed thus far to extend applicationswithout:
Modifying, recompiling, and relinking existing code Terminating and restarting executing daemons
The remainder of the Logging Server slides examine a set oftechniques for decoupling functionality from concurrencymechanisms, as well
Vanderbilt University 106
Advanced ACE Tutorial Douglas C. Schmidt
Concurrent OO Logging Server The structure of the Logging Server can benefit from concurrent
execution on a multi-processor platform
This section examines ACE C++ classes and patterns that extendthe logging server to incorporate concurrency
Note how most extensions require minimal changes to theexisting OO architecture...
This example also illustrates additional ACE components involvingsynchronization and multi-threading
Vanderbilt University 107
-
Adv
ance
dA
CE
Tuto
rial
Do
Con
curr
entO
OLo
ggin
gS
erve
rA
rchi
tect
ure
NE
TW
OR
K
SE
RV
ER
LO
GG
ING
S
ER
VE
R
Logg
ing
Han
dler
Logg
ing
Han
dler
Logg
ing
Acc
epto
r
1: S
OC
K
Acc
epto
r2:
acc
ept(
)4:
han
dle_
inpu
t()
5: s
paw
n()
3:
con
ne
ct()
6:
sen
d()
7: r
ecv
()8:
writ
e()
CL
IEN
TA
CL
IEN
TB
6:
sen
d()
7: r
ecv
()8:
writ
e()
Rea
ctor
Run
sea
chcl
ient
conn
ectio
nin
ase
para
teth
read
Van
derb
iltU
nive
rsity
Advanced ACE Tutorial Douglas C. Schmidt
Pseudo-code for Concurrent Server
Pseudo-code for multi-threaded Logging_Handler factoryLogging Server
void handler_factory (void) {initialize acceptor endpointforeach (pending connection event) {
accept connectionspawn a thread to handle connection andrun logging_handler() entry point
}}
Pseudo-code for logging_handler() function
void logging_handler (void) {foreach (incoming logging records from client)
call handle_log_record()exit thread
}
Vanderbilt University 109
Advanced ACE Tutorial Douglas C. Schmidt
Concurrency Overview
THREADS
PROCESS
SHARED ADDRESS SPACE
A thread is a sequence ofinstructions executed in oneor more processes
One process !
stand-alone systems More than one process !
distributed systems
Traditional OS processes contain a single thread of control
This simplifies programming since a sequence of execution steps isprotected from unwanted interference by other executionsequences...
Vanderbilt University 110
Advanced ACE Tutorial Douglas C. Schmidt
Traditional Approaches to OS Concurrency
1. Device drivers and programs with signal handlers utilize a limitedform of concurrency
e.g., asynchronous I/O
Note that concurrency encompasses more than multi-threading...
2. Many existing programs utilize OS processes to providecoarse-grained concurrency
e.g., Client/server database applications Standard network daemons like UNIX INETD
Multiple OS processes may share memory via memory mappingor shared memory and use semaphores to coordinate execution
The OS kernel scheduler dictates process behavior
Vanderbilt University 111
-
Advanced ACE Tutorial Douglas C. Schmidt
Evaluating Traditional OS Process-based Concurrency
Advantages
Easy to keep processes from interfering
A process combines security, protection, and robustness
Disadvantages
Complicated to program, e.g.,
Signal handling may be tricky Shared memory may be inconvenient
Inefficient
The OS kernel is involved in synchronization and processmanagement
Difficult to exert fine-grained control over scheduling and priorities
Vanderbilt University 112
Advanced ACE Tutorial Douglas C. Schmidt
Modern OS Concurrency
Modern OS platforms typically provide a standard set of APIs thathandle
Process/thread creation and destruction Various types of process/thread synchronization and mutual
exclusion Asynchronous facilities for interrupting long-running
processes/threads to report errors and control program behavior
Once the underlying concepts are mastered, its relatively easy tolearn different concurrency APIs
e.g., traditional UNIX process operations, Solaris threads, POSIXpthreads, WIN32 threads, Java threads, etc.
Vanderbilt University 113
Advanced ACE Tutorial Douglas C. Schmidt
Lightweight Concurrency
Modern operating systems provide lightweight mechanisms thatmanage and synchronize multiple threads within a process
Some systems also allow threads to synchronize across multipleprocesses
Benefits of threads
1. Relatively simple and efficient to create, control, synchronize, andcollaborate Threads share many process resources by default
2. Improve performance by overlapping computation andcommunication Threads may also consume less resources than processes
3. Improve program structure e.g., compared with using asynchronous I/O
Vanderbilt University 114 Adv
ance
dA
CE
Tuto
rial
Do
Exa
mpl
e:S
ingl
e-th
read
edvs
.M
ulti-
thre
aded
App
licat
ions
MU
LT
I-T
HR
EA
DE
D R
PC
CL
IEN
TS
ER
VE
RC
LIE
NT
USERKERNEL
TH
RE
AD
BL
OC
KE
D
USERKERNEL
SE
RV
ICE
EX
EC
UT
ES
RE
QU
ES
T
RE
SP
ON
SE
SE
RV
ER
CL
IEN
T
USERKERNEL
USERKERNEL
SE
RV
ICE
EX
EC
UT
ES
RE
QU
ES
T
RE
SP
ON
SE
SE
RV
ER
USERKERNEL
SE
RV
ICE
EX
EC
UT
ES
RE
QU
ES
T
RE
SP
ON
SE
SIN
GL
E-
TH
RE
AD
ED
R
PC
Van
derb
iltU
nive
rsity
-
Advanced ACE Tutorial Douglas C. Schmidt
Hardware and OS Concurrency Support
THREAD
PE
PROCESSINGELEMENT
LIGHTWEIGHTPROCESS
LWPUNIX
PROCESS
PE PE PE PEPE PE PE PE
SHARED MEMORY
KE
RN
EL-L
EV
EL
US
ER-L
EV
EL
LWP LWP LWPLWP LWP LWP LWP
Four typicalabstractions
1. Applicationthreads
2. Lightweightprocesses
3. Kernel threads
4. Processingelements
Vanderbilt University 116
Advanced ACE Tutorial Douglas C. Schmidt
Application ThreadsMost process resources areequally accessible to all threadsin a process, e.g.,
Virtual memory
User permissions and accesscontrol privileges
Open files
Signal handlers
Each thread also contains uniqueinformation, e.g.,
Identifier
Register set (e.g., PC and SP)
Run-time stack
Signal mask
Priority
Thread-specific data (e.g.,errno )
Note, there is no MMU protection for threads in a single process
Vanderbilt University 117
Advanced ACE Tutorial Douglas C. Schmidt
Kernel-level vs. User-level Threads
Application and system characteristics influence the choice ofuser-level vs. kernel-level threading
A high degree of virtual application concurrency implies user-levelthreads (i.e., unbound threads)
e.g., desktop windowing system on a uni-processor
A high degree of real application parallelism implies lightweightprocesses (LWPs) (i.e., bound threads)
e.g., video-on-demand server or matrix multiplication on amulti-processor
Vanderbilt University 118
Advanced ACE Tutorial Douglas C. Schmidt
Overview of OS Synchronization Mechanisms Threads share resources in a process address space
Therefore, they must use synchronization mechanisms to coordinatetheir access to shared data