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
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
/*
    Copyright 2021 Sojan James

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

        http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.
*/

use cyclonedds_sys::*;
use std::convert::From;
use std::future::Future;
use std::os::raw::c_void;
use std::pin::Pin;
use std::sync::{Arc, Mutex};
use std::task::{Context, Poll, Waker};
//use std::convert::TryInto;

pub use cyclonedds_sys::{DdsDomainId, DdsEntity};

use std::marker::PhantomData;

use crate::Sample;
use crate::error::ReaderError;
use crate::{dds_listener::DdsListener, dds_qos::DdsQos, dds_topic::DdsTopic, DdsReadable, Entity};
use crate::serdes::{TopicType, SampleBuffer};

/// Builder structure for reader
pub struct ReaderBuilder<T: TopicType> {
    maybe_qos: Option<DdsQos>,
    maybe_listener: Option<DdsListener>,
    is_async  : bool,
    phantom : PhantomData<T>,
}

impl <T>ReaderBuilder<T> where T: TopicType {
    pub fn new() -> Self {
        Self {
            maybe_qos: None,
            maybe_listener: None,
            is_async : false,
            phantom: PhantomData,
        }
    }
    /// Create a reader with async support.  If this is enabled,
    /// the builder creates listeners internally. Any listener
    /// passed separately via the `with_listener` api will be
    /// ignored. 
    pub fn as_async(mut self) -> Self {
        self.is_async = true;
        self
    }

    /// Create a reader with the specified Qos
    pub fn with_qos(mut self, qos : DdsQos) -> Self {
        self.maybe_qos = Some(qos);
        self
    }

    /// Created a reader with the specified listener.
    /// Note that this is ignored if an async reader
    /// is created.
    pub fn with_listener(mut self, listener : DdsListener) -> Self {
        self.maybe_listener = Some(listener);
        self
    }

    pub fn create(self,  
        entity: &dyn DdsReadable,
        topic: DdsTopic<T>) -> Result<DdsReader<T>, DDSError> {
            if self.is_async {
                DdsReader::create_async(entity,topic,self.maybe_qos)
            } else {
                DdsReader::create_sync_or_async(entity, topic, self.maybe_qos, self.maybe_listener, ReaderType::Sync)
            }
            
        }
}


enum ReaderType {
    Async(Arc<Mutex<(Option<Waker>,Result<(),crate::error::ReaderError>)>>),
    Sync,
}


 struct Inner<T: Sized + TopicType> {
    entity: DdsEntity,
    _listener: Option<DdsListener>,
    reader_type : ReaderType,
    _phantom: PhantomData<T>,
    // The callback closures that can be attached to a reader
}

pub struct DdsReader<T: Sized + TopicType> {
    inner : Arc<Inner<T>>,
}

impl<'a, T> DdsReader<T>
where
    T: Sized + TopicType,
{

    pub fn create(
        entity: &dyn DdsReadable,
        topic: DdsTopic<T>,
        maybe_qos: Option<DdsQos>,
        maybe_listener: Option<DdsListener>,
    ) -> Result<Self, DDSError> {
        Self::create_sync_or_async(entity, topic, maybe_qos, maybe_listener, ReaderType::Sync)
    }

    fn create_sync_or_async(
        entity: &dyn DdsReadable,
        topic: DdsTopic<T>,
        maybe_qos: Option<DdsQos>,
        maybe_listener: Option<DdsListener>,
        reader_type : ReaderType,
    ) -> Result<Self, DDSError> {
        unsafe {
            let w = dds_create_reader(
                entity.entity().entity(),
                topic.entity().entity(),
                maybe_qos.map_or(std::ptr::null(), |q| q.into()),
                maybe_listener
                    .as_ref()
                    .map_or(std::ptr::null(), |l| l.into()),
            );

            if w >= 0 {
                Ok(DdsReader {
                    inner : Arc::new(Inner {entity: DdsEntity::new(w),
                        _listener: maybe_listener,
                        reader_type,
                        _phantom: PhantomData,})
                })
            } else {
                Err(DDSError::from(w))
            }
        }
    }

    /// Create an async reader. This constructor must be used if using any of the async functions.
    pub fn create_async(
        entity: &dyn DdsReadable,
        topic: DdsTopic<T>,
        maybe_qos: Option<DdsQos>,
    ) -> Result<Self, DDSError> {

        let waker = Arc::new(<Mutex<(Option<Waker>,Result<(),crate::error::ReaderError>)>>::new((None,Ok(()))));
        let waker_cb = waker.clone();
        let requested_deadline_waker = waker.clone();
        
        let listener = DdsListener::new()
            .on_data_available(move|_entity| {
                //println!("Data available ");
                let mut maybe_waker = waker_cb.lock().unwrap();
                if let Some(waker) = maybe_waker.0.take() {
                    waker.wake();
                }
            })
            .on_requested_deadline_missed(move |entity, status| {
                println!("Deadline missed: Entity:{:?} Status:{:?}", unsafe {entity.entity()}, status);
                let mut maybe_waker = requested_deadline_waker.lock().unwrap();
                maybe_waker.1 = Err(ReaderError::RequestedDeadLineMissed);
                if let Some(waker) = maybe_waker.0.take() {
                    waker.wake();
                }
            })
            .hook();

        match Self::create_sync_or_async(entity, topic, maybe_qos, Some(listener),ReaderType::Async(waker) ) {
            Ok(reader) => {
                Ok(reader)
            },
            Err(e) => Err(e),
        }
        
    }

    /// read synchronously
    pub fn read_now(&self,buf: &mut SampleBuffer<T>) -> Result<usize,DDSError> {
        Self::readn_from_entity_now(self.entity(),buf,false)
    }

    /// take synchronously
    pub fn take_now(&self,buf: &mut SampleBuffer<T>) -> Result<usize,DDSError> {
        Self::readn_from_entity_now(self.entity(),buf,true)
    }

    /// Read multiple samples from the reader synchronously. The buffer for the sampes must be passed in.
    /// On success, returns the number of samples read.
    pub fn readn_from_entity_now(entity: &DdsEntity, buf: &mut SampleBuffer<T>, take: bool) -> Result<usize,DDSError> {

        let (voidp, info_ptr) = unsafe {buf.as_mut_ptr()};
        let voidpp = voidp as *mut *mut c_void;
        //println!("Infoptr:{:?}",info_ptr);

        let ret = unsafe {
            if take {
                dds_take(entity.entity(), voidpp ,  info_ptr as *mut _, buf.len() as size_t, buf.len() as u32)
            } else {
                dds_read(entity.entity(), voidpp ,  info_ptr as *mut _, buf.len() as size_t, buf.len() as u32)
            }
        };
        if ret > 0 {
            // If first sample is value we assume all are
            if buf.is_valid_sample(0) {
                   Ok(ret as usize) 
            } else {
                    Err(DDSError::NoData)
            }
        } else {
                Err(DDSError::OutOfResources)
        } 
    }
  
    /// Read samples asynchronously. The number of samples actually read is returned.
    pub async fn read(&self, samples : &mut SampleBuffer<T>) -> Result<usize,ReaderError> {
        if let ReaderType::Async(waker) = &self.inner.reader_type {
               let future_sample = SampleArrayFuture::new(self.inner.entity.clone(), waker.clone(),samples ,FutureType::Read);
                future_sample.await
           } else {
            Err(ReaderError::ReaderNotAsync)
        }
    }

    /// Get samples asynchronously. The number of samples actually read is returned.
    pub async fn take(&self, samples : &mut SampleBuffer<T>) -> Result<usize,ReaderError> {
        if let ReaderType::Async(waker) = &self.inner.reader_type {
            let future_sample = SampleArrayFuture::new(self.inner.entity.clone(), waker.clone(),samples ,FutureType::Take);
             future_sample.await
        } else {
            Err(ReaderError::ReaderNotAsync)
     }
    }

    pub fn create_readcondition(
        &'a mut self,
        mask: StateMask,
    ) -> Result<DdsReadCondition<T>, DDSError> {
        DdsReadCondition::create(self, mask)
    }
}

impl<'a, T> Entity for DdsReader<T>
where
    T: std::marker::Sized + TopicType,
{
    fn entity(&self) -> &DdsEntity {
        &self.inner.entity
    }
}

impl<'a, T> Drop for DdsReader<T>
where
    T: Sized + TopicType,
{
    fn drop(&mut self) {
        unsafe {
            //println!("Drop reader:{:?}", self.entity().entity());
            let ret: DDSError = cyclonedds_sys::dds_delete(self.inner.entity.entity()).into();
            if DDSError::DdsOk != ret {
                //panic!("cannot delete Reader: {}", ret);
                println!("Ignoring dds_delete failure for DdsReader");
            } else {
                //println!("Reader dropped");
            }
        }
    }
}
 
pub struct DdsReadCondition<'a, T: Sized + TopicType>(DdsEntity, &'a DdsReader<T>);

impl<'a, T> DdsReadCondition<'a, T>
where
    T: Sized + TopicType,
{
    fn create(reader: &'a DdsReader<T>, mask: StateMask) -> Result<Self, DDSError> {
        unsafe {
            let mask: u32 = *mask;
            let p = cyclonedds_sys::dds_create_readcondition(reader.entity().entity(), mask);
            if p > 0 {
                Ok(DdsReadCondition(DdsEntity::new(p), reader))
            } else {
                Err(DDSError::from(p))
            }
        }
    }
}

impl<'a, T> Entity for DdsReadCondition<'a, T>
where
    T: std::marker::Sized + TopicType,
{
    fn entity(&self) -> &DdsEntity {
        &self.0
    }
}

enum FutureType {
    Take,
    Read,
}

impl FutureType {
    fn is_take(&self) -> bool {
        match self {
            FutureType::Take => true,
            FutureType::Read => false,
        }
    }
}


struct SampleArrayFuture<'a,T> {
    entity : DdsEntity,
    waker : Arc<Mutex<(Option<Waker>,Result<(),crate::error::ReaderError>)>>,
    take_or_read : FutureType,
    buffer : &'a mut SampleBuffer<T>,
}


impl <'a,T>SampleArrayFuture<'a,T> {
    fn new(entity: DdsEntity, waker : Arc<Mutex<(Option<Waker>,Result<(),crate::error::ReaderError>)>>, buffer: &'a mut SampleBuffer<T>, ty : FutureType) -> Self {
        Self {
            entity,
            waker,
            take_or_read : ty,
            buffer,
        }
    }
}

impl <'a,T>Future for SampleArrayFuture<'a,T> where T: TopicType {
    type Output = Result<usize,ReaderError>;

    fn poll(mut self: Pin<&mut Self>, ctx: &mut Context) -> Poll<Self::Output> {

        // Lock the waker first in case a callback for read complete happens and we miss it
        // clone to avoid the lifetime problem with self
        let waker = self.waker.clone();
        let mut waker = waker.lock().unwrap();
        let is_take = self.take_or_read.is_take();
        let entity = self.entity.clone();
        
        // check if we have an error from any of the callbacks
        if let Err(e) = &waker.1 {
                return Poll::Ready(Err(e.clone()))    
        }
        

        match DdsReader::<T>::readn_from_entity_now(&entity, &mut self.buffer, is_take) {
            Ok(len) =>  Poll::Ready(Ok(len)),
            Err(DDSError::NoData) | Err(DDSError::OutOfResources) => {
                let _ = waker.0.replace(ctx.waker().clone()); 
                Poll::Pending
            },
            Err(e) => {    
                //println!("Error:{}",e);
                // Some other error happened
                Poll::Ready(Err(ReaderError::DdsError(e)))
            }
        }
    }
}



#[cfg(test)]
mod test {
    use core::panic;
    use std::time::Duration;

    use crate::{DdsParticipant, DdsSubscriber};
    use super::*;
    use crate::{DdsPublisher, DdsWriter};
    
    use cdds_derive::Topic;
    use serde_derive::{Deserialize, Serialize};
    use tokio::runtime::Runtime;


    #[repr(C)]
    #[derive(Serialize,Deserialize,Debug, PartialEq, Clone)]
    enum Position {
        Front,
        Back,
    }

    impl Default for Position {
        fn default() -> Self {
            Self::Front
        }
    }

    #[derive(Serialize,Deserialize,Topic, Debug, PartialEq)]
    struct TestTopic {
        a : u32,
        b : u16,
        c: String,
        d : Vec<u8>,
        #[topic_key]
        e : u32,
        #[topic_key_enum]
        pos : Position,
    }

    impl Default for TestTopic {
        fn default() -> Self {
            Self {
                a : 10,
                b : 20,
                c : "TestTopic".to_owned(),
                d : vec![1,2,3,4,5],
                e : 0,
                pos : Position::default(),
            }
        }
    }

    #[derive(Serialize,Deserialize,Topic, Debug, PartialEq)]
    struct AnotherTopic {
        pub value : u32,
        pub name : String,
        pub arr : [String;2],
        pub vec : Vec<String>,
        #[topic_key]
        pub key : u32,
    }

    impl Default for AnotherTopic {
        fn default() -> Self {
            assert!(Self::has_key());
            Self {
                value : 42,
                name : "the answer".to_owned(),
                arr : ["one".to_owned(), "two".to_owned()],
                vec : vec!["Hello".to_owned(), "world".to_owned()],
                key : 0,
            }
    }
    }

    #[test]
    fn test_reader_async() {
        
        let participant = DdsParticipant::create(None, None, None).unwrap();

        let topic = TestTopic::create_topic(&participant, Some("test_topic"), None, None).unwrap();
        let another_topic = AnotherTopic::create_topic(&participant, None, None, None).unwrap();

        let publisher = DdsPublisher::create(&participant, None, None).unwrap();

        let mut writer = DdsWriter::create(&publisher, topic.clone(), None, None).unwrap();
        let mut another_writer = DdsWriter::create(&publisher, another_topic.clone(), None, None).unwrap();


        let subscriber = DdsSubscriber::create(&participant, None, None).unwrap();
        let reader = DdsReader::create_async(&subscriber, topic, None).unwrap();
        let another_reader = DdsReader::create_async(&subscriber, another_topic, None).unwrap();

        let rt = Runtime::new().unwrap();

        let _result = rt.block_on(async {
            
            let _task = tokio::spawn(async move {
                let mut samplebuffer = SampleBuffer::new(1);
                if let Ok(t) = reader.take(&mut samplebuffer).await {
                    let sample = samplebuffer.iter().take(1).next().unwrap();
                    assert!(*sample == TestTopic::default());
                } else {
                    panic!("reader get failed");
                }
            });

            let _another_task = tokio::spawn(async move {
                let mut samples = AnotherTopic::create_sample_buffer(5);
                if let Ok(t) = another_reader.read(&mut samples).await {
                    assert_eq!(t,1);
                    for s in samples.iter() {

                        println!("Got sample {}", s.key);
                    }
                   
                } else {
                    panic!("reader get failed");
                }
            });

            // add a delay to make sure the data is not ready immediately
            tokio::time::sleep(std::time::Duration::from_millis(100)).await;
            let data = Arc::new(TestTopic::default());
            writer.write(data).unwrap();

            another_writer.write(Arc::new(AnotherTopic::default())).unwrap();


            tokio::time::sleep(std::time::Duration::from_millis(300)).await;

        });
    }
/* 
    #[test]
    fn test_requested_deadline_miss() {
        let participant = DdsParticipant::create(None, None, None).unwrap();
        let topic = TestTopic::create_topic(&participant, Some("test_topic"), None, None).unwrap();
        let publisher = DdsPublisher::create(&participant, None, None).unwrap();

        let writer_qos = DdsQos::create().unwrap().set_deadline(std::time::Duration::from_millis(50));
        let mut writer = DdsWriter::create(&publisher, topic.clone(), Some(writer_qos), None).unwrap();

       
        

        let reader_qos = DdsQos::create().unwrap().set_deadline(std::time::Duration::from_millis(500));
        let subscriber = DdsSubscriber::create(&participant, None, None).unwrap();
        let reader = DdsReader::create_async(&subscriber, topic, None).unwrap();

        let rt = Runtime::new().unwrap();

        let _result = rt.block_on(async {


            let t = tokio::spawn(async move {

                loop {
                    let d = writer.write(Arc::new(TestTopic::default())).unwrap();
                    tokio::time::sleep(Duration::from_millis(1000)).await;
                }

            });

            loop {

                let d = reader.read1().await;
                tokio::time::sleep(Duration::from_millis(700)).await;
                println!("reader returned:{:?}",d);
            }
            


        });

    }
*/
    

}