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
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
//! Tibco EMS binding.

use std::ffi::CString;
use std::ffi::CStr;
use std::ffi::c_void;
use std::collections::HashMap;
use std::io::Error;
use std::io::ErrorKind;
use tibco_ems_sys::tibems_status;
use tibco_ems_sys::tibemsDestinationType;
use tibco_ems_sys::tibems_bool;
use tibco_ems_sys::tibemsMsgType;
use log::{trace, error};
use serde::{Serialize, Deserialize};
use std::rc::Rc;
use std::ops::Deref;

pub mod admin;

#[cfg(feature = "streaming")]
pub mod stream;

/// holds the native Connection pointer
#[allow(dead_code)]
#[derive(Debug, Clone)]
pub struct Connection{
  pointer: Rc<usize>,
}

/// holds the native Session pointer
#[allow(dead_code)]
#[derive(Debug, Clone)]
pub struct Session{
  pointer: usize,
  producer_pointer: usize,
}

/// holds the native Consumer pointer
#[allow(dead_code)]
#[derive(Debug, Copy, Clone)]
pub struct Consumer{
  pointer: usize,
}

/// Destination, can either be Queue or Topic
#[allow(dead_code)]
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub enum Destination{
  Queue(String),
  Topic(String),
}

/// represents a Text Message
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TextMessage {
  /// message body
  pub body: String,
  /// message header
  pub header: Option<HashMap<String, TypedValue>>,
  /// reply to header
  pub reply_to: Option<Destination>,
  /// point to the ems native object
  pub pointer: Option<usize>,
}

impl Default for TextMessage{
  fn default() -> Self {
    TextMessage{
      body: "".to_string(),
      header: None,
      reply_to: None,
      pointer: None,
    }  
  }
}

/// represents a Binary Message
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BytesMessage {
  /// message body
  pub body: Vec<u8>,
  /// message header
  pub header: Option<HashMap<String, TypedValue>>,
  /// reply to header
  pub reply_to: Option<Destination>,
  /// point to the ems native object
  pub pointer: Option<usize>,  
}

impl Default for BytesMessage{
  fn default() -> Self {
    BytesMessage{
      body: vec![],
      header: None,
      reply_to: None,
      pointer: None,
    }  
  }
}

/// represents a Map Message
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct MapMessage {
  /// message body map properties
  pub body: HashMap<String, TypedValue>,
  /// message header
  pub header: Option<HashMap<String, TypedValue>>,
  /// reply to header
  pub reply_to: Option<Destination>,
  /// point to the ems native object
  pub pointer: Option<usize>,
}

impl Default for MapMessage{
  fn default() -> Self {
    MapMessage{
      body: HashMap::new(),
      header: None,
      reply_to: None,
      pointer: None,
    }  
  }
}

/// Message enum wich represents the different message types
#[allow(dead_code)]
#[derive(Debug, Clone)]
pub enum Message {
  /// represents a Text Message
  TextMessage(TextMessage),
  /// represents a Binary Message
  BytesMessage(BytesMessage),
  /// represents a Map Message
  MapMessage(MapMessage),
}

/// open a connection to the Tibco EMS server
pub fn connect(url: &str, user: &str, password: &str) -> Result<Connection, Error> {
  let mut connection_pointer: usize = 0;
  unsafe{
    let factory = tibco_ems_sys::tibemsConnectionFactory_Create();
    let c_url = CString::new(url).unwrap();
    let status = tibco_ems_sys::tibemsConnectionFactory_SetServerURL(factory, c_url.as_ptr());
    match status {
      tibems_status::TIBEMS_OK => trace!("tibemsConnectionFactory_SetServerURL: {:?}",status),
      _ => {
        error!("tibemsConnectionFactory_SetServerURL: {:?}",status);
        return Err(Error::new(ErrorKind::InvalidData, "cannot set server url"));
      },
    }
    let c_user = CString::new(user).unwrap();
    let c_password = CString::new(password).unwrap();
    let status = tibco_ems_sys::tibemsConnectionFactory_CreateConnection(factory, &mut connection_pointer, c_user.as_ptr(),c_password.as_ptr());
    match status {
      tibems_status::TIBEMS_OK => trace!("tibemsConnectionFactory_CreateConnection: {:?}",status),
      _ => {
        error!("tibemsConnectionFactory_CreateConnection: {:?}",status);
        return Err(Error::new(ErrorKind::NotConnected, "cannot create connection"));
      },
    }
    let status = tibco_ems_sys::tibemsConnection_Start(connection_pointer);
    match status {
      tibems_status::TIBEMS_OK => trace!("tibemsConnection_Start: {:?}",status),
      _ => {
        error!("tibemsConnection_Start: {:?}",status);
        return Err(Error::new(ErrorKind::NotConnected, "cannot start connection"));
      },
    }
  }
  let conn = Connection{pointer: Rc::from(connection_pointer)};
  Ok(conn)
}

//
// connection
//

impl<'stream> Connection {
  /// open a session
  pub fn session(&self) -> Result<Session, Error> {
    unsafe{
      let mut session_pointer:usize = 0;
      let connection_pointer = *self.pointer.deref();
      let status = tibco_ems_sys::tibemsConnection_CreateSession(connection_pointer, &mut session_pointer, tibco_ems_sys::tibems_bool::TIBEMS_FALSE, tibco_ems_sys::tibemsAcknowledgeMode::TIBEMS_AUTO_ACKNOWLEDGE);
      match status {
        tibems_status::TIBEMS_OK => trace!("tibemsConnection_CreateSession: {:?}",status),
        _ => {
          error!("tibemsConnection_CreateSession: {:?}",status);
          return Err(Error::new(ErrorKind::Other, "creating session failed"));
        },
      }
      let mut producer: usize = 0;
      let dest: usize = 0;
      let status = tibco_ems_sys::tibemsSession_CreateProducer(session_pointer,&mut producer,dest);
      match status {
        tibems_status::TIBEMS_OK => trace!("tibemsSession_CreateProducer: {:?}",status),
        _ => {
          error!("tibemsSession_CreateProducer: {:?}",status);
          return Err(Error::new(ErrorKind::Other, "creating producer failed"));
        },
      }
      let session = Session{pointer: session_pointer, producer_pointer: producer};
      Ok(session)
    }
  }

  /// open a session with transaction support
  pub fn transacted_session(&self)-> Result<Session, Error> {
    let session: Session;
    unsafe{
      let mut session_pointer:usize = 0;
      let connection_pointer = *self.pointer.deref();
      let status = tibco_ems_sys::tibemsConnection_CreateSession(connection_pointer, &mut session_pointer, tibco_ems_sys::tibems_bool::TIBEMS_FALSE, tibco_ems_sys::tibemsAcknowledgeMode::TIBEMS_EXPLICIT_CLIENT_ACKNOWLEDGE);
      match status {
        tibems_status::TIBEMS_OK => trace!("tibemsConnection_CreateSession: {:?}",status),
        _ => {
          error!("tibemsConnection_CreateSession: {:?}",status);
          return Err(Error::new(ErrorKind::Other, "creating session failed"));
        },
      }
      let mut producer: usize = 0;
      let dest: usize = 0;
      let status = tibco_ems_sys::tibemsSession_CreateProducer(session_pointer,&mut producer,dest);
      match status {
        tibems_status::TIBEMS_OK => trace!("tibemsSession_CreateProducer: {:?}",status),
        _ => {
          error!("tibemsSession_CreateProducer: {:?}",status);
          return Err(Error::new(ErrorKind::Other, "creating producer failed"));
        },
      }
      session = Session{pointer: session_pointer, producer_pointer: producer};
    }
    Ok(session)
  }
  /// get active url from a ft connection
  /// this is only required for admin connections, 
  /// normal connections automatically choose the active server
  pub fn get_active_url(&self) -> Result<String, Error> {
    let connection_pointer = *self.pointer.deref();
    unsafe{
      let buf_vec:Vec<i8> = vec![0; 0];
      let buf_ref: *const std::os::raw::c_char = buf_vec.as_ptr();
      let status = tibco_ems_sys::tibemsConnection_GetActiveURL(connection_pointer, &buf_ref);
      match status {
        tibems_status::TIBEMS_OK => trace!("tibemsConnection_GetActiveURL: {:?}",status),
        _ => {
          error!("tibemsConnection_GetActiveURL: {:?}",status);
          return Err(Error::new(ErrorKind::Other, "failed to retrieve active url"));
        },
      }
      let url = CStr::from_ptr(buf_ref).to_str().unwrap();
      return Ok(url.to_string());
    }
  }
  // open a consumer as stream of messages
  #[cfg(feature = "streaming")]
  pub fn open_stream<T: Into<Message>>(&'stream self,destination: &Destination, selector: Option<&str>) -> Result<stream::MessageStream<T>,Error> {
    let session = self.session().unwrap();
    let consumer = session.queue_consumer(destination, selector).unwrap();
    let stream = stream::MessageStream::<T>{
      connection: Rc::from(self.clone()),
      session: Rc::from(session),
      consumer: Rc::from(consumer),
      message: None,
    };
    Ok(stream)
  }
}

//
// consumer
//

impl Consumer {
  /// receive messages from a consumer
  /// 
  /// function return after wait time with a Message or None
  /// a wait time of None blocks until a message is available
  pub fn receive_message(&self, wait_time_ms: Option<i64>) -> Result<Option<Message>, Error> {
    unsafe{
      let mut msg_pointer:usize = 0;
      match wait_time_ms {
        Some(time_ms) => {
          let status = tibco_ems_sys::tibemsMsgConsumer_ReceiveTimeout(self.pointer, &mut msg_pointer, time_ms);
          match status {
            tibems_status::TIBEMS_OK => trace!("tibemsMsgConsumer_ReceiveTimeout: {:?}",status),
            tibems_status::TIBEMS_TIMEOUT => {
              return Ok(None);
            },
            _ => {
              let status_str = format!("{:?}",status);
              error!("tibemsMsgConsumer_ReceiveTimeout: {}",status_str);
              return Err(Error::new(ErrorKind::Other, format!("receive message failed: {}",status_str)));
            },
          }
        },
        None => {
          let status = tibco_ems_sys::tibemsMsgConsumer_Receive(self.pointer, &mut msg_pointer);
          match status {
            tibems_status::TIBEMS_OK => trace!("tibemsMsgConsumer_Receive: {:?}",status),
            _ => {
              let status_str = format!("{:?}",status);
              error!("tibemsMsgConsumer_Receive: {}",status_str);
              return Err(Error::new(ErrorKind::Other, format!("receive message failed: {}",status_str)));
            },
          }
        },
      }
      let msg = build_message_from_pointer(msg_pointer);
      return Ok(Some(msg));
    }
  }
}

// 
// session
//

impl Session {
  /// open a message consumer
  pub fn queue_consumer(&self, destination: &Destination, selector: Option<&str>) -> Result<Consumer, Error> {
    let consumer: Consumer;
    let mut destination_pointer: usize = 0;
    unsafe{
      //create destination
      match destination {
        Destination::Queue(name) => {
          let c_destination = CString::new(name.clone()).unwrap();
          let status = tibco_ems_sys::tibemsDestination_Create(&mut destination_pointer, tibemsDestinationType::TIBEMS_QUEUE, c_destination.as_ptr());
          match status {
            tibems_status::TIBEMS_OK => trace!("tibemsDestination_Create: {:?}",status),
            _ => {
              let status_str = format!("{:?}",status);
              error!("tibemsDestination_Create: {}",status_str);
              return Err(Error::new(ErrorKind::Other, format!("create destination failed: {}",status_str)));
            },
          }
        },
        Destination::Topic(name) => {
          let c_destination = CString::new(name.clone()).unwrap();
          let status = tibco_ems_sys::tibemsDestination_Create(&mut destination_pointer, tibemsDestinationType::TIBEMS_TOPIC, c_destination.as_ptr());
          match status {
            tibems_status::TIBEMS_OK => trace!("tibemsDestination_Create: {:?}",status),
            _ => {
              let status_str = format!("{:?}",status);
              error!("tibemsDestination_Create: {}",status_str);
              return Err(Error::new(ErrorKind::Other, format!("create destination failed: {}",status_str)));
            },
          }
        }
      }
      //open consumer
      let mut consumer_pointer:usize = 0;
      let c_selector:CString;
      match selector {
        Some(val) => c_selector=CString::new(val).unwrap(),
        _ => c_selector = CString::new("".to_string()).unwrap(),
      }
      let status = tibco_ems_sys::tibemsSession_CreateConsumer(self.pointer, &mut consumer_pointer,destination_pointer, c_selector.as_ptr(), tibco_ems_sys::tibems_bool::TIBEMS_TRUE);
      match status {
        tibems_status::TIBEMS_OK => trace!("tibemsSession_CreateConsumer: {:?}",status),
        _ => {
          let status_str = format!("{:?}",status);
          error!("tibemsSession_CreateConsumer: {}",status_str);
          return Err(Error::new(ErrorKind::Other, format!("create consumer failed: {}",status_str)));
        },
      }
      consumer = Consumer{pointer: consumer_pointer};
    }
    Ok(consumer)
  }

  /// close a session
  fn close(&self){
    unsafe{
      //destroy producer
      if self.producer_pointer != 0 {
        let status = tibco_ems_sys::tibemsMsgProducer_Close(self.producer_pointer);
        match status {
          tibems_status::TIBEMS_OK => trace!("tibemsMsgProducer_Close: {:?}",status),
          _ => error!("tibemsMsgProducer_Close: {:?}",status),
        }
      }
      let status = tibco_ems_sys::tibemsSession_Close(self.pointer);
      match status {
        tibems_status::TIBEMS_OK => trace!("tibemsSession_Close: {:?}",status),
        _ => error!("tibemsSession_Close: {:?}",status),
      }
    }
  }

  /// sending a message to a destination (only queues are supported)
  pub fn send_message<M: Into<Message>>(&self, destination: &Destination, message: M) -> Result<(), Error>{
    let message: Message = message.into();
    let mut dest: usize = 0;
    let mut local_producer: usize = 0;
    unsafe{
      match destination {
        Destination::Queue(name) => {
          let c_destination = CString::new(name.clone()).unwrap();
          let status = tibco_ems_sys::tibemsDestination_Create(&mut dest, tibemsDestinationType::TIBEMS_QUEUE, c_destination.as_ptr());
          match status {
            tibems_status::TIBEMS_OK => trace!("tibemsDestination_Create: {:?}",status),
            _ => {
              let status_str = format!("{:?}",status);
              error!("tibemsDestination_Create: {}",status_str);
              return Err(Error::new(ErrorKind::Other, format!("create destination failed: {}",status_str)));
            },
          }
        },
        Destination::Topic(name) => {
          let c_destination = CString::new(name.clone()).unwrap();
          let status = tibco_ems_sys::tibemsDestination_Create(&mut dest, tibemsDestinationType::TIBEMS_TOPIC, c_destination.as_ptr());
          match status {
            tibems_status::TIBEMS_OK => trace!("tibemsDestination_Create: {:?}",status),
            _ => {
              let status_str = format!("{:?}",status);
              error!("tibemsDestination_Create: {}",status_str);
              return Err(Error::new(ErrorKind::Other, format!("create destination failed: {}",status_str)));
            },
          }
        }
      }
      if self.producer_pointer == 0 {
        let status = tibco_ems_sys::tibemsSession_CreateProducer(self.pointer,&mut local_producer,dest);
        match status {
          tibems_status::TIBEMS_OK => trace!("tibemsSession_CreateProducer: {:?}",status),
          _ =>{
            let status_str = format!("{:?}",status);
            error!("tibemsSession_CreateProducer: {}",status_str);
            return Err(Error::new(ErrorKind::Other, format!("create produce failed: {}",status_str)));
          },
        }
      }
      let msg = build_message_pointer_from_message(&message);
      let status = tibco_ems_sys::tibemsMsgProducer_SendToDestination(
          self.producer_pointer, dest, msg);
      match status {
        tibems_status::TIBEMS_OK => trace!("tibemsMsgProducer_Send: {:?}",status),
        _ =>{
          let status_str = format!("{:?}",status);
          error!("tibemsMsgProducer_Send: {}",status_str);
          return Err(Error::new(ErrorKind::Other, format!("send message failed: {}",status_str)));
        },
      }
      //destroy producer if generated inline
      if self.producer_pointer == 0 {
        let status = tibco_ems_sys::tibemsMsgProducer_Close(local_producer);
        match status {
          tibems_status::TIBEMS_OK => trace!("tibemsMsgProducer_Close: {:?}",status),
          _ => error!("tibemsMsgProducer_Close: {:?}",status),
        }
      }
      //destroy message
      let status = tibco_ems_sys::tibemsMsg_Destroy(msg);
      match status {
        tibems_status::TIBEMS_OK => trace!("tibemsMsg_Destroy: {:?}",status),
        _ => error!("tibemsMsg_Destroy: {:?}",status),
      }
      //destroy destination
      let status = tibco_ems_sys::tibemsDestination_Destroy(dest);
      match status {
        tibems_status::TIBEMS_OK => trace!("tibemsDestination_Destroy: {:?}",status),
        _ => error!("tibemsDestination_Destroy: {:?}",status),
      }
    }
    Ok(())
  }

  /// request/reply
  pub fn request_reply<M: Into<Message>>(&self, destination: &Destination, message: M, timeout: i64) -> Result<Option<Message>, Error>{
    let message: Message = message.into();
    //create temporary destination
    let mut reply_dest: usize = 0;
    let mut dest: usize = 0;
    unsafe {
      match &destination {
        Destination::Queue(name) =>{
          let status = tibco_ems_sys::tibemsSession_CreateTemporaryQueue(self.pointer, &mut reply_dest);
          match status {
            tibems_status::TIBEMS_OK => trace!("tibemsSession_CreateTemporaryQueue: {:?}",status),
            _ => error!("tibemsSession_CreateTemporaryQueue: {:?}",status),
          }
          let c_destination = CString::new(name.clone()).unwrap();
          let status = tibco_ems_sys::tibemsDestination_Create(&mut dest, tibemsDestinationType::TIBEMS_QUEUE, c_destination.as_ptr());
          match status {
            tibems_status::TIBEMS_OK => trace!("tibemsDestination_Create: {:?}",status),
            _ => error!("tibemsDestination_Create: {:?}",status),
          }
        },
        Destination::Topic(name) =>{
          let status = tibco_ems_sys::tibemsSession_CreateTemporaryTopic(self.pointer, &mut reply_dest);
          match status {
            tibems_status::TIBEMS_OK => trace!("tibemsSession_CreateTemporaryTopic: {:?}",status),
            _ => error!("tibemsSession_CreateTemporaryTopic: {:?}",status),
          }
          let c_destination = CString::new(name.clone()).unwrap();
          let status = tibco_ems_sys::tibemsDestination_Create(&mut dest, tibemsDestinationType::TIBEMS_TOPIC, c_destination.as_ptr());
          match status {
            tibems_status::TIBEMS_OK => trace!("tibemsDestination_Create: {:?}",status),
            _ => error!("tibemsDestination_Create: {:?}",status),
          }
        }
      }
      let mut producer: usize = 0;
      let status = tibco_ems_sys::tibemsSession_CreateProducer(self.pointer,&mut producer, dest);
      match status {
        tibems_status::TIBEMS_OK => trace!("tibemsSession_CreateProducer: {:?}",status),
        _ => error!("tibemsSession_CreateProducer: {:?}",status),
      }
      let msg = build_message_pointer_from_message(&message);
      //set reply to
      let status = tibco_ems_sys::tibemsMsg_SetReplyTo(msg, reply_dest);
      match status {
        tibems_status::TIBEMS_OK => trace!("tibemsMsg_SetReplyTo: {:?}",status),
        _ => error!("tibemsMsg_SetReplyTo: {:?}",status),
      }
      let status = tibco_ems_sys::tibemsMsgProducer_Send(producer, msg);
      match status {
        tibems_status::TIBEMS_OK => trace!("tibemsMsgProducer_Send: {:?}",status),
        _ => error!("tibemsMsgProducer_Send: {:?}",status),
      }
      //destroy message
      let status = tibco_ems_sys::tibemsMsg_Destroy(msg);
      match status {
        tibems_status::TIBEMS_OK => trace!("tibemsMsg_Destroy: {:?}",status),
        _ => error!("tibemsMsg_Destroy: {:?}",status),
      }
      //destroy producer
      let status = tibco_ems_sys::tibemsMsgProducer_Close(producer);
      match status {
        tibems_status::TIBEMS_OK => trace!("tibemsMsgProducer_Close: {:?}",status),
        _ => error!("tibemsMsgProducer_Close: {:?}",status),
      }
      //destroy destination
      let status = tibco_ems_sys::tibemsDestination_Destroy(dest);
      match status {
        tibems_status::TIBEMS_OK => trace!("tibemsDestination_Destroy: {:?}",status),
        _ => error!("tibemsDestination_Destroy: {:?}",status),
      }
      //open consumer
      let mut consumer_pointer: usize = 0;
      let status = tibco_ems_sys::tibemsSession_CreateConsumer(self.pointer, &mut consumer_pointer,reply_dest, std::ptr::null(), tibco_ems_sys::tibems_bool::TIBEMS_TRUE);
      match status {
        tibems_status::TIBEMS_OK => trace!("tibemsSession_CreateConsumer: {:?}",status),
        _ => error!("tibemsSession_CreateConsumer: {:?}",status),
      }
      let mut reply_message: usize = 0;
      let status = tibco_ems_sys::tibemsMsgConsumer_ReceiveTimeout(consumer_pointer, &mut reply_message, timeout);
      match status {
        tibems_status::TIBEMS_OK => trace!("tibemsMsgConsumer_ReceiveTimeout: {:?}",status),
        tibems_status::TIBEMS_TIMEOUT =>{
          return Ok(None);
        },
        _ => error!("tibemsMsgConsumer_ReceiveTimeout: {:?}",status),
      }
      let result = build_message_from_pointer(reply_message);
      //close consumer
      let status = tibco_ems_sys::tibemsMsgConsumer_Close(consumer_pointer);
      match status {
        tibems_status::TIBEMS_OK => trace!("tibemsMsgConsumer_Close: {:?}",status),
        _ => error!("tibemsMsgConsumer_Close: {:?}",status),
      }
      //destroy temporary destination
      match &destination {
        Destination::Queue{..} =>{
          //destroy reply_to_queue
          let status = tibco_ems_sys::tibemsSession_DeleteTemporaryQueue(self.pointer, reply_dest);
          match status {
            tibems_status::TIBEMS_OK => trace!("tibemsSession_DeleteTemporaryQueue: {:?}",status),
            _ => error!("tibemsSession_DeleteTemporaryQueue: {:?}",status),
          }
        },
        Destination::Topic{..} =>{
          //destroy reply_to_queue
          let status = tibco_ems_sys::tibemsSession_DeleteTemporaryTopic(self.pointer, reply_dest);
          match status {
            tibems_status::TIBEMS_OK => trace!("tibemsSession_DeleteTemporaryTopic: {:?}",status),
            _ => error!("tibemsSession_DeleteTemporaryTopic: {:?}",status),
          }
        }
      }
      return Ok(Some(result));
    }
  }
}

impl Drop for Session {
  fn drop(&mut self) {
    self.close();
  }
}

//
// messages
//

impl From<MapMessage> for Message {
  fn from(msg: MapMessage) -> Self {
    Message::MapMessage(msg)
  }
}

impl From<TextMessage> for Message {
  fn from(msg: TextMessage) -> Self {
    Message::TextMessage(msg)
  }
}

impl From<BytesMessage> for Message {
  fn from(msg: BytesMessage) -> Self {
    Message::BytesMessage(msg)
  }
}

/// represents a typed value, which is used for message header and message properties
#[allow(dead_code)]
#[derive(Debug,Clone,Serialize, Deserialize, PartialEq)]
pub enum TypedValue {
  /// represents a String Value
  String(String),
  /// represents a integer Value
  Integer(i32),
  /// represents a long Value
  Long(i64),
  /// represents a float Value
  Float(f32),
  /// represents a double Value
  Double(f64),
  /// represents a binary Value
  Binary(Vec<u8>),
  /// represents a MapMessage Value
  Map(MapMessage),
  /// represents a boolean Value
  Boolean(bool),
}

impl Message{
  fn destroy(&self){
    let destroy_msg = | pointer: usize| unsafe {
      let status = tibco_ems_sys::tibemsMsg_Destroy(pointer);
      match status {
        tibems_status::TIBEMS_OK => trace!("tibemsMsg_Destroy: {:?}",status),
        _ => error!("tibemsMsg_Destroy: {:?}",status),
      }
    };
    match self {
      Message::TextMessage(msg) => {
        match msg.pointer {
          Some(pointer) => {
            destroy_msg(pointer);
          },
          None => {},
        }
      },
      Message::BytesMessage(msg) => {
        match msg.pointer {
          Some(pointer) => {
            destroy_msg(pointer);
          },
          None => {},
        }
      },
      Message::MapMessage(msg) => {
        match msg.pointer {
          Some(pointer) => {
            destroy_msg(pointer);
          },
          None => {},
        }
      },
    }
  }
  /// confirms the message by invoking tibemsMsg_Acknowledge
  pub fn confirm(&self){
    let ack_msg = |pointer:usize| unsafe {
      let status = tibco_ems_sys::tibemsMsg_Acknowledge(pointer);
      match status {
        tibems_status::TIBEMS_OK => trace!("tibemsMsg_Acknowledge: {:?}",status),
        _ => error!("tibemsMsg_Acknowledge: {:?}",status),
      }
    };
    match self {
      Message::TextMessage(msg) => {
        match msg.pointer {
          Some(pointer) => {
            ack_msg(pointer);
          },
          None => {},
        }
      },
      Message::BytesMessage(msg) => {
        match msg.pointer {
          Some(pointer) => {
            ack_msg(pointer);
          },
          None => {},
        }
      },
      Message::MapMessage(msg) => {
        match msg.pointer {
          Some(pointer) => {
            ack_msg(pointer);
          },
          None => {},
        }
      },
    }
  }
  /// rolls the message back by invoking tibemsMsg_Recover
  pub fn rollback(&self){
    let recover = |pointer: usize| unsafe{
      let status = tibco_ems_sys::tibemsMsg_Recover(pointer);
      match status {
        tibems_status::TIBEMS_OK => trace!("tibemsMsg_Recover: {:?}",status),
        _ => error!("tibemsMsg_Recover: {:?}",status),
      }
    };
    match self {
      Message::TextMessage(msg) => {
        match msg.pointer {
          Some(pointer) => {
            recover(pointer);
          },
          None => {},
        }
      },
      Message::BytesMessage(msg) => {
        match msg.pointer {
          Some(pointer) => {
            recover(pointer);
          },
          None => {},
        }
      },
      Message::MapMessage(msg) => {
        match msg.pointer {
          Some(pointer) => {
            recover(pointer);
          },
          None => {},
        }
      },
    }
  }
}

impl Drop for Message {
  fn drop(&mut self) {
    self.destroy();
  }
}

fn build_message_pointer_from_message(message: &Message) -> usize {
  let mut msg_pointer: usize = 0;
  unsafe{
    match message {
      Message::TextMessage(msg) => {
        let status = tibco_ems_sys::tibemsTextMsg_Create(&mut msg_pointer);
        match status {
          tibems_status::TIBEMS_OK => trace!("tibemsTextMsg_Create: {:?}",status),
          _ => error!("tibemsTextMsg_Create: {:?}",status),
        }
        let c_text = CString::new(msg.body.clone()).unwrap();
        let status = tibco_ems_sys::tibemsTextMsg_SetText(msg_pointer,c_text.as_ptr());
        match status {
          tibems_status::TIBEMS_OK => trace!("tibemsTextMsg_SetText: {:?}",status),
          _ => error!("tibemsTextMsg_SetText: {:?}",status),
        }
      },
      Message::BytesMessage(msg) => {
        let status = tibco_ems_sys::tibemsBytesMsg_Create(&mut msg_pointer);
        match status {
          tibems_status::TIBEMS_OK => trace!("tibemsBytesMsg_Create: {:?}",status),
          _ => error!("tibemsBytesMsg_Create: {:?}",status),
        }
        let content = msg.body.clone();
        let body_size = content.len();
        let body_ptr = content.as_ptr() as *const c_void;
        let status = tibco_ems_sys::tibemsBytesMsg_SetBytes(msg_pointer,body_ptr,body_size as u32);
        match status {
          tibems_status::TIBEMS_OK => trace!("tibemsBytesMsg_SetBytes: {:?}",status),
          _ => error!("tibemsBytesMsg_SetBytes: {:?}",status),
        }
      },
      Message::MapMessage(msg) => {
        let status = tibco_ems_sys::tibemsMapMsg_Create(&mut msg_pointer);
        match status {
          tibems_status::TIBEMS_OK => trace!("tibemsMapMsg_Create: {:?}",status),
          _ => error!("tibemsMapMsg_Create: {:?}",status),
        }
        for (key,val) in msg.body.clone() {
          let c_name = CString::new(key).unwrap();
          match val {
            TypedValue::Boolean(value) => {
              let status;
              if value {
                status = tibco_ems_sys::tibemsMapMsg_SetBoolean(msg_pointer, c_name.as_ptr(), tibems_bool::TIBEMS_TRUE);
              }else{
                status = tibco_ems_sys::tibemsMapMsg_SetBoolean(msg_pointer, c_name.as_ptr(), tibems_bool::TIBEMS_FALSE);
              }
              match status {
                tibems_status::TIBEMS_OK => trace!("tibemsMapMsg_SetBoolean: {:?}",status),
                _ => error!("tibemsMapMsg_SetBoolean: {:?}",status),
              }
            },
            TypedValue::String(value) => {
              let c_value = CString::new(value).unwrap();
              let status = tibco_ems_sys::tibemsMapMsg_SetString(msg_pointer, c_name.as_ptr(), c_value.as_ptr());
              match status {
                tibems_status::TIBEMS_OK => trace!("tibemsMapMsg_SetString: {:?}",status),
                _ => error!("tibemsMapMsg_SetString: {:?}",status),
              }
            },
            TypedValue::Integer(value) => {
              let status = tibco_ems_sys::tibemsMapMsg_SetInt(msg_pointer, c_name.as_ptr(), value);
              match status {
                tibems_status::TIBEMS_OK => trace!("tibemsMapMsg_SetInt: {:?}",status),
                _ => error!("tibemsMapMsg_SetInt: {:?}",status),
              }
            },
            TypedValue::Long(value) => {
              let status = tibco_ems_sys::tibemsMapMsg_SetLong(msg_pointer, c_name.as_ptr(), value);
              match status {
                tibems_status::TIBEMS_OK => trace!("tibemsMapMsg_SetLong: {:?}",status),
                _ => error!("tibemsMapMsg_SetLong: {:?}",status),
              }
            },
            TypedValue::Float(value) => {
              let status = tibco_ems_sys::tibemsMapMsg_SetFloat(msg_pointer, c_name.as_ptr(), value);
              match status {
                tibems_status::TIBEMS_OK => trace!("tibemsMapMsg_SetFloat: {:?}",status),
                _ => error!("tibemsMapMsg_SetFloat: {:?}",status),
              }
            },

            TypedValue::Double(value) => {
              let status = tibco_ems_sys::tibemsMapMsg_SetDouble(msg_pointer, c_name.as_ptr(), value);
              match status {
                tibems_status::TIBEMS_OK => trace!("tibemsMapMsg_SetDouble: {:?}",status),
                _ => error!("tibemsMapMsg_SetDouble: {:?}",status),
              }
            },
            TypedValue::Binary(_value) => {
              //TODO implement
              // let status = tibco_ems_sys::tibemsMapMsg_SetBytes(message: usize, name: *const c_char, bytes: *mut c_void, bytesSize: u64)Long(msg, c_name.as_ptr(), value);
              // match status {
                // tibems_status::TIBEMS_OK => trace!("tibemsMapMsg_SetLong: {:?}",status),
                // _ => error!("tibemsMapMsg_SetLong: {:?}",status),
              // }
            }
            _ => {
              panic!("missing map message type implementation for {:?}",val);
            },
          }
        }
      },
    }
    //set header
    let header = match message {
      Message::TextMessage(msg) => msg.header.clone(),
      Message::BytesMessage(msg) => msg.header.clone(),
      Message::MapMessage(msg) => msg.header.clone(),
    };
    match header {
      Some(headers)=>{
        for (key, val) in &headers {
          let c_name = CString::new(key.to_string()).unwrap();
          match val {
            TypedValue::String(value) => {
              let c_val = CString::new(value.as_bytes()).unwrap();
              let status = tibco_ems_sys::tibemsMsg_SetStringProperty(msg_pointer, c_name.as_ptr(), c_val.as_ptr());
              match status {
                tibems_status::TIBEMS_OK => trace!("tibemsMsg_SetStringProperty: {:?}",status),
                _ => error!("tibemsMsg_SetStringProperty: {:?}",status),
              }    
            },
            TypedValue::Boolean(value) => {
              let status;
              if *value {
                status = tibco_ems_sys::tibemsMsg_SetBooleanProperty(msg_pointer, c_name.as_ptr(), tibems_bool::TIBEMS_TRUE);
              } else {
                status = tibco_ems_sys::tibemsMsg_SetBooleanProperty(msg_pointer, c_name.as_ptr(), tibems_bool::TIBEMS_FALSE);
              }
              match status {
                tibems_status::TIBEMS_OK => trace!("tibemsMsg_SetBooleanProperty: {:?}",status),
                _ => error!("tibemsMsg_SetBooleanProperty: {:?}",status),
              }
            },
            TypedValue::Integer(value) => {
              let status = tibco_ems_sys::tibemsMsg_SetIntProperty(msg_pointer, c_name.as_ptr(), *value);
              match status {
                tibems_status::TIBEMS_OK => trace!("tibemsMsg_SetIntProperty: {:?}",status),
                _ => error!("tibemsMsg_SetIntProperty: {:?}",status),
              }    
            },
            TypedValue::Long(value) => {
              let status = tibco_ems_sys::tibemsMsg_SetLongProperty(msg_pointer, c_name.as_ptr(), *value);
              match status {
                tibems_status::TIBEMS_OK => trace!("tibemsMsg_SetLongProperty: {:?}",status),
                _ => error!("tibemsMsg_SetLongProperty: {:?}",status),
              }    
            },
            _ => {
              panic!("missing property type implementation for {:?}",val);
            }
          }
        }
      },
      None => {},
    }
  }
  msg_pointer
}

fn build_message_from_pointer(msg_pointer: usize) -> Message {
  let mut msg: Message;
  let mut header: HashMap<String,TypedValue> = HashMap::new();
  unsafe{
    let mut msg_type: tibemsMsgType = tibemsMsgType::TIBEMS_TEXT_MESSAGE;
    let status = tibco_ems_sys::tibemsMsg_GetBodyType(msg_pointer, &mut msg_type);
    match status {
      tibems_status::TIBEMS_OK => trace!("tibemsMsg_GetBodyType: {:?}",status),
      _ => error!("tibemsMsg_GetBodyType: {:?}",status),
    }
    match msg_type {
      tibemsMsgType::TIBEMS_TEXT_MESSAGE => {
        let buf_vec:Vec<i8> = vec![0; 0];
        let buf_ref: *const std::os::raw::c_char = buf_vec.as_ptr();
        let status = tibco_ems_sys::tibemsTextMsg_GetText(msg_pointer, & buf_ref);
        match status {
          tibems_status::TIBEMS_OK => trace!("tibemsTextMsg_GetText: {:?}",status),
          _ => error!("tibemsTextMsg_GetText: {:?}",status),
        }
        let content = CStr::from_ptr(buf_ref).to_str().unwrap();
        let status = tibco_ems_sys::tibemsMsg_GetMessageID(msg_pointer, &buf_ref);
        match status {
          tibems_status::TIBEMS_OK => trace!("tibemsMsg_GetMessageID: {:?}",status),
          _ => error!("tibemsMsg_GetMessageID: {:?}",status),
        }
        let message_id = CStr::from_ptr(buf_ref).to_str().unwrap();
        header.insert("MessageID".to_string(),TypedValue::String(message_id.to_string()));
        msg = Message::TextMessage(TextMessage{
          body: content.to_string(),
          header: None,
          pointer: Some(msg_pointer),
          reply_to: None,
        });
      },
      tibemsMsgType::TIBEMS_MAP_MESSAGE => {
        let buf_vec:Vec<i8> = vec![0; 0];
        let buf_ref: *const std::os::raw::c_char = buf_vec.as_ptr();
        let status = tibco_ems_sys::tibemsMsg_GetMessageID(msg_pointer, &buf_ref);
        match status {
          tibems_status::TIBEMS_OK => trace!("tibemsMsg_GetMessageID: {:?}",status),
          _ => error!("tibemsMsg_GetMessageID: {:?}",status),
        }
        //admin messages do not have a message id
        if buf_vec.len() > 0 {
          let message_id = CStr::from_ptr(buf_ref).to_str().unwrap();
          &header.insert("MessageID".to_string(),TypedValue::String(message_id.to_string()));  
        }
        let mut names_pointer: usize = 0;
        trace!("tibemsMapMsg_GetMapNames");
        let status = tibco_ems_sys::tibemsMapMsg_GetMapNames(msg_pointer, &mut names_pointer);
        match status {
          tibems_status::TIBEMS_OK => trace!("tibemsMapMsg_GetMapNames: {:?}",status),
          _ => error!("tibemsMapMsg_GetMapNames: {:?}",status),
        }
        let mut body_entries: HashMap<String, TypedValue> = HashMap::new();
        loop {
          let buf_vec:Vec<i8> = vec![0; 0];
          let buf_ref: *const std::os::raw::c_char = buf_vec.as_ptr();
          let status = tibco_ems_sys::tibemsMsgEnum_GetNextName(names_pointer, &buf_ref);
          match status {
            tibems_status::TIBEMS_OK =>{
              let header_name = CStr::from_ptr(buf_ref).to_str().unwrap();
              trace!("getting value for property: {}",header_name);
              let mut val_buf_vec:Vec<i8> = vec![0; 0];
              let mut val_buf_ref: *mut std::os::raw::c_char = val_buf_vec.as_mut_ptr();
              let status = tibco_ems_sys::tibemsMapMsg_GetString(msg_pointer, buf_ref, &mut val_buf_ref);
              match status {
                tibems_status::TIBEMS_OK =>{
                  trace!("tibemsMapMsg_GetString: {:?}",status);
                  if !val_buf_ref.is_null() {
                    let header_value = CStr::from_ptr(val_buf_ref).to_str().unwrap();
                    body_entries.insert(header_name.to_string(),TypedValue::String(header_value.to_string()));  
                  }
                },
                tibems_status::TIBEMS_CONVERSION_FAILED => {
                  let mut msg2: usize = 0;
                  let status = tibco_ems_sys::tibemsMapMsg_GetMapMsg(msg_pointer, buf_ref, &mut msg2);
                  match status {
                    tibems_status::TIBEMS_OK => trace!("tibemsMapMsg_GetMapMsg: {:?}",status),
                    _ => error!("tibemsMapMsg_GetMapMsg: {:?}",status),
                  }
                  let mut raw_message = build_message_from_pointer(msg2);
                  match &mut raw_message {
                    Message::TextMessage(_msg) => {},
                    Message::BytesMessage(_msg) => {},
                    Message::MapMessage(msg) => {
                      msg.pointer=None;
                      body_entries.insert(header_name.to_string(), TypedValue::Map(msg.clone()));
                    },
                  }
                },
                _ => error!("tibemsMapMsg_GetString: {:?}",status),
              }
              
            }
            tibems_status::TIBEMS_NOT_FOUND =>{
              break;
            }
            _ => {
              println!("tibemsMsgEnum_GetNextName: {:?}",status);
              break;
            }
          }
        }
        let status = tibco_ems_sys::tibemsMsgEnum_Destroy(names_pointer);
        match status {
          tibems_status::TIBEMS_OK => trace!("tibemsMsgEnum_Destroy: {:?}",status),
          _ => error!("tibemsMsgEnum_Destroy: {:?}",status),
        }
        msg = Message::MapMessage(MapMessage{
          body: body_entries,
          header: None,
          pointer: Some(msg_pointer),
          reply_to: None,
        });
      },
      tibemsMsgType::TIBEMS_BYTES_MESSAGE => {
        msg = Message::BytesMessage(BytesMessage{
          body: vec![],
          header: None,
          pointer: Some(msg_pointer),
          reply_to: None,
        });
      },
      _ => {
        //unknown
        panic!("BodyType {:?} not implemented",msg_type);
      }
    }
    // fetch header
    let mut header_enumeration: usize = 0;
    let status = tibco_ems_sys::tibemsMsg_GetPropertyNames(msg_pointer, &mut header_enumeration);
    match status {
      tibems_status::TIBEMS_OK => trace!("tibemsMsg_GetPropertyNames: {:?}",status),
      _ => error!("tibemsMsg_GetPropertyNames: {:?}",status),
    }
    loop {
      let buf_vec:Vec<i8> = vec![0; 0];
      let buf_ref: *const std::os::raw::c_char = buf_vec.as_ptr();
      let status = tibco_ems_sys::tibemsMsgEnum_GetNextName(header_enumeration, &buf_ref);
      match status {
        tibems_status::TIBEMS_OK =>{
          let header_name = CStr::from_ptr(buf_ref).to_str().unwrap();
          let val_buf_vec:Vec<i8> = vec![0; 0];
          let val_buf_ref: *const std::os::raw::c_char = val_buf_vec.as_ptr();
          let status = tibco_ems_sys::tibemsMsg_GetStringProperty(msg_pointer, buf_ref, &val_buf_ref);
          match status {
            tibems_status::TIBEMS_OK => trace!("tibemsMsg_GetStringProperty: {:?}",status),
            _ => error!("tibemsMsg_GetStringProperty: {:?}",status),
          }
          let header_value = CStr::from_ptr(val_buf_ref).to_str().unwrap();
          header.insert(header_name.to_string(),TypedValue::String(header_value.to_string()));
        }
        tibems_status::TIBEMS_NOT_FOUND =>{
          break;
        }
        _ => {
          println!("tibemsMsgEnum_GetNextName: {:?}",status);
          break;
        }
      }
    }
    let status = tibco_ems_sys::tibemsMsgEnum_Destroy(header_enumeration);
    match status {
      tibems_status::TIBEMS_OK => trace!("tibemsMsgEnum_Destroy: {:?}",status),
      _ => error!("tibemsMsgEnum_Destroy: {:?}",status),
    }
    //add header to message
    match &mut msg {
      Message::TextMessage(msg) => msg.header=Some(header),
      Message::BytesMessage(msg) => msg.header=Some(header),
      Message::MapMessage(msg) => msg.header=Some(header),
    }
    // look for replyTo header
    let mut reply_destination: usize = 0;
    let status = tibco_ems_sys::tibemsMsg_GetReplyTo(msg_pointer, &mut reply_destination);
    match status {
      tibems_status::TIBEMS_OK => trace!("tibemsMsg_GetReplyTo: {:?}",status),
      _ => error!("tibemsMsg_GetReplyTo: {:?}",status),
    }
    if reply_destination != 0 {
      //has a destination
      let mut destination_type = tibemsDestinationType::TIBEMS_UNKNOWN;
      let status = tibco_ems_sys::tibemsDestination_GetType(reply_destination, &mut destination_type);
      match status {
        tibems_status::TIBEMS_OK => trace!("tibemsDestination_GetType: {:?}",status),
        _ => error!("tibemsDestination_GetType: {:?}",status),
      }
      let buf_size = 1024;
      let buf_vec:Vec<i8> = vec![0; buf_size];
      let buf_ref: *const std::os::raw::c_char = buf_vec.as_ptr();
      let status = tibco_ems_sys::tibemsDestination_GetName(reply_destination, buf_ref, buf_size);
      match status {
        tibems_status::TIBEMS_OK => trace!("tibemsDestination_GetName: {:?}",status),
        _ => error!("tibemsDestination_GetName: {:?}",status),
      }
      let destination_name: String = CStr::from_ptr(buf_ref).to_str().unwrap().to_string();
      let reply_destination_obj: Option<Destination>;
      match destination_type {
        tibemsDestinationType::TIBEMS_QUEUE =>{
          reply_destination_obj = Some(Destination::Queue(destination_name));
        },
        tibemsDestinationType::TIBEMS_TOPIC =>{
          reply_destination_obj = Some(Destination::Topic(destination_name));
        },
        _ =>{
          //ignore unknown type
          reply_destination_obj = None;
        }
      }
      //add replyTo to message
      match &mut msg {
        Message::TextMessage(msg) => msg.reply_to=reply_destination_obj,
        Message::BytesMessage(msg) => msg.reply_to=reply_destination_obj,
        Message::MapMessage(msg) => msg.reply_to=reply_destination_obj,
      }
    }      
  }
  msg
}

#[cfg(test)]
mod tests {
  use super::*;

  #[test]
  fn test_connection_failure() -> Result<(), String>{
    let result = connect("tcp://example.org:7222", "admin", "admin");
    match result{
      Ok(_val) => {
        return Err("no error was returned".to_string());
      },
      Err(_err) => {
        return Ok(());
      },
    }
  }
}