Network Filters

Overview

A network filter is a mechanism that allows transformation of data sent through TCP/IP sockets to be performed in a pluggable, layered fashion. Coherence for .NET supports custom filters, thus enabling users to modify the contents of the network traffic and is commonly used to add compression and encryption to data.

Custom Filters

To create a new filter, create a .NET class that implements the Tangosol.IO.IWrapperStreamFactory interface and optionally implements the Tangosol.Util.IXmlConfigurable interface. The IWrapperStreamFactory interface defines one method:

Stream GetStream(Stream stream);

that provides the stream to be wrapped ("filtered") (either on input - received message, or output - sending message) and expects a stream back that wraps the original stream. This method is called for each incoming and outgoing message.

Configuring Filters

There are two steps to configuring a filter. The first is to declare the filter in the <filters> XML element of the cache factory configuration file:

<coherence>
  <cluster-config>
    <filters>
      <filter>
        <filter-name>netFilter</filter-name>
        <filter-class>My.Example.NetFilter, MyAssembly</filter-class>
        <init-params>
          <init-param>
            <param-name>param1</param-name>
            <param-value>value1</param-value>
          </init-param>
          <init-param>
            <param-name>param2</param-name>
            <param-value>value2</param-value>
          </init-param>
        </init-params>
      </filter>
    </filters>
  </cluster-config>
...
</coherence>

The second step is to attach the filter to one or more specific services. To specify the filter for a specific service, for example the ExtendTcpCacheService service, add a <filter-name> element to the <use-filters> element of the service declaration in the cache configuration file.

<remote-cache-scheme>
  <scheme-name>extend-direct</scheme-name>
  <service-name>ExtendTcpCacheService</service-name>
  <initiator-config>
    <tcp-initiator>
    ...
    </tcp-initiator>

    <outgoing-message-handler>
    ...
    </outgoing-message-handler>

    <use-filters>
      <filter-name>netFilter</filter-name>
    </use-filters>

    ...
</remote-cache-scheme>

If the filter implements IXmlConfigurable, after instantiating the filter, Coherence will set the Config property with the following XML element:

<config>
  <param1>value1</param1>
  <param2>value2</param2>
</config>