========================================
ExplicitServer Sample
========================================

This sample represents CEP application that uses the Object Model API.
Based on the explicit instantiation of a CEP server object, the
design and control of the CEP objects is entirely specified in its main
routine. The only external dependency is a user-defined function, which is
implemented in a separate Visual Studio Project. The user registers the
adapters and the query logic (in form of a LINQ query template) explicitly into
the engine and then binds them into a query, using specific adapter
configurations.

This application uses its own in-process CEP server. Necessary steps to modify 
the example towards a remote CEP server are explained below.

The sample data represents traffic control data. Two input streams are
processed:

  1. Road sensor readings from several sensors, delivering throughput and
     speed of vehicles.
  2. Reference data, containing sensor IDs and their location codes.

Here, both data streams are read from CSV files, using the appropriate text
file input adapter. On the output side, the text file output adapter is used
with an empty string as filename, which will cause the adapter to write its
results directly to the console window.

The sample query shows how to compute the average of the vehicle count for a
sliding one-minute window for each sensor separately and how to join the
real-time data stream with the reference data stream. Modeling such static or
slowly changing data as a CEP stream is an alternative to implementing a custom
lookup function towards a reference data store.

The result is represented by the outgoing stream of INSERT events. Each such
event is the average vehicle count for the minute preceding the given
timestamp, for all values that have passed the included filter condition.

This sample also demonstrates the usage of diagnostic views at runtime. After
the input data has been entirely consumed, a snapshot of diagnostic data about
the query is taken and displayed on the console.


========================================
Remote Server
========================================

This section explains how to modify the ExplicitServer sample project in order to 
turn it into an application that connects to a remote StreamInsight host
running on a different machine. It demonstrates the following aspects of
Microsoft StreamInsight:

  * Provisioning the StreamInsight product for a client-server architecture
  * Code changes to the ExplicitServer and adapter project for client-server
  * Provisioning of assemblies for the client-server case
  * As a secondary aspect, it shows how to use the debugger tool with the 
    standalone StreamInsight host

----------------------------------------
Installation
----------------------------------------

- You need to install the full Microsoft StreamInsight product on both the 
  client and the server machine.

- Consider making a backup copy of the CEPmetadata.sdf file in the Host\ 
  directory. This allows you to easily revert partial metadata changes 
  resulting from incomplete runs of the ExplicitServer application against
  the StreamInsightHost.

----------------------------------------
Code Changes 
----------------------------------------

ExplicitServer.cs:

- The client application needs to connect to the remote StreamInsightHost 
  server instead of creating a server in-process. This can be done by using 
  the following statement which needs to replace Server.Create():

    Server.Connect(new System.ServiceModel.EndpointAddress("http://<server>/StreamInsight"));

- Using the EndpointAddress class requires a reference to the ServiceModel 
  library:

    using System.ServiceModel;


- The input and output file paths need to point to valid files on the server 
  machine. Consider placing them in a well known directory on the server using 
  for instance the C: drive instead of relative path names:

    var sensorInputConf = new TextFileInputConfig
    {
        InputFileName = @"C:\Input Data\TrafficSensors.csv",
        Delimiter = '\t',
        CTIfrequency = 9,
        CultureName = "en-US",
        InputFieldOrders = new List<string>() { "SensorId", "AverageSpeed", "VehicularCount" }
    };
    var locationInputConf = new TextFileInputConfig
    {
        InputFileName = @"C:\Input Data\TrafficSensorLocations.csv",
        Delimiter = '\t',
        CTIfrequency = 100,
        CultureName = "en-US",
        InputFieldOrders = new List<string>() { "SensorId", "LocationId" }
    };

...

    var outputConf = new TextFileOutputConfig
    {
        OutputFileName = @"C:\Output Data\OutputEvents.csv",
        Delimiter = '\t',
        AdapterStopSignal = "StopAdapter"
    };

- Add a sleep after you have started the query to give you enough time to 
  attach the debugger to the host to see the diagnostics of the running query.
  You can simply specify an infinite sleep so that the client application
  will never reach the query.Stop(), which would discard the diagnostics.

    query.Start();
    System.Threading.Thread.Sleep(-1);
 

TextFilePointOutput.cs:

- Since client and server are running in different processes, adapters can no 
  longer communicate directly from the server to the client using an 
  EventWaitHandle. For simplicity, you can comment out the line that opens the 
  handle:

    // _adapterStopSignal = EventWaitHandle.OpenExisting(configInfo.AdapterStopSignal);


----------------------------------------
Provisioning 
----------------------------------------

Assemblies:
You need to provision the required assemblies in the working directories of
both the StreamInsightHost and the ExplicitServer application. Copy the following
assemblies from the binaries directory of the samples solutions to the working
directories on the server and client machines:

- TextFileInputAdapter.dll
- TextFileOutputAdapter.dll
- UserExtensions.dll

Firewall:
If you have a firewall that blocks web service communication between the client
and the server, you need to configure your firewall to that the ExplicitServer
application can open a connection to "http://<myserver>/StreamInsight".

Start:
- Execute StreamInsightHost.exe from the Host/ directory in a command shell on 
  the server machine.
- Execute ExplicitServer.exe and let it run into the Sleep(-1).
- Start the EventFlowDebugger and connect it to
  "http://<myserver>/StreamInsight". You can then access the diagnostics for 
  the query through the Object Explorer on the left hand side.
