Hello World application

This page shows how to create a DataStorm application with C++.

On this page:

Writing and compiling a writer in C++

The source code for the writer takes only a few lines and is shown in full here:

C++
#include <DataStorm/DataStorm.h>

using namespace std;

int
main(int argc, char* argv[])
{
    try
    {
        DataStorm::Node node(argc, argv);

        DataStorm::Topic<string, string> topic(node, "hello");
    	auto writer = DataStorm::makeSingleKeyWriter(topic, "foo");
        topic.waitForReaders();

        writer.update("hello");

    	topic.waitForNoReaders();
    }
    catch(const std::exception& ex)
    {
        cerr << ex.what() << endl;
        return 1;
    }
    return 0;
}

This writer create a topic named "hello" and a writer for the key "foo". It waits for a reader to connect, then sends a value update "hello", and finally waits for the reader to disconnect. 

Every DataStorm source file starts with an include directive for DataStorm.h, which contains the definitions for the DataStorm run time. The body of the main method goes through the following steps:

  1. We instantiate a DataStorm node. The node constructor is provided with the argc/argv parameters to allow the node to parse DataStorm command line arguments which might be provided to the program.
  2. A topic named "hello" is created. The topic template takes two template parameters: the key and value types supported by the topic. In this example, we create a topic which uses string as the key and as the value types.
  3. A writer for the key "foo" is created using the topic created in the previous step. The writer is a single key writer, it can only be used to write values for the key "foo".
  4. The waitForReaders method call on the topic waits for readers to connect to the topic from this writer. This method will return as soon as a reader connects.
  5. The writer is used to send a value update. The update is sent with the value "hello".
  6. The waitForNoReaders method waits for all the readers to be disconnected form the topic to return.

Assuming that we have the writer code in a file called Writer.cpp, we can compile it as follows:

c++ -c Writer.cpp

This compiles both the application code. Depending on your platform, you may have to add additional include directives or other options to the compiler; please see the demo programs that ship with DataStorm for the details.

Finally, we need to link the writer into an executable:

c++ -o writer Writer.o -lDataStorm -lIce++11

Again, depending on the platform, the actual list of libraries you need to link against may be longer. The demo programs that ship with DataStorm contain all the detail.

Writing and compiling a reader in C++

The reader code looks very similar to the writer. Here it is in full source code:

C++
#include <DataStorm/DataStorm.h>

using namespace std;

int
main(int argc, char* argv[])
{
    try
    {
        DataStorm::Node node(argc, argv);
        DataStorm::Topic<string, string> topic(node, "hello");
        auto reader = DataStorm::makeSingleKeyReader(topic, "foo");
        auto sample = reader.getNextUnread();
        cout << sample.getKey() << " says " << sample.getValue() << "!" << endl;
    }
    catch(const std::exception& ex)
    {
        cerr << ex.what() << endl;
        return 1;
    }
    return 0;
}

Note that the overall code layout is the same as for the writer: we include the header for the DataStorm run time, and we instantiate a DataStorm node.

The reader code does the following:

  1. As for the writer, we initialize the DataStorm run time by creating a DataStorm node.
  2. We create a topic named "hello" with the same key and value types. It's important that the key and value types match the types used by the "hello" node in the writer.
  3. A single key reader is created on the topic for the key "foo".
  4. We call getNextUnread on the reader object to read the next available sample. This method will only return once a sample is received.
  5. We print the key and value associated with the received sample.

Compiling and linking the reader looks much the same as for the writer:

c++ -c Reader.cpp
c++ -o reader Reader.cpp -lDataStorm -lIce++11

Running Writer and Reader in C++

To run writer and reader, we first start the writer in a separate window:

./writer

At this point, we won't see anything because the writer simply waits for a reader to connect to it. We run the reader in a different window:

./reader

The reader runs, prints out "foo says hello!" and exits. Once the reader exits, the writer exits as well.