Skip to content

Commit 39acbec

Browse files
committed
Add DataStorm demos
1 parent 00482e3 commit 39acbec

File tree

85 files changed

+3226
-2
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

85 files changed

+3226
-2
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ server
44
locator
55
publisher
66
subscriber
7+
reader
8+
writer
79
talk
810
chatserver
911
chatgl2client
@@ -17,6 +19,8 @@ chatpollclient
1719
!**/msbuild/locator
1820
!**/msbuild/publisher
1921
!**/msbuild/subscriber
22+
!**/msbuild/reader
23+
!**/msbuild/writer
2024

2125
!**/Sources/client
2226
!**/Sources/server

cpp/DataStorm/README.md

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
# C++ Demos
2+
3+
- [Overview](#overview)
4+
- [Building and Running the Demos on Linux and macOS](#building-and-running-the-demos-on-linux-and-macos)
5+
- [Building and Running the Demos on Windows](#building-and-running-the-demos-on-windows)
6+
7+
## Overview
8+
9+
This directory contains C++ sample programs for DataStorm. These examples are
10+
provided to get you started on using a particular DataStorm feature or coding
11+
technique.
12+
13+
## Building and Running the Demos on Linux and macOS
14+
15+
### Prerequisites
16+
17+
The makefiles require GNU make 3.80 or later.
18+
19+
On macOS, the command-line demos require the Xcode Command Line Tools to be
20+
installed (use `xcode-select --install` to install them).
21+
22+
If you've installed DataStorm in a non-standard location or want to use the
23+
source tree build, you'll need to set the `DATASTORM_HOME` environment variable
24+
with the path name of the installation directory or source tree:
25+
```
26+
export DATASTORM_HOME=~/testing/datastorm
27+
```
28+
29+
### Building the Demos
30+
31+
Review the settings in `make/Make.rules`. For example, set `OPTIMIZE=yes`
32+
to build with optimization.
33+
34+
When you're ready to start the build, run `make`, for example:
35+
```
36+
make V=1
37+
```
38+
39+
`V=1` in the example above turns on verbose output.
40+
41+
The `clean` and `distclean` targets allow you clean all the demos. `clean`
42+
removes the binary files created by the build; `distclean` removes all these
43+
files plus the C++ files generated by slice2cpp.
44+
45+
Note that `make clean` only removes the binary files for the configuration and
46+
platform specified with the `CONFIGS` and `PLATFORMS` variables in `Make.rules`.
47+
48+
You can also build or clean a single demo with `make <demo-path>[_clean|_distclean]`,
49+
for example:
50+
```
51+
make V=1 stock
52+
make stock_distclean
53+
```
54+
55+
### Running the Demos
56+
57+
Refer to the README.md file in each demo directory for usage instructions.
58+
59+
## Building and Running the Demos on Windows
60+
61+
### Building the Demos
62+
63+
Open the solution file `DataStorm C++ demos.sln` to build the sample programs.
64+
65+
The demos are configured to use the DataStorm NuGet package provided by ZeroC.
66+
The build downloads automatically this DataStorm NuGet package.
67+
68+
If you have disabled the automatic download of NuGet packages by Visual Studio,
69+
you need to restore the packages before you build using `Tools > NuGet Package
70+
Manager > Manage NuGet Packages for Solution...` in Visual Studio.
71+
72+
Then select your target configuration (Debug or Release) and platform (Win32
73+
or x64). Right click on the desired demo in the Solution Explorer window and
74+
select `Build`.
75+
76+
### Running the Demos
77+
78+
You need to add the DataStorm and Ice `bin` directories to your PATH before running
79+
the demos.
80+
81+
If you are building Debug, add the Debug `bin` directories with a command similar
82+
to:
83+
```
84+
set PATH=%USERPROFILE%\datastorm\demos\cpp\packages\zeroc.datastorm.v143.1.1.1\build\native\bin\x64\Debug;%USERPROFILE%\datastorm\demos\cpp\packages\zeroc.ice.v143.3.7.9\build\native\bin\x64\Debug;%PATH%
85+
```
86+
87+
If you are building Release, add the Release `bin` directories with a command
88+
similar to:
89+
```
90+
set PATH=%USERPROFILE%\datastorm\demos\cpp\packages\zeroc.datastorm.v143.1.1.1\build\native\bin\x64\Release;%USERPROFILE%\datastorm\demos\cpp\packages\zeroc.ice.v143.3.7.9\build\native\bin\x64\Release;%PATH%
91+
```
92+
93+
Then refer to the README.md file in each demo directory for usage instructions.

cpp/DataStorm/clock/Makefile.mk

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Copyright (c) ZeroC, Inc. All rights reserved.
2+
3+
$(demo)_dependencies = DataStorm Ice
4+
5+
demos += $(demo)

cpp/DataStorm/clock/README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
This demo illustrates how to implement a custom encoder and decoder for the
2+
topic value type `chrono::system_clock::time_point`.
3+
4+
To run the demo, start the writer and specify the name of a city:
5+
```
6+
writer
7+
```
8+
9+
In a separate window, start the reader:
10+
```
11+
reader
12+
```
13+
14+
The reader will print the time sent by the writer. You can start multiple
15+
writers and readers.

cpp/DataStorm/clock/Reader.cpp

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
//
2+
// Copyright (c) ZeroC, Inc. All rights reserved.
3+
//
4+
5+
#include <DataStorm/DataStorm.h>
6+
#include <Ice/Ice.h>
7+
8+
#include <chrono>
9+
#include <iostream>
10+
11+
using namespace std;
12+
13+
namespace DataStorm
14+
{
15+
template<> struct Encoder<chrono::system_clock::time_point>
16+
{
17+
static Ice::ByteSeq encode(const chrono::system_clock::time_point&)
18+
{
19+
assert(false); // Not used by the reader but it still needs to be declared.
20+
return Ice::ByteSeq{};
21+
}
22+
};
23+
24+
template<> struct Decoder<chrono::system_clock::time_point>
25+
{
26+
static chrono::system_clock::time_point decode(const Ice::ByteSeq& data)
27+
{
28+
// Decode the number of seconds since epoch. The value is encoded in a way which doesn't depend on the
29+
// platform endianess (little endian with variable number of bytes).
30+
long long int value = 0;
31+
for (auto p = data.rbegin(); p != data.rend(); ++p)
32+
{
33+
value = value * 256 + static_cast<long long int>(*p);
34+
}
35+
return chrono::time_point<chrono::system_clock>(chrono::seconds(value));
36+
}
37+
};
38+
};
39+
40+
int
41+
main(int argc, char* argv[])
42+
{
43+
try
44+
{
45+
// CtrlCHandler must be called before the node is created or any other threads are started.
46+
Ice::CtrlCHandler ctrlCHandler;
47+
48+
// Instantiates node.
49+
DataStorm::Node node(argc, argv, "config.reader");
50+
51+
// Shutdown the node on Ctrl-C.
52+
ctrlCHandler.setCallback([&node](int) { node.shutdown(); });
53+
54+
// Instantiates the "time" topic.
55+
DataStorm::Topic<string, chrono::system_clock::time_point> topic(node, "time");
56+
57+
// Instantiate a reader to read the time from all the topic cities.
58+
auto reader = DataStorm::makeAnyKeyReader(topic);
59+
60+
// Wait for at least on writer to connect.
61+
reader.waitForWriters();
62+
63+
// Prints out the received samples.
64+
reader.onSamples(
65+
nullptr,
66+
[](const DataStorm::Sample<string, chrono::system_clock::time_point>& sample)
67+
{
68+
auto time = chrono::system_clock::to_time_t(sample.getValue());
69+
char timeString[100];
70+
if (strftime(timeString, sizeof(timeString), "%x %X", localtime(&time)) == 0)
71+
{
72+
timeString[0] = '\0';
73+
}
74+
cout << "received time for `" << sample.getKey() << "': " << timeString << endl;
75+
});
76+
77+
// Exit once the user hits Ctrl-C to shutdown the node.
78+
node.waitForShutdown();
79+
}
80+
catch (const std::exception& ex)
81+
{
82+
cerr << ex.what() << endl;
83+
return 1;
84+
}
85+
return 0;
86+
}

cpp/DataStorm/clock/Writer.cpp

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
// Copyright (c) ZeroC, Inc. All rights reserved.
2+
3+
#include <DataStorm/DataStorm.h>
4+
#include <Ice/Ice.h>
5+
6+
#include <iostream>
7+
#include <string>
8+
#include <thread>
9+
10+
using namespace std;
11+
12+
namespace DataStorm
13+
{
14+
template<> struct Encoder<chrono::system_clock::time_point>
15+
{
16+
static Ice::ByteSeq encode(const chrono::system_clock::time_point& time)
17+
{
18+
// Encode the number of seconds since epoch. The value is encoded in a way which doesn't depend on the
19+
// platform endianess (little endian with variable number of bytes).
20+
Ice::ByteSeq data;
21+
auto value = chrono::time_point_cast<chrono::seconds>(time).time_since_epoch().count();
22+
while (value)
23+
{
24+
data.push_back(static_cast<std::byte>(value % 256));
25+
value = value / 256;
26+
}
27+
return data;
28+
}
29+
};
30+
31+
template<> struct Decoder<chrono::system_clock::time_point>
32+
{
33+
static chrono::system_clock::time_point decode(const Ice::ByteSeq&)
34+
{
35+
assert(false); // Not used by the reader but it still needs to be declared.
36+
return chrono::system_clock::time_point();
37+
}
38+
};
39+
};
40+
41+
int
42+
main(int argc, char* argv[])
43+
{
44+
try
45+
{
46+
// CtrlCHandler must be called before the node is created or any other threads are started.
47+
Ice::CtrlCHandler ctrlCHandler;
48+
49+
// Instantiates node.
50+
DataStorm::Node node(argc, argv, "config.writer");
51+
52+
// Shutdown the node on Ctrl-C.
53+
ctrlCHandler.setCallback([&node](int) { node.shutdown(); });
54+
55+
// Asks for city name to publish updates
56+
string city;
57+
cout << "Please enter city name: ";
58+
getline(cin, city);
59+
60+
// Instantiates the "time" topic.
61+
DataStorm::Topic<string, chrono::system_clock::time_point> topic(node, "time");
62+
63+
// Instantiate a writer to write the time from the given city.
64+
auto writer = DataStorm::makeSingleKeyWriter(topic, city);
65+
66+
while (!node.isShutdown())
67+
{
68+
writer.update(chrono::system_clock::now());
69+
this_thread::sleep_for(chrono::seconds(1));
70+
}
71+
}
72+
catch (const std::exception& ex)
73+
{
74+
cerr << ex.what() << endl;
75+
return 1;
76+
}
77+
return 0;
78+
}

cpp/DataStorm/clock/config.reader

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#
2+
# Use the localhost interface by default for DataStorm endpoints
3+
#
4+
Ice.Default.Host=localhost
5+
6+
#
7+
# DataStorm server endpoint
8+
#
9+
#DataStorm.Node.Server.Endpoints=tcp
10+
11+
#
12+
# DataStorm multicast endpoint
13+
#
14+
#DataStorm.Node.Multicast.Endpoints=udp -h 239.255.0.1 -p 10000
15+
16+
#
17+
# DataStorm Tracing
18+
#
19+
#DataStorm.Trace.Topic=1
20+
#DataStorm.Trace.Data=1
21+
#DataStorm.Trace.Session=1
22+
23+
#
24+
# Ice tracing
25+
#
26+
#Ice.Trace.Network=1

cpp/DataStorm/clock/config.writer

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#
2+
# Use the localhost interface by default for DataStorm endpoints
3+
#
4+
Ice.Default.Host=localhost
5+
6+
#
7+
# DataStorm server endpoint
8+
#
9+
#DataStorm.Node.Server.Endpoints=tcp
10+
11+
#
12+
# DataStorm multicast endpoint
13+
#
14+
#DataStorm.Node.Multicast.Endpoints=udp -h 239.255.0.1 -p 10000
15+
16+
#
17+
# DataStorm Tracing
18+
#
19+
#DataStorm.Trace.Topic=1
20+
#DataStorm.Trace.Data=1
21+
#DataStorm.Trace.Session=1
22+
23+
#
24+
# Ice tracing
25+
#
26+
#Ice.Trace.Network=1
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<packages>
3+
<package id="zeroc.datastorm.v143" version="1.1.0" targetFramework="native" />
4+
<package id="zeroc.ice.v143" version="3.7.8" targetFramework="native" />
5+
<package id="zeroc.icebuilder.msbuild" version="5.0.9" targetFramework="native" />
6+
</packages>

0 commit comments

Comments
 (0)