Acceptor-Connector an Object Creational Pattern for Connecting and Initializing Communication Services
Total Page:16
File Type:pdf, Size:1020Kb
Acceptor-Connector An Object Creational Pattern for Connecting and Initializing Communication Services Douglas C. Schmidt [email protected] Department of Computer Science Washington University St. Louis, MO 63130, USA An earlier version of this paper appeared in a chapter in TRACKING the book Pattern Languages of Program Design 3, edited SATELLITES STATION by Robert Martin, Frank Buschmann, and Dirke Riehle pub- PEERS lished by Addison-Wesley, 1997. 1Intent STATUS INFO The Acceptor-Connector design pattern decouples connec- WIDE AREA NETWORK tion establishment and service initialization in a distributed COMMANDS BULK DATA system from the processing performed once a service is ini- TRANSFER tialized. This decoupling is achieved with three compo- nents: acceptors, connectors,andservice handlers. A con- GATEWAY nector actively establishes a connection with a remote ac- ceptor component and initializes a service handler to pro- cess data exchanged on the connection. Likewise, an ac- LOCAL AREA NETWORK ceptor passively waits for connection requests from remote GROUND connectors, establishing a connection upon arrival of such a STATION PEERS request, and initializing a service handler to process data ex- changed on the connection. The initialized service handlers Figure 1: The Physical Architecture of a Connection- then perform application-specific processing and communi- oriented Application-level Gateway cate via the connection established by the connector and ac- ceptor components. The Gateway transmits data between its Peers using the connection-oriented TCP/IP protocol [1]. In our exam- 2 Example ple network configuration, each service is bound to a con- nection endpoint designated by an IP host address and a TCP To illustrate the Acceptor-Connector pattern, consider the port number. The port number uniquely identifies the type of multi-service, application-level Gateway shown in Fig- service. Maintaining separate connections for each type of ure 1. In general, a Gateway decouples cooperating com- service/port increases the flexibility of routing strategies and ponents in a distributed system and allows them to interact provides more robust error handling if network connections without having direct dependencies on each other. The par- shutdown unexpectedly. ticular Gateway in Figure 1 routes data between different In our distributed application, Gateway and Peers must service endpoints running on remote Peers used to mon- be able to change their connection roles to support different itor and control a satellite constellation. Each service in use-cases. In particular, either may initiate a connection ac- the Peers sends and receives several types of data via the tively or may wait passively for connection requests. For Gateway, such as status information, bulk data, and com- example, in one configuration, the Gateway may actively mands. In general, Peers can be distributed throughout lo- initiate connections to remote Peers in order to route data cal area networks (LANs) and wide-area networks (WANs). to them. In another configuration, the Gateway may pas- The Gateway is a router that coordinates the communi- sively receive connection requests from Peers which then cation among its PeersFrom˙ the Gateway’s perspective, route data through the Gateway to another Peer.Like- the Peer services whose data it routes differ solely in terms wise, Peers may be active connection initiators in one use- of their application-level communication protocols, which case and then be passive connection acceptors in another use- may use different framing formats and payload types. case. 1 Due to the nature of our distributed application, con- may be necessary to extend the Gateway to interoper- ventional designs that designate connection establishment ate with a directory service that runs over the IPX/SPX and service initialization roles a priori and hard-code them communication protocol, rather than TCP/IP.. into the Gateway and Peer components are too inflexi- It should be possible to decouple (1) the connection ble. Such a design overly couples the connection establish- roles, i.e., which process initiates a connection vs. ac- ment, service initialization, and service processing compo- cepts the connection, from (2) the communication roles, nents. This tight coupling make it hard to change connection i.e., which service endpoint is the client or the server. In roles independently of the communication roles. general, the distinction between “client” and “server” refer to communication roles, which may be orthogo- 3 Context nal to connection roles. For instance, clients often play the active role when initiating connections with a pas- A client/server application in a distributed system that uti- sive server. However, these connection roles can be re- lizes connection-oriented protocols to communicate between versed. For example, a client that plays an active com- service endpoints. munication role may wait passively for another process to connect to it. The example in Section 2 illustrates this latter use-case. 4Problem It should be possible to write communication software that is portable to many OS platforms in order to max- Distributed applications often contain complex code that per- imize availability and market share. Many low-level forms connection establishment and service initialization. In network programming APIs have semantics that are general, the processing of data exchanged between service only superficially different. Therefore, it hard to write endpoints in a distributed application is largely independent portable application using low-level APIs, such as sock- of configuration issues such as (1) which endpoint initiated ets and TLI, due to syntactic incompatibilities. the connection, i.e.,theconnection role vs. the communca- tion role and (2) the connection management protocol vs. the It should be possible to shield programmers from the network programming API. These issues are outlined below: lack of typesafety in low-level network programming APIs like sockets or TLI. For example, connection es- Connection role vs. communication role: Connection establishment roles are inherently asymmetrical, i.e., the pas- tablishment code should be completely decoupled from sive service endpoint waits and the active service endpoint subsequent data transport code to ensure that endpoints initiates the connection. Once the connection is established, are used correctly. Without this strong decoupling, for however, the communication role can be orthogonal to the instance, services may mistakenly read or write data on connection role. Thus, data can be transferred between ser- passive-mode transport endpoint factories that should vice endpoints in any manner that obeys the service’s com- only be used to accept connections. munication protocol. Common communication protocols in- It should be possible to reduce connection latency by clude peer-to-peer, request-response, and oneway streaming. using OS features like asynchronous connection es- tablishment. For instance, applications with a large The connection management protocol vs. the network programming API: Different network programming in- number of peers may need to asynchronously establish terfaces, such as sockets or TLI, provide different APIs to many connections concurrently. Efficient and scalable establish connections using various connection management connection establishment is particularly important for protocols. Regardless of the protocol used to establish a con- applications that run over long-latency WANs. nection, however, data can be transferred between endpoints It should be possible to reuse as much general-purpose using uniform message passing operations, e.g., send/recv connection establishment and service initialization soft- calls. ware as possible in order to leverage prior development In general, the strategies for connection establishment and effort. service initialization change much less frequently than ap- plication service implementations and communication pro- tocols. Thus, decoupling these aspects so that they can vary 5 Solution independently is essential for developing and maintaining distributed applications. The following forces impact the so- For each service offered by a distributed application, use the lution to the problem of separating the connection and ini- Acceptor-Connector pattern to decouple connection estab- tialization protocols from the communication protocol: lishment and service initialization from subsequent process- ing performed by the two endpoints of a service once they It should be easy to add new types of services, new ser- are connected and initialized. vice implementations, and new communication proto- Introduce two factories that produce connected and ini- cols without affecting the existing connection establish- tialized service handlers, which implement the application ment and service initialization software. For instance, it services. The first factory, called acceptor, creates and ini- 2 Service Handler Service Service Handler Connector Handler ACTIVATES Acceptor connect(sh, addr) n 1 peer_acceptor_ ACTIVATES peer_stream_ complete() open() accept() 1 n open() NOTIFY WHEN NOTIFY WHEN CONNECTION COMPLETES CONNECTION ARRIVES Dispatcher Figure 2: Structure of Participants in the Acceptor-Connector Pattern tializes a transport endpoint that passively listens at a par- Handler and uses its transport endpoint factory to accept a ticular address for connection requests from remote connec- new connection into the Service Handler.