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:
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:
- 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.
- 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.
- 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".
waitForReadersmethod 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.
- The writer is used to send a value update. The update is sent with the value "hello".
waitForNoReadersmethod 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:
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:
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:
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:
- As for the writer, we initialize the DataStorm run time by creating a DataStorm node.
- 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.
- A single key reader is created on the topic for the key "foo".
- We call
getNextUnreadon the reader object to read the next available sample. This method will only return once a sample is received.
- We print the key and value associated with the received sample.
Compiling and linking the reader looks much the same as for the writer:
Running Writer and Reader in C++
To run writer and reader, we first start the writer in a separate window:
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:
The reader runs, prints out "foo says hello!" and exits. Once the reader exits, the writer exits as well.