{"id":6540,"date":"2023-04-11T17:50:47","date_gmt":"2023-04-11T17:50:47","guid":{"rendered":"https:\/\/www.modernescpp.com\/index.php\/broker\/"},"modified":"2023-04-11T17:50:47","modified_gmt":"2023-04-11T17:50:47","slug":"broker","status":"publish","type":"post","link":"https:\/\/www.modernescpp.com\/index.php\/broker\/","title":{"rendered":"Broker"},"content":{"rendered":"<p>The Broker Pattern structures distributed software systems that interact with remote service invocations. It is responsible for coordinating the communication, its results, and exceptions.<\/p>\n<p><!--more--><\/p>\n<h2><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-6537\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2023\/04\/Broker.png\" alt=\"Broker\" width=\"650\" height=\"336\" style=\"display: block; margin-left: auto; margin-right: auto;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2023\/04\/Broker.png 1223w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2023\/04\/Broker-300x155.png 300w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2023\/04\/Broker-1024x529.png 1024w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2023\/04\/Broker-768x397.png 768w\" sizes=\"auto, (max-width: 650px) 100vw, 650px\" \/><\/h2>\n<p>The Broker Pattern from the book&nbsp; <a href=\"https:\/\/en.wikipedia.org\/wiki\/Pattern-Oriented_Software_Architecture\">&#8220;<span id=\"productTitle\" class=\"a-size-extra-large\">Pattern-Oriented Software Architecture, Volume 1<\/span><\/a>&#8221; helps solve many challenges of distributed systems, such as finding the appropriate service provider, communicating with them securely, using the right programming language, or dealing with errors. This will not go into the details. It should only provide you with a rough idea of the Broker Pattern. For further information, study the pattern in the book <a href=\"https:\/\/en.wikipedia.org\/wiki\/Pattern-Oriented_Software_Architecture\">&#8220;<span id=\"productTitle\" class=\"a-size-extra-large\">Pattern-Oriented Software Architecture, Volume 1<\/span><\/a>&#8220;.<\/p>\n<h2>Broker<\/h2>\n<h3>Purpose<\/h3>\n<ul>\n<li>A complex software system should be designed as a set of decoupled and interacting subsystems.<\/li>\n<li>This has the following consequences:\n<ul>\n<li>All subsystems must communicate with each other using an <a href=\"https:\/\/en.wikipedia.org\/wiki\/Inter-process_communication\">inter-process communication<\/a> protocol (IPC)<\/li>\n<li>A subsystem must find its appropriate service<\/li>\n<li>The services must be managed<\/li>\n<\/ul>\n<\/li>\n<li>The used service should be transparent for the client<\/li>\n<\/ul>\n<h3>Solution<\/h3>\n<ul>\n<li>Introduce a broker that brings the service provider and the service user together.<\/li>\n<li>The service providers register with the broker. The client requests the broker, which connects it to the service provider.<\/li>\n<li>The broker provided the infrastructure for communicating, finding, and configuring the subsystems through a simple API.<\/li>\n<\/ul>\n<h3>Structure<\/h3>\n<p>The Broker consists of five components:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-6538\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2023\/04\/BrokerStructure.png\" alt=\"BrokerStructure\" width=\"500\" height=\"245\" style=\"display: block; margin-left: auto; margin-right: auto;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2023\/04\/BrokerStructure.png 757w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2023\/04\/BrokerStructure-300x147.png 300w\" sizes=\"auto, (max-width: 500px) 100vw, 500px\" \/><\/p>\n<h3><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-6539\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2023\/04\/BrokerCRC.png\" alt=\"BrokerCRC\" width=\"650\" height=\"528\" style=\"display: block; margin-left: auto; margin-right: auto;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2023\/04\/BrokerCRC.png 829w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2023\/04\/BrokerCRC-300x244.png 300w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2023\/04\/BrokerCRC-768x623.png 768w\" sizes=\"auto, (max-width: 650px) 100vw, 650px\" \/><\/h3>\n<p><strong><code>Client<\/code><\/strong><\/p>\n<ul>\n<li>Implements the application functionality<\/li>\n<li>Sends requests to the server via the<code> Client-Side Proxy<\/code><\/li>\n<\/ul>\n<p><strong><code>Client-side Proxy<\/code><\/strong><\/p>\n<ul>\n<li>Encapsulates system-specific functionality<\/li>\n<li>Speaks the language of the<code> Client<\/code><\/li>\n<li>Mediates between the client and the broker<\/li>\n<\/ul>\n<p><strong><code>Server<\/code><\/strong><\/p>\n<ul>\n<li>Implements services<\/li>\n<li>Registers itself with the<code> Broker<\/code><code> <br \/><\/code><\/li>\n<\/ul>\n<p><strong><code>Server-side Proxy<\/code><\/strong><\/p>\n<ul>\n<li>Calls server services<\/li>\n<li>Speaks the language of the <code>Server<\/code><\/li>\n<li>Encapsulates system-specific functionality<\/li>\n<li>Mediates between the <code>Server<\/code> and the <code>Broker<\/code><code> <\/code><\/li>\n<\/ul>\n<p><strong><code>Broker<\/code><\/strong><\/p>\n<ul>\n<li>(Un-)registers servers<\/li>\n<li>Transfers messages and errors<\/li>\n<li>Locates servers<\/li>\n<\/ul>\n<p>There are more interesting aspects of the Broker Architecture.<\/p>\n<\/p>\n<h4>Interface Definition Language<\/h4>\n<p>Typically, the server&#8217;s services are specified in an <a href=\"https:\/\/en.wikipedia.org\/wiki\/Interface_description_language\">interface definition language <\/a>(IDL).&nbsp; The IDL is the base&nbsp; <code>Client-Side Proxy<\/code>, and the&nbsp;<code>Server-Side Proxy. <\/code>Here are the two typical steps:<\/p>\n<ol>\n<li>Use the programming language agnostic IDL to generate the stub and skeleton for a specific programming language. This can often be done for various programming languages.<\/li>\n<li>Implement the <code>Client-Side Proxy<\/code> and the&nbsp;<code>Server-Side Proxy<\/code> based on the stub and skeleton.<\/li>\n<\/ol>\n<p>The IDL may also be registered within the <code>Broker<\/code>.This helps the broker to find the appropriate <code>Server-Side Proxy<\/code>, when asked by the <code>Client-Side Proxy<\/code>.<\/p>\n<p>The benefit of the Broker architecture is that clients and servers can communicate with each other, although they speak different programming languages.<\/p>\n<p>So far, I have described the static structure of the Broker Pattern. Let&#8217;s consider the interplay between the client and the server.<\/p>\n<h4>The Client has a Request<\/h4>\n<p>When a client wants to use a remote service, it asks the <code>Broker<\/code> for it. The <code>Broker<\/code> returns the <code>Client-Side Proxy<\/code> that implements the remote service&#8217;s interface. The <code>Client-Side Proxy<\/code> manages the caching of data, the inter-process communication, or the <a href=\"https:\/\/en.wikipedia.org\/wiki\/Marshalling_(computer_science)\">marshaling<\/a>\/serialization of data. It connects with the <code>Server-Side Proxy<\/code> that calls the server. The&nbsp;<code>Server-Side Proxy<\/code> has a similar job, such as the <code>Client-Side Proxy<\/code>. It essentially unmarshals the data and speaks the language of the server. When the server sends back the function call result, it calls its <code>Server-Side Proxy<\/code>, which communicates with the client-side proxy.&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<h4>The Server registers itself<\/h4>\n<p>During the initialization of the system, the Broker starts itself and enters its event loop. The server initializes and registers itself with the Broker. The server receives the registration confirmation from the Broker and enters its event loop.<\/p>\n<h4>Additional Brokers<\/h4>\n<p>Sometimes there is more than one Broker. One Broker can, therefore, delegate the services request to another Broker. In this advanced architecture, each Broker must support two protocols. One internal protocol for its <code>Client-Side Proxy<\/code> and <code>Server-Side Proxy <\/code>and one external protocol for the other Broker.<\/p>\n<p>&nbsp;<\/p>\n<p>There are many examples of Broker architectures.<\/p>\n<h3>Examples<\/h3>\n<h4><a href=\"https:\/\/web.cs.wpi.edu\/~rek\/DCS\/D04\/SunRPC.html\">SunRPC<\/a><\/h4>\n<p>The program <code>rpcgen<\/code> generates from an interface description stubs, skeletons, and an XDR file for data conversion<\/p>\n<p><code>rpcgen<\/code> provides an API for remote function calls in C<\/p>\n<h4><a href=\"https:\/\/en.wikipedia.org\/wiki\/Java_remote_method_invocation\">Remote Method Invocation (RMI)<\/a>:<\/h4>\n<p>The <code>rmic<\/code> compiler generates the stubs and skeletons from a server interface in Java<\/p>\n<p>In contrast to the function references in <a href=\"https:\/\/web.cs.wpi.edu\/~rek\/DCS\/D04\/SunRPC.html\">SunRPC<\/a>, these are object references<\/p>\n<p>Since Java 5, the stubs and skeletons are implicitly created by the Java Virtual Machine<\/p>\n<h4><a href=\"https:\/\/en.wikipedia.org\/wiki\/Common_Object_Request_Broker_Architecture\">Common Object Request Broker Architecture (CORBA)<\/a>:<\/h4>\n<p>CORBA uses the IDL for the interface description<\/p>\n<p>The syntax of the IDL is C++ oriented<\/p>\n<p>CORBA refers to objects such as RMI<\/p>\n<p>Standardized implementation of the <code>IDL2Language<\/code> compiler exists for Ada, C, C++, Lisp, Smalltalk, Java, COBOL, PL\/I, and Python<\/p>\n<p>C++ Implementation: <a href=\"http:\/\/www.dre.vanderbilt.edu\/~schmidt\/ACE.html\">The ADAPTIVE Communication Environment (ACE) <\/a><\/p>\n<h4><a href=\"https:\/\/en.wikipedia.org\/wiki\/SOAP\">Simple Object Access Protocol (SOAP)<\/a>:<\/h4>\n<p>The interface description language is called <a href=\"https:\/\/en.wikipedia.org\/wiki\/Web_Services_Description_Language\">Web Service Definition Language (WSDL)<\/a><\/p>\n<p>WSDL is a text-based protocol (XML)<\/p>\n<p>The WSDL is not only declarative but specifies the type of data transfer and the service points<\/p>\n<p>Implementations of a <a href=\"https:\/\/en.wikipedia.org\/wiki\/Web_Services_Description_Language\">wsdl2Compiler <\/a>code generator exist in Java, C++, Python, Perl, &#8230;<\/p>\n<p>C++ Implementation: <a href=\"https:\/\/www.genivia.com\/dev.html\">gSOAP<\/a><\/p>\n<h3>Pros and Cons<\/h3>\n<h4>Pros<\/h4>\n<ul>\n<li>Location independence of the client and server through the intermediary<\/li>\n<li>The client is independent of implementation changes of the server<\/li>\n<li>Modifications of the IDL can be implemented easily so that only slight adaptations on the client and server are necessary<\/li>\n<li>It is easy to port the Broker to a different system because the client and server don&#8217;t use system-specific functionality<\/li>\n<li>Clients or servers speaking other programming languages are pretty easy to add if an IDL to the programming language compiler is available<\/li>\n<li>New services are straightforward to add because they can use the existing Broker architecture<\/li>\n<li>SOAP is a text-based protocol; This makes it pretty easy to analyze the communication with UNIX-based command line tools or implement a simple program sending text.<\/li>\n<\/ul>\n<h4>Cons<\/h4>\n<ul>\n<li>Due to the many indirections (<code>Client<\/code> -&gt; <code>Client-Side Proxy<\/code> -&gt; <code>Broker<\/code> -&gt; <code>Server-Side Proxy<\/code> -&gt; <code>Server<\/code>), data marshaling, and inter-process communication, the performance is not good enough; after the first communication of the <code>Client-Side Proxy<\/code> and the&nbsp;<code>Server-Side Proxy<\/code> is performed, both components often talk directly with each other without the intermediate <code>Broker<\/code><\/li>\n<li>The communication of the parties depends on many components and is, therefore, difficult to debug in the error case; besides SOAP, the other protocols are binary<\/li>\n<\/ul>\n<h2>What&#8217;s Next?<\/h2>\n<p>The <a href=\"https:\/\/en.wikipedia.org\/wiki\/Model%E2%80%93view%E2%80%93controller\">Model-View-Controller (MVC)<\/a> is one of the classic architectural patterns. It divides the program logic of a user interface into separate components model, view, and controller. The model manages the data and rules of the application. The view represents the data, and the controller interacts with the user. Let me present the MVC in my next post.<\/p>\n<p>&nbsp;<\/p>\n<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The Broker Pattern structures distributed software systems that interact with remote service invocations. It is responsible for coordinating the communication, its results, and exceptions.<\/p>\n","protected":false},"author":21,"featured_media":6537,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[379],"tags":[],"class_list":["post-6540","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-patterns"],"_links":{"self":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/6540","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/users\/21"}],"replies":[{"embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/comments?post=6540"}],"version-history":[{"count":0,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/6540\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media\/6537"}],"wp:attachment":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media?parent=6540"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/categories?post=6540"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/tags?post=6540"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}