22 Network Filters for .NET Clients

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.

22.1 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 two methods:

Example 22-1 Methods on the IWrapperStreamFactory Interface

Stream GetInputStream(Stream stream);
Stream GetOutputStream(Stream stream);

that provide the input/output stream to be wrapped ("filtered") (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.

22.2 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. This is illustrated in Example 22-2:

Example 22-2 Configuring a Filter

<coherence>
  <cluster-config>
    <filters>
      <filter>
        <filter-name>gzip</filter-name>
        <filter-class>Tangosol.Net.CompressionFilter, Coherence</filter-class>
      </filter>
    </filters>
  </cluster-config>
...
</coherence>

Note:

GZip compression filter is supported in .NET framework version 2.0 or higher.

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.

Example 22-3 Attaching a Filter to a Service

<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>gzip</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:

Example 22-4 Setting the Config Property for a Filter that Implements IXmlConfigurable

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