版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
事件型设计模式事件型设计模式常用的四种用于处理事件的设计模式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. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2026年智能车载对讲机项目公司成立分析报告
- 2026浙江嘉兴市海宁市青少年宫公益性岗位招聘2人备考题库及完整答案详解1套
- 2026江西九江瑞昌市国投建设工程集团有限公司招聘变更2人备考题库附参考答案详解(综合题)
- 2026湖北事业单位联考咸宁市招聘132人备考题库附答案详解(完整版)
- 2026江西上饶市余干县中医院招聘司机1人备考题库附答案详解ab卷
- 2026湖北武汉消防招聘270人备考题库带答案详解(综合卷)
- 2026福建漳州招商局经济技术开发区海滨学校招聘2人备考题库附答案详解(研优卷)
- 2026辽宁沈阳市文体旅产业发展集团有限公司所属企业中层岗位招聘8人备考题库及一套完整答案详解
- 2026江西赣州市龙南市殡葬服务中心招聘会计人员1人备考题库附参考答案详解(b卷)
- 2026江西吉安市农作物良种场招聘见习人员4人备考题库含答案详解(研优卷)
- 体育课堂管理办法
- 作业人员安全管理档案
- 集装箱海运进出口流程
- 三年级英语下册阅读理解真题
- 电竞酒店前台收银员培训
- 《矿山压力与岩层控制》教案
- 开票税点自动计算器
- 2020泰和安TS-C-6001AG TS-C-6001AT应急照明控制器安装使用说明书 Ver.1.0,2020.09
- 冰雪项目策划方案
- 办公场地选址方案
- 光伏项目危险源辨识风险评价及控制措施清单
评论
0/150
提交评论