29#include "rte/status.h"
31#include "rte/ipc/rte_ipc_pubsub.h"
48#define TRACK_OCCUPIED 1
49#define TRACK_UNKNOWN 2
62 printf(
"[Publisher] Track status publisher starting\n");
66 {.track_id = 1, .occupancy = TRACK_EMPTY, .speed_limit = 100, .description =
"Clear"},
67 {.track_id = 2, .occupancy = TRACK_OCCUPIED, .speed_limit = 40, .description =
"Train present"},
68 {.track_id = 3, .occupancy = TRACK_EMPTY, .speed_limit = 80, .description =
"Clear"},
69 {.track_id = 1, .occupancy = TRACK_OCCUPIED, .speed_limit = 50, .description =
"Train arriving"},
70 {.track_id = 2, .occupancy = TRACK_EMPTY, .speed_limit = 100, .description =
"Train left"},
73 for (
int i = 0; i < 5; i++) {
78 printf(
"[Publisher] Publishing track %u status: %s\n",
84 RTE_LOG_ERROR(
"Publish failed: %d", result);
88 printf(
"[Publisher] Publisher done\n");
103 printf(
"[Signal Manager] Started\n");
105 for (
int i = 0; i < 5; i++) {
110 &status,
sizeof(status),
115 printf(
"[Signal Manager] Track %u: ", status.
track_id);
117 if (status.
occupancy == TRACK_OCCUPIED) {
118 printf(
"Setting signal to RED (stop)\n");
120 printf(
"Setting signal to GREEN (go)\n");
122 printf(
"Setting signal to YELLOW (caution)\n");
125 printf(
"[Signal Manager] No update received (timeout)\n");
129 printf(
"[Signal Manager] Done\n");
140 printf(
"[Speed Manager] Started\n");
142 for (
int i = 0; i < 5; i++) {
147 &status,
sizeof(status),
152 printf(
"[Speed Manager] Track %u: Speed limit = %u km/h\n",
155 printf(
"[Speed Manager] No update received (timeout)\n");
159 printf(
"[Speed Manager] Done\n");
170 printf(
"[Route Manager] Started\n");
172 for (
int i = 0; i < 5; i++) {
177 &status,
sizeof(status),
182 if (status.
occupancy == TRACK_OCCUPIED) {
183 printf(
"[Route Manager] Track %u occupied - rerouting trains\n",
186 printf(
"[Route Manager] Track %u available - planning route\n",
190 printf(
"[Route Manager] No update received (timeout)\n");
194 printf(
"[Route Manager] Done\n");
204 rte_log_initialize();
207 printf(
"═══════════════════════════════════════════════════════════════\n");
208 printf(
"IPC Publish-Subscribe Example\n");
209 printf(
"═══════════════════════════════════════════════════════════════\n");
212 RTE_LOG_INFO(
"Creating pub-sub topic...");
216 .topic_name =
"track_status",
225 RTE_LOG_ERROR(
"Failed to create topic: %d", status);
229 RTE_LOG_INFO(
"Creating subscribers...");
235 .subscriber_name =
"signal_manager",
242 .subscriber_name =
"speed_manager",
249 .subscriber_name =
"route_manager",
255 RTE_LOG_INFO(
"Starting publisher and subscribers...");
258 pthread_t pub_thread, sig_thread, spd_thread, rte_thread;
266 pthread_join(pub_thread, NULL);
267 pthread_join(sig_thread, NULL);
268 pthread_join(spd_thread, NULL);
269 pthread_join(rte_thread, NULL);
272 RTE_LOG_INFO(
"Cleaning up...");
279 printf(
"═══════════════════════════════════════════════════════════════\n");
280 printf(
"Example Complete\n");
281 printf(
"═══════════════════════════════════════════════════════════════\n");
284 printf(
"Key Observations:\n");
285 printf(
"✓ Publisher (Track DB) sends status changes\n");
286 printf(
"✓ Three subscribers (Signal, Speed, Route) receive same message\n");
287 printf(
"✓ Each subscriber reacts independently\n");
288 printf(
"✓ Decoupled: Publisher doesn't know about subscribers\n");
289 printf(
"✓ Event-driven: Subscribers wake up when status changes\n");
rte_status_t
Common result/status codes.
static void * speed_manager_thread(void *arg)
Speed Manager - Reacts to track status by updating speed limits.
static void * route_manager_thread(void *arg)
Route Manager - Reacts to track status by planning routes.
static void * track_publisher_thread(void *arg)
Simulate track status changes and publish.
static void * signal_manager_thread(void *arg)
Signal Manager - Reacts to track status by updating signals.
rte_status_t rte_ipc_pubsub_topic_destroy(rte_ipc_pubsub_topic_t topic)
Destroy a publish-subscribe topic.
rte_status_t rte_ipc_pubsub_receive(rte_ipc_pubsub_subscriber_t subscriber, void *message_out, size_t message_size, rte_duration_ms_t timeout_ms)
Receive a message from subscribed topic (blocking with timeout).
struct rte_ipc_pubsub_subscriber_s * rte_ipc_pubsub_subscriber_t
Subscriber handle for receiving from a topic.
rte_status_t rte_ipc_pubsub_unsubscribe(rte_ipc_pubsub_subscriber_t subscriber)
Unsubscribe from a topic.
struct rte_ipc_pubsub_topic_s * rte_ipc_pubsub_topic_t
Publish-subscribe topic handle.
rte_status_t rte_ipc_pubsub_subscribe(rte_ipc_pubsub_subscriber_t *subscriber_out, const rte_ipc_pubsub_subscriber_config_t *config)
Subscribe to a topic.
rte_status_t rte_ipc_pubsub_topic_create(rte_ipc_pubsub_topic_t *topic_out, const rte_ipc_pubsub_topic_config_t *config)
Create a publish-subscribe topic.
rte_status_t rte_ipc_pubsub_publish(rte_ipc_pubsub_topic_t topic, const void *message, size_t message_size)
Publish a message to all subscribers on a topic.
Configuration for a subscriber.
Configuration for a pub-sub topic.
Track status event (published to all subscribers).