stinger-ipc 0.0.5__py3-none-any.whl → 0.0.7__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (28) hide show
  1. {stinger_ipc-0.0.5.dist-info → stinger_ipc-0.0.7.dist-info}/METADATA +2 -2
  2. {stinger_ipc-0.0.5.dist-info → stinger_ipc-0.0.7.dist-info}/RECORD +27 -23
  3. {stinger_ipc-0.0.5.dist-info → stinger_ipc-0.0.7.dist-info}/entry_points.txt +1 -0
  4. stingeripc/components.py +5 -5
  5. stingeripc/lang_symb.py +21 -4
  6. stingeripc/templates/html/app.js.jinja2 +117 -0
  7. stingeripc/templates/html/index.html.jinja2 +38 -0
  8. stingeripc/templates/html/style.css.jinja2 +187 -0
  9. stingeripc/templates/rust/Cargo.toml.jinja2 +1 -1
  10. stingeripc/templates/rust/client/Cargo.toml.jinja2 +7 -5
  11. stingeripc/templates/rust/client/examples/client.rs.jinja2 +9 -10
  12. stingeripc/templates/rust/client/src/lib.rs.jinja2 +112 -55
  13. stingeripc/templates/rust/{connection → payloads}/Cargo.toml.jinja2 +8 -4
  14. stingeripc/templates/rust/{connection → payloads}/examples/pub_and_recv.rs.jinja2 +11 -11
  15. stingeripc/templates/rust/payloads/src/lib.rs.jinja2 +4 -0
  16. stingeripc/templates/rust/{connection → payloads}/src/payloads.rs.jinja2 +10 -2
  17. stingeripc/templates/rust/server/Cargo.toml.jinja2 +4 -4
  18. stingeripc/templates/rust/server/examples/server.rs.jinja2 +8 -12
  19. stingeripc/templates/rust/server/src/lib.rs.jinja2 +92 -69
  20. stingeripc/tools/cli.py +73 -0
  21. stingeripc/tools/markdown_generator.py +4 -2
  22. stingeripc/tools/python_generator.py +12 -10
  23. stingeripc/tools/rust_generator.py +46 -27
  24. stingeripc/templates/rust/connection/src/lib.rs.jinja2 +0 -262
  25. {stinger_ipc-0.0.5.dist-info → stinger_ipc-0.0.7.dist-info}/WHEEL +0 -0
  26. {stinger_ipc-0.0.5.dist-info → stinger_ipc-0.0.7.dist-info}/licenses/LICENSE +0 -0
  27. {stinger_ipc-0.0.5.dist-info → stinger_ipc-0.0.7.dist-info}/top_level.txt +0 -0
  28. /stingeripc/templates/rust/{connection → payloads}/src/handler.rs.jinja2 +0 -0
@@ -1,262 +0,0 @@
1
-
2
- pub mod payloads;
3
-
4
- extern crate paho_mqtt as mqtt;
5
- use uuid::Uuid;
6
- use futures::{StreamExt};
7
- use mqtt::{Message, QOS_1, QOS_2};
8
- use std::collections::HashMap;
9
- use tokio::sync::mpsc::{self, Sender, Receiver};
10
- use tokio::sync::mpsc::error::SendError;
11
- use std::time::Duration;
12
- use std::sync::{Arc, Mutex};
13
- use serde::Serialize;
14
- use serde_json::{self};
15
-
16
- #[derive(Clone, Debug)]
17
- pub struct ReceivedMessage {
18
- pub message: mqtt::Message,
19
- pub subscription_id: i32,
20
- }
21
-
22
- #[derive(Clone)]
23
- pub struct MessageStreamer {
24
- pub subscriptions: Arc<Mutex<HashMap<i32, Sender<ReceivedMessage>>>>,
25
- pub strm: mqtt::AsyncReceiver<Option<mqtt::Message>>
26
- }
27
-
28
- impl MessageStreamer {
29
- pub async fn receive_loop(&mut self) -> Result<(), paho_mqtt::Error> {
30
- println!("Starting message receive loop...");
31
- while let Some(msg_opt) = self.strm.next().await {
32
- if let Some(msg) = msg_opt {
33
- let mut sub_id_itr = msg.properties().iter(mqtt::PropertyCode::SubscriptionIdentifier);
34
- while let Some(sub_id_prop) = sub_id_itr.next() {
35
- let opt_sub_id = sub_id_prop.get_int();
36
- if let Some(sub_id) = opt_sub_id {
37
- if let Some(tx) = self.subscriptions.lock().unwrap().get(&sub_id) {
38
- let tx2 = tx.clone();
39
- let received_msg = ReceivedMessage {
40
- message: msg.clone(),
41
- subscription_id: sub_id,
42
- };
43
- tokio::spawn(async move {
44
- if let Err(e) = tx2.send(received_msg).await {
45
- eprintln!("Failed to send message: {}", e);
46
- }
47
- });
48
- } else {
49
- println!("No subscription found for ID: {}", sub_id);
50
- }
51
- } else {
52
- println!("No subscription ID found in message properties.");
53
- }
54
- }
55
- } else {
56
- println!("Received None, stream closed.");
57
- }
58
-
59
- }
60
- Ok(())
61
- }
62
- }
63
-
64
- #[derive(Clone)]
65
- pub struct MessagePublisher {
66
- pub channel: Sender<mqtt::Message>
67
- }
68
-
69
- impl MessagePublisher {
70
- pub async fn publish(&mut self, msg: mqtt::Message) -> Result<(), SendError<Message>> {
71
- println!("Message publisher publishing message to {}", msg.topic());
72
- self.channel.send(msg).await
73
- }
74
-
75
- pub async fn publish_simple(&mut self, topic: String, payload: String) -> Result<(), SendError<Message>> {
76
- let msg = mqtt::Message::new(topic, payload, QOS_1);
77
- self.publish(msg).await
78
- }
79
-
80
- pub async fn publish_structure<T: Serialize>(&mut self, topic: String, data: &T) -> Result<(), SendError<Message>> {
81
- let payload = serde_json::to_string(data).unwrap().into_bytes();
82
- let mut pub_props = mqtt::Properties::new();
83
- let _ = pub_props.push_string(mqtt::PropertyCode::ContentType, "application/json");
84
- let msg = mqtt::MessageBuilder::new()
85
- .topic(topic)
86
- .payload(payload)
87
- .properties(pub_props)
88
- .qos(QOS_1)
89
- .finalize();
90
- self.publish(msg).await
91
- }
92
-
93
- pub async fn publish_retained_structure<T: Serialize>(&mut self, topic: String, data: &T) -> Result<(), SendError<Message>> {
94
- let payload = serde_json::to_string(data).unwrap().into_bytes();
95
- let mut pub_props = mqtt::Properties::new();
96
- let _ = pub_props.push_string(mqtt::PropertyCode::ContentType, "application/json");
97
- let msg = mqtt::MessageBuilder::new()
98
- .topic(topic)
99
- .payload(payload)
100
- .properties(pub_props)
101
- .qos(QOS_1)
102
- .retained(true)
103
- .finalize();
104
- self.publish(msg).await
105
- }
106
-
107
- pub async fn publish_request_structure<T: Serialize>(&mut self, topic: String, data: &T, response_topic: &str, correlation_id: Uuid) -> Result<(), SendError<Message>> {
108
- println!("Publishing request structure to {} with responses going to {}", topic, response_topic);
109
- let uuid_vec: Vec<u8> = correlation_id.as_bytes().to_vec();
110
- let mut pub_props = mqtt::Properties::new();
111
- let _ = pub_props.push_binary(mqtt::PropertyCode::CorrelationData, uuid_vec);
112
- let _ = pub_props.push_string(mqtt::PropertyCode::ResponseTopic, response_topic);
113
- let _ = pub_props.push_string(mqtt::PropertyCode::ContentType, "application/json");
114
- let payload = serde_json::to_string(data).unwrap().into_bytes();
115
- let msg = mqtt::MessageBuilder::new()
116
- .topic(topic)
117
- .qos(QOS_2)
118
- .properties(pub_props)
119
- .payload(payload)
120
- .finalize();
121
- self.publish(msg).await
122
- }
123
-
124
- pub async fn publish_response_structure<T: Serialize>(&mut self, topic: String, data: &T, correlation_id: Option<Vec<u8>>) -> Result<(), SendError<Message>> {
125
- let mut pub_props = mqtt::Properties::new();
126
- if let Some(corr_id) = correlation_id {
127
- let _ = pub_props.push_binary(mqtt::PropertyCode::CorrelationData, corr_id);
128
- }
129
- let payload = serde_json::to_string(data).unwrap().into_bytes();
130
- let _ = pub_props.push_string(mqtt::PropertyCode::ContentType, "application/json");
131
- let msg = mqtt::MessageBuilder::new()
132
- .topic(topic)
133
- .payload(payload)
134
- .qos(QOS_2)
135
- .properties(pub_props)
136
- .finalize();
137
- self.publish(msg).await
138
- }
139
- }
140
-
141
- pub struct Connection {
142
- pub client: paho_mqtt::AsyncClient,
143
- pub client_id: String,
144
- pub next_subscription_id: i32,
145
- pub subscriptions: Arc<Mutex<HashMap<i32, Sender<ReceivedMessage>>>>,
146
- pub pub_chan_rx: Receiver<mqtt::Message>,
147
- pub pub_chan_tx: Sender<mqtt::Message>,
148
- }
149
-
150
- impl Connection {
151
- pub async fn new(broker: &str) -> Result<Self, paho_mqtt::Error> {
152
- println!("Creating new connection object");
153
- let client_id_uuid = Uuid::new_v4();
154
- let client_id = client_id_uuid.to_string();
155
- let create_opts = mqtt::CreateOptionsBuilder::new()
156
- .server_uri(broker)
157
- .mqtt_version(mqtt::MQTT_VERSION_5)
158
- .allow_disconnected_send_at_anytime(true)
159
- .client_id(client_id.clone())
160
- .finalize();
161
- let (tx, rx) = mpsc::channel(32);
162
- let client = paho_mqtt::AsyncClient::new(create_opts)?;
163
- Ok(Self {
164
- client,
165
- client_id,
166
- next_subscription_id: 5,
167
- subscriptions: Arc::new(Mutex::new(HashMap::new())),
168
- pub_chan_rx: rx,
169
- pub_chan_tx: tx,
170
- })
171
- }
172
-
173
- {%for broker_name, broker in stinger.brokers.items()%}
174
- pub async fn new_{{broker.class_name | snake_case}}() -> Result<Self, paho_mqtt::Error> {
175
- Connection::new("{{broker.hostname or 'localhost'}}:{{broker.port or 1883}}").await
176
- }
177
- {%endfor%}
178
-
179
- fn get_subscription_id(&mut self) -> i32 {
180
- let id = self.next_subscription_id;
181
- self.next_subscription_id += 1;
182
- id
183
- }
184
-
185
- pub async fn get_streamer(&mut self) -> MessageStreamer {
186
- let strm = self.client.get_stream(25);
187
- MessageStreamer {
188
- subscriptions: self.subscriptions.clone(),
189
- strm,
190
- }
191
- }
192
-
193
- pub fn get_publisher(&self) -> MessagePublisher {
194
- let channel = self.pub_chan_tx.clone();
195
- MessagePublisher {
196
- channel,
197
- }
198
- }
199
-
200
- pub async fn connect(&self) -> Result<paho_mqtt::ServerResponse, paho_mqtt::Error> {
201
- println!("Connecting to mqtt");
202
- let conn_opts = mqtt::ConnectOptionsBuilder::new_v5()
203
- .keep_alive_interval(Duration::from_secs(20))
204
- .clean_start(true)
205
- .finalize();
206
-
207
- self.client.connect(conn_opts).await
208
- }
209
-
210
- pub async fn disconnect(&self) -> Result<paho_mqtt::ServerResponse, paho_mqtt::Error> {
211
- self.client.disconnect(None).await
212
- }
213
-
214
- pub async fn publish(&self, msg: mqtt::Message) -> Result<(), paho_mqtt::Error> {
215
- println!("Publishing message to {}", msg.topic());
216
- let pub_result = self.client.publish(msg).await;
217
- pub_result
218
- }
219
-
220
- async fn add_subscription_receiver(
221
- &mut self,
222
- subscription_id: i32,
223
- tx: Sender<ReceivedMessage>,
224
- ) -> Result<(), paho_mqtt::Error> {
225
- let mut subscriptions = self.subscriptions.lock().unwrap();
226
- subscriptions.insert(subscription_id, tx);
227
- Ok(())
228
- }
229
-
230
- pub async fn subscribe(&mut self, topic: &str, tx: Sender<ReceivedMessage>) -> Result<i32, paho_mqtt::Error> {
231
- println!("Subscribing to {}", topic);
232
- let sub_opts = mqtt::SubscribeOptions::new(true, false, mqtt::RetainHandling::SendRetainedOnSubscribe);
233
- let mut sub_props = mqtt::Properties::new();
234
- let subscription_id = self.get_subscription_id();
235
- let si_prop = mqtt::Property::new(mqtt::PropertyCode::SubscriptionIdentifier, subscription_id as i32).unwrap();
236
- let _ = sub_props.push(si_prop);
237
- let _sub_result = self.client.subscribe_with_options(topic, paho_mqtt::QOS_1, sub_opts, sub_props).await;
238
- self.add_subscription_receiver(subscription_id, tx).await?;
239
- Ok(subscription_id)
240
- }
241
-
242
- pub async fn start_loop(&mut self) {
243
- println!("Starting MQTT loop...");
244
-
245
- let mut streamer = self.get_streamer().await;
246
- let _stream_task = tokio::spawn(async move {
247
- let _ = streamer.receive_loop().await;
248
- });
249
-
250
- loop {
251
- let opt_msg = self.pub_chan_rx.recv().await;
252
- if let Some(msg) = opt_msg {
253
- let pub_result = self.publish(msg).await;
254
- match pub_result {
255
- Ok(_r) => println!("Message was published"),
256
- Err(e) => eprintln!("Error publishing: {:?}", e),
257
- }
258
- }
259
- }
260
- }
261
-
262
- }