-
Notifications
You must be signed in to change notification settings - Fork 215
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Bring DataStorm demos to ice-demos (#227)
- Loading branch information
Showing
88 changed files
with
3,246 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# Copyright (c) ZeroC, Inc. All rights reserved. | ||
|
||
$(demo)_dependencies = DataStorm Ice | ||
|
||
demos += $(demo) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
This demo illustrates how to implement a custom encoder and decoder for the topic value type | ||
`chrono::system_clock::time_point`. | ||
|
||
To run the demo, start the writer and specify the name of a city: | ||
|
||
```shell | ||
writer | ||
``` | ||
|
||
In a separate window, start the reader: | ||
|
||
```shell | ||
reader | ||
``` | ||
|
||
The reader will print the time sent by the writer. You can start multiple writers and readers. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
// | ||
// Copyright (c) ZeroC, Inc. All rights reserved. | ||
// | ||
|
||
#include <DataStorm/DataStorm.h> | ||
#include <Ice/Ice.h> | ||
|
||
#include <chrono> | ||
#include <iostream> | ||
|
||
using namespace std; | ||
|
||
namespace DataStorm | ||
{ | ||
template<> struct Encoder<chrono::system_clock::time_point> | ||
{ | ||
static Ice::ByteSeq encode(const chrono::system_clock::time_point&) | ||
{ | ||
assert(false); // Not used by the reader but it still needs to be declared. | ||
return Ice::ByteSeq{}; | ||
} | ||
}; | ||
|
||
template<> struct Decoder<chrono::system_clock::time_point> | ||
{ | ||
static chrono::system_clock::time_point decode(const Ice::ByteSeq& data) | ||
{ | ||
// Decode the number of seconds since epoch. The value is encoded in a way which doesn't depend on the | ||
// platform endianess (little endian with variable number of bytes). | ||
long long int value = 0; | ||
for (auto p = data.rbegin(); p != data.rend(); ++p) | ||
{ | ||
value = value * 256 + static_cast<long long int>(*p); | ||
} | ||
return chrono::time_point<chrono::system_clock>(chrono::seconds(value)); | ||
} | ||
}; | ||
}; | ||
|
||
int | ||
main(int argc, char* argv[]) | ||
{ | ||
try | ||
{ | ||
// CtrlCHandler must be called before the node is created or any other threads are started. | ||
Ice::CtrlCHandler ctrlCHandler; | ||
|
||
// Instantiates node. | ||
DataStorm::Node node(argc, argv, "config.reader"); | ||
|
||
// Shutdown the node on Ctrl-C. | ||
ctrlCHandler.setCallback([&node](int) { node.shutdown(); }); | ||
|
||
// Instantiates the "time" topic. | ||
DataStorm::Topic<string, chrono::system_clock::time_point> topic(node, "time"); | ||
|
||
// Instantiate a reader to read the time from all the topic cities. | ||
auto reader = DataStorm::makeAnyKeyReader(topic); | ||
|
||
// Wait for at least on writer to connect. | ||
reader.waitForWriters(); | ||
|
||
// Prints out the received samples. | ||
reader.onSamples( | ||
nullptr, | ||
[](const DataStorm::Sample<string, chrono::system_clock::time_point>& sample) | ||
{ | ||
auto time = chrono::system_clock::to_time_t(sample.getValue()); | ||
char timeString[100]; | ||
if (strftime(timeString, sizeof(timeString), "%x %X", localtime(&time)) == 0) | ||
{ | ||
timeString[0] = '\0'; | ||
} | ||
cout << "received time for `" << sample.getKey() << "': " << timeString << endl; | ||
}); | ||
|
||
// Exit once the user hits Ctrl-C to shutdown the node. | ||
node.waitForShutdown(); | ||
} | ||
catch (const std::exception& ex) | ||
{ | ||
cerr << ex.what() << endl; | ||
return 1; | ||
} | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
// Copyright (c) ZeroC, Inc. All rights reserved. | ||
|
||
#include <DataStorm/DataStorm.h> | ||
#include <Ice/Ice.h> | ||
|
||
#include <iostream> | ||
#include <string> | ||
#include <thread> | ||
|
||
using namespace std; | ||
|
||
namespace DataStorm | ||
{ | ||
template<> struct Encoder<chrono::system_clock::time_point> | ||
{ | ||
static Ice::ByteSeq encode(const chrono::system_clock::time_point& time) | ||
{ | ||
// Encode the number of seconds since epoch. The value is encoded in a way which doesn't depend on the | ||
// platform endianess (little endian with variable number of bytes). | ||
Ice::ByteSeq data; | ||
auto value = chrono::time_point_cast<chrono::seconds>(time).time_since_epoch().count(); | ||
while (value) | ||
{ | ||
data.push_back(static_cast<std::byte>(value % 256)); | ||
value = value / 256; | ||
} | ||
return data; | ||
} | ||
}; | ||
|
||
template<> struct Decoder<chrono::system_clock::time_point> | ||
{ | ||
static chrono::system_clock::time_point decode(const Ice::ByteSeq&) | ||
{ | ||
assert(false); // Not used by the reader but it still needs to be declared. | ||
return chrono::system_clock::time_point(); | ||
} | ||
}; | ||
}; | ||
|
||
int | ||
main(int argc, char* argv[]) | ||
{ | ||
try | ||
{ | ||
// CtrlCHandler must be called before the node is created or any other threads are started. | ||
Ice::CtrlCHandler ctrlCHandler; | ||
|
||
// Instantiates node. | ||
DataStorm::Node node(argc, argv, "config.writer"); | ||
|
||
// Shutdown the node on Ctrl-C. | ||
ctrlCHandler.setCallback([&node](int) { node.shutdown(); }); | ||
|
||
// Asks for city name to publish updates | ||
string city; | ||
cout << "Please enter city name: "; | ||
getline(cin, city); | ||
|
||
// Instantiates the "time" topic. | ||
DataStorm::Topic<string, chrono::system_clock::time_point> topic(node, "time"); | ||
|
||
// Instantiate a writer to write the time from the given city. | ||
auto writer = DataStorm::makeSingleKeyWriter(topic, city); | ||
|
||
while (!node.isShutdown()) | ||
{ | ||
writer.update(chrono::system_clock::now()); | ||
this_thread::sleep_for(chrono::seconds(1)); | ||
} | ||
} | ||
catch (const std::exception& ex) | ||
{ | ||
cerr << ex.what() << endl; | ||
return 1; | ||
} | ||
return 0; | ||
} |
Oops, something went wrong.