1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
use super::destination::MessageDestination;
use super::{ClassOfService, DeliveryMode, Message};
use crate::SolClientReturnCode;
use num_traits::FromPrimitive;
use solace_rs_sys as ffi;
use std::ffi::{c_void, CString, NulError};
use std::ptr;
use std::time::SystemTime;
use thiserror::Error;
use tracing::warn;

#[derive(Error, Debug)]
pub enum MessageBuilderError {
    #[error("builder recieved invalid args")]
    InvalidArgs(#[from] NulError),
    #[error("{0} arg need to be set")]
    MissingRequiredArgs(String),
    #[error("{0} size need to be less than {1} found {2}")]
    SizeErrorArgs(String, usize, usize),
    #[error("timestamp needs to be greater than UNIX_EPOCH")]
    TimestampError,
    #[error("solClient returned not ok code")]
    SolClientError,
    #[error("solClient message aloc failed")]
    MessageAlocFailure,
}

type Result<T> = std::result::Result<T, MessageBuilderError>;

pub struct OutboundMessage {
    _msg_ptr: ffi::solClient_opaqueMsg_pt,
}

unsafe impl Send for OutboundMessage {}

impl Drop for OutboundMessage {
    fn drop(&mut self) {
        let msg_free_result = unsafe { ffi::solClient_msg_free(&mut self._msg_ptr) };
        if SolClientReturnCode::from_i32(msg_free_result) != Some(SolClientReturnCode::Ok) {
            warn!("message was not dropped properly");
        }
    }
}

impl<'a> Message<'a> for OutboundMessage {
    unsafe fn get_raw_message_ptr(&self) -> ffi::solClient_opaqueMsg_pt {
        self._msg_ptr
    }
}

#[derive(Default)]
pub struct OutboundMessageBuilder {
    delivery_mode: Option<DeliveryMode>,
    destination: Option<MessageDestination>,
    message: Option<Vec<u8>>,
    correlation_id: Option<Vec<u8>>,
    class_of_service: Option<ClassOfService>,
    seq_number: Option<u64>,
    priority: Option<u8>,
    application_id: Option<Vec<u8>>,
    application_msg_type: Option<Vec<u8>>,
    user_data: Option<Vec<u8>>,
    sender_ts: Option<SystemTime>,
}

impl OutboundMessageBuilder {
    /// Creates a new [`OutboundMessageBuilder`].
    pub fn new() -> Self {
        Self::default()
    }
    pub fn delivery_mode(mut self, mode: DeliveryMode) -> Self {
        self.delivery_mode = Some(mode);
        self
    }

    pub fn application_id<M>(mut self, application_id: M) -> Self
    where
        M: Into<Vec<u8>>,
    {
        self.application_id = Some(application_id.into());
        self
    }

    pub fn application_msg_type<M>(mut self, message_type: M) -> Self
    where
        M: Into<Vec<u8>>,
    {
        self.application_msg_type = Some(message_type.into());
        self
    }

    pub fn destination(mut self, destination: MessageDestination) -> Self {
        self.destination = Some(destination);
        self
    }

    pub fn class_of_service(mut self, cos: ClassOfService) -> Self {
        self.class_of_service = Some(cos);
        self
    }

    pub fn seq_number(mut self, seq_num: u64) -> Self {
        self.seq_number = Some(seq_num);
        self
    }

    pub fn sender_timestamp(mut self, ts: SystemTime) -> Self {
        self.sender_ts = Some(ts);
        self
    }

    pub fn priority(mut self, priority: u8) -> Self {
        self.priority = Some(priority);
        self
    }

    pub fn user_data<D>(mut self, data: D) -> Self
    where
        D: Into<Vec<u8>>,
    {
        self.user_data = Some(data.into());

        self
    }

    pub fn payload<M>(mut self, message: M) -> Self
    where
        M: Into<Vec<u8>>,
    {
        // for attaching the message to the ptr, we have a couple of options
        // based on those options, we can create a couple of interfaces
        //
        // solClient_msg_setBinaryAttachmentPtr (solClient_opaqueMsg_pt msg_p, void *buf_p, solClient_uint32_t size)
        // Given a msg_p, set the contents of a Binary Attachment Part to the given pointer and size.
        //
        // solClient_msg_setBinaryAttachment (solClient_opaqueMsg_pt msg_p, const void *buf_p, solClient_uint32_t size)
        // Given a msg_p, set the contents of the binary attachment part by copying in from the given pointer and size.
        //
        // solClient_msg_setBinaryAttachmentString (solClient_opaqueMsg_pt msg_p, const char *buf_p)
        // Given a msg_p, set the contents of the binary attachment part to a UTF-8 or ASCII string by copying in from the given pointer until null-terminated.
        //
        // we will only use the binary ptr methods
        self.message = Some(message.into());

        self
    }

    pub fn correlation_id<M>(mut self, id: M) -> Self
    where
        M: Into<Vec<u8>>,
    {
        self.correlation_id = Some(id.into());
        self
    }

    pub fn build(self) -> Result<OutboundMessage> {
        // message allocation
        let mut msg_ptr: ffi::solClient_opaqueMsg_pt = ptr::null_mut();
        let msg_alloc_result = unsafe { ffi::solClient_msg_alloc(&mut msg_ptr) };
        if Some(SolClientReturnCode::Ok) != SolClientReturnCode::from_i32(msg_alloc_result) {
            return Err(MessageBuilderError::MessageAlocFailure);
        };

        // We do not check the return code for many of the setter functions since they only fail
        // on invalid msg_ptr. We validated the message ptr above, so no need to double check.

        // delivery_mode
        let Some(delivery_mode) = self.delivery_mode else {
            return Err(MessageBuilderError::MissingRequiredArgs(
                "delivery_mode".to_owned(),
            ));
        };
        unsafe { ffi::solClient_msg_setDeliveryMode(msg_ptr, delivery_mode as u32) };

        // destination
        let Some(destination) = self.destination else {
            return Err(MessageBuilderError::MissingRequiredArgs(
                "destination".to_owned(),
            ));
        };
        // destination is being copied by solClient_msg_setDestination
        // so it is fine to create a ptr for the destination.dest
        let mut destination: ffi::solClient_destination = ffi::solClient_destination {
            destType: destination.dest_type.to_i32(),
            dest: destination.dest.as_ptr(),
        };
        unsafe {
            ffi::solClient_msg_setDestination(
                msg_ptr,
                &mut destination,
                std::mem::size_of::<ffi::solClient_destination>(),
            )
        };

        if let Some(user_data) = self.user_data {
            if user_data.len()
                > ffi::SOLCLIENT_BUFINFO_MAX_USER_DATA_SIZE
                    .try_into()
                    .unwrap()
            {
                return Err(MessageBuilderError::SizeErrorArgs(
                    "user_data".to_owned(),
                    user_data.len(),
                    ffi::SOLCLIENT_BUFINFO_MAX_USER_DATA_SIZE
                        .try_into()
                        .unwrap(),
                ));
            }
            // We pass the ptr which is then copied over
            unsafe {
                ffi::solClient_msg_setUserData(
                    msg_ptr,
                    user_data.as_ptr() as *const c_void,
                    user_data.len() as u32,
                )
            };
        }

        // binary attachment
        // We pass the ptr which is then copied over
        let Some(message) = self.message else {
            return Err(MessageBuilderError::MissingRequiredArgs(
                "message".to_owned(),
            ));
        };
        unsafe {
            ffi::solClient_msg_setBinaryAttachment(
                msg_ptr,
                message.as_ptr() as *const c_void,
                message.len() as u32,
            )
        };

        // correlation_id
        if let Some(id) = self.correlation_id {
            // correlation_id is copied over
            unsafe { ffi::solClient_msg_setCorrelationId(msg_ptr, CString::new(id)?.as_ptr()) };
        }

        // Class of Service
        if let Some(cos) = self.class_of_service {
            unsafe { ffi::solClient_msg_setClassOfService(msg_ptr, cos.into()) };
        }

        // Sequence Number
        if let Some(seq_number) = self.seq_number {
            unsafe { ffi::solClient_msg_setSequenceNumber(msg_ptr, seq_number) };
        }

        // Priority
        if let Some(priority) = self.priority {
            unsafe { ffi::solClient_msg_setPriority(msg_ptr, priority.into()) };
        }

        // Sender timestamp
        if let Some(ts) = self.sender_ts {
            let ts = ts
                .duration_since(SystemTime::UNIX_EPOCH)
                .map_err(|_| MessageBuilderError::TimestampError)?;
            let ts: i64 = ts
                .as_millis()
                .try_into()
                .map_err(|_| MessageBuilderError::TimestampError)?;

            unsafe { ffi::solClient_msg_setSenderTimestamp(msg_ptr, ts) };
        }

        // Application ID
        if let Some(id) = self.application_id {
            // application id is copied over
            unsafe {
                ffi::solClient_msg_setApplicationMessageId(msg_ptr, CString::new(id)?.as_ptr())
            };
        }

        // Application Message Type
        if let Some(message_type) = self.application_msg_type {
            // application msg type is copied over
            unsafe {
                ffi::solClient_msg_setApplicationMsgType(
                    msg_ptr,
                    CString::new(message_type)?.as_ptr(),
                )
            };
        }

        Ok(OutboundMessage { _msg_ptr: msg_ptr })
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::message::{DestinationType, MessageDestination};

    #[test]
    fn it_should_build_message() {
        let dest = MessageDestination::new(DestinationType::Topic, "test_topic").unwrap();
        let _ = OutboundMessageBuilder::new()
            .delivery_mode(DeliveryMode::Direct)
            .destination(dest)
            .payload("Hello")
            .build()
            .unwrap();
    }

    #[test]
    fn it_should_build_with_same_topic() {
        let dest = MessageDestination::new(DestinationType::Topic, "test_topic").unwrap();
        let message = OutboundMessageBuilder::new()
            .delivery_mode(DeliveryMode::Direct)
            .destination(dest)
            .payload("Hello")
            .build()
            .unwrap();
        let message_destination = message.get_destination().unwrap().unwrap();

        assert!("test_topic" == message_destination.dest.to_string_lossy());
    }

    #[test]
    fn it_should_build_with_same_corralation_id() {
        let dest = MessageDestination::new(DestinationType::Topic, "test_topic").unwrap();
        let message = OutboundMessageBuilder::new()
            .delivery_mode(DeliveryMode::Direct)
            .destination(dest)
            .correlation_id("test_correlation")
            .payload("Hello")
            .build()
            .unwrap();

        let correlation_id = message.get_correlation_id().unwrap().unwrap();

        assert!("test_correlation" == correlation_id);
    }

    #[test]
    fn it_should_build_have_valid_exp() {
        let dest = MessageDestination::new(DestinationType::Topic, "test_topic").unwrap();
        let message = OutboundMessageBuilder::new()
            .delivery_mode(DeliveryMode::Direct)
            .destination(dest)
            .payload("Hello")
            .build()
            .unwrap();

        assert!(0 == message.get_expiration());
    }

    #[test]
    fn it_should_build_with_same_cos() {
        let dest = MessageDestination::new(DestinationType::Topic, "test_topic").unwrap();
        let message = OutboundMessageBuilder::new()
            .delivery_mode(DeliveryMode::Direct)
            .destination(dest)
            .class_of_service(ClassOfService::Two)
            .payload("Hello")
            .build()
            .unwrap();

        assert!(ClassOfService::Two == message.get_class_of_service().unwrap());
    }

    #[test]
    fn it_should_build_with_same_seq_num() {
        let dest = MessageDestination::new(DestinationType::Topic, "test_topic").unwrap();
        let message = OutboundMessageBuilder::new()
            .delivery_mode(DeliveryMode::Direct)
            .destination(dest)
            .seq_number(45)
            .payload("Hello")
            .build()
            .unwrap();

        assert!(45 == message.get_sequence_number().unwrap().unwrap());
    }

    #[test]
    fn it_should_build_with_same_priority() {
        let dest = MessageDestination::new(DestinationType::Topic, "test_topic").unwrap();
        let message = OutboundMessageBuilder::new()
            .delivery_mode(DeliveryMode::Direct)
            .destination(dest)
            .priority(3)
            .payload("Hello")
            .build()
            .unwrap();

        assert!(3 == message.get_priority().unwrap().unwrap());

        let dest = MessageDestination::new(DestinationType::Topic, "test_topic").unwrap();
        let message = OutboundMessageBuilder::new()
            .delivery_mode(DeliveryMode::Direct)
            .destination(dest)
            .payload("Hello")
            .build()
            .unwrap();

        assert!(message.get_priority().unwrap().is_none());
    }

    #[test]
    fn it_should_build_with_same_application_id() {
        let dest = MessageDestination::new(DestinationType::Topic, "test_topic").unwrap();
        let message = OutboundMessageBuilder::new()
            .delivery_mode(DeliveryMode::Direct)
            .destination(dest)
            .application_id("test_id")
            .payload("Hello")
            .build()
            .unwrap();

        assert!(Some("test_id") == message.get_application_message_id());

        let dest = MessageDestination::new(DestinationType::Topic, "test_topic").unwrap();
        let message = OutboundMessageBuilder::new()
            .delivery_mode(DeliveryMode::Direct)
            .destination(dest)
            .payload("Hello")
            .build()
            .unwrap();

        assert!(message.get_application_message_id().is_none());
    }

    #[test]
    fn it_should_build_with_same_application_msg_type() {
        let dest = MessageDestination::new(DestinationType::Topic, "test_topic").unwrap();
        let message = OutboundMessageBuilder::new()
            .delivery_mode(DeliveryMode::Direct)
            .destination(dest)
            .application_msg_type("test_id")
            .payload("Hello")
            .build()
            .unwrap();

        assert!(Some("test_id") == message.get_application_msg_type());

        let dest = MessageDestination::new(DestinationType::Topic, "test_topic").unwrap();
        let message = OutboundMessageBuilder::new()
            .delivery_mode(DeliveryMode::Direct)
            .destination(dest)
            .payload("Hello")
            .build()
            .unwrap();

        assert!(message.get_application_msg_type().is_none());
    }

    #[test]
    fn it_should_build_with_same_string_payload() {
        let dest = MessageDestination::new(DestinationType::Topic, "test_topic").unwrap();
        let message = OutboundMessageBuilder::new()
            .delivery_mode(DeliveryMode::Direct)
            .destination(dest)
            .payload("Hello")
            .build()
            .unwrap();

        let raw_payload = message.get_payload().unwrap().unwrap();

        assert!(b"Hello" == raw_payload);
    }

    #[test]
    fn it_should_build_with_same_user_data() {
        let dest = MessageDestination::new(DestinationType::Topic, "test_topic").unwrap();
        let message = OutboundMessageBuilder::new()
            .delivery_mode(DeliveryMode::Direct)
            .destination(dest)
            .payload("Hello")
            .user_data(32_u32.to_be_bytes())
            .build()
            .unwrap();

        let raw_user_data = message.get_user_data().unwrap().unwrap();

        assert!(32_u32.to_be_bytes() == raw_user_data);
    }

    #[test]
    fn it_should_build_with_same_sender_timestamp() {
        let dest = MessageDestination::new(DestinationType::Topic, "test_topic").unwrap();
        let now = SystemTime::now();
        let message = OutboundMessageBuilder::new()
            .delivery_mode(DeliveryMode::Direct)
            .destination(dest)
            .payload("Hello")
            .sender_timestamp(now)
            .build()
            .unwrap();

        let ts = message.get_sender_timestamp().unwrap().unwrap();

        let now = now
            .duration_since(SystemTime::UNIX_EPOCH)
            .unwrap()
            .as_millis();
        let ts = ts
            .duration_since(SystemTime::UNIX_EPOCH)
            .unwrap()
            .as_millis();

        assert!(now == ts);
    }
}