




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
事件型设计模式事件型设计模式常用的四种用于处理事件的设计模式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. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 公司合同股东合同范例
- 商品英文订购合同范本
- 10后劳动合同范本
- 加盟车辆合同范本
- 合同范本中英
- 后勤物资合同范本
- 商场家电采购合同范例
- 合作修车合同范本
- 合同范本文排版
- 刺绣成品购买合同范本
- 小学数学北师大版三年级下长方形的面积教案
- 2024年全国职业院校技能大赛高职组(中药传统技能赛项)考试题库(含答案)
- DGJ32 J 67-2008 商业建筑设计防火规范
- 2024年上海交通大学招考聘用高频考题难、易错点模拟试题(共500题)附带答案详解
- 浙江省金华市2024年初中毕业升学适应性检测 科学试题卷
- 延长石油招聘笔试试题
- DB-T 29-22-2024 天津市住宅设计标准
- 2024年赣州职业技术学院单招职业适应性测试题库及答案解析
- DL∕T 5209-2020 高清版 混凝土坝安全监测资料整编规程
- 2024年山东省潍坊市中考数学真题试题(含答案及解析)
- 开票税点自动计算器
评论
0/150
提交评论