use iceoryx2::port::publisher::Publisher;
use iceoryx2::prelude::*;
use crate::ipc_protocol::PlaySongAtHeartbeat;
use super::music_sync_subscriber::E_MIDI_MUSIC_SYNC_SERVICE;
#[derive(Debug)]
pub struct MusicSyncPublisher {
publisher: Publisher<ipc::Service, PlaySongAtHeartbeat, ()>,
}
impl MusicSyncPublisher {
pub fn new() -> Result<Self, String> {
let node = NodeBuilder::new()
.create::<ipc::Service>()
.map_err(|e| format!("Node creation failed: {e:?}"))?;
let service = node
.service_builder(
&ServiceName::new(E_MIDI_MUSIC_SYNC_SERVICE)
.map_err(|e| format!("Invalid service name: {e:?}"))?,
)
.publish_subscribe::<PlaySongAtHeartbeat>()
.open_or_create()
.map_err(|e| format!("Failed to create/open service: {e:?}"))?;
let publisher = service
.publisher_builder()
.create()
.map_err(|e| format!("Failed to create publisher: {e:?}"))?;
Ok(Self { publisher })
}
pub fn publish(&mut self, msg: &PlaySongAtHeartbeat) -> Result<(), String> {
self.publisher
.send_copy(*msg)
.map(|_| ())
.map_err(|e| format!("Failed to publish: {e:?}"))
}
}
unsafe impl Send for MusicSyncPublisher {}
unsafe impl Sync for MusicSyncPublisher {}