Distributed Command Pattern is a pattern for connected systems which implements command pattern. It frees designers from thinking about the communication and helps them concentrate on implementing commands as if it is a regular desktop application. The framework takes care of the communication.

Introduction

Distributed Command Pattern is a proposed pattern for solving the extra architectural design issues which need to be considered for connected systems than designing regular applications. The goal of this pattern is to “Have the same design for both standalone and connected systems”. This pattern allows developers to concentrate on designing a regular application following the Command Pattern [Gamma 1995] without ever thinking that this application is going to connect with other systems. This pattern frees a developer from thinking about the transport and communication protocol while designing the commands, and keeps the architecture simple. Designers need not worry about how to send necessary data to the server when some command gets executed and how to process received data from the server and then reflect on the UI of the connected applications. When a command gets executed locally on one running application, distributed design pattern takes care of the hurdles of executing the same command in all connected systems simultaneously in order to keep them in the same state. From a designer’s point of view, the architecture remains as simple as a regular desktop application designed using regular command pattern having no network awareness, yet it performs all the necessary communications in order to execute the commands on all connected applications as if the command has been invoked internally.

Overview of Command Pattern

Command Pattern is a very widely used design pattern for both web and desktop applications. It allows developers to think and design architecture in terms of actions performed by the user and the system. For example, when a file is opened, a FileOpenCommand is issued and necessary code is written only for that command. This simplifies architecture design because when implementing an action, the job needed to do is narrow and focused. Command Pattern also forces design of a very reusable system because the small command classes are reusable by design and can be invoked either by the user or by any system. For example, a user can issue FileOpenCommand by selecting “Open” from “File” menu, or it can be automatically invoked by a plug-in or by a macro, or by another command which needs to open a file. As a result, the command is written only once and maintained in only one place, but it offers wide reusability.

The problem of distributed systems

While designing the architecture of connected systems, the design needs to consider how commands are executed not only in the application where it originated, but also on all other connected systems. For example, in a chat application which is connected to a chat room, if one user writes a message, the message needs to be shown on the user’s screen and also simultaneously on screens of all other users who are present on the chat room. This raises an architectural design issue where a command needs to be invoked on the originating application and necessary data be sent to the server in order to broadcast the message to all other connected users. Each connected user needs to receive the data from server, translate it, and then show it on the screen.

This situation results in extra architectural design and coding for the communication performed for each action. First of all, following the command pattern, we can only perform action on the same application, more specifically, on the same process. Then we need to prepare for a network transmission. So, there is some specific protocol developed to maintain communication with the server and the client. Both the server and the client maintain a code base to understand this protocol. When a message is received from the network, the message needs to be processed and then some specific code needs to be written in order to perform actions based on the message.

Here is a design which shows how a typical chat application is developed:

Figure 1 - Regular Connected Applications.

The drawbacks of this design are:

For each action, there is a command, and there is a protocol for transmitting necessary data to the server and also for the clients to receive data from the server.

If a command is changed, the protocol also needs to be modified in order to reflect the changed execution.

For each action, the developer needs to think of three issues:

A command needs to be developed in order to perform the action in the application locally.

A message for sending to the server and the logic for packing necessary data.

A receiver for receiving that message, the logic for processing it and performing necessary action.

Distributed Command Pattern

Distributed command pattern solves the problems by introducing an architecture where the same command is invoked automatically on connected systems when any one system executes a command. This pattern offers a way to invoke a command in-process on a connected system from another connected system. There is no need to send a custom message containing required data to the server to notify about the command that is executed, and there is also no need for clients to read and parse a message in order to extract necessary data and then act according to the message. The very command that is executed on a system is serialized and transmitted to other systems and then executed locally in order to simulate the same situation that occurred on the originating system.

Distributed Command Pattern has the following benefits:

A Command class is developed exactly the same way as it is developed for simple standalone systems which have no network awareness.

There is no need for designers to develop custom protocols or design messages in order to send or receive necessary data for each command.

When a command is invoked, it not only gets executed on the same process, but also on other connected processes and connected computers over the network simultaneously.

It enables connected systems to be “in-sync” with all others without knowing about others’ presence.

It frees developers from thinking and implementing how to have all connected systems in-sync with each other as the exact same command is executed in all processes just as if the command was invoked locally in each connected system.

Developers need not worry whether the command is invoked from the same process or is coming from another process.

It reduces the amount of code needed for communication as there is a unified way to send or receive any command.

It is possible to seamlessly implement undo/redo of commands. If one application performs “undo”, all connected system perform “undo” exactly the same way.

Here is an illustration, how a command gets executed on all connected systems simultaneously:

Figure 2 - Distributed Execution of commands

Here is a sample code of a Command class which shows that the command actually has no awareness of connected systems:

There is another variance of the command pattern where a command is not directly executed by the invoker. It is executed via a command executor, namely Command Façade. Here’s how the variance looks like:

Figure 3a - Another variant of command pattern

The difference of this pattern is that, a command is not directly executed by the invoker. Invoker creates the instance of the Command and passes the command to the Command Facade. Command Facade actually calls the Execute method of the Command.

The benefit of this pattern is that, Command Facade can intercept any command execution and it can provide commands with some context and additional metadata which commands may need during execution. It also acts as a central command execution chamber. In Distributed Command Pattern, this specific feature is essential.

Step 2. Introducing a bus

In Distributed Command Pattern, the command façade is actually a command bus. Command Bus is created following the idea of Message Bus pattern [Snell 2004]. Message Bus introduces a bus which knows how to format a message and how to send the message via some channel. In DCP, Command Bus is also like a Message Bus. It maintains a pipeline of command executors which actually perform the execution of the command. Whenever a command is sent to the Command Bus, it informs all executors in the pipeline to execute the command. The executors work as channels and actually do the execution of the command.

Figure 4 - Command Bus

The command bus does not change the way a command is executed. Invoker writes the same code as before:

Step 3. Introducing executors

Executors provide the execution environment for commands. Although the commands contain necessary code to be executed, command executors provide necessary support to the commands during execution. For example, imagine a Windows form which has a text box. A command needs to add some text to the text box when executed. But the command is a separate class and it has no reference of the text box on the form. So, the form, being a command executor, intercepts the command from the command bus, and provides the command object with the reference to the text box. It then executes the command.

This is one type of local executor, where the command is executed in-process. There is another type of command executor which is called “DistributedCommandExecutor” which has the responsibility to send or receive commands via transporters. This type of executors are responsible for keeping all connected applications in-sync and provide the command distribution service.

However, the distributed executor does not execute a command directly. It just sends the command using a transporter to another connected application. On the other end, there is another distributed command executor which receives the command from its own transporter. It then notifies the command bus that a command is received and the bus again sends the command through the pipeline for execution. This time, the local executor on the other end receives the command and executes the command in its own context. This results in execution of the same command on another computer just as if the command was created and executed there.

Figure 5 - High Level Architecture

Step 4. Introducing transporter

Transporter provides the communication service. It sits between a distributed command executor and the network communication library to provide transparent message transportation service. A transporter is created by the application according to the communication media it uses, and the application itself attaches the transporter with a distributed command executor. For example, when a client application connects to a server using a TCP/IP connection, it uses a transporter which has the ability to send and receive data using Sockets in binary mode. After successfully establishing a connection, a distributed executor is created for the transporter and the executor is put in the command bus for transmitting commands. For example:

The above pseudo code shows how a distributed executor is associated with a transporter. When the executor is added in the bus, it is ready to receive and send any command that is executed on the bus. As a result, when a command is executed on the bus, the command is sent to the distributed executor and the executor sends the command to the transporter which in turn transmits the message using the Socket it has created.

The architecture of Distributed Command Pattern is a type of pipeline architecture which can be viewed in this way also:

Figure 6 - Pipeline nature of the Distributed Command Pattern

Technical Architecture

The technical architecture of the DCP framework is as follows:

Figure 7 - Technical Architecture

Sequence of execution

When a command is sent to the command bus, the following execution occurs:

Command bus passes the command to each executor in the command pipeline.

If the executor is a local command executor, it executes the command directly.

If the executor is a distributed command executor, it decorates the command using a RemoteCommand class (Decorator Pattern [Gamma 1995]). This class contains the actual command and additionally contains a transporter ID which identifies from where the command is being sent.

The remote command is then sent to a transporter in order to send the command to a destination using some communication protocol.

On the other end, the transporter layer receives the message. It creates the command object from the message.

The command is sent to the distributed command executor which is attached with the transporter. The executor receives the command, and then it modifies the “Source” property of the RemoteCommand to set the transporter ID from which it received the command.

The executor notifies its command bus about the arrival of the command.

That command bus notifies all of its command executors in its pipeline to process the command.

During this time, the executor which originally notified the command bus about the received command again gets called by the command bus as it is in its pipeline. But it sees from the source property of the remote command that, it is that executor which sent the command to the bus. So, it does not again send the command to the transporter and thus prevents an infinite loop.

How to apply this pattern

Applications interested to implement this pattern needs to do the following:

Create the command bus only once, and once created, it does not change.

Create the transportation layer which implements ITransporter only once, and once created, it does not change.

Create a Local Command Executor in order to execute commands locally, and once created, it does not change usually.

Create a Distributed Command Executor in order to send commands to other connected applications using the transporter, and once created, it does not change usually.

Develop as many commands as needed according to requirement.

Sample chat application

A good example of the Distributed Command Pattern is a chat application. The chat application has two modes – server move and client mode.

Figure 8 - Sample Chat Application with DCP

Server Mode

The server starts a transporter to listen for connection from other clients. It also creates a local command bus and registers itself to receive the commands sent to the bus in order to execute the command locally.

Whenever a client connects to the server, the transporter created for the client is attached with a newly created Distributed Command Executor and then it is added in the command bus. As a result, any command that is sent to the bus gets executed both locally and is also sent to the connected client.

If the client sends any command, then the bus receives the command and then it sends the command through the pipeline again for execution. The command gets executed locally on the server and also the command is broadcasted to all other clients connected to the server. As a result, if one client says, “Hi”, all the clients and the server show “Hi”.

Client Mode

The client first creates a Command Bus and attaches itself as a command executor in order to execute commands locally.

When it connects to the server, it creates a Distributed Command Executor and adds the executor to the command bus. As a result, any command executed on the client is also sent to the server.

Similarly, in order to disconnect and notify everyone about leaving the chat room, this code is sufficient:

this._MyBus.Execute( new ClientLeftCommand( client ) );

Local Command Executor

Local command executor receives commands from the command bus and executes them locally. For example, the main form (the only form in the app) is a local command executor. It basically receives a command, decorates it with necessary UI support, and executes the command. For example, the following generic code is sufficient for a local command executor:

The Transportation Layer – HTTP Transportation

This application has a very handy transportation layer which communicates using HTTP protocol. There are two classes, one is “CollaborationServer” which acts as a server, and another is “CollaborationClient” which acts as a client to the server.

The transportation layer is completely generic. It accepts HTTP packets and sends the packets over the network. This library can also be used in a wide variety of applications in order to exchange both textual and binary data. It can also be used to create an HTTP server without writing any communication code at all as it already uses HTTP protocol to exchange data. Here’s a sample basic conversation:

Request:

GET / HTTP/1.1
Correlation-ID: 0B83745D-2AAB-4bce-8AC9-B8A590F07768

Response:

HTTP/1.1 200 Ok
Correlation-ID: 0B83745D-2AAB-4bce-8AC9-B8A590F07768

This library is a modified version of the original Bi-Directional HTTP Connection [Szymanski 2004]. It is customized and enhanced to make a client-server collaboration library. The specialty of this library is it opens only one TCP/IP socket and provides both ways communication over the same socket. As a result, it is a firewall friendly solution for peer-to-peer communication.

The collaboration library is fully object oriented. It sends or receives commands as HTTP messages. There is an HTTPMessage class which contains details about the message being sent or received. This HTTPMessage is inherited by a generic class called “Packet”. There are two types of packets, PacketRequest which is used to send a message to another party and a PacketResponse which is returned by the other party.

Figure 9 - Message and HTTP Packet hierarchy

An application only needs to extend PacketRequest and optionally GenerciSuccessPacket in order to provide its own messages. For example, WhatIsYourNameRequest class can extend PacketRequest to send request for identification from the other end and the other end can return MyIdentityResponse (extends GenericSuccessPacket) in order to respond to this request.

The CommandPacketRequest is the packet which takes a command and serializes it and converts the command to an HTTP Message. Similarly, it gets an HTTPMessage and constructs a command from the message body.

///<spanclass="code-SummaryComment"><summary></span>

The following are the classes which provide Distributed Command execution over HTTP protocol:

Figure 10 - DistributedCommandExecutorHTTP and Transporter

What is in the sample application

In the sample application, you will get the following:

A ready to use framework for Distributed Command Execution.

A very powerful communication library which offers bi-directional HTTP communication.

Comments and Discussions

I can understand your "Think in commands" statement an perhaps it could be solved using commands only.

This brings out another issue. You know assume that the server and clients share the same code base.
What if the server needs to execute some weird piece of code, lets say using tons of 3rd party libraries, an you don't want to distribute that to all you clients since they are merely interested in the results.

I haven't tested this or walked through your code enough to say that this will solve the problem entirely, it's just a thought that popped up in my mind this morning;

You now have an ICommand only. What if you created an IRequestCommand or and IServerCommand. These commands still get executed over the command bus (since they could implement ICommand) but clients can be taught to ignore them using different types of command busses (ServerCommandBus and ClientCommandBus of ICommandBus).

I haven't made 2 version of the ICommand because of pure "design pattern" reason. If I introduce these two concepts, the design pattern becomes too specific. Ofcourse, you can extend the core pattern and implement all these, in fact, that's what design patterns are for. You have found a great extensibility feature and that adds a bonus to this patterns success.