Tải bản đầy đủ - 0 (trang)
Chapter 9. The ACE Streams Framework

Chapter 9. The ACE Streams Framework

Tải bản đầy đủ - 0trang

9.1Overview

ThePipesandFiltersarchitecturalpatternisacommonwayof

organizinglayered/modularapplications[SG96].Thispattern

definesanarchitectureforprocessingastreamofdatainwhich

eachprocessingstepisencapsulatedinsometypeoffilter

component.Dataispassedbetweenadjacentfiltersviaa

communicationmechanism,whichcanrangefromIPCchannels

connectinglocalorremoteprocessestosimplepointersthat

referenceobjectswithinthesameprocess.Eachfiltercanadd,

modify,orremovedatabeforepassingitalongtothenextfilter.

Filtersareoftenstateless,inwhichcasedatapassingthrough

thefilteraretransformedandpassedalongtothenextfilter

withoutbeingstored.

CommonexamplesofthePipesandFilterspatterninclude

TheUNIXpipeIPCmechanism[Ste92]usedbyUNIXshells

tocreateunidirectionalpipelines

SystemVSTREAMS[Rit84],whichprovidesaframeworkfor

integratingbidirectionalprotocolsintotheUNIXkernel

TheACEStreamsframeworkisbasedonthePipesandFilters

pattern.Thisframeworksimplifiesthedevelopmentof

layered/modularapplicationsthatcancommunicatevia

bidirectionalprocessingmodules.Thischapterdescribesthe

followingclassesintheACEStreamsframework:

ACEClass



ACE_Task



Description



Acohesiveunitofapplication-definedfunctionalitythatusesmessagesto

communicaterequests,responses,data,andcontrolinformationandcan

queueandprocessmessagessequentiallyorconcurrently.



ACE_Module Adistinctbidirectionalprocessinglayerinanapplicationthatcontainstwo

ACE_Taskobjects—onefor"reading"andonefor"writing"

ACE_Stream ContainsanorderedlistofinterconnectedACE_Moduleobjectsthatcanbe

usedtoconfigureandexecutelayeredapplication-definedservices



ThemostimportantrelationshipsbetweenclassesintheACE

StreamsframeworkareshowninFigure9.1.Theseclassesplay

thefollowingrolesinaccordancewiththePipesandFilters

pattern[POSA1]:

Figure9.1.TheACEStreamsFrameworkClasses



Filterclassesaretheprocessingunitsinastreamthat

enrich,refine,ortransforminputdata.Thefilterclassesin

theACEStreamsframeworkareimplementedbysubclasses

ofACE_Task.

Pipeclassesdenotetheinterconnectionsbetweenthe

filters.ThepipeclassesintheACEStreamsframeworkare

providedbyACE_Moduleobjectsthatcontain

interconnectedACE_Taskobjectslinkedtogethertocreate

acompletebidirectionalACE_Stream.Application-defined

methodsinadjacentinterconnectedtaskscollaborateby

exchangingdataandcontrolmessageblocks.

TheACEStreamsframeworkprovidesthefollowingbenefits:

Enhancedreuseandflexibility.AnyACE_Taskcanbe

pluggedintoanyACE_ModuleandanyACE_Modulecanbe

pluggedintoanyACE_Stream.Thisflexibilityenables



multipleapplicationstosystematicallyreuseexisting

modulesandtasks,allofwhichcanbeconfigured

dynamically.

Transparent,incrementalevolution.Application

functionalitycanbeimplementedinacontrolledmannerby

adding,removing,andchangingmodulesandtasks.This

abilitytoevolveanapplication'sdesignandfunctionalityis

particularlyusefulinagiledevelopmentprocesses,suchas

ExtremeProgramming(XP)[Bec00].

Macro-levelperformancetuning.Applicationscanadapt

tovaryingdeploymentscenariosandrun-time

environmentsbyselectivelyomittingunnecessaryservice

functionalityorreconfiguringamixtureofACE_Taskand

ACE_Moduleobjectstoprovidecontextuallyoptimalservice

functionality.

Inherentmodularity.ThestructureenforcedbytheACE

Streamsframeworkpromoteswell-accepteddesign

practicesofstrongcohesionandminimalcoupling.Modular

designshelptoreducethecomplexityofeachlayerand

improvetheoverallimplementationofnetworked

applicationsandservices.Testinganddocumentationare

alsomoreeasilyaccomplishedwhenbasedonahighly

modulardesign,makingapplicationmaintenanceand

systematicreuseeasier,aswell.

Easytolearn.TheACEStreamsframeworkcontainsan

intuitiveclassstructurebasedonthepatternsanddesignof

theSystemVSTREAMSframework,asdiscussedinSidebar

59.However,evendeveloperswhohaven'tbeenexposedto

SystemVSTREAMScanquicklygraspthestructureofthe

PipesandFilterspatternandthenusethatpatternasa

basisforlearningtherestoftheACEStreamsframework

classes.



TheACE_TaskclasswasdescribedinChapter6.Thischapter

thereforefocuseslargelyondescribingthecapabilitiesprovided

bytheACE_ModuleandACE_Streamclasses.However,we

illustratehowalltheclassesintheACEStreamsframeworkcan

beusedtodevelopautilityprogramthatformatsandprintslog

recordsstoredbyourloggingservers.Ifyouaren'tfamiliarwith

thePipesandFilterspatternfromPOSA1,werecommendthat

youreadaboutitfirstbeforedelvingintothedetailedexamples

inthischapter.



Sidebar59:ACEStreamsRelationshiptoSystemVSTREAMS

TheclassnamesanddesignoftheACEStreamsframeworkcorrespondtosimilar

componentryinSystemVSTREAMS[Rit84,Rag93].Thetechniquesusedto

supportextensibilityandconcurrencyinthesetwoframeworksdiffer

significantly,however.Forexample,application-definedfunctionalityisaddedin

SystemVSTREAMSviatablesofpointerstoCfunctions,whereasintheACE

Streamsframeworkit'saddedbysubclassingfromACE_Task,whichprovides

greatertypesafetyandextensibility.TheACEStreamsframeworkalsousesthe

ACETaskframeworktoenhancethecoroutine-basedconcurrencymechanisms

usedinSystemVSTREAMS.TheseACEenhancementsenablemoreeffectiveuse

ofmultipleCPUsonsharedmemorymultiprocessingplatforms[SS95b]by

reducingthelikelihoodofdeadlockandsimplifyingflowcontrolbetween

ACE_TaskactiveobjectsinanACE_Stream.



9.2TheACE_ModuleClass

Motivation

Manynetworkedapplicationscanbemodeledasanordered

seriesofprocessinglayersthatarerelatedhierarchicallyand

thatexchangemessagesbetweenadjacentlayers.Forexample,

kernel-level[Rit84,Rag93]anduser-level[SS95b,HJE95]

protocolstacks,callcentermanagers[SS94],andotherfamilies

ofnetworkedapplicationscanbenefitfromamessage-passing

designbasedonalayered/modularservicearchitecture.As

discussedinSection2.1.4,eachlayercanhandleaselfcontainedportion(suchasinputoroutput,eventanalysis,

eventfiltering,orserviceprocessing)ofaserviceornetworked

application.

TheACE_Taskclassprovidesareuseablecomponentthatcan

easilybeusedtoseparateprocessingintostagesandpassdata

betweenthem.SinceACE_Taskobjectsareindependent,

however,additionalstructureisrequiredtoorderACE_Task

objectsintobidirectional"reader-writer"pairsthatcanbe

assembledandmanagedasaunit.Redevelopingthisstructure

inmultipleprojectsistediousandunnecessarybecausethe

structureisfundamentallyapplicationindependent.Toavoid

thisredundantdevelopmenteffort,therefore,theACEStreams

frameworkdefinestheACE_Moduleclass.



ClassCapabilities

ACE_Moduledefinesadistinctlayerofapplication-defined

functionality.Thisclassprovidesthefollowingcapabilities:

EachACE_Moduleisabidirectionalapplication-defined

processinglayercontainingapairofreaderandwritertasks



thatderivefromACE_Task.Layereddesignscanbe

expressedeasilyusingACE_Module,whichsimplifies

development,training,andevolution.

TheACEServiceConfiguratorframeworksupportsdynamic

constructionofACE_Moduleobjectsthatcanbeconfigured

intoanACE_Streamatruntime.Layereddesignsbasedon

ACE_Modulearethereforehighlyextensible.

ThereaderandwriterACE_Taskobjectscontainedinan

ACE_ModulecollaboratewithadjacentACE_Taskobjects

bypassingmessagesviaapublichookmethod,which

promotesloosecouplingandsimplifiesreconfiguration.

TheobjectscomposedintoanACE_Modulecanbevaried

andreplacedindependently,whichlowersmaintenanceand

enhancementcosts.

TheinterfaceforACE_ModuleisshowninFigure9.2andits

keymethodsareshowninthefollowingtable:

Figure9.2.TheACE_ModuleClass



Method



ACE_Module()open()

ACE_Module()close()



Description



Initializeamoduleandallocateitsresources.



Destroyamoduleandreleaseitsresources.



reader()writer()



Set/getthereader-andwriter-sidetasks.



name()



Set/getthenameofthemodule.



TheACE_TaskclassdescribedinSection6.3providesan

object-orientedprocessingabstractionthatcanbespecializedto

targetanyparticularapplicationdomain,suchasnetwork

protocolstacks[SW95]orcustomercarecallcenter

management[SS94].TheACE_Taskmessagepassingand

queueingmechanismprovidesastraightforwardwaytodividea

domain'sworkintodistinctstepsandmoveworkanddata

betweenthemefficientlyandbidirectionally.Manydomains

havesymmetricprocessingstepsfordatabeingreadand

written.Forexample,protocolprocessingofteninvolves



symmetrictasksforverifyingandapplyingsecuritytransforms,

suchasencryption.AnACE_Moduleprovidesauniformand

flexiblecompositionmechanismthatrelatesinstancesofthese

application-definedACE_Taskobjectsinto

A"reader"sidetoprocessmessagessentupstreamtothe

ACE_Modulelayer

A"writer"sidetoprocessmessagessentdownstreamtothe

ACE_Modulelayer

Thetwotasksthatcompriseabidirectionalmodulearereferred

toassiblings.Intheencryptionexample,forinstance,the

readertaskwouldverifyanddecryptreceiveddatawhileits

siblingwritertaskwouldencryptdatabeforeit'swritten.An

ACE_Modulewouldbecomposedfromoneofeachofthese

tasks,andconfiguredintothestreamappropriately.

Incaseswherealayeractivelyprocessesdatainonlyone

direction,theinactivesiblingtaskcanbespecifiedasaNULL

pointer,whichtriggerstheACE_Moduletoinstallan

ACE_Thru_Taskthatsimplyforwardsallmessagestothenext

module'staskwithoutmodifyingthem.Thisdesignmaintains

thelayeringevenwhenlayersarerearranged,added,or

removed.



Example

Thisexampledevelopsautilityprogramcalled

display_logfile,whichreadslogrecordsstoredbyour

loggingservers,formatstheinformation,andprintsitina

human-readableformat.Asshownonpage90inC++NPv1,

mostfieldsinalogrecordarestoredinaCDR-encodedbinary

format,whichisconcisebutnoteasilyunderstoodbyhumans.

Toimplementthedisplay_logfileprogram,weimplement



thefollowingclassesderivedfromACE_Module:

LogrecReaderisamodulethatconvertsthelogrecordsin

alogfileintoacanonicalformatthat'sthenpassedto,and

processedby,othermodulesinanACE_Stream.

LogrecFormatterisamodulethatdetermineshowthe

fieldsinthelogrecordwillbeformatted,forexampleby

convertingthemfrombinarytoASCII.

LogrecSeparatorisamodulethatinsertsmessageblocks

containingaseparatorstringbetweentheexistingmessage

blocksinacompositelogrecordmessage.

LogrecWriterisamodulethatprintsformattedlogrecord

messagestothestandardoutput,wheretheycanbe

redirectedtoafile,printer,orconsole.

Figure9.3illustratesthestructureofthemodulesinthe

ACE_Streamthatcomprisethedisplay_logfileprogram.

Thisprogramusesaproducer/consumerconcurrencymodel,

wheretheLogrec_ReaderandtheLogrec_Writerrunas

activeobjectsthatproduceandconsumemessages,

respectively.Figure9.3alsoillustratesthestructureofthe

compositeACE_Message_BlockcreatedbytheLogrec

Readermoduleandmanipulatedbytheotherfiltermodulesin

theACE_Stream.

Figure9.3.TheACE_StreamStructureforthe

display_logfileProgram



Intheexamplebelow,weinheritfromACE_Taskand

ACE_ModuletocreatetheLogrecReader,Logrec

Formatter,LogrecSeparator,andLogrecWritermodules.

ThefollowingLogrec_ModuletemplateandLOGRECMODULE

macroareusedtosimplifymanyoftheexamplesbelow:



template

classLogrec_Module:publicACE_Module{

public:

Logrec_Module(constACE_TCHAR*name)

:ACE_Module

(name,

&task_,//Initializewriter-side

0,//Ignorereader-sidetask

0,

ACE_Module::M_DELETE

private:

TASKtask_;

};

#defineLOGREC_MODULE(NAME)\

typedefLogrec_ModuleNAME##_Module

Sincetheflowofmessagesisunidirectional(fromLogrec

ReadertoLogrecWriter)weinitializeonlythewriter-sidetask

intheACE_Moduleconstructor.BypassingaNULLpointertothe

reader-sidetaskparameter,theACE_Moduleconstructorwill



Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Chapter 9. The ACE Streams Framework

Tải bản đầy đủ ngay(0 tr)

×