




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
事件型设计模式事件型设计模式常用的四种用于处理事件的设计模式ReactorProactorAsynchronousCompletionTokenAcceptor-Connector.Reactor设计模式描述Reactor模式允许事件驱动的系统复用和分发对服务请求,这些请求来自与一个或多个客户端Reactor有时也叫做Dispatcher,Notifier结构Reactor结构TheReactorarchitecturalpatternallowsevent-drivenapplicationstodemultiplex&dispatchservicerequeststhataredeliveredtoanapplicationfromoneormoreclientsHandleownsdispatches*notifies**handlesetReactorhandle_events()register_handler()remove_handler()EventHandlerhandle_event()get_handle()ConcreteEventHandlerAhandle_event()get_handle()ConcreteEventHandlerBhandle_event()get_handle()SynchronousEventDemuxerselect()<<uses>>Reactor动态过程:MainProgram:ConcreteEventHandler:Reactor:SynchronousEventDemultiplexerregister_handler()get_handle()handle_events()select()handle_event()HandleHandlesHandlesCon.EventHandlerEventsservice()eventObservationsNoteinversionofcontrolAlsonotehowlong-runningeventhandlerscandegradetheQoSsincecallbacksstealthereactor’sthread!InitializephaseEventhandlingphase结构Reactor使用中注意的问题Reactor中注意以下组件事件监听组件的性能和时间间隔有无必要设定定时器事件事件处理采用同步还是异步方式事件处理的调度可否采用队列Proactor设计模式Proactor设计模式允许事件驱动的系统在异步操作完成时触发操作,使得此时系统能够复用和分发系统请求,借此提高并发处理中的性能TheProactor结构ProblemDevelopingsoftwarethatachievesthepotentialefficiency&scalabilityofasyncI/Oishardduetotheseparationintime&spaceofasyncoperationinvocations&theirsubsequentcompletioneventsSolutionApplytheProactorarchitecturalpattern(P2)tomakeefficientuseofasyncI/OHandle<<executes>>*<<uses>>isassociatedwith<<enqueues>><<dequeues>><<uses>><<uses>>Initiator<<demultiplexes
&dispatches>><<invokes>>EventQueueCompletionAsynchronousOperationProcessorexecute_async_op()AsynchronousOperationasync_op()AsynchronousEventDemuxerget_completion_event()Proactorhandle_events()CompletionHandlerhandle_event()ConcreteCompletionHandlerThispatternallowsevent-drivenapplicationstoefficientlydemultiplex&dispatchservicerequeststriggeredbythecompletionofasyncoperations,therebyachievingtheperformancebenefitsofconcurrencywithoutincurringitsmanyliabilitiesProactor动态结构ResultCompletionHandlerCompletion:AsynchronousOperation:ProactorCompletionHandlerexec_async_handle_Resultservice():AsynchronousOperationProcessor:Initiatorasync_operation()Resulthandle_events()eventeventEv.Queueoperation():CompletionEventQueueResultevent()InitiateoperationProcessoperationRuneventloopGenerate&queuecompletioneventDequeuecompletionevent&performcompletionprocessingNotesimilarities&differenceswiththeReactorpattern,e.g.:BothprocesseventsviacallbacksHowever,it’sgenerallyeasiertomulti-threadaproactorAsynchronousCompletionToken
设计模式AsynchronousCompletionToken(异步完成令牌)允许系统在异步操作完成之后及时处理相应。AsynchronousCompletionTokenStructure&ParticipantsDynamicInteractionshandle_event()Acceptor-Connector设计模式Acceptor-Connector(接收器-连接器)封装网络连接成为通讯的对等端点Acceptor-Connector常用于P2P系统当中,分别封装客户端和服务器14Acceptor-Connector结构Handleownsdispatches*notifies**handlesetReactorhandle_events()register_handler()remove_handler()EventHandlerhandle_event()get_handle()ConnectorSynchronousEventDemuxerselect()<<uses>>AcceptorServiceHandlerTheAcceptor-ConnectorPattern<<activate>>owns*usesuses<<creates>>ownsusesowns<<activate>>******usesnotifiesnotifiesnotifiesConnectorConnector()connect()complete()handle_event()ConcreteServiceHandlerBConcreteServiceHandlerAConcreteAcceptorConcreteConnectorAcceptorAcceptor()Accept()handle_event()peer_acceptor_ServiceHandleropen()handle_event()set_handle()peer_stream_Dispatcherselect()handle_events()register_handler()remove_handler()TransportHandleTransportHandleTransportHandleAcceptorDynamicsServiceHandlerEvents:Application:Acceptor:Dispatcherregister_handler()handle_events()accept()open()register_handler()handle_event()service():ServiceHandleropen()ACCEPT_EVENTHandle1Acceptor:Handle2Handle2Handle2Passive-modeendpointinitializephaseServicehandlerinitializephaseServiceprocessingphaseTheAcceptorensuresthatpassive-modetransportendpointsaren’tusedtoread/writedataaccidentallyAndviceversafordatatransportendpoints…ThereistypicallyoneAcceptorfactoryper-service/per-portAdditionaldemuxingcanbedoneathigherlayers,alaCORBASynchronousConnectorDynamicsHandleAddr:Application:Connector:Dispatcher:ServiceHandlerhandle_events()connect()open()register_handler()handle_event()service()ServiceHandlerEventsServiceHandlerHandleget_handle()MotivationforSynchronySyncconnectioninitiationphaseServicehandlerinitializephaseServiceprocessingphaseIftheservicesmustbeinitializedinafixedorder&theclientcan’tperformusefulworkuntilallconnectionsareestablishedIfconnectionlatencyisnegligiblee.g.,connectingwithaserveronthesamehostviaa‘loopback’deviceIfmultiplethreadsofcontrolareavailable&itisefficienttouseathread-per-connectiontoconnecteachservicehandlersynchronouslyAsynchronousConnectorDynamicsAddr:Application:Connector:Dispatcher:ServiceHandlerhandle_events()complete()connect()open()register_handler()handle_event()service()ServiceHandlerConnectorCONNECTEVENTEventsregister_handler()ServiceHandlerHandleHandleHandleget_handle()Motivation
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 中国胡椒基丙酮行业调查报告
- 2025年中国玻纤胎沥青瓦市场供需现状及投资战略研究报告
- 健康看电视的课件图片
- 2024年家具清洗用品:洗衣皂项目资金申请报告代可行性研究报告
- 2025年华东区域物流地产分析报告
- 2024年1420MM五机架冷连轧机项目资金申请报告代可行性研究报告
- 蕲春县殡葬管理办法细则
- 蛋糕店冷库管理办法标准
- 衡水农村天然气管理办法
- 装修户管理台账管理办法
- 会计师事务所绩效考核方案
- 健身教练独立承包协议(2024年版)
- 洗衣机设备保修条款
- 日语被动态练习
- 金属废弃物生物冶金提取与利用
- 广西燃气安全检查标准 DBJ T45-1472-2023(2023年7月1日实施)
- 2023-2024学年第二学期期末学业质量检测八年级语文试卷
- 统编版(2024)一年级语文上册拼音1《a o e》精美课件
- 双氧水罐罐区安全设计规范
- 2024-2029全球及中国福利管理系统行业市场发展分析及前景趋势与投资发展研究报告
- 新标准英语小学五年级下各模块习题
评论
0/150
提交评论