Skip to content

Commit 16606be

Browse files
committed
Minor updates
1 parent 337b04a commit 16606be

File tree

4 files changed

+29
-24
lines changed

4 files changed

+29
-24
lines changed

example/sample-projects/databus-shapes/client/main.cpp

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ using namespace ftxui;
3232
struct GlobalState {
3333
std::mutex mutex;
3434
std::map<std::string, ShapeMsg> shapes;
35+
ScreenInteractive* screen = nullptr;
3536
} g_state;
3637

3738
int main() {
@@ -49,13 +50,22 @@ int main() {
4950
return -1;
5051
}
5152

53+
auto screen = ScreenInteractive::Fullscreen();
54+
g_state.screen = &screen;
55+
5256
// 2. Setup Participant and Handlers
5357
auto group = std::make_shared<dmq::Participant>(transport);
5458
static Serializer<void(ShapeMsg)> serializer;
5559

5660
auto shapeHandler = [](const std::string& topic, ShapeMsg msg) {
57-
std::lock_guard<std::mutex> lock(g_state.mutex);
58-
g_state.shapes[topic] = msg;
61+
{
62+
std::lock_guard<std::mutex> lock(g_state.mutex);
63+
g_state.shapes[topic] = msg;
64+
}
65+
// Data-Driven Refresh: Trigger UI redraw immediately on data arrival
66+
if (g_state.screen) {
67+
g_state.screen->PostEvent(Event::Custom);
68+
}
5969
};
6070

6171
group->RegisterHandler<ShapeMsg>(SystemTopic::SquareId, serializer, [&](ShapeMsg m) { shapeHandler(SystemTopic::Square, m); });
@@ -67,26 +77,21 @@ int main() {
6777
std::thread netThread([&]() {
6878
while (running) {
6979
group->ProcessIncoming();
70-
std::this_thread::sleep_for(std::chrono::milliseconds(10));
80+
std::this_thread::sleep_for(std::chrono::milliseconds(5)); // High-freq check
7181
}
7282
});
7383

7484
// 4. FTXUI Render Loop
75-
auto screen = ScreenInteractive::Fullscreen();
76-
7785
auto renderer = Renderer([&] {
78-
// Create a canvas that fills the available space
7986
auto c = Canvas(200, 100);
8087

8188
{
8289
std::lock_guard<std::mutex> lock(g_state.mutex);
8390
for (auto const& [topic, shape] : g_state.shapes) {
8491
if (topic == SystemTopic::Square) {
85-
// Draw Square (using block coordinates)
8692
for(int x=0; x<10; ++x)
8793
for(int y=0; y<10; ++y) c.DrawBlock(shape.x * 2 + x, shape.y * 2 + y, true, Color::Blue);
8894
} else if (topic == SystemTopic::Circle) {
89-
// Draw Circle
9095
c.DrawBlockCircle(shape.x * 2, shape.y * 2, 10, Color::Red);
9196
}
9297
}
@@ -99,34 +104,24 @@ int main() {
99104
canvas(std::move(c)) | flex | border,
100105
hbox({
101106
filler(),
102-
text("Newest data received via DataBus Multicast | 'q' to quit") | dim,
107+
text("Data-Driven Rendering (Sync with DataBus) | 'q' to quit") | dim,
103108
filler()
104109
})
105110
});
106111
});
107112

108-
// Handle 'q' to quit and sink all other events to prevent character artifacts
109113
auto component = CatchEvent(renderer, [&](Event event) {
110114
if (event == Event::Character('q')) {
111115
screen.Exit();
112116
return true;
113117
}
114-
return true; // Sink all events
115-
});
116-
117-
// Refresh UI at 20fps to reduce flickering
118-
std::thread refresher([&] {
119-
while (running) {
120-
std::this_thread::sleep_for(std::chrono::milliseconds(50));
121-
screen.PostEvent(Event::Custom);
122-
}
118+
return false;
123119
});
124120

125121
screen.Loop(component);
126122

127123
running = false;
128124
netThread.join();
129-
refresher.join();
130125
transport.Close();
131126

132127
std::cout << "\r" << std::flush;

example/sample-projects/databus-shapes/server/main.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,10 @@ int main() {
3636
std::cout << "Local Interface: " << localIP << std::endl;
3737

3838
#ifdef DMQ_DATABUS_SPY
39+
// Start Spy Bridge to export DataBus traffic to the Spy Console via Multicast
40+
// Note: Use port 9999 to isolate spy traffic from app traffic on 8000.
3941
SpyBridge::StartMulticast("239.1.1.1", 9999, localIP);
42+
4043
dmq::DataBus::RegisterStringifier<ShapeMsg>(SystemTopic::Square, [](const ShapeMsg& m) {
4144
return "Square X=" + std::to_string(m.x) + " Y=" + std::to_string(m.y);
4245
});
@@ -83,7 +86,7 @@ int main() {
8386
dmq::DataBus::Publish<ShapeMsg>(SystemTopic::Circle, circle);
8487

8588
angle += 0.1f;
86-
std::this_thread::sleep_for(std::chrono::milliseconds(50));
89+
std::this_thread::sleep_for(std::chrono::milliseconds(33));
8790
}
8891

8992
transport.Close();

src/delegate-mq/predef/transport/linux-udp/MulticastTransport.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,12 @@ class MulticastTransport : public ITransport
5555
return -1;
5656
}
5757

58+
// Disable loopback so we don't receive our own packets
59+
int loop = 0;
60+
if (setsockopt(m_socket, IPPROTO_IP, IP_MULTICAST_LOOP, &loop, sizeof(loop)) < 0) {
61+
std::cerr << "IP_MULTICAST_LOOP failed." << std::endl;
62+
}
63+
5864
// Set the interface for outgoing multicast data
5965
struct in_addr localAddr;
6066
inet_aton(localInterface, &localAddr);

src/delegate-mq/predef/transport/win32-udp/MulticastTransport.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,13 @@ class MulticastTransport : public ITransport
4141
m_addr.sin_port = htons(port);
4242
inet_pton(AF_INET, groupAddr, &m_addr.sin_addr);
4343

44+
// Disable loopback so we don't receive our own packets
45+
int loop = 0;
46+
setsockopt(m_socket, IPPROTO_IP, IP_MULTICAST_LOOP, (const char*)&loop, sizeof(loop));
47+
4448
in_addr localAddr;
4549
inet_pton(AF_INET, localInterface, &localAddr);
4650
setsockopt(m_socket, IPPROTO_IP, IP_MULTICAST_IF, (const char*)&localAddr, sizeof(localAddr));
47-
48-
int ttl = 3;
49-
setsockopt(m_socket, IPPROTO_IP, IP_MULTICAST_TTL, (const char*)&ttl, sizeof(ttl));
5051

5152
std::cout << "[Multicast] Created PUB on " << localInterface << ", Group: " << groupAddr << ":" << port << std::endl;
5253
}

0 commit comments

Comments
 (0)