Cilia/Getting Started Tutorial/Chains

From Wiki Adele Team
Jump to: navigation, search

Developing Cilia Chains

In this lesson, you will manipulate Cilia mediation chains. We will extend the Hello Mediator example to add other mediators to the chain. In addition, we will study other capabilities of mediators and bindings.

Adding a new Mediator (Encryptor)

This example will add a new mediator to our Hello World example. This new Mediator takes the data produced by the Hello Mediator instance and encrypts them. The data produced by the Encryptor mediator is sent to the standard output (console) in the same way that the Hello World Chain did.

To do that, we have to modify the mediation chain as follow:

Hello-chain-2.png

Encryptor Mediator Implementation

Processor Definition

The Encryptor class implements the processor of the Mediator. It takes the data content and then applies a DES encryption.

package fr.liglab.adele.cilia.components.mediators;

import fr.liglab.adele.cilia.Data;

/**
 * Data Encryption with DES
 * 
 */
public class Encryptor {
   
   /**
    * Encrypts data content
    * @param data
    * @return
    */
   public Data process(Data data) {
      if (data != null) {
         String strToEncrypt = (String)data.getContent();
         byte[] temp = SimpleChiper.getInstance().encrypt(strToEncrypt);
         data.setContent(temp);
      }
      return data;
   }
}

The processor description is as follows:

   <processor name="EncryptorProcessor"
      classname="fr.liglab.adele.cilia.components.mediators.Encryptor">
      <cilia:method name="process" data.type="fr.liglab.adele.cilia.Data" />
   </processor>

Mediator Description

In this mediator we use also the immmediate scheduler and the multicast dispatcher.

   
   <mediator-component name="EncryptorMediator" category="samples">
      <cilia:processor name="EncryptorProcessor" />
      <cilia:scheduler name="immediate-scheduler" />
      <cilia:dispatcher name="multicast-dispatcher" />
       <ports>
          <in-port name="unique" type="*" />
          <out-port name="unique" type="*" />
       </ports>
   </mediator-component>

Chain Description

<cilia>

  <chain id="HelloWorldChain-Enc">

    <!-- Adapters instances definition -->
    <adapters>
        <adapter-instance type="gui-adapter" id="entryAdapter" />
        <adapter-instance type="console-adapter" id="exitAdapter">
          <property name="console.header" value="ENCRYPTOR.OUT:" />
        </adapter-instance>
    </adapters>

    <!-- Mediators instances definition -->
    <mediators>
      <mediator-instance type="HelloMediator" id="hello"/>

 
      <mediator-instance type="EncryptorMediator" id="encryptor"/>

    </mediators>

    <!-- Bindings definition -->
    <bindings>
      <binding from="entryAdapter:unique" to="hello:unique" />
      <binding from="hello:unique" to="encryptor:unique" />
      <binding from="encryptor:unique" to="exitAdapter:unique" />
    </bindings>

  </chain>
</cilia>

In the new architecture an instance (e.g. encryptor) of the Encrypto Mediator is created. We use direct binding to connect the hello and encryptor mediators instances. Finally, we have defined the binding sending to the console (using the exitAdapter) from the encryptor. In this new architecture, the exitAdapter adapter instance has been configured to print out a header message before the print out the data.

A graphical representation of the chain is shown in the next figure:

Hello-chain-2.png


Hello-Encryption Chain Execution

Using the sources

Download the projects' sources (see the resources section). The download directory has a structure as follows:

  • encryption-mediators/ : the encryption mediation project directory.
  • hello-mediator/ : the hello world mediator directory
  • tp2_2.dscilia : the cilia chain Hello-Encryption Chain descriptor file

Use maven to build the mediators’ artifacts (mvn clean install). In each subdirectory a target directory is created and inside these directories you can find the respective artifacts (hello-mediator-1.0.0-SNAPSHOT.jar, encryption-mediators-1.0.0-SNAPSHOT.jar). Deploy these artifacts into the platform (copy into the load directory) and deploy also the chain description tp2_2.dscilia.

A window is presented, type an arbitrary text, into the console an incomprehensible message is shown.

Tp2-execution-encryptor.png

Using the compiled artifacts

Download hello-mediator-1.0.0-SNAPSHOT.jar | encryption-mediators-1.0.0-SNAPSHOT.jar | tp2_2.dscilia

Deploy the three artifacts into the load directory of the platform.

Using Other Mediators Capabilities

We will see now the different capabilities of Cilia mediators. More particularly, how the other mediator elements (scheduler and dispatcher) could change the mediator’s behavior. We will extend the last mediation chain by introducing a third Mediator, the Decryptor mediator. This mediator decrypts messages encrypted by its predecessor mediator. However, this decryption process is realized in a batch way, the decryption logic is executed by blocks of three encrypted messages. The chain architecture is as follow:

Hello-chain-3.png


Decryptor Mediator decrypt the encrypted messages generated by the Encryptor Mediator. This new Chain is described as follow:

<cilia>

  <chain id="chain_tp2_3">
    <mediators>
      <mediator-instance id="hello" type="HelloMediator"/>
      <mediator-instance id="encryptor" type="EncryptorMediator"/>
      <mediator-instance id="decryptor" type="DecryptorMediator">
        <scheduler>
          <property name="count">
              <item key="max" value="(data.name=*)"/>
          </property> 
          <property name="condition" value="(max=3)"/>
        </scheduler>
      </mediator-instance>
    </mediators>
   
    <bindings>
      <binding to="hello" protocol="gui-console"/>
      <binding from="hello" to="encryptor"/>
      <binding from="encryptor" to="decryptor"/>
      <binding from="encryptor" protocol="console">
        <property name="console.header" value="ENCRYPTOR.OUT:"/>
      </binding>
      <binding from="decryptor" protocol="console">
        <property name="console.header" value="DECRYPTOR.OUT:"/>
      </binding>
    </bindings>
   
  </chain>
</cilia>

Two main points to retain from this new chain:

  • The encryptor mediator can send multiple copies of its resulting process to different destinations.
  • The decryptor mediator does not process data received one by one but by block of 3 data.

Indeed, each mediator type is defined by three essential constituents:

  • Scheduler : performs the incoming data synchronization. The Scheduler’s synchronization logic (e.g. timer or counter) decides when to send collected data to the processor. Based on this, the scheduler triggers the data processing notification.
  • Processor : The processor implements the Mediator’s main data-processing algorithm. It is notified by the scheduler when the data are ready to be processed, it processes the data and passes the result to the dispatcher.
  • Dispatcher : The dispatcher receives the processed data from the processor. It decides where to deliver the data based on its internal logic and on the data content. The dispatcher’s destination choice is a logical one, as a consequence of the loosely-coupled relations between Mediators.

In the above example, encryptor mediator uses a multicast-dispatcher dispatcher, which as its name suggests, dispatches a copy of the resulting data to all recipients. The decryptor mediator uses counter-scheduler scheduler which starts the processing after predetermined number of data were collected for the mediator. The user can override the configuration properties of those internal constituents to change the mediator's behavior.

Execution of Chains

Download hello-mediator-1.0.0-SNAPSHOT.jar | encryption-mediators-1.0.0-SNAPSHOT.jar | tp2_3.dscilia


Example Resources

Source code in the SVN repository

svn checkout svn://svn.ligforge.imag.fr/svnroot/cilia/trunk/examples/cilia-tp2-projects

svn checkout https://svn.ligforge.imag.fr/svnroot/cilia/trunk/examples/cilia-tp2-projects

Browse the SVN code here.

Snapshot of source code

Get a snapshot of the SVN project here.

List of compiled artefacts

Encryptors Mediator bundle

Encryptor Chain File

Encryptor-Decryptor Chain File