File: normMsgr.cpp

package info (click to toggle)
norm 1.5.9%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 9,680 kB
  • sloc: cpp: 123,494; xml: 7,536; tcl: 5,460; makefile: 3,442; python: 1,898; java: 1,750; ansic: 642; sh: 21; csh: 8
file content (1352 lines) | stat: -rw-r--r-- 44,931 bytes parent folder | download | duplicates (3)
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
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352

#include "normApi.h"
#include <unistd.h>      // for read() and write()
#include <stdio.h>       // for printf(), etc
#include <stdlib.h>      // for srand()
#include <string.h>      // for strrchr(), memset(), etc
#include <sys/time.h>    // for gettimeofday()
#include <arpa/inet.h>   // for htons()
#include <fcntl.h>       // for, well, fnctl()
#include <errno.h>       // obvious child
#include <assert.h>      // embarrassingly obvious

#define SHOOT_NOW 1

#include "protoCheck.h"

class NormMsgr
{
    public:
        NormMsgr();
        ~NormMsgr();
        
        // some day build these directly into NORM API
        enum CCMode {NORM_FIXED, NORM_CC, NORM_CCE, NORM_CCL};
        
        enum 
        {
            MSG_SIZE_MAX = 65535,
            MSG_HEADER_SIZE = 2
        };  
            
        // helper class and methods for message management
        class MessageQueue;
        class Message
        {
            friend class MessageQueue;
            public:
                Message();
                Message(char* buffer, unsigned int size);
                ~Message();
                
                bool Init(unsigned int size);
                void Destroy();
                
                unsigned int GetSize() const
                    {return msg_size;}
                const char* GetHeader() const
                    {return msg_header;}
                char* AccessHeader()
                    {return msg_header;}
                const char* GetBuffer() const
                    {return msg_buffer;}
                char* AccessBuffer()
                    {return msg_buffer;}
                
                void ResetIndex(unsigned int index = 0)
                    {msg_index = index;}
                void IncrementIndex(unsigned int count)
                    {msg_index += count;}
                unsigned int GetIndex() const
                    {return msg_index;}
                
                bool IsComplete() const
                    {return (msg_index == (MSG_HEADER_SIZE + msg_size));}
                
            private:
                unsigned int    msg_size;
                unsigned int    msg_index;
                char            msg_header[MSG_HEADER_SIZE];  // this could be externalized to NormMsgr::input_msg_header / output_msg_header members
                char*           msg_buffer;
                Message*        prev;
                Message*        next;
        };  // end class NormMsgr::Message    
        
        class MessageQueue
        {
            public:
                MessageQueue();
                ~MessageQueue();
                
                bool IsEmpty() const
                    {return (NULL == head);}
                
                void Prepend(Message& msg);
                void Append(Message& msg);
                void Remove(Message& msg);
                Message* RemoveHead();
                Message* RemoveTail();
                
                void Destroy();
                
                unsigned int GetCount() const
                    {return msg_count;}
                
            private:
                Message*        head;
                Message*        tail;
                unsigned int    msg_count;
        };  // end class NormMsgr::MessageQueue
        Message* NewMessage(unsigned int size);
        
        void Destroy();
            
        bool OpenNormSession(NormInstanceHandle instance, 
                             const char*        addr,
                             unsigned short     port,
                             NormNodeId         nodeId);
        void CloseNormSession();
        
        void SetNormCongestionControl(CCMode ccMode);
        void SetNormTxRate(double bitsPerSecond)
        {
            assert(NORM_SESSION_INVALID != norm_session);
            NormSetTxRate(norm_session, bitsPerSecond);
        }
        void SetNormMulticastInterface(const char* ifaceName)
        {
            assert(NORM_SESSION_INVALID != norm_session);
            NormSetMulticastInterface(norm_session, ifaceName);
        } 
        void SetLoopback(bool state)
        {
            loopback = state;
            if (NORM_SESSION_INVALID != norm_session)
                NormSetMulticastLoopback(norm_session, state);
        }   
        void SetNormMessageTrace(bool state)
        {
            assert(NORM_SESSION_INVALID != norm_session);
            NormSetMessageTrace(norm_session, state);
        } 
        void AddAckingNode(NormNodeId ackId)
        {
            assert(NORM_SESSION_INVALID != norm_session);
            NormAddAckingNode(norm_session, ackId);
            norm_acking = true;  // invoke ack-based flow control
        }
        void SetFlushing(bool state)
            {norm_flushing = state;}
        
        bool Start(bool sender, bool receiver);
        void Stop()
            {is_running = false;}
        bool IsRunning() const
            {return is_running;}
        
        unsigned long GetSentCount()
            {return sent_count;}
        
        void HandleNormEvent(const NormEvent& event);
        
        // Sender methods
        FILE* GetInputFile() const
            {return input_file;}
        int GetInputDescriptor() const
            {return input_fd;}
        bool InputReady() const
            {return input_ready;}
        void SetInputReady()
            {input_ready = true;}
        bool InputNeeded() const
            {return input_needed;}
        bool InputMessageReady() const
            {return ((NULL != input_msg) && !input_needed);}
        bool ReadInput();
        
        bool TxReady() const;
        bool SendMessage();
        bool EnqueueMessageObject();
        
        // Receiver methods
        void SetOutputFile(FILE* filePtr)
        {
            output_file = filePtr;
            output_fd = fileno(filePtr);
        }
        FILE* GetOutputFile() const
            {return output_file;}
        int GetOutputDescriptor() const
            {return output_fd;}
        void SetOutputReady()
            {output_ready = true;}
        bool OutputReady() const
            {return output_ready;}
        bool OutputPending() const
            {return (NULL != output_msg);}
        bool RxNeeded() const
            {return rx_needed;}
        bool WriteOutput();
        
        void OmitHeader(bool state) 
            {omit_header = state;}
        
        // These can only be called post-OpenNormSession
        void SetAutoAck(bool enable)
        {
                NormTrackingStatus trackingMode = enable? NORM_TRACK_RECEIVERS : NORM_TRACK_NONE;
                NormSetAutoAckingNodes(norm_session, trackingMode);
                norm_acking = enable;
        }
        void SetSilentReceiver(bool state)
            {NormSetSilentReceiver(norm_session, state);}
        void SetTxLoss(double txloss)
            {NormSetTxLoss(norm_session, txloss);}
            
    private:
        bool                is_running;                                                  
        FILE*               input_file;      // stdin by default  
        int                 input_fd;
        bool                input_ready;                       
        bool                input_needed;    //                   
        bool                input_finished;                       
        Message*            input_msg;       // current input message being read/sent*   
        MessageQueue        input_msg_list;  // list of enqueued messages (in norm sender cache)
            
        NormSessionHandle   norm_session;
        bool                is_multicast;
        bool                loopback;
        unsigned int        norm_tx_queue_max;   // max number of objects that can be enqueued at once 
        unsigned int        norm_tx_queue_count; // count of unacknowledged enqueued objects (TBD - optionally track size too)
        bool                norm_flow_control_pending;
        bool                norm_tx_vacancy;
        bool                norm_acking;
        bool                norm_flushing;
        NormObjectHandle    norm_flush_object;
        NormObjectHandle    norm_last_object;
        unsigned long       sent_count;
        
        bool                rx_needed;
        FILE*               output_file;
        int                 output_fd;
        bool                output_ready;
        Message*            output_msg;
        MessageQueue        output_msg_queue;
        MessageQueue        temp_msg_queue;
        // These are some options mainly for testing purposes
        bool                omit_header;  // if "true", receive message length header is _not_ written to output
        bool                rx_silent;
        double              tx_loss;
            
};  // end class NormMsgr

NormMsgr::NormMsgr()
 : input_file(stdin), input_fd(fileno(stdin)), input_ready(false), input_needed(false), input_finished(false),
   input_msg(NULL), norm_session(NORM_SESSION_INVALID), is_multicast(false), loopback(false), 
   norm_tx_queue_max(8192), norm_tx_queue_count(0), 
   norm_flow_control_pending(false), norm_tx_vacancy(true), norm_acking(false), 
   norm_flushing(true), norm_flush_object(NORM_OBJECT_INVALID), norm_last_object(NORM_OBJECT_INVALID),
   sent_count(0),  rx_needed(false), output_file(stdout), output_fd(fileno(stdout)), 
   output_ready(true), output_msg(NULL), 
   omit_header(false), rx_silent(false), tx_loss(0.0)
{
}

NormMsgr::~NormMsgr()
{
    Destroy();
}

void NormMsgr::Destroy()
{
    if (NULL != input_msg) 
    {
        delete input_msg;
        input_msg = NULL;
    }
    input_msg_list.Destroy();
    if (NULL != output_msg) 
    {
        delete output_msg;
        delete output_msg;
    }
    output_msg_queue.Destroy();
}


bool NormMsgr::OpenNormSession(NormInstanceHandle instance, const char* addr, unsigned short port, NormNodeId nodeId)
{
    if (NormIsUnicastAddress(addr))
        is_multicast = false;
    else
        is_multicast = true;
    norm_session = NormCreateSession(instance, addr, port, nodeId);
    if (NORM_SESSION_INVALID == norm_session)
    {
        fprintf(stderr, "normMsgr error: unable to create NORM session\n");
        return false;
    }
    
    if (is_multicast)
    {
        NormSetRxPortReuse(norm_session, true);
        if (loopback)
            NormSetMulticastLoopback(norm_session, true);
    }
    
    
    // Set some default parameters (maybe we should put parameter setting in Start())
    fprintf(stderr, "setting rx cache limit to %u\n", norm_tx_queue_max);
    if (norm_tx_queue_max > 65535/2) norm_tx_queue_max = 65535/2;
    NormSetRxCacheLimit(norm_session, norm_tx_queue_max);
    NormSetDefaultSyncPolicy(norm_session, NORM_SYNC_ALL);
    
    if (!is_multicast)
        NormSetDefaultUnicastNack(norm_session, true);
    NormSetTxCacheBounds(norm_session, 10*1024*1024, norm_tx_queue_max, norm_tx_queue_max);
    
    //NormSetMessageTrace(norm_session, true);
    
    //NormSetTxRobustFactor(norm_session, 20);
    
    return true;
}  // end NormMsgr::OpenNormSession()

void NormMsgr::CloseNormSession()
{
    if (NORM_SESSION_INVALID == norm_session) return;
    NormDestroySession(norm_session);
    norm_session = NORM_SESSION_INVALID;
}  // end NormMsgr::CloseNormSession()

void NormMsgr::SetNormCongestionControl(CCMode ccMode)
{
    assert(NORM_SESSION_INVALID != norm_session);
    switch (ccMode)
    {
        case NORM_CC:  // default TCP-friendly congestion control
            NormSetEcnSupport(norm_session, false, false, false);
            break;
        case NORM_CCE: // "wireless-ready" ECN-only congestion control
            NormSetEcnSupport(norm_session, true, true);
            break;
        case NORM_CCL: // "loss tolerant", non-ECN congestion control
            NormSetEcnSupport(norm_session, false, false, true);
            break;
        case NORM_FIXED:
            NormSetEcnSupport(norm_session, false, false, false);
            break;
    }
    if (NORM_FIXED != ccMode)
        NormSetCongestionControl(norm_session, true);
    else
        NormSetCongestionControl(norm_session, false);
}  // end NormMsgr::SetNormCongestionControl()

bool NormMsgr::Start(bool sender, bool receiver)
{
    fprintf(stderr, "enter NormMsgr::Start() ...\n");
    // TBD - make these command-line accessible
    unsigned int bufferSize = 64*1024*1024;
    unsigned int segmentSize = 1400;
    unsigned int blockSize = 64;
    unsigned int numParity = 0;
    unsigned int txSockBufferSize = 4*1024*1024;
    unsigned int rxSockBufferSize = 6*1024*1024;
    
    if (receiver)
    {
        if (!NormStartReceiver(norm_session, bufferSize))
        {
            fprintf(stderr, "normMsgr error: unable to start NORM receiver\n");
            return false;
        }
        // Note: NormPreallocateRemoteSender() MUST be called AFTER NormStartReceiver()
        NormPreallocateRemoteSender(norm_session, bufferSize, segmentSize, blockSize, numParity, bufferSize);
        NormSetRxSocketBuffer(norm_session, rxSockBufferSize);
        rx_needed = true;
        fprintf(stderr, "normMsgr: receiver ready.\n");
    }
    if (sender)
    {
        if (norm_acking)
        {   
            // ack-based flow control enabled on command-line, 
            // so disable timer-based flow control
            NormSetFlowControl(norm_session, 0.0);
        }
        NormSetGrttEstimate(norm_session, 0.001);
        //NormSetGrttMax(norm_session, 0.100);
        NormSetBackoffFactor(norm_session, 0);
        
        // Pick a random instance id for now
        struct timeval currentTime;
        gettimeofday(&currentTime, NULL);
        srand(currentTime.tv_usec);  // seed random number generator
        NormSessionId instanceId = (NormSessionId)rand();
        if (!NormStartSender(norm_session, instanceId, bufferSize, segmentSize, blockSize, numParity))
        {
            fprintf(stderr, "normMsgr error: unable to start NORM sender\n");
            if (receiver) NormStopReceiver(norm_session);
            return false;
        }
        //NormSetAutoParity(norm_session, 2);
        NormSetTxSocketBuffer(norm_session, txSockBufferSize);
        input_needed = true;
    }
    //ProtoCheckResetLogging();
    is_running = true;
    return true;
}  // end NormMsgr::Start();

// Returns "true" when a complete message has been read
bool NormMsgr::ReadInput()
{
    assert(input_needed);
    while (input_needed)
    {
        size_t readLength;
        char* bufferPtr;
        if ((NULL == input_msg) || (input_msg->GetIndex() < MSG_HEADER_SIZE)) 
        {
            if (NULL == input_msg)
            {
                // Allocate a new message/buffer for input
                if (NULL == (input_msg = new Message()))
                {
                    perror("normMsgr new Message error");
                    Stop();  // fatal out of memory error
                    return false;
                }
            }
            // need to read msg "length" header
            readLength = MSG_HEADER_SIZE - input_msg->GetIndex();
            bufferPtr = input_msg->AccessHeader() + input_msg->GetIndex();
        }
        else
        {
            unsigned int offset = input_msg->GetIndex() - MSG_HEADER_SIZE;
            readLength = input_msg->GetSize() - offset;
            bufferPtr = input_msg->AccessBuffer() + offset;
        }
        ssize_t result = read(input_fd, bufferPtr, readLength);
        if (result > 0)
        {
            input_msg->IncrementIndex(result);
            if ((size_t)result < readLength) 
            {
                // Still need more input
                // (wait for next input notification to read more)
                return false;
            }
            else if (MSG_HEADER_SIZE == input_msg->GetIndex())
            {
                // We have now read the message size header
                // TBD - support other message header formats?
                // (for now, assume 2-byte message length header)
                uint16_t msgSize ;
                memcpy(&msgSize, input_msg->GetHeader(), MSG_HEADER_SIZE);
                msgSize = ntohs(msgSize) - MSG_HEADER_SIZE;
                if (!input_msg->Init(msgSize))
                {
                    perror("normMsgr: input message initialization error");
                    Stop();  // fatal out of memory error
                    return false;
                }    
            }
            if (input_msg->IsComplete())  // have read complete header and message
            {
                // We have now read in the complete message
                input_needed = false;
            }
        }
        else if (0 == result)
        {
             // end-of-file reached, TBD - trigger final flushing and wrap-up
            fprintf(stderr, "normMsgr: input end-of-file detected (last:%p)...\n", norm_last_object);
            delete input_msg;
            input_msg = NULL;
            input_ready = false;
            input_needed = false;
            input_finished = true;
            if (norm_acking)
            {
                if (NORM_OBJECT_INVALID == norm_last_object)
                    is_running = false;  // everything sent and acked
                else if (!norm_flushing)
                    NormSetWatermark(norm_session, norm_last_object, true);
            }
        }
        else  // result < 0
        {
            switch (errno)
            {
                case EINTR:
                    continue;  // interupted, try again
                case EAGAIN:
                    // input starved, wait for next notification
                    break;
                default:
                    perror("normMsgr error reading input");
                    break;
            }
            input_ready = false;
            return false;
        }
    }  
    return true;
}  // end NormMsgr::ReadInput()

bool NormMsgr::WriteOutput()
{
    while (NULL != output_msg)
    {
        size_t writeLength;
        const char* bufferPtr;
        if (output_msg->GetIndex() < MSG_HEADER_SIZE)
        {
            writeLength = MSG_HEADER_SIZE - output_msg->GetIndex();
            bufferPtr = output_msg->GetHeader() + output_msg->GetIndex();
        }
        else
        {
            unsigned int offset = output_msg->GetIndex() - MSG_HEADER_SIZE;
            writeLength = output_msg->GetSize() - offset;
            bufferPtr = output_msg->GetBuffer() + offset;
        }
        ssize_t result = write(output_fd, bufferPtr, writeLength);
        if (result >= 0)
        {
            output_msg->IncrementIndex(result);
            if ((size_t)result < writeLength)
                output_ready = false; // blocked, wait for output notification
        }
        else
        {
            switch (errno)
            {
                case EINTR:
                    continue;  // interupted, try again
                case EAGAIN:
                    // input starved, wait for next notification
                    output_ready = false;
                    break;
                default:
                    perror("normMsgr error writing output");
                    break;
            }
            return false;
        }
        if (output_msg->IsComplete())
        {
            fflush(output_file);
            //delete output_msg;
            
            temp_msg_queue.Append(*output_msg); // cache for debugging purposes
            if (temp_msg_queue.GetCount() > 32)
            {
                //fprintf(stderr, "deleting cached recv'd message ...\n");
                delete temp_msg_queue.RemoveHead();
            }
            output_msg = output_msg_queue.RemoveHead();
            if (NULL != output_msg)
            {
                if (omit_header)
                    output_msg->ResetIndex(MSG_HEADER_SIZE);
                else
                    output_msg->ResetIndex();
            }
            else
            {
                rx_needed = true;
            }
            break;
        }
    }
    return true;
}  // end NormMsgr::WriteOutput()

bool NormMsgr::TxReady() const
{
    // This returns true if new tx data can be enqueued to NORM
    // This is based on the state with respect to prior successful data
    // data enqueuing (or stream writing) and NORM_TX_QUEUE_EMPTY or
    // NORM_TX_QUEUE_VACANCY notifications (tracked by the "norm_tx_vacancy" variable,
    // _and_ (if ack-based flow control is enabled) the norm_tx_queue_count or 
    // norm_stream_buffer_count status.
    if (norm_tx_vacancy)
    {
        if (norm_tx_queue_count >= norm_tx_queue_max)
            return false;  // still waiting for ACK
        return true;
    }
    else
    {
        return false;
    }
}  // end NormMsgr::NormTxReady()

bool NormMsgr::SendMessage()
{
    if (EnqueueMessageObject())
    {
        // Our buffered message was sent, so reset input indices
        // and request next message from input
        input_msg_list.Append(*input_msg);
        input_msg = NULL;
        input_needed = true;
        sent_count++;
        return true;   
    }
    // else will be prompted to retry by NORM event (queue vacancy, watermark completion)
    return false;
}  // end NormMsgr::SendMessage()

bool NormMsgr::EnqueueMessageObject()
{
    if (norm_acking) 
    {
        assert(norm_tx_queue_count < norm_tx_queue_max);
        if (norm_tx_queue_count >= norm_tx_queue_max)
            return false;
    }
        
    // Enqueue the message data for transmission
    NormObjectHandle object = NormDataEnqueue(norm_session, input_msg->AccessBuffer(), input_msg->GetSize());
    if (NORM_OBJECT_INVALID == object)
    {
        // This might happen if a non-acking receiver is present and
        // has nacked for the oldest object in the queue even if all                
        // of our acking receivers have acknowledged it.  
        fprintf(stderr, "NO VACANCY count:%u max:%u\n", norm_tx_queue_count, norm_tx_queue_max);
        norm_tx_vacancy = false;     
        return false;
    }
    //NormObjectRetain(object);
    NormObjectSetUserData(object, input_msg); // so we can remove/delete upon purge
    if (norm_acking)
    {
        // ack-based flow control has been enabled
        norm_tx_queue_count++;
        if (!norm_flow_control_pending && (norm_tx_queue_count >= (norm_tx_queue_max / 2)))
        {
            NormSetWatermark(norm_session, object, true);  // overrideFlush == true
            norm_last_object = object;
            norm_flow_control_pending = true;
        }
        else if (norm_flushing)  // per-message acking
        {
#ifdef SHOOT_FIRST
            NormSetWatermark(norm_session, object, true);
            norm_last_object = object;
#else // ACK_LATER
            if (norm_flow_control_pending)
            {
                norm_flush_object = object;  // will be used as watermark upon flow control ack
            }
            else
            {
                NormSetWatermark(norm_session, object, true);
                norm_last_object = object;
            }
#endif // SHOOT_FIRST/ACK_LATER
        }
        else
        {
            //norm_last_object = object;
        }
    }
    return true;
}  // end NormMsgr::EnqueueMessageObject()

void NormMsgr::HandleNormEvent(const NormEvent& event)
{
    switch (event.type)
    {
        case NORM_TX_QUEUE_EMPTY:
        case NORM_TX_QUEUE_VACANCY:
            norm_tx_vacancy = true;
            break;
            
        case NORM_GRTT_UPDATED:
            //fprintf(stderr, "new GRTT = %lf\n", NormGetGrttEstimate(norm_session));
            break;
            
        case NORM_TX_WATERMARK_COMPLETED:
            if (NORM_ACK_SUCCESS == NormGetAckingStatus(norm_session))
            {
                //fprintf(stderr, "WATERMARK COMPLETED\n");
                norm_last_object = NORM_OBJECT_INVALID;
                if (norm_flow_control_pending)
                {
                    norm_tx_queue_count -= (norm_tx_queue_max / 2);
                    norm_flow_control_pending = false;
                    if (NORM_OBJECT_INVALID != norm_flush_object)
                    {
                        NormSetWatermark(norm_session, norm_flush_object, true);
                        norm_last_object = norm_flush_object;
                        norm_flush_object = NORM_OBJECT_INVALID;
                    }
                }
                if (input_finished && (NORM_OBJECT_INVALID == norm_last_object))
                    is_running = false;
            }
            else
            {
                // TBD - we could see who did and how didn't ACK and possibly remove them
                //       from our acking "membership".  For now, we are infinitely
                //       persistent by resetting watermark ack request without clearing
                //       flow control
                if (NORM_OBJECT_INVALID == norm_flush_object)
                {
                    NormResetWatermark(norm_session);
                }
                else  // might as request ack for most recent enqueued object
                {
                    NormSetWatermark(norm_session, norm_flush_object, true);
                    norm_last_object = norm_flush_object;
                    norm_flush_object = NORM_OBJECT_INVALID;
                }
            }
            break; 
            
        case NORM_TX_OBJECT_PURGED:
        {
            NormDataDetachData(event.object);
            Message* msg = (Message*)NormObjectGetUserData(event.object);
            if(event.object == norm_flush_object)
                norm_flush_object = NORM_OBJECT_INVALID;
            if (NULL != msg)
            {
                input_msg_list.Remove(*msg);
                delete msg;
            }
            //NormObjectRelease(event.object);
            //fprintf(stderr, "normMsgr LOGGING ALLOCATIONS\n");
            //ProtoCheckLogAllocations(stderr);
            break;
        }   
        
        case NORM_REMOTE_SENDER_INACTIVE:
            //fprintf(stderr, "REMOTE SENDER INACTIVE node: %u\n", NormNodeGetId(event.sender));
            //NormNodeDelete(event.sender);
            //logAllocs = true;
            break;
        
        case NORM_RX_OBJECT_ABORTED:
            //fprintf(stderr, "NORM_RX_OBJECT_ABORTED\n");// %hu\n", NormObjectGetTransportId(event.object));
            break;
            
        case NORM_RX_OBJECT_COMPLETED:
        {   
            sent_count++;
            char* data = NormDataDetachData(event.object);
            if (NULL != data)
            {
                Message* msg = new Message(data, NormObjectGetSize(event.object));
                if (NULL == msg)
                {
                    perror("normMsgr: new Message() error");
                    delete[] data;  // TBD - may need to finally implement NormDelete() function!
                    // TBD Stop() as a fatal out of memory error?
                    break;
                }
                if (NULL == output_msg)
                {
                    output_msg = msg;
                    if (omit_header)
                        output_msg->ResetIndex(MSG_HEADER_SIZE);
                    else
                        output_msg->ResetIndex();
                }
                else
                {
                    output_msg_queue.Append(*msg);
                }
                rx_needed = false;
            }
            // else TBD - "termination" info-only object?
            break;
        }
            
        default:
            break;     
    }
    //NormReleasePreviousEvent(NormGetInstance(norm_session));
            
}  // end NormMsgr::HandleNormEvent()


void Usage()
{
    fprintf(stderr, "Usage: normMsgr id <nodeIdInteger> {send &| recv} [addr <addr>[/<port>]]\n"
                    "                [ack auto|<node1>[,<node2>,...]] [output <outFile>]\n"
                    "                [cc|cce|ccl|rate <bitsPerSecond>] [interface <name>] [loopback]\n"
                    "                [debug <level>] [trace] [log <logfile>] [silent]\n"
                    "                [flush {none|passive|active}] [omit] [txloss <lossFraction>]\n");
}
int main(int argc, char* argv[])
{
    // REQUIRED parameters initiailization
    NormNodeId nodeId = NORM_NODE_NONE;
    bool send = false;
    bool recv = false;
    
    char sessionAddr[64];
    strcpy(sessionAddr, "224.1.2.3");
    unsigned int sessionPort = 6003;
    
    bool autoAck = false;
    NormNodeId ackingNodeList[256]; 
    unsigned int ackingNodeCount = 0;
    bool flushing = false;
    
    double txRate = 0.0; // used for non-default NORM_FIXED ccMode
    NormMsgr::CCMode ccMode = NormMsgr::NORM_CC;
    const char* mcastIface = NULL;
    
    int debugLevel = 0;
    const char* debugLog = NULL;  // stderr by default
    bool trace = false;
    bool omitHeaderOnOutput = false;
    bool silentReceiver = false;
    double txloss = 0.0;
    bool loopback = false;
    
    NormMsgr normMsgr;
    
    // Parse command-line
    int i = 1;
    while (i < argc)
    {
        const char* cmd = argv[i++];
        size_t len = strlen(cmd);
        if (0 == strncmp(cmd, "send", len))
        {
            send = true;
        }
        else if (0 == strncmp(cmd, "recv", len))
        {
            recv = true;
        }
        else if (0 == strncmp(cmd, "loopback", len))
        {
            loopback = true;
        }
        else if (0 == strncmp(cmd, "addr", len))
        {
            if (i >= argc)
            {
                fprintf(stderr, "nodeMsgr error: missing 'addr[/port]' value!\n");
                Usage();
                return -1;
            }
            const char* addrPtr = argv[i++];
            const char* portPtr = strchr(addrPtr, '/');
            if (NULL == portPtr)
            {
                strncpy(sessionAddr, addrPtr, 63);
                sessionAddr[63] = '\0';
            }
            else
            {
                size_t addrLen = portPtr - addrPtr;
                if (addrLen > 63) addrLen = 63;  // should issue error message
                strncpy(sessionAddr, addrPtr, addrLen);
                sessionAddr[addrLen] = '\0';
                portPtr++;
                sessionPort = atoi(portPtr);
            }
        }
        else if (0 == strncmp(cmd, "output", len))
        {
            if (i >= argc)
            {
                fprintf(stderr, "normMsgr error: missing output 'device' name!\n");
                Usage();
                return -1;
            }
            FILE* outfile = fopen(argv[i++], "w+");
            if (NULL == outfile)
            {
                perror("normMsgr output device fopen() error");
                Usage();
                return -1;
            }
            normMsgr.SetOutputFile(outfile);
        }
        else if (0 == strncmp(cmd, "id", len))
        {
            if (i >= argc)
            {
                fprintf(stderr, "nodeMsgr error: missing 'id' value!\n");
                Usage();
                return -1;
            }
            nodeId = atoi(argv[i++]);
        }
        else if (0 == strncmp(cmd, "ack", len))
        {
            // comma-delimited acking node id list
            if (i >= argc)
            {
                fprintf(stderr, "nodeMsgr error: missing 'id' <nodeId> value!\n");
                Usage();
                return -1;
            }
            const char* alist = argv[i++];
            if (0 == strcmp("auto", alist))
            {
                autoAck = true;
            }
            else
            {
                autoAck = false;
                while ((NULL != alist) && (*alist != '\0'))
                {
                    // TBD - Do we need to skip leading white space?
                    int id;
                    if (1 != sscanf(alist, "%d", &id))
                    {
                        fprintf(stderr, "nodeMsgr error: invalid acking node list!\n");
                        Usage();
                        return -1;
                    }
                    ackingNodeList[ackingNodeCount] = NormNodeId(id);
                    ackingNodeCount++;
                    alist = strchr(alist, ',');
                    if (NULL != alist) alist++;  // point past comma
                }
            }
        }
        else if (0 == strncmp(cmd, "flush", len))
        {
            // "none", "passive", or "active"
            if (i >= argc)
            {
                fprintf(stderr, "nodeMsgr error: missing 'flush' <mode>!\n");
                Usage();
                return -1;
            }
            const char* mode = argv[i++];
            if (0 == strcmp(mode, "none"))
            {
                flushing = false;
            }
            else if (0 == strcmp(mode, "passive"))
            {
                flushing = false;
            }
            else if (0 == strcmp(mode, "active"))
            {
                flushing = true;
            }
            else
            {
                fprintf(stderr, "normMsgr error: invalid 'flush' mode \"%s\"\n", mode);
                return -1;
            }   
        }
        else if (0 == strncmp(cmd, "rate", len))
        {
            if (i >= argc)
            {
                fprintf(stderr, "nodeMsgr error: missing 'rate' <bitsPerSecond> value!\n");
                Usage();
                return -1;
            }
            if (1 != sscanf(argv[i++], "%lf", &txRate))
            {
                fprintf(stderr, "nodeMsgr error: invalid transmit rate!\n");
                Usage();
                return -1;
            }       
            // set fixed-rate operation
            ccMode = NormMsgr::NORM_FIXED;     
        }
        else if (0 == strcmp(cmd, "cc"))
        {
            ccMode = NormMsgr::NORM_CC;
        }
        else if (0 == strcmp(cmd, "cce"))
        {
            ccMode = NormMsgr::NORM_CCE;
        }
        else if (0 == strcmp(cmd, "ccl"))
        {
            ccMode = NormMsgr::NORM_CCL;
        }
        else if (0 == strncmp(cmd, "interface", len))
        {
            if (i >= argc)
            {
                fprintf(stderr, "nodeMsgr error: missing 'interface' <name>!\n");
                Usage();
                return -1;
            }
            mcastIface = argv[i++];
        }
        else if (0 == strncmp(cmd, "omit", len))
        {
            omitHeaderOnOutput = true;
        }
        else if (0 == strncmp(cmd, "silent", len))
        {
            silentReceiver = true;
        }
        else if (0 == strncmp(cmd, "txloss", len))
        {
            if (1 != sscanf(argv[i++], "%lf", &txloss))
            {
                fprintf(stderr, "nodeMsgr error: invalid 'txloss' value!\n");
                Usage();
                return -1;
            }
        }
        else if (0 == strncmp(cmd, "debug", len))
        {
            if (i >= argc)
            {
                fprintf(stderr, "nodeMsgr error: missing 'debug' <level>!\n");
                Usage();
                return -1;
            }
            debugLevel = atoi(argv[i++]);
        }
        else if (0 == strncmp(cmd, "log", len))
        {
            if (i >= argc)
            {
                fprintf(stderr, "nodeMsgr error: missing 'log' <fileName>!\n");
                Usage();
                return -1;
            }
            debugLog = argv[i++];
        }
        else if (0 == strncmp(cmd, "trace", len))
        {
            trace = true;
        }
        else if (0 == strncmp(cmd, "help", len))
        {
            Usage();
            return 0;
        }
        else
        {
            fprintf(stderr, "nodeMsgr error: invalid command \"%s\"!\n", cmd);
            Usage();
            return -1;
        }
    }
    
    if (!send && !recv)
    {
        fprintf(stderr, "normMsgr error: not configured to send or recv!\n");
        Usage();
        return -1;
    }
    if (NORM_NODE_NONE == nodeId)
    {
        fprintf(stderr, "normMsgr error: no local 'id' provided!\n");
        Usage();
        return -1;
    }
    
    // TBD - should provide more error checking of calls
    NormInstanceHandle normInstance = NormCreateInstance();
    NormSetDebugLevel(debugLevel);
    if (NULL != debugLog)
        NormOpenDebugLog(normInstance, debugLog);
    
    normMsgr.SetLoopback(loopback);
    normMsgr.SetFlushing(flushing);
    
    if (omitHeaderOnOutput) normMsgr.OmitHeader(true);
    
    if (!normMsgr.OpenNormSession(normInstance, sessionAddr, sessionPort, (NormNodeId)nodeId))
    {
        fprintf(stderr, "normMsgr error: unable to open NORM session\n");
        NormDestroyInstance(normInstance);
        return -1;
    }
    
    if (silentReceiver) normMsgr.SetSilentReceiver(true);
    if (txloss > 0.0) normMsgr.SetTxLoss(txloss);
    
    if (autoAck)
    {
        normMsgr.SetAutoAck(true);
    }
    else
    {
        for (unsigned int i = 0; i < ackingNodeCount; i++)
            normMsgr.AddAckingNode(ackingNodeList[i]);
    }
    
    normMsgr.SetNormCongestionControl(ccMode);
    if (NormMsgr::NORM_FIXED == ccMode)
        normMsgr.SetNormTxRate(txRate);
    if (NULL != mcastIface)
        normMsgr.SetNormMulticastInterface(mcastIface);
    
    if (trace) normMsgr.SetNormMessageTrace(true);
    
    // TBD - set NORM session parameters
    normMsgr.Start(send, recv); 
    
    int normfd = NormGetDescriptor(normInstance);
    // Get input/output descriptors and set to non-blocking i/o
    int inputfd = normMsgr.GetInputDescriptor();
    if (-1 == fcntl(inputfd, F_SETFL, fcntl(inputfd, F_GETFL, 0) | O_NONBLOCK))
        perror("normMsgr: fcntl(inputfd, O_NONBLOCK) error");
    int outputfd = normMsgr.GetOutputDescriptor();
    if (-1 == fcntl(outputfd, F_SETFL, fcntl(outputfd, F_GETFL, 0) | O_NONBLOCK))
        perror("normMsgr: fcntl(outputfd, O_NONBLOCK) error");
    fd_set fdsetInput, fdsetOutput;
    FD_ZERO(&fdsetInput);
    FD_ZERO(&fdsetOutput);
    struct timeval lastTime;
    gettimeofday(&lastTime, NULL);
    while (normMsgr.IsRunning())
    {
        int maxfd = -1;
        // Only wait on NORM if needed for tx readiness
        bool waitOnNorm = true;
        if (!(normMsgr.RxNeeded() || normMsgr.InputMessageReady()))
            waitOnNorm = false; // no need to wait
        else if (normMsgr.InputMessageReady() && normMsgr.TxReady())  
            waitOnNorm = false; // no need to wait if already tx ready
        if (waitOnNorm)
        {
            maxfd = normfd;
            FD_SET(normfd, &fdsetInput);
        }
        else
        {
            FD_CLR(normfd, &fdsetInput);
        }
        if (normMsgr.InputNeeded() && !normMsgr.InputReady())
        {   
            FD_SET(inputfd, &fdsetInput);
            if (inputfd > maxfd) maxfd = inputfd;
        }   
        else
        {
            FD_CLR(inputfd, &fdsetInput);
        }
        if (normMsgr.OutputPending() && !normMsgr.OutputReady())
        {
            FD_SET(outputfd, &fdsetOutput);
            if (outputfd > maxfd) maxfd = outputfd;
        }
        else
        {   
            FD_CLR(outputfd, &fdsetOutput);
        }
        if (maxfd >= 0)
        {
            struct timeval timeout;
            timeout.tv_sec = 1;
            timeout.tv_usec = 0;
            int result = select(maxfd+1, &fdsetInput, &fdsetOutput, NULL, &timeout);
            switch (result)
            {
                case -1:
                    switch (errno)
                    {
                        case EINTR:
                        case EAGAIN:
                            continue;
                        default:
                            perror("normMsgr select() error");
                            // TBD - stop NormMsgr
                            break;
                    }
                    break;
                case 0:
                    // shouldn't occur for now (no timeout)
                    //fprintf(stderr, "normMsgr timeout ...\n");
                    continue;
                default:
                    if (FD_ISSET(inputfd, &fdsetInput))
                    {
                        normMsgr.SetInputReady();
                    }   
                    /*if (FD_ISSET(normfd, &fdsetInput))
                    {
                        NormEvent event;
                        while (NormGetNextEvent(normInstance, &event, false))
                            normMsgr.HandleNormEvent(event);
                    }*/  
                    if (FD_ISSET(outputfd, &fdsetOutput))
                    {
                        normMsgr.SetOutputReady();
                    }
                    break; 
            }
        }
        
        NormSuspendInstance(normInstance);
        NormEvent event;
        while (NormGetNextEvent(normInstance, &event, false))
            normMsgr.HandleNormEvent(event);
        // As a result of reading input or NORM notification events,  
        // we may be ready to read input and/or send a message if it's been read
        const int LOOP_MAX = 100;
        int loopCount = 0;
        while (loopCount < LOOP_MAX)
        {
            loopCount++;
            if (normMsgr.InputNeeded() && normMsgr.InputReady())
                normMsgr.ReadInput();
            if (normMsgr.InputMessageReady() && normMsgr.TxReady())
                normMsgr.SendMessage();
            // and/or output a received message if we need
            if (normMsgr.OutputPending() && normMsgr.OutputReady())
                normMsgr.WriteOutput();  
        }
        NormResumeInstance(normInstance);
            
    }  // end while(normMsgr.IsRunning()
    
    
    
    NormCloseDebugLog(normInstance);
    
    fprintf(stderr, "destroying session ...\n");
    normMsgr.CloseNormSession();
    
    fprintf(stderr, "destroying instance ...\n");
    
    NormDestroyInstance(normInstance);
    
    normMsgr.Destroy();
    
    fprintf(stderr, "normMsgr exiting ...\n");
    
}  // end main()

NormMsgr::Message::Message()
 : msg_size(0), msg_index(0), msg_buffer(NULL),
   prev(NULL), next(NULL)
{
}

NormMsgr::Message::Message(char* buffer, unsigned int size)
 : msg_size(size), msg_index(size+MSG_HEADER_SIZE), msg_buffer(buffer),
   prev(NULL), next(NULL)
{
    assert(2 == MSG_HEADER_SIZE);
    uint16_t msgSize = size + MSG_HEADER_SIZE;
    msgSize = htons(msgSize);
    memcpy(msg_header, &msgSize, MSG_HEADER_SIZE);
} 

NormMsgr::Message::~Message()
{
    // in future we may need to use NormDelete()
    // to delete msg_buffer if it came from NORM
    if (NULL != msg_buffer)
    {
        //fprintf(stderr, "deleting msg_buffer ...\n");
        delete[] msg_buffer;
        msg_buffer = NULL;
    }
}

bool NormMsgr::Message::Init(unsigned int size)
{
    if ((NULL != msg_buffer) && (size != msg_size))
    {
        delete[] msg_buffer;
        msg_buffer = NULL;
    }
    if (NULL == msg_buffer) 
        msg_buffer = new char[size];
    if (NULL != msg_buffer)
    {
        assert(2 == MSG_HEADER_SIZE);
        uint16_t msgSize = size + MSG_HEADER_SIZE;
        msgSize = htons(msgSize);
        memcpy(msg_header, &msgSize, MSG_HEADER_SIZE);
        msg_index = MSG_HEADER_SIZE;  // empty message
        msg_size = size;  
        return true;
    }
    else
    {
        msg_index = msg_size = 0;
        return false;
    }
}  // end NormMsgr::Message::Init()

NormMsgr::MessageQueue::MessageQueue()
 : head(NULL), tail(NULL), msg_count(0)
{
}

NormMsgr::MessageQueue::~MessageQueue()
{
}

void NormMsgr::MessageQueue::Destroy()
{
    while (NULL != head)
    {
        Message* msg = RemoveHead();
        delete msg;
    }
}  // end NormMsgr::MessageQueue::Destroy()

void NormMsgr::MessageQueue::Prepend(Message& msg)
{
    msg.prev = NULL;
    if (NULL != head)
        head->prev = &msg;
    else
        tail = &msg;
    msg.next = head;
    head = &msg;
    msg_count++;
}  // end NormMsgr::MessageQueue::Prepend()

void NormMsgr::MessageQueue::Append(Message& msg)
{
    msg.next = NULL;
    if (NULL != tail)
        tail->next = &msg;
    else 
        head = &msg;
    msg.prev = tail;
    tail = &msg;
    msg_count++;
}  // end NormMsgr::MessageQueue::Append()

void NormMsgr::MessageQueue::Remove(Message& msg)
{
    if (NULL == msg.prev)
        head = msg.next;
    else
        msg.prev->next = msg.next;
    if (NULL == msg.next)
        tail = msg.prev;
    else
        msg.next->prev = msg.prev;
    msg.prev = msg.next = NULL;
    msg_count--;
}  // end NormMsgr::MessageQueue::Remove()

NormMsgr::Message* NormMsgr::MessageQueue::RemoveHead()
{
    Message* msg = head;
    if (NULL != head) Remove(*head);
    return msg;
}  // end NormMsgr::MessageQueue::RemoveHead()

NormMsgr::Message* NormMsgr::MessageQueue::RemoveTail()
{
    Message* msg = tail;
    if (NULL != tail) Remove(*tail);
    return msg;
}  // end NormMsgr::MessageQueue::RemoveTail()