Distributed Real-Time Systems (TI-DRTS) POSA2: Reactor Architectural Pattern

Similar documents
Reactor. An Object Behavioral Pattern for Demultiplexing and Dispatching Handles for Synchronous Events. Douglas C. Schmidt

Distributed Real-Time Systems (TI-DRTS) POSA2: Acceptor/Connector Design Pattern

Introduction to Patterns and Frameworks

TCP/IP - Socket Programming

Applying a Pattern Language to Develop Application-level Gateways

Why Threads Are A Bad Idea (for most purposes)

Developing Flexible and High-performance Web Servers with Frameworks and Patterns

Web Server Architectures

Socket Programming. Request. Reply. Figure 1. Client-Server paradigm

Asynchrony for the Masses Marc Duerner

Design Patterns in C++

Tutorial on Socket Programming

Applying Patterns and Frameworks to Develop Object-Oriented Communication Software

Session NM059. TCP/IP Programming on VMS. Geoff Bryant Process Software

CMLOG: A Common Message Logging System

eel: Tools for Debugging, Visualization and Verification of Event-Driven Software

3.5. cmsg Developer s Guide. Data Acquisition Group JEFFERSON LAB. Version

Design and Performance of an Object-Oriented Framework for High-Speed Electronic Medical Imaging

ELEN 602: Computer Communications and Networking. Socket Programming Basics

Active Object. An Object Behavioral Pattern for Concurrent Programming. R. Greg Lavender Douglas C. Schmidt

Lecture 17. Process Management. Process Management. Process Management. Inter-Process Communication. Inter-Process Communication

Design Document. Offline Charging Server (Offline CS ) Version i -

Introduction to Socket Programming Part I : TCP Clients, Servers; Host information

Socket Programming. Srinidhi Varadarajan

Applying a Pattern Language to Develop Extensible ORB Middleware


ICT SEcurity BASICS. Course: Software Defined Radio. Angelo Liguori. SP4TE lab.

Operating System Manual. Realtime Communication System for netx. Kernel API Function Reference.

Network Programming. Writing network and internet applications.

How To Write A Microsoft Powerbook For An Microsoft Acheive (Programming) (Programmer) (Orchestraic) (For Microsoft) (Permanent) (Powerbook) (Operations) (Memory)

Infrastructure that supports (distributed) componentbased application development

Storage Class Extensibility in the Brown Object Storage System

Embedded Programming in C/C++: Lesson-1: Programming Elements and Programming in C

SMTP-32 Library. Simple Mail Transfer Protocol Dynamic Link Library for Microsoft Windows. Version 5.2

Writing a C-based Client/Server

Unix Network Programming

Chapter 3. Internet Applications and Network Programming

Integrating the Internet into Your Measurement System. DataSocket Technical Overview

UI Performance Monitoring

µtasker Document FTP Client

CORBAservices. Naming. Part of the CORBA Naming Service Interface in IDL. CORBA Naming Service

MatrixSSL Porting Guide

Socket Programming. Kameswari Chebrolu Dept. of Electrical Engineering, IIT Kanpur

Network programming, DNS, and NAT. Copyright University of Illinois CS 241 Staff 1

Patterns in. Lecture 2 GoF Design Patterns Creational. Sharif University of Technology. Department of Computer Engineering

The POSIX Socket API

SOFT 437. Software Performance Analysis. Ch 5:Web Applications and Other Distributed Systems

Channel Access Client Programming. Andrew Johnson Computer Scientist, AES-SSG

3F6 - Software Engineering and Design. Handout 10 Distributed Systems I With Markup. Steve Young

Virtuozzo Virtualization SDK

Socket Programming in C/C++

Stock Trader System. Architecture Description

The Command Dispatcher Pattern

NS3 Lab 1 TCP/IP Network Programming in C

System calls. Problem: How to access resources other than CPU

C#5.0 IN A NUTSHELL. Joseph O'REILLY. Albahari and Ben Albahari. Fifth Edition. Tokyo. Sebastopol. Beijing. Cambridge. Koln.

Communication Networks. Introduction & Socket Programming Yuval Rochman

Specific Simple Network Management Tools

MatrixSSL Developer s Guide

Designing a Home Alarm using the UML. And implementing it using C++ and VxWorks

Interprocess Communication Message Passing

CORBA Programming with TAOX11. The C++11 CORBA Implementation

Multiprocessor Support for Event-Driven Programs

GTask Developing asynchronous applications for multi-core efficiency

Dragan Juričić, PBZ May 2015

Operating Systems Design 16. Networking: Sockets

Monitor Object. An Object Behavioral Pattern for Concurrent Programming. Douglas C. Schmidt

Let s Chat! (Part 1)

Middleware and Distributed Systems. Design of Scalable Servers. Martin v. Löwis. Donnerstag, 16. Februar 12

Experiences with an Object-Oriented Architecture for Developing Dynamically Extensible Distributed System Management Software

HelenOS IPC and Behavior Protocols

Programming in C# with Microsoft Visual Studio 2010

C# Cookbook. Stephen Teilhet andjay Hilyard. O'REILLY 8 Beijing Cambridge Farnham Köln Paris Sebastopol Taipei Tokyo '"J""'

Delayline: A Wide-Area Network Emulation Tool

Introduction to LabVIEW Design Patterns

EXAM PRO:Design & Develop Windows Apps Using MS.NET Frmwk 4. Buy Full Product.

: provid.ir

Asynchronous Provisioning Platform (APP)

q for Gods Whitepaper Series (Edition 7) Common Design Principles for kdb+ Gateways

Application Development with TCP/IP. Brian S. Mitchell Drexel University

The publisher offers discounts on this book when ordered in quantity for bulk purchases and special sales. For more information, please contact:

Intel EP80579 Software for Security Applications on Intel QuickAssist Technology Cryptographic API Reference

IT304 Experiment 2 To understand the concept of IPC, Pipes, Signals, Multi-Threading and Multiprocessing in the context of networking.

Network Programming TDC 561

Department of Electrical Engineering and Computer Science MASSACHUSETTS INSTITUTE OF TECHNOLOGY Operating System Engineering: Fall 2005

Real-Time Systems Prof. Dr. Rajib Mall Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

Operating Systems. 05. Threads. Paul Krzyzanowski. Rutgers University. Spring 2015

Pattern Language Overview

MPLAB Harmony System Service Libraries Help

Using NSM for Event Notification. Abstract. with DM3, R4, and Win32 Devices

sndio OpenBSD audio & MIDI framework for music and desktop applications

Chapter 6, The Operating System Machine Level

Data Communication & Networks G

Decomposition into Parts. Software Engineering, Lecture 4. Data and Function Cohesion. Allocation of Functions and Data. Component Interfaces

UNIX Sockets. COS 461 Precept 1

Thread-Specific Storage for C/C++ An Object Behavioral Pattern for Accessing per-thread State Efficiently

LabVIEW Advanced Programming Techniques

Freescale MQX USB Device User Guide

CS222: Systems Programming

Introduction CORBA Distributed COM. Sections 9.1 & 9.2. Corba & DCOM. John P. Daigle. Department of Computer Science Georgia State University

Transcription:

Distributed Real-Time Systems (TI-DRTS) POSA2: Reactor Architectural Pattern Version: 31-08-2009

Abstract The Reactor architectural pattern allows event-driven applications to demultiplex & dispatch service requests that are delivered to an application from one or more clients Slide 2

Example a Distributed Logging Service TCP communication from clients to a logging server Slide 3

Context An event-driven application that receives multiple service requests simultaneously, but processes them synchronously and serially Slide 4

Solution Synchronously wait for the arrival of indication events on one or more event sources (e.g. connected socket handles) Demultiplex and dispatch the events to services that process them Perform the application specific functionality in service handlers Slide 5

Reactor based Framework Reactor: Event loop Framework Architecture ADTs Strings Files Locks INVOKES Slide 6

Reactor Pattern Structure (1) Handle owns 1 1 EventHandler +handle_event() +get_handle() Abstract base class (= interface) Concrete EventHandlerA handle_event() get_handle() Concrete EventHandlerB handle_event() get_handle() Slide 7

Reactor Pattern Structure (2) Application «creates» Reactor 1 dispatches * EventHandler +handle_events() +register_handler() +remove_handler() 1 * Handle owns 1 1 +handle_event() +get_handle() Concrete EventHandlerA handle_event() get_handle() Concrete EventHandlerB handle_event() get_handle() Slide 8

Reactor Pattern Structure (3) Application «creates» Reactor +handle_events() +register_handler() +remove_handler() Synchronous Event Demultiplexer +select() «uses» 1 1 1 * dispatches Handle * 1 owns * 1 Concrete EventHandlerA handle_event() get_handle() EventHandler +handle_event() +get_handle() Concrete EventHandlerB handle_event() get_handle() Slide 9

Reactor Sequence Diagram : Main Program : Concrete Event Handler : Reactor : Synchronous Event Demultiplexer 1. Initialize phase Con. Event Handler Events register_handler() get_handle() Handle 2. Event handling phase handle_events() handle_event() service() Handles Handles select() event Observations Note inversion of control Also note how long-running event handlers can degrade the QoS since callbacks steal the reactor s thread! Slide 10

Implementation Steps 1. Define the event handler interface 2. Define the reactor interface 3. Implement the reactor interface 4. Determine the number of reactors needed in an application 5. Implement the concrete event handlers Slide 11

1. Define the Event Handler Interface Single Method Dispatch (Imp. 1.2) class Event_Handler { public: virtual void handle_event(handle handle, Event_Type et) = 0; virtual HANDLE get_handle() const = 0; }; typedef unsigned int Event_Type enum { READ_EVENT = 01, // ACCEPT_EVENT alias READ_EVENT ACCEPT_EVENT= 01, WRITE_EVENT= 02, TIMEOUT_EVENT= 04 // etc. }; Slide 12

1. Define the Event Handler Interface Multi Method Dispatch (Imp. 1.2) class Event_Handler { public: virtual void handle_input(handle handle) = 0; virtual void handle_output(handle handle) = 0; virtual void handle_timeout(const Time_Value &) = 0; virtual void handle_close(handle handle, Event_Type et) = 0; virtual HANDLE get_handle() const = 0; }; Slide 13

2. Define the Reactor Interface class Reactor { public: virtual void register_handler(event_handler *eh, Event_Type et) = 0; virtual void register_handler(handle h, Event_Handler *eh, Event_Type et) = 0; virtual void remove_handler(event_handler *eh, Event_Type et) = 0; virtual void remove_handler(handle h, Event_Type et) const = 0; // Entry point into the reactive event loop void handle_events(time_value *timeout =0); // Define a singleton access point static Reactor *instance(); private: Reactor_Implementation *reactor_impl_; // uses the GoF Bridge pattern }; Slide 14

Reactor Implementation (Bridge) Reactor +register_handler( ) reactor_impl_ Reactor Implementation +register_handler( ) reactor_impl_->register_handler(..) Select_Reactor_ Implementation void Select_Reactor_Implementation::register_handler(Event_handler *eh, Event_type et) { HANDLE handle= eh->get_handle(); // } Slide 15

Bridge Design Pattern (GoF) Client Abstraction operation() imp Implementor operationimp() imp->operationimp() Refined Abstraction Concrete ImplementorA operationimp() Concrete ImplementorB operationimp() Slide 16

3.2 Choose a Synchronous Demultiplexer Mechanism The synchronous event demultiplexer, as well as the handles and handle sets, are often existing operating system mechanisms (e.g. select() ) Operating System Demux Mechanisms: select(): Unix, Win32, VxWork poll(): Unix, System V, release 4. WaitForMultipleObjects(): Win32 Slide 17

Select as Demultiplexer Mechanism int select(u_int max_handle_plus_1, fd_set *read_fds, fd_set *write_fds, fd_set *except_fds, timeval *timeout); The select() function examines the three file descriptor set (fd_set) to see if any of their handles are ready for reading, writing or have an exceptional condition or check for a timeout Slide 18

Data Structure fd_set: Example #define FD_SETSIZE 64 typedef struct fd_set { u_int fd_count; /* how many are SET? */ SOCKET fd_array[fd_setsize]; /* an array of SOCKETs */ } fd_set; Slide 19

3.3 Implement a Demultiplexing Table Unix implementation example, where handle values are contiguous ints class Demux_Table { public: // Convert <Tuple> array to <fd_set>s void convert_to_fd_sets(fd_set &read_fds, fd_set &write_fds, fd_set &except_fds); struct Tuple { // Pointer to <Event_Handler> that process // the indication event arriving on the handle Event_Handler *event_handler_; // Bit-mask that tracks which types of indication events // <Event_Handler> is registered for Event_Type event_type_; } Tuple table_[fd_setsize]; // FD_SETSIZE macro defined }; // in <sys/socket.h> Slide 20

3.4 Define the Concrete Reactor Implementation (Unix example) class Select_Reactor_Implementation : public Reactor_Implementation { public: void handle_events(time_value *timeout = 0) { fd_set read_fds, write_fds, except_fds; demux_table_.convert_to_fd_sets(read_fds,write_fds, except_fds); HANDLE max_handle = MAX_NO_OF HANDLES; int result = select( max_handle+1, &read_fds, &write_fds, &except_fds, timeout); if (result <=0) throw // handle error or timeout cases here for (HANDLE h=0; h <=max_handle; h++) { if ( FD_ISSET(&read_fds, h) ) demux_table_.table_[h].event_handler_-> handle_event(h, READ_EVENT); // perform the same for WRITE_EVENT and EXCEPT_EVENTs } }; private: Demux_table demux_table_; } Slide 21

Logging Server Example (1) Client 4. connect() :Logging Acceptor 1. register_handler() :Reactor 2. handle_events() 3. select() Logging Server 6. accept() 7. create() 5. handle_ event() a:logging Handler 8. register_handler() Scenario: Client connects to the logging server Slide 22

Logging Server Example (2) a :Logging Handler 3. recv() 4. write() Logging Server Client 1. send() 2. handle_event() :Reactor 5. return b :Logging Handler Scenario: client sends a logging record Slide 23

Logging Server main program const u_short PORT = 10000; // logging server port number int main() { INET_Addr addr(port); // Logging server address // Initialize logging server endpoint and register // with reactor singleton Logging_Acceptor la(addr, Reactor::instance() ); } // Event loop that processes client connection requests // and log records reactively while ( 1 ) Reactor::instance()->handle_events(); Slide 24

Reactor Pattern - Benefits Separation of concerns This pattern decouples application-independent demuxing & dispatching mechanisms from application-specific hook method functionality Modularity, reusability, & configurability This pattern separates event-driven application functionality into several components, which enables the configuration of event handler components that are loosely integrated via a reactor Portability By decoupling the reactor s interface from the lower-level OS synchronous event demuxing functions used in its implementation, the Reactor pattern improves portability Coarse-grained concurrency control This pattern serializes the invocation of event handlers at the level of event demuxing & dispatching within an application process or thread Slide 25

Reactor Pattern - Liabilities Restricted applicability This pattern can be applied efficiently only if the OS supports synchronous event demuxing on handle sets Non-pre-emptive In a single-threaded application, concrete event handlers that borrow the thread of their reactor can run to completion & prevent the reactor from dispatching other event handlers Complexity of debugging & testing It is hard to debug applications structured using this pattern due to its inverted flow of control, which oscillates between the framework infrastructure & the method call-backs on application-specific event handlers Slide 26

Relation to other POSA2 Patterns Slide 27

See Also Reactor is related to: Observer (GoF), Publisher-Subscriber (POSA1) where all dependent are informed In the Reactor a single handler is informed Chain of Responsibility (GoF) Searches the chain to locate the first matching handler The Reactor associates a specific event handler with a particular source of events Proactor The Reactor is a synchronous variant of the asynchronous Proactor pattern Slide 28

Logging Server - Example Repetition see slides 24-26, where two scenarios and the main server program are shown The following slides will show the two concrete Event_Handler subclasses: Logging_Acceptor accepts a new client connection Logging_Handler receives client log records Slide 29

Application Logging Server Example Reactor handle_events() register_handler() remove_handler() 1 1 * dispatches Handle * owns 1 1 Event_Handler handle_event() get_handle() Logging_ Acceptor handle_event() get_handle() Logging_ Handler handle_event() get_handle() Slide 30

Logging_Acceptor class (1) class Logging_Acceptor : public Event_Handler { public: Logging_Acceptor(const INET_Addr &addr, Reactor *reactor) : acceptor_(addr), reactor_(reactor) { reactor_->register_handler(this, ACCEPT_EVENT); } virtual void handle_event(handle, Event_Type event_type) { if (event_type == ACCEPT_EVENT) { SOCK_Stream client_connection; acceptor_.accept(client_connection); } // Create a new <Logging_Handler> Logging_Handler *handler = new Logging_Handler( client_connection, reactor_); } Slide 31

Logging_Acceptor class (2) class Logging_Acceptor : public Event_Handler { public: // continued from previous slide virtual HANDLE get_handle() const { return acceptor_.get_handle(); } private: // Socket factory that accepts client connections SOCK_Acceptor acceptor_; }; // Cached <Reactor> Reactor *reactor_; Slide 32

Logging_Handler class (1) class Logging_Handler : public Event_Handler { public: Logging_Handler(const SOCK_Stream &stream, Reactor *reactor) : peer_stream_(stream), reactor_(reactor) { reactor_->register_handler(this, READ_EVENT); } virtual void handle_event(handle, Event_Type event_type) { if (event_type == READ_EVENT) { Log_Record log_record; int result= peer_stream_.recv(&log_record, sizeof log_record); if (result!= STREAM_ERROR) log_record.write(stdout); else { reactor_->remove_handler(this,read_event); delete this; // Deallocate ourselves! } } Slide 33

Logging_Handler class (2) class Logging_Handler : public Event_Handler { public: // continued from previous slide virtual HANDLE get_handle() const { return peer_stream_.get_handle(); } private: // Receives logging records from a connected client SOCK_Stream peer_stream_; Reactor reactor_; } Slide 34