arzmq-sys 0.5.4

Low-level bindings to the zeromq library
Documentation
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
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475

// normApp.cpp - Command-line NORM "demo" application

// Note this code does _not_ use the NORM API.  It is kept as a demonstrator
// and test tool and serves as a performance comparison reference for testing
// the threaded NORM API code.  So this should _not_ be used as a NORM
// programming example!!!!

#include "protokit.h"
#include "normSession.h"
#include "normPostProcess.h"

#include <stdio.h>   // for stdout/stderr printouts
#include <stdlib.h>
#include <ctype.h>  // for "isspace()"

#ifdef UNIX
#include <sys/types.h>
#include <sys/wait.h>  // for "waitpid()"
#include <signal.h>
#else
#ifndef _WIN32_WCE
#include <io.h>  // for _mktmp()
#endif // _WIN32-WCE
#endif // if/else UNIX/WIN32

// Command-line application using Protolib EventDispatcher
class NormApp : public NormController, public ProtoApp
{
    public:
        NormApp();
        virtual ~NormApp();
        
        // Overrides from ProtoApp or NsProtoSimAgent base
        bool OnStartup(int argc, const char*const* argv);
        bool ProcessCommands(int argc, const char*const* argv);
        void OnShutdown();
        
        bool OnCommand(const char* cmd, const char* val);
        
        static void DoInputReady(ProtoDispatcher::Descriptor descriptor, 
                                 ProtoDispatcher::Event      theEvent, 
                                 const void*                 userData);
        
    private:
        // we use this class to cache tx file paths
        // and their last tx object id to support the
        // "requeue" option      
        class FileCacheItem  : public ProtoTree::Item
        {
            public:
                FileCacheItem(const char* thePath);
                ~FileCacheItem();
                
                void SetObjectId(NormObjectId objectId)
                    {object_id = objectId;}
                NormObjectId GetObjectId() const
                    {return object_id;}
                    
            private:
                // ProtoTree::Item overrides
                const char* GetKey() const 
                    {return file_path;}
                unsigned int GetKeysize() const
                    {return (PATH_MAX << 3);}
                
                char            file_path[PATH_MAX];
                NormObjectId    object_id;
        };  // end class NormApp::FileCacheItem
        
        class Notification : public ProtoList::Item
        {
            public:
                NormController::Event event;
        };  // end class NormApp::Notification
        class NotificationQueue : public ProtoListTemplate<Notification> {};
        Notification* GetNotification(); // get from pool or create as needed
            
        void ShowHelp();
        void OnInputReady();
        bool AddAckingNodes(const char* ackingNodeList);
            
        enum CmdType {CMD_INVALID, CMD_NOARG, CMD_ARG};
        CmdType CommandType(const char* cmd);
    
        virtual void Notify(NormController::Event event,
                        class NormSessionMgr* sessionMgr,
                        class NormSession*    session,
                        class NormNode*       node,
                        class NormObject*     object);
        
        bool OnIntervalTimeout(ProtoTimer& theTimer);
        void OnControlEvent(ProtoSocket& theSocket, ProtoSocket::Event theEvent);
    
        static const char* const cmd_list[];
        
        enum EcnMode {ECN_OFF, ECN_ON, ECN_ONLY};
        
        enum {MSG_BUFFER_SIZE = 65535};
        
#ifdef UNIX
        static void SignalHandler(int sigNum);
#endif // UNIX
            
        ProtoPipe           control_pipe;  // for remote control
        bool                control_remote;
        NormSessionMgr      session_mgr;
        NormSession*        session;
        NormStreamObject*   tx_stream;
        NormStreamObject*   rx_stream;
        NotificationQueue   notify_queue;
        NotificationQueue   notify_pool;
        bool                notify_reentry;
              
        // application parameters
        FILE*               input;  // input stream
        FILE*               output; // output stream
        char*               output_io_buffer;
        unsigned int        output_io_bufsize;
        char                input_buffer[MSG_BUFFER_SIZE];
        unsigned int        input_index;
        unsigned int        input_length;
        bool                input_active;
        bool                push_mode;
        NormStreamObject::FlushMode msg_flush_mode;
        bool                input_messaging; // stream input mode   
        UINT16              input_msg_length;
        UINT16              input_msg_index;
        bool                msg_test;
        unsigned int        msg_test_length;
        UINT32              msg_test_seq;
        char                output_buffer[MSG_BUFFER_SIZE];
        UINT16              output_index;
        bool                output_messaging;
        UINT16              output_msg_length;
        bool                output_msg_sync;
        bool                precise;
        bool                boost;
        
        // NormSession common parameters 
        char*               address;        // session address
        UINT16              port;           // session port number
        UINT16              tx_port;
        UINT8               ttl;
        bool                loopback;
        char*               interface_name; // for multi-home hosts
        double              tx_rate;        // bits/sec
        double              tx_rate_min;
        double              tx_rate_max;
        bool                cc_enable;
        EcnMode             ecn_mode;
        bool                tolerate_loss;  // for "lost tolerant" congestion control option
        UINT32              node_id;
        
        // NormSession sender-only parameters
        UINT16              segment_size;
        UINT16              ndata;
        UINT16              nparity;
        UINT16              auto_parity;
        UINT16              extra_parity;
        double              backoff_factor;
        double              grtt_estimate; // initial grtt estimate
        NormSession::ProbingMode grtt_probing_mode;
        double              group_size;
        unsigned long       tx_buffer_size; // bytes
        unsigned int        tx_sock_buffer_size;
        unsigned long       tx_cache_min;
        unsigned long       tx_cache_max;
        NormObjectSize      tx_cache_size;        
        
        NormFileList        tx_file_list;
        char                tx_file_name[PATH_MAX];
        bool                tx_file_info;
        ProtoTree           tx_file_cache; 
        bool                tx_one_shot;
        bool                tx_ack_shot;
        bool                tx_file_queued;
        int                 tx_robust_factor;
        
        // save last obj/block/seg id for later in case needed
        NormObjectId        tx_last_object_id;
        NormBlockId         tx_last_block_id;
        NormSegmentId       tx_last_segment_id;
        double              tx_object_interval;
        int                 tx_repeat_count;
        double              tx_repeat_interval;
        bool                tx_repeat_clear;
        int                 tx_requeue;       // master requeue value
        int                 tx_requeue_count; // current requeue counter vale
        ProtoTimer          interval_timer;
        char*               acking_node_list; // comma-delimited string
        bool                acking_flushes;
        bool                watermark_pending;
        
        // NormSession receiver-only parameters
        unsigned long       rx_buffer_size; // bytes
        unsigned int        rx_sock_buffer_size;
        NormFileList        rx_file_cache;
        char*               rx_cache_path;
        NormPostProcessor*  post_processor;
        bool                unicast_nacks;
        bool                silent_receiver;
        bool                low_delay;
        bool                realtime;
        int                 rx_robust_factor;
        bool                rx_persistent;
        bool                process_aborted_files;
        bool                preallocate_sender;
        NormSenderNode::RepairBoundary repair_boundary;
        
        // Debug parameters
        bool                tracing;
        double              tx_loss;
        double              rx_loss;
    
}; // end class NormApp

NormApp::FileCacheItem::FileCacheItem(const char* thePath)
{
    strncpy(file_path, thePath, PATH_MAX);
}

NormApp::FileCacheItem::~FileCacheItem()
{
}

NormApp::NormApp()
 : control_pipe(ProtoPipe::MESSAGE), control_remote(false), 
   session_mgr(GetTimerMgr(), GetSocketNotifier(), &dispatcher),
   session(NULL), tx_stream(NULL), rx_stream(NULL), notify_reentry(false), input(NULL), output(NULL), 
   output_io_buffer(NULL), output_io_bufsize(0),
   input_index(0), input_length(0), input_active(false),
   push_mode(false), msg_flush_mode(NormStreamObject::FLUSH_PASSIVE),
   input_messaging(false), input_msg_length(0), input_msg_index(0),
   msg_test(false), msg_test_length(0), msg_test_seq(0),
   output_index(0), output_messaging(false), output_msg_length(0), output_msg_sync(false),
   precise(false), boost(false),
   address(NULL), port(0), ttl(32), loopback(false), interface_name(NULL),
   tx_rate(64000.0), tx_rate_min(-1.0), tx_rate_max(-1.0), 
   cc_enable(false), ecn_mode(ECN_OFF), tolerate_loss(false),
   node_id(NORM_NODE_ANY), segment_size(1024), ndata(32), nparity(16), auto_parity(0), extra_parity(0),
   backoff_factor(NormSession::DEFAULT_BACKOFF_FACTOR), grtt_estimate(NormSession::DEFAULT_GRTT_ESTIMATE), 
   grtt_probing_mode(NormSession::PROBE_ACTIVE), group_size(NormSession::DEFAULT_GSIZE_ESTIMATE),
   tx_buffer_size(1024*1024), tx_sock_buffer_size(0), tx_cache_min(8), tx_cache_max(256), tx_cache_size((UINT32)20*1024*1024),
   tx_file_info(true), tx_one_shot(false), tx_ack_shot(false), tx_file_queued(false),
   tx_robust_factor(NormSession::DEFAULT_ROBUST_FACTOR), tx_object_interval(0.0), tx_repeat_count(0), 
   tx_repeat_interval(2.0), tx_repeat_clear(true), tx_requeue(0), tx_requeue_count(0), acking_node_list(NULL), 
   acking_flushes(false), watermark_pending(false), rx_buffer_size(1024*1024), rx_sock_buffer_size(0),
   rx_cache_path(NULL), post_processor(NULL), unicast_nacks(false), silent_receiver(false), 
   low_delay(false), realtime(false), rx_robust_factor(NormSession::DEFAULT_ROBUST_FACTOR), rx_persistent(true), process_aborted_files(false),
   preallocate_sender(false), repair_boundary(NormSenderNode::BLOCK_BOUNDARY), tracing(false), tx_loss(0.0), rx_loss(0.0)
{
    control_pipe.SetListener(this, &NormApp::OnControlEvent);
    control_pipe.SetNotifier(&GetSocketNotifier());
    
    // Init tx_timer for 1.0 second interval, infinite repeats
    session_mgr.SetController(this);
    
    interval_timer.SetListener(this, &NormApp::OnIntervalTimeout);
    interval_timer.SetInterval(0.0);
    interval_timer.SetRepeat(0);
    
    tx_file_name[0] = '\0';
    
    struct timeval currentTime;
    ProtoSystemTime(currentTime);
    srand(currentTime.tv_usec);
}

NormApp::~NormApp()
{
    notify_queue.Destroy();
    notify_pool.Destroy();
    if (address) delete[] address;
    if (interface_name) delete[] interface_name;
    
    tx_file_cache.Destroy();
    
    if (rx_cache_path) delete[] rx_cache_path;
    if (post_processor) delete post_processor;
    if (acking_node_list) delete[] acking_node_list;
    if (NULL != output_io_buffer)
    {
        delete[] output_io_buffer;
        output_io_buffer = NULL;
    }
}

NormApp::Notification* NormApp::GetNotification()
{
    Notification* notification = notify_pool.RemoveHead();
    if (NULL == notification)
    {
        if (NULL == (notification = new Notification()))
        {
            PLOG(PL_ERROR, "NormApp::GetNotification() new Notification error: %s\n", GetErrorString());
            return NULL;
        }
    }
    return notification;
}  // end NormApp::GetNotification()

// NOTE on message flushing mode:
//
// "none"    - messages are aggregated to make NORM_DATA
//             payloads a full "segmentSize"
//
// "passive" - At the end-of-message, short NORM_DATA
//             payloads are sent as needed so that
//             the message is immediately sent
//             (i.e. not held for aggregation)
// "active"  - Same as "passive", plus NORM_CMD(FLUSH)
//             messages are generated until new
//             message is written to stream
//             (This helps keep receivers in sync
//              with sender when intermittent message
//              traffic is sent)


const char* const NormApp::cmd_list[] = 
{
    "-help",         // show this help"
    "+debug",        // debug level
    "+log",          // log file name
    "+trace",        // message tracing on
    "+txloss",       // tx packet loss percent (for testing)
    "+rxloss",       // rx packet loss percent (for testing)
    "+address",      // session destination address/port
    "+txport",       // use specific transmission source port number 
    "+ttl",          // multicast hop count scope
    "+loopback",     // "off" or "on" to recv our own packets
    "+interface",    // multicast interface name to use
    "+cc",           // congestion control on/off
    "+ecn",          // explicit congestion notification 'on','off', or 'only'\n"
    "-tolerant",     // enables "loss-tolerant" congestion control operation
    "+rate",         // tx date rate (bps)
    "+limit",        // tx rate limits <rateMin:rateMax>
    "-push",         // push stream writes for real-time messaging
    "+flush",        // message flushing mode ("none", "passive", or "active")
    "+input",        // send stream input
    "+output",       // recv stream output
    "+minput",       // send message stream input
    "+moutput",      // recv message stream output
    "+sendfile",     // file/directory list to transmit
    "+info",         // 'on' | 'off' to enable/disable file info (name) transmission (default = 'on')
    "+interval",     // delay time (sec) between files (0.0 sec default)
    "+repeatcount",  // How many times to repeat the file/directory list tx
    "+rinterval",    // Interval (sec) between file/directory list repeats
    "+requeue",      // <count> how many times files are retransmitted w/ same objId
    "+boundary",     // 'block' or 'file' to set NORM_REPAIR_BOUNDARY (default is 'block')
    "-oneshot",      // Transmit file(s), exiting upon TX_FLUSH_COMPLETED
    "-ackshot",      // Transmit file(s), exiting upon TX_WATERMARK_COMPLETED
    "-updatesOnly",  // only send updated files on repeat transmission
    "+ackingNodes",  // comma-delimited list of node id's to from which to collect acks
    "-ackflush",     // when set, acking completion truncates end-of-transmission flushing
    "+id",           // set the local NormNodeId to a specific value
    "+rxcachedir",   // recv file cache directory
    "+segment",      // payload segment size (bytes)
    "+block",        // User data packets per FEC coding block (blockSize)
    "+parity",       // FEC packets calculated per coding block (nparity)
    "+auto",         // Number of FEC packets to proactively send (<= nparity)
    "+extra",        // Number of extra FEC packets sent in response to repair requests
    "+backoff",      // Backoff factor to use
    "+grtt",         // Set sender's initial GRTT estimate
    "+probe",        // {'active', 'passive' | 'none'} Set sender;s GRTT probing mode 'active' is default)
    "+gsize",        // Set sender's group size estimate
    "+txbuffer",     // Size of sender's buffer
    "+txsockbuffer", // tx socket buffer size
    "+txcachebounds",// <countMin:countMax:sizeMax> limits on sender tx object caching
    "+txrobustfactor", // integer tx robust factor
    "+rxbuffer",     // Size receiver allocates for buffering each sender
    "+rxsockbuffer", // Optional recv socket buffer size.
    "-unicastNacks", // unicast instead of multicast feedback messages
    "-silentReceiver", // "silent" (non-nacking) receiver (EMCON mode) (must set for sender too)
    "-presetSender",   // causes receiver to preallocate resources for remote sender w/ segmentSize, block, and parity params
    "-lowDelay",     // for silent receivers only, delivers data/files to app sooner\n"
    "-realtime",     // for NACKing (non-silent) receivers, flips buffer mgmnt to favor low latency over reliability
    "+rxpersist",    // "off" or "on" to make receiver keep state on sender forever ("on" by default)
    "-saveAborts",   // save (and possibly post-process) aborted receive files\n"
    "+processor",    // receive file post processing command
    "+instance",     // specify norm instance name for remote control commands
    "-precise",      // run the NormApp ProtoDispatcher in "precise timing" mode
    "-boost",        // run the NormApp with "boosted" process priority (super user only)
    "+mtest",        // <size> send test messages via NORM_OBJECT_STREAM
    "+stest",        // <size> send test stream of bytes via NORM_OBJECT_STREAM at 
    "+obuf",         // <size> non-standard buffer size for "output" or "moutput" FILE* (incl. stdout)
    NULL          
};

// Thanks to Marinho Barcellos taking initiative with this help function
void NormApp::ShowHelp() 
{
      // TBD: this should be taken automatically from the cmd array
    fprintf(stderr, 
        "List of commands taken by \"norm\" (+ indicates that an argument is required):\n"
        "   -help,         // show this help\n"
        "   +debug,        // debug level\n"
        "   +log,          // log file name\n"
        "   +trace,        // message tracing on\n"
        "   +txloss,       // tx packet loss percent (for testing)\n"
        "   +rxloss,       // rx packet loss percent (for testing)\n"
        "   +address,      // session destination address\n"
        "   +txport,       // use a specific transmission source port number\n"
        "   +ttl,          // multicast hop count scope\n"
        "   +loopback,     // 'on' or 'off' to recv our own packets (default = 'off')\n"
        "   +interface,    // multicast interface name to use\n"
        "   +cc,           // congestion control 'on' or 'off'\n"
        "   +ecn,          // explicit congestion notification 'on' or 'off'\n"
        "   +rate,         // tx date rate (bps)\n"
        "   +limit         // tx rate bounds <rateMin:rateMax>\n"
        "   -push,         // push stream writes for real-time messaging\n"
        "   +flush,        // message flushing mode ('none', 'passive', or 'active')\n"
        "   +input,        // send stream input\n"
        "   +output,       // recv stream output\n"
        "   +minput,       // sender message stream input\n"
        "   +moutput,      // receiver message stream output\n"
        "   +sendfile,     // file/directory list to transmit\n"
        "   +info,         // 'on' | 'off' to enable/disable file info (name) transmission (default = 'on')\n"
        "   +interval,     // delay time (sec) between files (0.0 sec default)\n"
        "   +repeatcount,  // How many times to repeat the file/directory list tx\n"
        "   +rinterval,    // Interval (sec) between file/directory list repeats\n"
        "   +requeue,      // <count> how many times files are retransmitted w/ same objId\n"
        "   +boundary      // 'block' or 'file' to set NORM_REPAIR_BOUNDARY (default is 'block')\n"
        "   -oneshot,      // Exit upon sender TX_FLUSH_COMPLETED event (sender exits after transmission)\n"
        "   -ackshot,      // Exit upon sender TX_WATERMARK_COMPLETED event (sender exits after transmission)\n"
        "   -updatesOnly,  // only send updated files on repeat transmission\n"
        "   +ackingNodes,  // comma-delimited list of node id's to from which to collect acks\n"
        "   -ackflush,     // when set, acking completion truncates flushing\n"
        "   +id,           // set the local NormNodeId to a specific value\n"
        "   +rxcachedir,   // recv file cache directory\n"
        "   +segment,      // payload segment size (bytes)\n"
        "   +block,        // User data packets per FEC coding block (blockSize)\n"
        "   +parity,       // FEC packets calculated per coding block (nparity)\n"
        "   +auto,         // Number of FEC packets to proactively send (<= nparity)\n"
        "   +extra,        // Number of extra FEC packets sent in response to repair requests\n"
        "   +backoff,      // Backoff factor to use\n"
        "   +grtt,         // Set sender's initial GRTT estimate\n"
        "   +probe,        // {'active', 'passive' | 'none'} Set sender;s GRTT probing mode 'active' is default)\n"
        "   +gsize,        // Set sender's group size estimate\n"
        "   +txbuffer,     // Size of sender's buffer\n"
        "   +txcachebounds,// <countMin:countMax:sizeMax> limits on sender tx object caching\n"
        "   +rxbuffer,     // Size receiver allocates for buffering each sender\n"
        "   +rxsockbuffer, // Optional recv socket buffer size.\n"
        "   -unicastNacks, // unicast instead of multicast feedback messages\n"
        "   -silentReceiver, // silent (non-nacking) receiver (EMCON mode) (must set for sender too)\n"
        "   -lowDelay,     // for silent receivers only, delivers data/files to app sooner\n"
        "   -presetSender, // causes receiver to preallocate resources for remote sender w/ segmentSize, block, and parity params\n"
        "   -realtime,     // for NACKing (non-silent) receivers, flips buffer mgmnt to favor low latency over reliability\n"
        "   +rxpersist,    // 'off' or 'on' to make receiver keep state on sender forever ('on' by default)\n"
        "   -saveAborts,   // save (and possibly post-process) aborted receive files\n"
        "   +processor,    // receive file post processing command\n"
        "   +instance,     // specify norm instance name for remote control commands\n"
        "   -precise,      // run the NormApp ProtoDispatcher in 'precise timing' mode\n"
        "   -boost,        // run the NormApp with 'boosted' process priority (super user only)\n"
        "   +mtest,        // <size> send test messages via NORM_OBJECT_STREAM\n"
        "   +stest,        // <size> send test stream of bytes via NORM_OBJECT_STREAM\n" 
        "\n");
}  // end NormApp::ShowHelp() 


void NormApp::OnControlEvent(ProtoSocket& /*theSocket*/, ProtoSocket::Event theEvent)
{
    switch (theEvent)
    {
        case ProtoSocket::RECV:
        {
            char buffer[8192];
            unsigned int len = 8192;
            if (control_pipe.Recv(buffer, len))
            {
                buffer[len] = '\0';
                PLOG(PL_DEBUG, "norm: received command \"%s\"\n", buffer);
                char* cmd = buffer;
                char* val = NULL;
                for (unsigned int i = 0; i < len; i++)
                {
                    if (isspace(buffer[i]))
                    {
                        buffer[i++] = '\0';   
                        val = buffer + i;
                        break;
                    }
                }
                if (!OnCommand(cmd, val))
                    PLOG(PL_ERROR, "NormApp::OnControlEvent() bad command received\n");  
            }
            else
            {
                PLOG(PL_ERROR, "NormApp::OnControlEvent() error receiving remote command\n");   
            }
            break;
        }
            
        default: 
            PLOG(PL_ERROR, "NormApp::OnControlEvent() unhandled event type: %d\n", theEvent);
            break; 
    }
}  // end NormApp::OnControlEvent()
    
bool NormApp::OnCommand(const char* cmd, const char* val)
{
    CmdType type = CommandType(cmd);
    ASSERT(CMD_INVALID != type);
    size_t len = strlen(cmd);
    if ((CMD_ARG == type) && !val)
    {
        PLOG(PL_FATAL, "NormApp::OnCommand(%s) missing argument\n", cmd);
        return false;        
    }
    
    if (!strncmp("help", cmd, len))
    {   
        ShowHelp();
        return false;
    }
    
    if (control_remote)
    {
        // Since the "instance" already exists, we send the command
        // to the pre-existing "instance" instead of processing it ourself
        char buffer[8192];
        buffer[0] = '\0';
        if (cmd) strncpy(buffer, cmd, 8191);
        size_t len = strlen(buffer);
        len = (len > 8191) ? 8191 : len;
        if (val)
        {
            buffer[len++] = ' ';
            strncat(buffer, val, 8192 - strlen(buffer));
        }
        len = strlen(buffer);
        len = (len > 8191) ? 8191 : len;
        buffer[len++] = '\0';
        unsigned int numBytes = (unsigned int)len;
        if (!control_pipe.Send(buffer, numBytes))
		{
            PLOG(PL_FATAL, "NormApp::OnCommand() error sending command to remote instance\n");
            return false;
        }     
        else
        {
            PLOG(PL_FATAL, "NormApp::OnCommand() sent \"%s\" to remote\n", buffer);
        }   
    }
    
    if (!strncmp("debug", cmd, len))
    {
        
        int debugLevel = atoi(val);
        if ((debugLevel < 0) || (debugLevel > 12))
        {
            PLOG(PL_FATAL, "NormApp::OnCommand(segment) invalid debug level!\n");   
            return false;
        }
        SetDebugLevel(debugLevel);
    }
    else if (!strncmp("log", cmd, len))
    {
        OpenDebugLog(val);
    }
    else if (!strncmp("trace", cmd, len))
    {
        if (!strcmp("on", val))
            tracing = true;
        else if (!strcmp("off", val))
            tracing = false;
        else
        {
            PLOG(PL_FATAL, "NormApp::OnCommand(trace) invalid argument!\n");   
            return false;
        }
        if (session) session->SetTrace(tracing);
    }
    else if (!strncmp("precise", cmd, len))
    {
        precise = true;  // NormApp::dispatcher will run in "precision" mode
    }
    else if (!strncmp("boost", cmd, len))
    {
        boost = true;  // normApp will run w/ high process priority
    }
    else if (!strncmp("txloss", cmd, len))
    {
        double txLoss = atof(val);
        if (txLoss < 0)
        {
            PLOG(PL_FATAL, "NormApp::OnCommand(txloss) invalid txRate!\n");   
            return false;
        }
        tx_loss = txLoss;
        if (session) session->SetTxLoss(txLoss);
    }
    else if (!strncmp("rxloss", cmd, len))
    {
        double rxLoss = atof(val);
        if (rxLoss < 0)
        {
            PLOG(PL_FATAL, "NormApp::OnCommand(rxloss) invalid txRate!\n");   
            return false;
        }
        rx_loss = rxLoss;
        if (session) session->SetRxLoss(rxLoss);
    }
    else if (!strncmp("address", cmd, len))
    {
        size_t len = strlen(val);
        if (address) delete[] address;
        if (!(address = new char[len+1]))
        {
            PLOG(PL_FATAL, "NormApp::OnCommand(address) allocation error:%s\n",
                GetErrorString()); 
            return false;
        }
        strcpy(address, val);
        char* ptr = strchr(address, '/');
        if (!ptr)
        {
            delete[] address;
            address = NULL;
            PLOG(PL_FATAL, "NormApp::OnCommand(address) missing port number!\n");   
            return false;
        }
        *ptr++ = '\0';
        int portNum = atoi(ptr);
        if ((portNum < 1) || (portNum > 65535))
        {
            delete[] address;
            address = NULL;
            PLOG(PL_FATAL, "NormApp::OnCommand(address) invalid port number!\n");   
            return false;
        }
        port = portNum;
    }
    else if (!strncmp("txport", cmd, len))
    {
        int txPort = atoi(val);
        if ((txPort < 0) || (txPort > 65535))
        {
            PLOG(PL_FATAL, "NormApp::OnCommand(txport) invalid port number!\n");   
            return false;
        }
        tx_port = (UINT16)txPort;
        if (session) session->SetTxPort(txPort);
    }
    else if (!strncmp("ttl", cmd, len))
    {
        int ttlTemp = atoi(val);
        if ((ttlTemp < 0) || (ttlTemp > 255))
        {
            PLOG(PL_FATAL, "NormApp::OnCommand(ttl) invalid value!\n");   
            return false;
        }
        bool result = session ? session->SetTTL((UINT8)ttlTemp) : true;
        ttl = result ? ttlTemp : ttl;
        if (!result)
        {
            PLOG(PL_FATAL, "NormApp::OnCommand(ttl) error setting socket ttl!\n");   
            return false;
        }
    }
    else if (!strncmp("loopback", cmd, len))
    {
        bool loopTemp = loopback;
        if (!strcmp("on", val))
            loopTemp = true;
        else if (!strcmp("off", val))
            loopTemp = false;
        else
        {
            PLOG(PL_FATAL, "NormApp::OnCommand(loopback) invalid argument!\n");   
            return false;
        }
        bool result = session ? session->SetLoopback(loopTemp) : true;
        loopback = result ? loopTemp : loopback;
        if (loopback && (NULL != session) && session->Address().IsMulticast())
            session->SetRxPortReuse(true);
        if (!result)
        {
            PLOG(PL_FATAL, "NormApp::OnCommand(loopback) error setting socket loopback!\n");   
            return false;
        }
    }
    else if (!strncmp("interface", cmd, len))
    {
        if (interface_name) delete[] interface_name;
        if (!(interface_name = new char[strlen(val)+1]))
        {
            PLOG(PL_FATAL, "NormApp::OnCommand(interface) error allocating string: %s\n",
                GetErrorString());
            return false;
        }
        strcpy(interface_name, val);
    }
    else if (!strncmp("rate", cmd, len))
    {
        double txRate = atof(val);
        if (txRate < 0)
        {
            PLOG(PL_FATAL, "NormApp::OnCommand(rate) invalid txRate!\n");   
            return false;
        }
        tx_rate = txRate;
        if (session) session->SetTxRate(txRate);
    }
    else if (!strncmp("limit", cmd, len))
    {
        double rateMin, rateMax;
        if (2 != sscanf(val, "%lf:%lf", &rateMin, &rateMax))
        {
            PLOG(PL_FATAL, "NormApp::OnCommand(limit) invalid txRate limits!\n");
            return false;
        }
        tx_rate_min = rateMin;
        tx_rate_max = rateMax;
        if (session) session->SetTxRateBounds(rateMin, rateMax);
    }
    else if (!strncmp("cc", cmd, len))
    {
        if (!strcmp("on", val))
        {
            cc_enable = true;
        }
        else if (!strcmp("off", val))
        {
            cc_enable = false;   
        }
        else
        {
            PLOG(PL_FATAL, "NormApp::OnCommand(cc) invalid option!\n");   
            return false;
        }
        if (session) session->SetCongestionControl(cc_enable);
    }
    else if (!strncmp("ecn", cmd, len))
    {
        // (TBD) change this to "off" "on" and "only"
        if (!strcmp("on", val))
        {
            ecn_mode = ECN_ON;
            if (session) session->SetEcnSupport(true, false, tolerate_loss);
        }
        else if (!strcmp("off", val))
        {
            ecn_mode = ECN_OFF;   
            if (session) session->SetEcnSupport(false, false, tolerate_loss);
        }
        else if (!strcmp("only", val))
        {
            ecn_mode = ECN_ONLY;   
            if (session) session->SetEcnSupport(true, true, tolerate_loss);
        }
        else
        {
            PLOG(PL_FATAL, "NormApp::OnCommand(ecn) invalid option!\n");   
            return false;
        }
    }
    else if (!strncmp("tolerant", cmd, len))
    {
        tolerate_loss = true;
        if (session) session->SetEcnSupport(ECN_OFF != ecn_mode, ECN_ONLY == ecn_mode, true);   
    }
    else if (!strncmp("input", cmd, len))
    {
        if (!strcmp(val, "STDIN"))
	    {
            input = stdin;
	    }
        else if (!(input = fopen(val, "rb")))
        {
            PLOG(PL_FATAL, "NormApp::OnCommand(input) fopen() error: %s\n",
                    GetErrorString());
            return false;   
        }
        input_index = input_length = 0;
        input_messaging = false;
#ifdef UNIX
        // Set input non-blocking
        if(-1 == fcntl(fileno(input), F_SETFL, fcntl(fileno(input), F_GETFL, 0) | O_NONBLOCK))
            perror("NormApp::OnCommand(input) fcntl(F_SETFL(O_NONBLOCK)) error");
#endif // UNIX
    }
    else if (!strncmp("output", cmd, len))
    {
        if (!strcmp(val, "STDOUT"))
	    {
            output = stdout;
	    }
        else if (!(output = fopen(val, "wb")))
        {
            PLOG(PL_FATAL, "NormApp::OnCommand(output) fopen() error: %s\n",
                    GetErrorString());
            return false;   
        }
        output_messaging = false;
        output_index = 0;
        output_msg_sync = true;
    }
    else if (!strncmp("stest", cmd, len))
    {
        msg_test = true;
        msg_test_length = atoi(val);
        input_messaging = false;
    }
    else if (!strncmp("minput", cmd, len))
    {
        if (!strcmp(val, "STDIN"))
	    {
            input = stdin;
	    }
        else if (!(input = fopen(val, "rb")))
        {
            PLOG(PL_FATAL, "NormApp::OnCommand(input) fopen() error: %s\n",
                    GetErrorString());
            return false;   
        }
        input_index = input_length = 0;
        input_messaging = true;
#ifdef UNIX
        // Set input non-blocking
        if(-1 == fcntl(fileno(input), F_SETFL, fcntl(fileno(input), F_GETFL, 0) | O_NONBLOCK))
            perror("NormApp::OnCommand(input) fcntl(F_SETFL(O_NONBLOCK)) error");
#endif // UNIX
    }
    else if (!strncmp("moutput", cmd, len))
    {
        if (!strcmp(val, "STDOUT"))
	    {
            output = stdout;
	    }
        else if (!(output = fopen(val, "wb")))
        {
            PLOG(PL_FATAL, "NormApp::OnCommand(output) fopen() error: %s\n",
                    GetErrorString());
            return false;   
        }
        output_messaging = true;
        output_msg_length = output_index = 0;
        output_msg_sync = false;
    }
    else if (!strncmp("mtest", cmd, len))
    {
        msg_test = true;
        msg_test_length = atoi(val);
        input_messaging = true;
    }
    else if (!strncmp("obuf", cmd, len))
    {
        if (NULL != output_io_buffer) delete[] output_io_buffer;
        output_io_bufsize = atoi(val);
        if (NULL == (output_io_buffer = new char[output_io_bufsize]))
        {
            PLOG(PL_FATAL, "NormApp::OnCommand(obuf): error allocating buffer: %s\n",
                    GetErrorString());
            return false;   
        }
    }
    else if (!strncmp("sendfile", cmd, len))
    {
        if (!tx_file_list.Append(val))
        {
            PLOG(PL_FATAL, "NormApp::OnCommand(sendfile) Error appending \"%s\" "
                    "to tx file list.\n", val);
            return false;   
        }
        // TBD - add code here to see if "session" exists (i.e. already running)
        // and, if so, StartSender operation, if needed, and kick-off "OnIntervalTimeout()"
        // process if needed to kick-start file transmission.  To do this, we probably
        // need to create a NormApp::StartSender() method that can be used here as well
        // as for initial command-line sender startup, if applicable.
        // (This will allow the "send" command to be invoked at run-time via the ProtoPipe
        //  remote control interface).
    }
    else if (!strncmp("info", cmd, len))
    {
        if (!strcmp("on", val))
            tx_file_info = true;
        else if (!strcmp("off", val))
            tx_file_info = false;
        else
        {
            PLOG(PL_FATAL, "NormApp::OnCommand(info) invalid argument!\n");   
            return false;
        }
    }
    else if (!strncmp("interval", cmd, len))
    {
        if (1 != sscanf(val, "%lf", &tx_object_interval))
        {
            PLOG(PL_FATAL, "NormApp::OnCommand(interval) Invalid tx object interval: %s\n", val);
            return false;
        } 
    }    
    else if (!strncmp("repeatcount", cmd, len))
    {
        tx_repeat_count = atoi(val);  
    }
    else if (!strncmp("rinterval", cmd, len))
    {
        if (1 != sscanf(val, "%lf", &tx_repeat_interval)) 
            tx_repeat_interval = -1.0;
        if (tx_repeat_interval < 0.0)
        {
            PLOG(PL_FATAL, "NormApp::OnCommand(rinterval) Invalid tx repeat interval: %s\n",
                     val);
            tx_repeat_interval = 0.0;
            return false;
        }
    }       
    else if (!strncmp("requeue", cmd, len))
    {
        tx_requeue = tx_requeue_count = atoi(val); 
    } 
    else if (!strncmp("boundary", cmd, len))
    {
        if (0 == strcmp("block", val))
        {
            repair_boundary = NormSenderNode::BLOCK_BOUNDARY;
        }
        else if (0 == strcmp("file", val))
        {
            repair_boundary = NormSenderNode::OBJECT_BOUNDARY;
        }
        else
        {
            PLOG(PL_FATAL, "NormApp::OnCommand(boundary) error: invalid repair boundary \"%s\"\n", val);
            return false;
        }
        if (session) session->ReceiverSetDefaultRepairBoundary(repair_boundary);
    }    
    else if (!strncmp("oneshot", cmd, len))
    {
        tx_one_shot = true;
    }    
    else if (!strncmp("ackshot", cmd, len))
    {
        tx_ack_shot = true;
    }      
    else if (!strncmp("updatesOnly", cmd, len))
    {
        tx_file_list.InitUpdateTime(true);
    }      
    else if (!strncmp("ackingNodes", cmd, len))
    {
        size_t length = strlen(val);
        if (NULL != acking_node_list)
            length += strlen(acking_node_list) + 1;
        char* tempString = new char[length + 1];
        if (NULL == tempString)
        {
            PLOG(PL_FATAL, "NormApp::OnCommand(ackingNodes) error: %s\n", GetErrorString());
            return false;
        }
        if (NULL != acking_node_list)
        {
            strcpy(tempString, acking_node_list);
            strcat(tempString, ",");
            delete[] acking_node_list;
        }
        else
        {
            tempString[0] = '\0';
        }
        acking_node_list = tempString;
        strcat(acking_node_list, val);
        if (NULL != session) return AddAckingNodes(acking_node_list);
    }    
    else if (!strncmp("ackflush", cmd, len))
    {
        acking_flushes = true;
    }
    else if (!strncmp("id", cmd, len))
    {
        node_id = atoi(val);
    }
    else if (!strncmp("rxcachedir", cmd, len))
    {
        size_t length = strlen(val);   
        // Make sure there is a trailing PROTO_PATH_DELIMITER
        if (PROTO_PATH_DELIMITER != val[length-1]) 
            length += 2;
        else
            length += 1;
        if (!(rx_cache_path = new char[length]))
        {
             PLOG(PL_FATAL, "NormApp::OnCommand(rxcachedir) alloc error: %s\n",
                    GetErrorString());
            return false;  
        }
        strcpy(rx_cache_path, val);
        rx_cache_path[length-2] = PROTO_PATH_DELIMITER;
        rx_cache_path[length-1] = '\0';
    }
    else if (!strncmp("segment", cmd, len))
    {
        int segmentSize = atoi(val);
        if ((segmentSize < 0) || (segmentSize > 65535))
        {
            PLOG(PL_FATAL, "NormApp::OnCommand(segment) invalid segment size!\n");   
            return false;
        }
        segment_size = segmentSize;
    }
    else if (!strncmp("block", cmd, len))
    {
        int blockSize = atoi(val);
        if ((blockSize < 1) || (blockSize > 65535))
        {
            PLOG(PL_FATAL, "NormApp::OnCommand(block) invalid block size!\n");   
            return false;
        }
        ndata = blockSize;
    }
    else if (!strncmp("parity", cmd, len))
    {
        int numParity = atoi(val);
        if ((numParity < 0) || (numParity > 65534))
        {
            PLOG(PL_FATAL, "NormApp::OnCommand(parity) invalid value!\n");   
            return false;
        }
        nparity = numParity;
    }
    else if (!strncmp("auto", cmd, len))
    {
        int autoParity = atoi(val);
        if ((autoParity < 0) || (autoParity > 65534))
        {
            PLOG(PL_FATAL, "NormApp::OnCommand(auto) invalid value!\n");   
            return false;
        }
        auto_parity = autoParity;
        if (session) session->SenderSetAutoParity(autoParity);
    }
    else if (!strncmp("extra", cmd, len))
    {
        int extraParity = atoi(val);
        if ((extraParity < 0) || (extraParity > 65534))
        {
            PLOG(PL_FATAL, "NormApp::OnCommand(extra) invalid value!\n");   
            return false;
        }
        extra_parity = extraParity;
        if (session) session->SenderSetExtraParity(extraParity);
    }
    else if (!strncmp("backoff", cmd, len))
    {
        double backoffFactor = atof(val);
        if (backoffFactor < 0)
        {
            PLOG(PL_FATAL, "NormApp::OnCommand(backoff) invalid value!\n");   
            return false;
        }
        backoff_factor = backoffFactor;
        if (session) session->SetBackoffFactor(backoffFactor);
    }
    else if (!strncmp("grtt", cmd, len))
    {
        double grttEstimate = atof(val);
        if (grttEstimate < 0)
        {
            PLOG(PL_FATAL, "NormApp::OnCommand(grtt) invalid value!\n");   
            return false;
        }
        grtt_estimate = grttEstimate;
        if (session) session->SenderSetGrtt(grttEstimate);
    }
    else if (!strncmp("probe", cmd, len))
    {
        size_t valLen = strlen(val);
        if (!strncmp("none", val, valLen))
           grtt_probing_mode = NormSession::PROBE_NONE;
        else if (!strncmp("passive", val, valLen))
            grtt_probing_mode = NormSession::PROBE_PASSIVE;
        else if (!strncmp("active", val, valLen))
            grtt_probing_mode = NormSession::PROBE_ACTIVE;
        else
        {
            PLOG(PL_FATAL, "NormApp::OnCommand(probe) invalid grtt probing mode!\n");   
            return false;
        }
        if (NULL != session) session->SetGrttProbingMode(grtt_probing_mode);
    }
    else if (!strncmp("gsize", cmd, len))
    {
        double groupSize = atof(val);
        if (groupSize < 0)
        {
            PLOG(PL_FATAL, "NormApp::OnCommand(gsize) invalid value!\n");   
            return false;
        }
        group_size = groupSize;
        if (session) session->SenderSetGroupSize(groupSize);
    }
    else if (!strncmp("txbuffer", cmd, len))
    {
        if (1 != sscanf(val, "%lu", &tx_buffer_size))
        {
            PLOG(PL_FATAL, "NormApp::OnCommand(txbuffer) invalid value!\n");   
            return false;
        }
    }
    else if (!strncmp("txcachebounds", cmd, len))
    {
        unsigned long countMin, countMax, sizeMax;
        if (3 != sscanf(val, "%lu:%lu:%lu\n", &countMin, &countMax, &sizeMax))
        {
            PLOG(PL_FATAL, "NormApp::OnCommand(txcachebounds) invalid value!\n");   
            return false;
        }
        tx_cache_min = countMin;
        tx_cache_max = countMax;
        tx_cache_size = sizeMax;
        if (session) 
        {
            session->SetTxCacheBounds(tx_cache_size, tx_cache_min, tx_cache_max);
            session->SetRxCacheMax(tx_cache_max);
        }
    }
    else if (!strncmp("txrobustfactor", cmd, len))
    {
        tx_robust_factor = atoi(val);
        if (session) session->SetTxRobustFactor(tx_robust_factor);
    }
    else if (!strncmp("rxbuffer", cmd, len))
    {
        if (1 != sscanf(val, "%lu", &rx_buffer_size))
        {
            PLOG(PL_FATAL, "NormApp::OnCommand(rxbuffer) invalid value!\n");   
            return false;
        }
    }
    else if (!strncmp("rxsockbuffer", cmd, len))
    {
        if (1 != sscanf(val, "%u", &rx_sock_buffer_size))
        {
            PLOG(PL_FATAL, "NormApp::OnCommand(rxsockbuffer) invalid value!\n");   
            return false;
        }
        if (session && (rx_sock_buffer_size > 0)) 
            session->SetRxSocketBuffer(rx_sock_buffer_size);
    }
    else if (!strncmp("txsockbuffer", cmd, len))
    {
	    if (1 != sscanf(val, "%u", &tx_sock_buffer_size))
	    {
		    PLOG(PL_FATAL, "NormApp::OnCommand(txsockbuffer) invalid value!\n");
		    return false;
	    }
	    if (session && (tx_sock_buffer_size))
	    	session->SetTxSocketBuffer(tx_sock_buffer_size);
    }
    else if (!strncmp("unicastNacks", cmd, len))
    {
        unicast_nacks = true;
        if (session) session->ReceiverSetUnicastNacks(true);
    }
    else if (!strncmp("silentReceiver", cmd, len))
    {
        silent_receiver = true;
        if (session) 
        {
            session->ReceiverSetSilent(true);
            session->SndrSetEmcon(true);
        }
    }
    else if (!strncmp("presetSender", cmd, len))
    {
        preallocate_sender = true;
    }
    else if (!strncmp("lowDelay", cmd, len))
    {
        low_delay = true;
        if (session) session->RcvrSetMaxDelay(1);
    }
    else if (!strncmp("realtime", cmd, len))
    {
        realtime = true;
        if (session) session->RcvrSetRealtime(true);
    }
    else if (!strncmp("rxrobustfactor", cmd, len))
    {
        rx_robust_factor = atoi(val);
        if (session) session->SetRxRobustFactor(rx_robust_factor);
    }
    else if (!strncmp("rxpersist", cmd, len))
    {
        if (!strcmp("on", val))
            rx_persistent = true;
        else if (!strcmp("off", val))
            rx_persistent = false;
        else
        {
            PLOG(PL_FATAL, "NormApp::OnCommand(rxpersist) invalid argument!\n");   
            return false;
        }
    }
    else if (!strncmp("saveAborts", cmd, len))
    {
        process_aborted_files = true;
    }
    else if (!strncmp("push", cmd, len))
    {
        push_mode = true;
        if (tx_stream) tx_stream->SetPushMode(push_mode);
    }
    else if (!strncmp("flush", cmd, len))
    {
        size_t valLen = strlen(val);
        if (!strncmp("none", val, valLen))
            msg_flush_mode = NormStreamObject::FLUSH_NONE;
        else if (!strncmp("passive", val, valLen))
            msg_flush_mode = NormStreamObject::FLUSH_PASSIVE;
        else if (!strncmp("active", val, valLen))
            msg_flush_mode = NormStreamObject::FLUSH_ACTIVE;
        else
        {
            PLOG(PL_FATAL, "NormApp::OnCommand(flush) invalid msg flush mode!\n");   
            return false;
        }
        if (tx_stream) tx_stream->SetFlushMode(msg_flush_mode);
    }
    else if (!strncmp("processor", cmd, len))
    {
        if (!post_processor->SetCommand(val))
        {
            PLOG(PL_FATAL, "NormApp::OnCommand(processor) error!\n");   
            return false;
        }
    }
    else if (!strncmp("instance", cmd, len))
    {
        // First, try to connect to see if instance is already running
        if (control_pipe.Connect(val))
        {
            control_remote = true;
        }
        else if (control_pipe.Listen(val))
        {
            control_remote = false;
        }
        else
        {
            PLOG(PL_FATAL, "NormApp::OnCommand(instance) error opening control pipe\n");
            return false;   
        }
    }
    return true;
}  // end NormApp::OnCommand()


NormApp::CmdType NormApp::CommandType(const char* cmd)
{
    if (!cmd) return CMD_INVALID;
    size_t len = strlen(cmd);
    bool matched = false;
    CmdType type = CMD_INVALID;
    const char* const* nextCmd = cmd_list;
    while (*nextCmd)
    {
        if (!strncmp(cmd, *nextCmd+1, len))
        {
            if (matched)
            {
                // ambiguous command (command should match only once)
                return CMD_INVALID;
            }
            else
            {
                matched = true;   
                if ('+' == *nextCmd[0])
                    type = CMD_ARG;
                else
                    type = CMD_NOARG;
            }
        }
        nextCmd++;
    }
    return type;
}  // end NormApp::CommandType()


bool NormApp::ProcessCommands(int argc, const char*const* argv)
{
    int i = 1;
    while ( i < argc)
    {
        CmdType cmdType = CommandType(argv[i]);   
        switch (cmdType)
        {
            case CMD_INVALID:
                PLOG(PL_FATAL, "NormApp::ProcessCommands() Invalid command:%s\n", 
                        argv[i]);
                return false;
            case CMD_NOARG:
                if (!OnCommand(argv[i], NULL))
                {
                    PLOG(PL_FATAL, "NormApp::ProcessCommands() OnCommand(%s) error\n", 
                            argv[i]);
                    return false;
                }
                i++;
                break;
            case CMD_ARG:
                if (!OnCommand(argv[i], argv[i+1]))
                {
                    PLOG(PL_FATAL, "NormApp::ProcessCommands() OnCommand(%s, %s) error\n", 
                            argv[i], argv[i+1]);
                    return false;
                }
                i += 2;
                break;
        }
    }
    return true;
}  // end NormApp::ProcessCommands()

bool NormApp::AddAckingNodes(const char* ackingNodeList)
{
    if (NULL == ackingNodeList) return true;
    if (NULL != session)
    {
        const char* ptr = ackingNodeList;
        while ((NULL != ptr) && ('\0' != *ptr))
        {
            const char* end = strchr(ptr, ',');
            size_t len = (NULL != end) ? (end - ptr) : strlen(ptr);
            if (len >= 256)
            {
                PLOG(PL_FATAL, "NormApp::AddAckingNodes() error: bad acking node list\n");
                return false;
            }
            char nodeString[256];
            strncpy(nodeString, ptr, len);
            nodeString[len] = '\0';
            bool isNumber = true;
            for (size_t i = 0; i < len; i++)
            {
                if (0 == isdigit(nodeString[i]))
                {
                    isNumber = false;
                    break;
                }
            }
            unsigned long nodeId;
            if (isNumber)
            {
                if (1 != sscanf(nodeString, "%lu", &nodeId))
                {
                    PLOG(PL_FATAL, "NormApp::AddAckingNodes() error: bad acking node id: %s\n", nodeString);
                    return false;
                }
            }
            else
            {
                // It's an address or host name
                ProtoAddress nodeAddr;
                if (!nodeAddr.ResolveFromString(nodeString))
                {
                    PLOG(PL_FATAL, "NormApp::AddAckingNodes() error: bad acking node id: %s\n", nodeString);
                    return false;
                }
                nodeId = nodeAddr.EndIdentifier();
            }
            if (!session->SenderAddAckingNode((UINT32)nodeId))
            {
                PLOG(PL_FATAL, "NormApp::AddAckingNodes() error: couldn't add acking node \n");
                return false;
            }
            ptr = (NULL != end) ? ++end : NULL;
        }
        return true;
    }
    else
    {
        PLOG(PL_FATAL, "NormApp::AddAckingNodes() error: no session instantiated\n");
        return false;
    }
}  // end NormApp::AddAckingNodes()


void NormApp::DoInputReady(ProtoDispatcher::Descriptor /*descriptor*/, 
                           ProtoDispatcher::Event      /*theEvent*/, 
                           const void*                 userData)
{
    ((NormApp*)userData)->OnInputReady();
}  // end NormApp::DoDataReady()


void NormApp::OnInputReady()
{
    
    //DMSG(0, "NormApp::OnInputReady() ...\n");
    bool endOfStream = false;
    // write to the stream while input is available _and_
    // the stream has buffer space for input
    while (input || msg_test)
    {
        bool inputStarved = false;
            
        if (0 == input_length)
        {
            // We need input ...
            UINT16 readLength;
            if (input_messaging)
            {
                if (input_msg_length)
                {
                    UINT16 bufferSpace = MSG_BUFFER_SIZE - input_index;
                    UINT16 msgRemainder = input_msg_length - input_msg_index;
                    readLength = MIN(bufferSpace, msgRemainder);
                }
                else if (input_index < 2)
                {
                    readLength = 2 - input_index;   
                }
                else
                {
                    memcpy(&input_msg_length, input_buffer, 2);
                    input_msg_length = ntohs(input_msg_length);
                    ASSERT(input_msg_length >= 2);
                    UINT16 bufferSpace = MSG_BUFFER_SIZE - input_index;
                    UINT16 msgRemainder = input_msg_length - 2;
                    readLength = MIN(bufferSpace, msgRemainder);  
                }
            }
            else
            {
                readLength = segment_size;   
            }            
            
            if (msg_test)
            {
                // we're just sending our test message
                int hdrOffset;
                if (input_messaging)
                {
                    input_msg_length = msg_test_length;
                    UINT16 temp16 = ntohs(msg_test_length);
                    memcpy(input_buffer, &temp16, 2);
                    hdrOffset = 2;
                }
                else
                {
                    hdrOffset = 0;
                }
                int hdrLen = snprintf(input_buffer + hdrOffset, MSG_BUFFER_SIZE - hdrOffset, "NORM Test Msg %08u ", msg_test_seq++);
                char testChar = 'a' + msg_test_seq % 26;
                memset(input_buffer + hdrLen + hdrOffset, testChar, msg_test_length - (hdrLen + hdrOffset));
                input_buffer[msg_test_length - 1] = '\n';
                input_length = msg_test_length;
                input_index = 0;
            }
            else
            {
                // Read from "input" into our "input_buffer"
                size_t result = fread(input_buffer+input_index, sizeof(char), readLength, input);
                if (result > 0)
                {
                    if (input_messaging)
                    {
                        if (input_msg_length)
                        {
                            input_length = input_index + result;
                            input_index = 0;
                        }
                        else
                        {
                            input_length = 0;
                            input_index += (unsigned int)result;
                        }
                    }
                    else
                    {
                        input_length = input_index + result;
                        input_index = 0;    
                    }             
                }
                else
                {
                    if (feof(input))
                    {
                        PLOG(PL_ALWAYS, "norm: input end-of-file.\n");
                        if (input_active) 
                        {
    #ifdef WIN32
                            ASSERT(0);  // no Win32 support for stream i/o yet
    #else
                            dispatcher.RemoveGenericInput(fileno(input));
    #endif // if/else WIN32/UNIX
                            input_active = false;
                        }
                        if (stdin != input) fclose(input);
                        input = NULL;
                        endOfStream = true;   
                        tx_stream->SetFlushMode(NormStreamObject::FLUSH_ACTIVE);
                    }
                    else if (ferror(input))
                    {
    #ifndef WIN32  // no Win32 stream support yet
                        switch (errno)
                        {
                            case EINTR:
                                continue;
                            case EAGAIN:
                                // Input not ready, will get notification when ready
                                inputStarved = true;
                                break;
                            default:
                                PLOG(PL_ERROR, "norm: input error:%s\n", GetErrorString());
                                break;   
                        }
    #endif // !WIN32
                        clearerr(input);
                    }
                } 
            }  // end if/else msg_test
        }  // end if (0 == input_length)
        
        unsigned int writeLength = input_length;// ? input_length - input_index : 0;
            
        if (writeLength || endOfStream)
        {
            unsigned int wroteLength = tx_stream->Write(input_buffer+input_index,
                                                        writeLength, false);
            input_length -= wroteLength;
            if (0 == input_length)
                input_index = 0;
            else
                input_index += wroteLength;
            if (input_messaging && (0 != wroteLength)) 
            {
                input_msg_index += wroteLength;
                if (input_msg_index == input_msg_length)
                {
                    input_msg_index = 0;
                    input_msg_length = 0;  
                    // Mark EOM 
                    tx_stream->Write(NULL, 0, true); 
                    // No need to explicitly flush with "flush mode" set.
                    //tx_stream->Flush();
                }
            }
            if (wroteLength < writeLength) 
            {
                // Stream buffer full, temporarily deactive "input" and 
                // wait for next TX_QUEUE_EMPTY notification
                if (input_active)
                {
#ifdef WIN32
                    ASSERT(0);  // no Win32 support for stream i/o yet
#else
                    dispatcher.RemoveGenericInput(fileno(input));
#endif // if/else WIN32/UNIX
                    input_active = false;   
                }
                break;  
            }
        }
        else if (inputStarved)
        {
            // Input not ready, wait for "input" to be ready
            // (Activate/reactivate "input" as necessary
            if (!input_active)
            {
#ifdef WIN32
                ASSERT(0);  // no Win32 support for stream i/o yet
                if (false)
#else
                if (dispatcher.InstallGenericInput(fileno(input), NormApp::DoInputReady, this))
#endif // if/else WIN32/UNIX
                    input_active = true;
                else
                    PLOG(PL_ERROR, "NormApp::OnInputReady() error adding input notification!\n");
            } 
            break; 
        }
    }  // end while (input)
}  // NormApp::OnInputReady()


void NormApp::Notify(NormController::Event event,
                     class NormSessionMgr* sessionMgr,
                     class NormSession*    session,
                     class NormNode*       sender,
                     class NormObject*     object)
{
    if (notify_reentry)
    {
        Notification* notification;
        if (NULL == (notification = GetNotification()))
        {
            PLOG(PL_ERROR, "NormApp::Notify() error: unable to create Notification item!\n");
            return;
        }
        notification->event = event;
        return;
    }
    notify_reentry = true;
    switch (event)
    {
        case TX_QUEUE_VACANCY:
            PLOG(PL_DEBUG, "NormApp::Notify(TX_QUEUE_VACANCY) ...\n");
            if (NULL != object)
            {
                if ((msg_test || input) && (object == tx_stream))
                    OnInputReady();
            }
            break;
        case TX_QUEUE_EMPTY:
            // Write to stream as needed
            PLOG(PL_DEBUG, "NormApp::Notify(TX_QUEUE_EMPTY) ...\n");
            if (NULL != object)
            {
                if ((msg_test || input) && (object == tx_stream))
                    OnInputReady();
            }
            else if (tx_object_interval >= 0.0)
            {
                // Can queue a new object for transmission 
                if (interval_timer.IsActive()) interval_timer.Deactivate();
                if (interval_timer.GetInterval() > 0.0)
                    ActivateTimer(interval_timer); 
                else
                    OnIntervalTimeout(interval_timer);
            }
            break;
            
        case TX_OBJECT_SENT:
            PLOG(PL_DEBUG, "NormApp::Notify(TX_OBJECT_SENT) ...\n");
            break;
            
        case TX_FLUSH_COMPLETED:
            PLOG(PL_DEBUG, "NormApp::Notify(TX_FLUSH_COMPLETED) ...\n");
            if (tx_one_shot)
            {
                PLOG(PL_ALWAYS, "norm: transmit flushing completed, exiting.\n");
                Stop();
            }
            else if (tx_object_interval < 0.0)
            {
                // Can queue a new object for transmission 
                if (interval_timer.IsActive()) interval_timer.Deactivate();
                if (interval_timer.GetInterval() > 0.0)
                    ActivateTimer(interval_timer); 
                else
                    OnIntervalTimeout(interval_timer);
            }
            break;
            
        case TX_WATERMARK_COMPLETED:
        {
            PLOG(PL_DEBUG, "NormApp::Notify(TX_WATERMARK_COMPLETED) ...\n");
            //NormSession::AckingStatus status = session->SenderGetAckingStatus(NORM_NODE_ANY);
            watermark_pending = false;  // enable new watermark to be set
            if (tx_ack_shot)
            {
                PLOG(PL_ALWAYS, "norm: transmit flushing completed, exiting.\n");
                Stop();     
            }
            break;
        }
        
        case REMOTE_SENDER_ACTIVE:
            PLOG(PL_DEBUG, "NormApp::Notify(REMOTE_SENDER_ACTIVE) ...\n");
            break;
        
        case REMOTE_SENDER_INACTIVE:
            PLOG(PL_DEBUG, "NormApp::Notify(REMOTE_SENDER_INACTIVE) ...\n");
            if (!rx_persistent) 
                static_cast<NormSenderNode*>(sender)->FreeBuffers();
            break;
           
           
        case RX_OBJECT_NEW:
        {
            PLOG(PL_DEBUG, "NormApp::Notify(RX_OBJECT_NEW) ...\n");
            struct timeval currentTime;
            ProtoSystemTime(currentTime);
            time_t secs = (time_t)currentTime.tv_sec;
            struct tm* timePtr = gmtime(&secs); 
            PLOG(PL_INFO, "%02d:%02d:%02d.%06lu start rx object>%hu sender>%lu\n",
		            timePtr->tm_hour, timePtr->tm_min, timePtr->tm_sec, 
		            (unsigned long)currentTime.tv_usec, (UINT16)object->GetId(), sender->GetId());
            
            // It's up to the app to "accept" the object
            switch (object->GetType())
            {
                case NormObject::STREAM:
                {
                    // Only have one rx_stream at a time for now.
                    // Reset stream i/o mgmt                    
                    output_msg_length = output_index = 0;
                    if (output_messaging) output_msg_sync = false;
                    
                    // object Size() has recommended buffering size
                    NormObjectSize size;
                    if (silent_receiver)
                        size =  rx_buffer_size;
                    else
                        size = (UINT32)object->GetSize().LSB();  
                    ASSERT(0 == size.MSB());
                    if (((NormStreamObject*)object)->Accept(size.LSB()))
                    {
                        rx_stream = (NormStreamObject*)object;
                    }
                    else
                    {
                        PLOG(PL_ERROR, "NormApp::Notify(RX_OBJECT_NEW) stream object accept error!\n");
                    }
                }
                break;                        
                case NormObject::FILE:
                {
                    if (rx_cache_path)
                    {
                        // (TBD) re-arrange so if we've already recv'd INFO,
                        // we can use that for file name right away ???
                        // (TBD) Manage recv file name collisions, etc ...
                        char fileName[PATH_MAX];
                        strcpy(fileName, rx_cache_path);
                        size_t catMax = strlen(fileName);
                        if (catMax > PATH_MAX)
                            catMax = 0;
                        else
                            catMax = PATH_MAX - catMax;
                        strcat(fileName, "normTempXXXXXX");
#ifdef WIN32
#ifdef _WIN32_WCE
                        bool tempFileOK = false;
                        for (int i = 0; i < 255; i++)
                        {
                            strncpy(fileName, rx_cache_path, PATH_MAX);
                            catMax = strlen(fileName);
                            if (catMax > PATH_MAX) 
                                catMax = 0;
                            else
                                catMax = PATH_MAX - catMax;
                            strncat(fileName, "normTempXXXXXX", catMax);
                            char tempName[16];
                            sprintf(tempName, "normTemp%06u", i);
                            strcat(fileName, tempName);
                            if(!NormFile::IsLocked(fileName))
                            {
                                tempFileOK = true;
                                break;
                            }
                        }
                        if (!tempFileOK)
#else
                        if (!_mktemp(fileName))
#endif // if/else _WIN32_WCE
#else
                        int fd = mkstemp(fileName); 
                        if (fd >= 0)
                        {
                            close(fd);
                        }
                        else   
#endif // if/else WIN32         
                        {
                            PLOG(PL_ERROR, "NormApp::Notify(RX_OBJECT_NEW) Warning: mkstemp() error: %s\n",
                                    GetErrorString());  
                        } 
                        if (!static_cast<NormFileObject*>(object)->Accept(fileName))
                        {
                            PLOG(PL_ERROR, "NormApp::Notify(RX_OBJECT_NEW) file object accept error!\n");
                        }
                    }
                    else
                    {
                        PLOG(PL_ERROR, "NormApp::Notify(RX_OBJECT_NEW) no rx cache for file\n");   
                    }                    
                }
                break;
                case NormObject::DATA: 
                    PLOG(PL_ERROR, "NormApp::Notify() FILE/DATA objects not _yet_ supported...\n");      
                    break;
                    
                case NormObject::NONE:
                    break;
            }   
            break;
        }
            
        case RX_OBJECT_INFO:
            PLOG(PL_DEBUG, "NormApp::Notify(RX_OBJECT_INFO) ...\n");
            switch(object->GetType())
            {
                case NormObject::FILE:
                {
                    // Rename rx file using newly received info
                    char fileName[PATH_MAX];
                    strncpy(fileName, rx_cache_path, PATH_MAX);
                    UINT16 pathLen = (UINT16)strlen(rx_cache_path);
                    pathLen = MIN(pathLen, PATH_MAX);
                    UINT16 len = object->GetInfoLength();
                    len = MIN(len, (PATH_MAX - pathLen));
                    strncat(fileName, object->GetInfo(), len);
                    // Convert '/' in file info to directory delimiters
                    for (UINT16 i = pathLen; i < (pathLen+len); i++)
                    {
                        if ('/' == fileName[i]) 
                            fileName[i] = PROTO_PATH_DELIMITER;
                    }
                    pathLen += len;
                    if (pathLen < PATH_MAX) fileName[pathLen] = '\0';
                    
                    // Deal with concurrent rx name collisions
                    // (TBD) and implement overwrite policy
                    //       and cache files in cache mode
                    
                    if (!(static_cast<NormFileObject*>(object)->Rename(fileName)))
                    {
                        PLOG(PL_ERROR, "NormApp::Notify() Error renaming rx file: %s\n",
                                fileName);
                    }
                    break;
                }
                case NormObject::DATA:
                case NormObject::STREAM:
                case NormObject::NONE:
                    break;
            }  // end switch(object->GetType())
            break;
            
        case RX_OBJECT_UPDATED:
            PLOG(PL_TRACE, "NormApp::Notify(RX_OBJECT_UPDATED) ...\n");
            switch (object->GetType())
            {
                case NormObject::FILE:
                    // (TBD) update reception progress display when applicable
                    // Call object->SetNotifyOnUpdate(true) here to keep
                    // the update notifications coming. Otherwise they stop!
                    break;
                
                case NormObject::STREAM:
                {
                    if (object != rx_stream)
                    {
                        PLOG(PL_ERROR, "NormApp::Notify(RX_OBJECT_UPDATED) update for invalid stream\n");
                        break;   
                    }
                    // Read the stream when it's updated  
                    ASSERT(NULL != output);
                    bool reading = true;
                    bool seekMsgStart;
                    while (reading)
                    {
                        unsigned int readLength;
                        if (output_messaging)
                        {
                            if (output_msg_length)
                            {
                                readLength = output_msg_length - output_index;
                            }
                            else if (output_index < 2)
                            {
                                readLength = 2 - output_index;
                            }
                            else
                            {
                                memcpy(&output_msg_length, output_buffer, 2);
                                output_msg_length = ntohs(output_msg_length);
                                ASSERT(output_msg_length >= 2);
                                readLength = output_msg_length - output_index;
                            }     
                            seekMsgStart = output_msg_sync ? false : true;                                            
                        }
                        else
                        {
                            output_index = 0;
                            readLength = 512; 
                            seekMsgStart = false;   
                        } 
                        
                        if(!((NormStreamObject*)object)->Read(output_buffer+output_index, 
                                                              &readLength, seekMsgStart))
                        {
                            // The stream broke
                            if (output_messaging)
                            {
                                if (output_msg_sync)
                                    PLOG(PL_ERROR, "NormApp::Notify() detected broken stream ...\n");
                                output_msg_length = output_index = 0;
                                output_msg_sync = false;
                                if (seekMsgStart)
                                    break;  // didn't find msg start yet
                                else
                                    continue;  // try to re-sync
                            }
                        }
                        else if (readLength > 0)
                        {
                            output_msg_sync = true;
                            output_index += readLength;
                        }
                        else
                        {
                            reading = false;
                        }

                        unsigned int writeLength;
                        if (output_messaging)
                        {
                            if (output_msg_length && (output_index >= output_msg_length))
                            {
                                writeLength = output_msg_length;
                                output_msg_length = 0;
                                output_index = 0;
                            }
                            else
                            {
                                writeLength = 0;
                            }
                        }
                        else
                        {
                            writeLength = readLength;
                            output_index = 0;
                        }      
                        
                        unsigned int put = 0;
                        while (put < writeLength)
                        {
                            size_t result = fwrite(output_buffer+put, sizeof(char), writeLength-put, output);
                            
                            if (result)
                            {
                                put += (unsigned int)result;   
                            }
                            else
                            {
                                if (ferror(output))
                                {
#ifndef WIN32  // no stream support in this app for Win32
                                    if (EINTR == errno) 
                                    {
                                        clearerr(output);
                                        continue;
                                    }
                                    else
#endif // !WIN32
                                    {
                                        PLOG(PL_ERROR, "norm: output error:%s\n", GetErrorString());
                                        clearerr(output);
                                        break;
                                    }   
                                }
                            }   
                        }  // end while(put < nBytes)
                        if (writeLength > 0) 
                        {
                            memset(output_buffer, 0, writeLength); // why did do this?
                            fflush(output);
                        }
                    }  // end while (reading)
                    
                    break;
                }
                                        
                case NormObject::DATA: 
                    PLOG(PL_ERROR, "NormApp::Notify() DATA objects not _yet_ supported...\n");      
                    break;
                case NormObject::NONE:
                    break;
            }  // end switch (object->GetType())
            break;
            
        case RX_OBJECT_COMPLETED:
        {
            // (TBD) if we're not archiving files we should
            //       manage our cache, deleting the cache
            //       on shutdown ...
            PLOG(PL_DEBUG, "NormApp::Notify(RX_OBJECT_COMPLETED) ...\n");
            // We had to split the debug output line here into two because of some
            // weirdness with the underlying PLOG() code and too many arguments?
            if (GetDebugLevel() >= PL_INFO)
            {
                struct timeval currentTime;
                ProtoSystemTime(currentTime);
                time_t secs = (time_t)currentTime.tv_sec;
                struct tm* timePtr = gmtime(&secs);
                PLOG(PL_INFO, "%02d:%02d:%02d.%06u completed rx object>%hu ",
		                (int)timePtr->tm_hour, (int)timePtr->tm_min, (int)timePtr->tm_sec, (unsigned int)currentTime.tv_usec, 
                        (UINT16)object->GetId());
                //TRACE("sender>%lu\n", sender->GetId());
            }
            switch(object->GetType())
            {
                case NormObject::FILE:
                {
                    const char* filePath = static_cast<NormFileObject*>(object)->GetPath();
                    //DMSG(0, "norm: Completed rx file: %s\n", filePath);
                    if (post_processor->IsEnabled())
                    {
                        if (!post_processor->ProcessFile(filePath))
                        {
                            PLOG(PL_ERROR, "norm: post processing error\n");
                        }  
                    }
                    break;
                }
                    
                case NormObject::STREAM:
                    ASSERT(0);
                    break;
                case NormObject::DATA:
                    ASSERT(0);
                    break;
                case NormObject::NONE:
                    break;
            }
            break;
        }
        
        case RX_OBJECT_ABORTED:
        {
            PLOG(PL_FATAL, "NormApp::Notify(RX_OBJECT_ABORTED) ...\n");
            struct timeval currentTime;
            ProtoSystemTime(currentTime);
            time_t secs = (time_t)currentTime.tv_sec;
            struct tm* timePtr = gmtime(&secs); 
            PLOG(PL_INFO, "%02d:%02d:%02d.%06lu aborted rx object>%hu sender>%lu\n",
		            timePtr->tm_hour, timePtr->tm_min, timePtr->tm_sec, 
		            (unsigned long)currentTime.tv_usec, (UINT16)object->GetId(), sender->GetId());
            switch(object->GetType())
            {
                case NormObject::FILE:
                {
                    const char* filePath = static_cast<NormFileObject*>(object)->GetPath();
                    if (process_aborted_files)
                    {
                        // in case file size isn't padded properly
                        static_cast<NormFileObject*>(object)->PadToSize();
                        if (post_processor->IsEnabled())
                        {
                            if (!post_processor->ProcessFile(filePath))
                            {
                                PLOG(PL_ERROR, "norm: post processing error\n");
                            }  
                        }
                    }
                    else
                    {
                        NormFile::Unlink(filePath);
                    }
                    break;
                }
                default:
                    break;
            }
            break;
        }
        
        default:
        {
            PLOG(PL_DEBUG, "NormApp::Notify() unhandled event: %d\n", event);
            break;
        }
    }  // end switch(event)
    notify_reentry = false;
    // Now handle any deferred, reentrant notifications
    Notification* notification;
    while (NULL != (notification = notify_queue.RemoveHead()))
    {
        Notify(notification->event, sessionMgr, session, sender, object);
        notify_pool.Append(*notification);
    }
}  // end NormApp::Notify()


bool NormApp::OnIntervalTimeout(ProtoTimer& theTimer)
{
    char fileName[PATH_MAX];
    if (('\0' != tx_file_name[0]) || tx_file_list.GetNextFile(fileName))
    {
        tx_repeat_clear = true;
        
        // The "tx_file_name" is used to cache the path of the current file for enqueue
        // re-attempt upon possible flow control NormSession::QueueTxFile() failure
        if ('\0' == tx_file_name[0])
            strcpy(tx_file_name, fileName);
        else
            strcpy(fileName, tx_file_name);  // used cached tx_file_name for enqueue re-attempt
        
        // 1) Build up the full path name of the file
        char pathName[PATH_MAX];
        tx_file_list.GetCurrentBasePath(pathName);
        size_t len = strlen(pathName);
        len = MIN(len, PATH_MAX);
        size_t maxLen = PATH_MAX - len;
        char* ptr = fileName + len;
        len = strlen(ptr);
        len = MIN(len, maxLen);
        // (TBD) Make sure len <= segment_size)
        char fileNameInfo[PATH_MAX];
        strncpy(fileNameInfo, ptr, len);
        // Normalize directory delimiters in file name info
        for (unsigned int i = 0; i < len; i++)
        {
            if (PROTO_PATH_DELIMITER == fileNameInfo[i]) 
                fileNameInfo[i] = '/';
        }
        
        FileCacheItem* fileCacheItem = NULL;
        NormFileObject* obj = NULL;
        if (0 != tx_requeue)
        {
            // Is this file already in our "requeue" tx_file_cache?
            if (NULL != (fileCacheItem = static_cast<FileCacheItem*>(tx_file_cache.Find(fileName, PATH_MAX << 3))))
            {
                // Try to find and requeue cached tx object
                obj = static_cast<NormFileObject*>(session->SenderFindTxObject(fileCacheItem->GetObjectId()));
                bool requeueSuccess = false;
                if (NULL != obj) requeueSuccess = session->RequeueTxObject(obj);
                if (!requeueSuccess)
                {
                    PLOG(PL_ERROR, "norm warning: requeue attempt exceeded configured tx cache bounds!\n");
                    // TBD - remove file from tx_file_cache???
                }
                // RequeueTxObject() is not subject to flow control since the object is already buffered
                tx_file_name[0] = '\0'; // reset so next file will be fetched
            }
        }
        if (NULL == obj)
        {
            // This makes sure it's not an empty file
            // (TBD - provide for empty files to be sent)
            if (0 == NormFile::GetSize(fileName))
            {
                PLOG(PL_WARN, "norm warning: empty file \"%s\" will not be transmitted\n");
                tx_file_name[0] = '\0'; // reset so next file will be fetched
                return OnIntervalTimeout(theTimer);
            }
            // It's a new object
            const char* infoPtr = tx_file_info ? fileNameInfo : NULL;
            UINT16 infoLen = tx_file_info ? (UINT16)len : 0;
            if (NULL == (obj = session->QueueTxFile(fileName, infoPtr, infoLen)))
            {
                PLOG(PL_DEBUG, "NormApp::OnIntervalTimeout() error queuing tx file: %s\n", fileName);
                // The code currently assumes that QueueTxFile() failed because of
                // flow control and so we simply return and wait for a TX_QUEUE_EMPTY notification
                // to trigger a re-attempt to enqueue the file name in "tx_file_name"
                if (interval_timer.IsActive()) interval_timer.Deactivate();
                // Old behavior was to wait a second and go to next file
                // (commented out here)
                //interval_timer.SetInterval(1.0);
                //ActivateTimer(interval_timer);
                return false;
            }
            else
            {
                tx_file_name[0] = '\0';  // reset so next file will be fetched
            }
            if (0 != tx_requeue)
            {
                if (NULL == fileCacheItem)
                {
                    // Cache for "requeue" if applicable
                    // It's not in the cache, so add it ...
                    if (NULL == (fileCacheItem = new FileCacheItem(fileName)))
                    {
                        PLOG(PL_ERROR, "NormApp::OnIntervalTimeout(): new FileCachItem error: %s\n",
                                GetErrorString());
                    }
                    tx_file_cache.Insert(*fileCacheItem);
                }
                if (NULL != fileCacheItem)
                    fileCacheItem->SetObjectId(obj->GetId());
            }
        }
        
        tx_file_queued = true;
        
        
        struct timeval currentTime;
        ProtoSystemTime(currentTime);
        time_t secs = (time_t)currentTime.tv_sec;
        struct tm* timePtr = gmtime(&secs); 
        PLOG(PL_INFO, "%02d:%02d:%02d.%06lu enqueued tx object>%hu sender>%lu\n",
		        timePtr->tm_hour, timePtr->tm_min, timePtr->tm_sec, 
		        (unsigned long)currentTime.tv_usec, (UINT16)obj->GetId(), session->LocalNodeId());
        
        // save last obj/block/seg id for later in case needed
        tx_last_object_id = obj->GetId();
        tx_last_block_id = obj->GetFinalBlockId();
        tx_last_segment_id = obj->GetBlockSize(tx_last_block_id) - 1;
        if (!watermark_pending && (NULL != acking_node_list) && !tx_ack_shot)
        {
            session->SenderSetWatermark(tx_last_object_id, 
                                        tx_last_block_id,
                                        tx_last_segment_id,
                                        acking_flushes);
            watermark_pending = true;  // only allow one pending watermark at a time
        }
        //DMSG(0, "norm: File \"%s\" queued for transmission.\n", fileName);
        interval_timer.SetInterval(tx_object_interval);
    }
    else if (0 != tx_requeue_count)
    {
        if (tx_requeue_count > 0) tx_requeue_count--;
        tx_file_list.ResetIterator();
        return OnIntervalTimeout(interval_timer);
    }
    else if (0 != tx_repeat_count)
    {
        // (TBD) When repeating, remove previous instance from tx queue???
        if (tx_repeat_count > 0) tx_repeat_count--;
        tx_file_cache.Destroy();
        tx_requeue_count = tx_requeue;
        tx_file_list.ResetIterator();
        if (tx_repeat_interval > tx_object_interval)
        {
            if (interval_timer.IsActive()) interval_timer.Deactivate();
            interval_timer.SetInterval(tx_repeat_interval - tx_object_interval);
            ActivateTimer(interval_timer);
            return false;       
        }
        else
        {
            if (tx_repeat_clear)
            {
                tx_repeat_clear = false;
                return OnIntervalTimeout(interval_timer);
            }
            else
            {
                tx_repeat_clear = true;
                if (interval_timer.IsActive()) interval_timer.Deactivate();
                interval_timer.SetInterval(tx_repeat_interval);
                ActivateTimer(interval_timer);
                return false; 
            }
        }
    }
    else
    {
        tx_repeat_clear = true;
        PLOG(PL_ALWAYS, "norm: End of tx file list reached.\n");  
        if (tx_ack_shot && !watermark_pending && tx_file_queued)
        {
            session->SenderSetWatermark(tx_last_object_id, 
                                        tx_last_block_id,
                                        tx_last_segment_id,
                                        acking_flushes);
            watermark_pending = true;  // only allow one pending watermark at a time
        }
    }   
    return true;
}  // end NormApp::OnIntervalTimeout()

bool NormApp::OnStartup(int argc, const char*const* argv)
{
    if (argc < 3) 
    {
        ShowHelp();
        return false; 
    }
       
    if (!(post_processor = NormPostProcessor::Create()))
    {
        PLOG(PL_FATAL, "NormApp::OnStartup() error creating post processor\n");
        return false;   
    }
    
    if (!ProcessCommands(argc, argv))
    {
        PLOG(PL_FATAL, "NormApp::OnStartup() error processing command-line commands\n");
        return false; 
    }
    
    

    if (control_remote) return false;

#ifdef UNIX    
    signal(SIGCHLD, SignalHandler);
#endif // UNIX
        
    // Validate our application settings
    if (!address)
    {
        PLOG(PL_FATAL, "NormApp::OnStartup() Error! no session address given.\n");
        return false;
    }
    if (!input && !output && tx_file_list.IsEmpty() && !rx_cache_path && !msg_test)
    {
        PLOG(PL_FATAL, "NormApp::OnStartup() Error! no \"input\", \"output\", "
                "\"sendfile\", \"stest\", \"mtest\", or \"rxcache\" given.\n");
        return false;
    }   
    
    if ((NULL != output) && (NULL != output_io_buffer))
    {
        if (0 != setvbuf(output, output_io_buffer, _IOFBF, output_io_bufsize))
        {
            PLOG(PL_ERROR, "NormApp::OnStartup() setvbuf() error: %s\n", GetErrorString());
        }
    }
    
    // Create a new session on multicast group/port
    session = session_mgr.NewSession(address, port, node_id);
    if (session)
    {
        // Common session parameters
        session->SetTxPort(tx_port);
        session->SetTxRate(tx_rate);
        session->SetTxRateBounds(tx_rate_min, tx_rate_max);
        session->SetTrace(tracing);
        session->SetTxLoss(tx_loss);
        session->SetRxLoss(rx_loss);
        session->SetTTL(ttl);
        session->SetLoopback(loopback);
        if (loopback && session->Address().IsMulticast())
            session->SetRxPortReuse(true);
        if (NULL != interface_name)
            session->SetMulticastInterface(interface_name);
        
        session->SetEcnSupport(ECN_OFF != ecn_mode, ECN_ONLY == ecn_mode, tolerate_loss);
        
        // We turn off flow control when no NACKing is happening
        // (TBD - make flow control a command-line option)
        if (silent_receiver)
            session->SetFlowControl(0.0); 
        
        if (msg_test || input || !tx_file_list.IsEmpty())
        {
            NormObjectId baseId = (unsigned short)(rand() * (65535.0/ (double)RAND_MAX));
            session->SenderSetBaseObjectId(baseId);
            session->SetCongestionControl(cc_enable);
            session->SetBackoffFactor(backoff_factor);
            session->SenderSetGrtt(grtt_estimate);
            session->SetGrttProbingMode(grtt_probing_mode);
            session->SenderSetGroupSize(group_size);
            session->SetTxRobustFactor(tx_robust_factor);
            session->SetTxCacheBounds(tx_cache_size, tx_cache_min, tx_cache_max);
            if (!AddAckingNodes(acking_node_list))
            {
                PLOG(PL_FATAL, "NormApp::OnStartup() error: bad acking node list\n");
                session_mgr.Destroy();
                return false;
            }
            // redundant info transmission if we have silent receivers
            if (silent_receiver) session->SndrSetEmcon(true);
            // We also use the baseId as our sender's "instance id" for illustrative purposes
            UINT16 instanceId = baseId;
            if (!session->StartSender(instanceId, tx_buffer_size, segment_size, ndata, nparity))
            {
                PLOG(PL_FATAL, "NormApp::OnStartup() start sender error!\n");
                session_mgr.Destroy();
                return false;
            }              
	        if (tx_sock_buffer_size > 0)
		        session->SetTxSocketBuffer(tx_sock_buffer_size);
            session->SenderSetAutoParity(auto_parity);
            session->SenderSetExtraParity(extra_parity);
            if (input || msg_test)
            {
                // Open a stream object to write to (QueueTxStream(stream bufferSize))
                tx_stream = session->QueueTxStream(tx_buffer_size);
                if (!tx_stream)
                {
                    PLOG(PL_FATAL, "NormApp::OnStartup() queue tx stream error!\n");
                    session_mgr.Destroy();
                    return false;
                }
                tx_stream->SetFlushMode(msg_flush_mode);
                tx_stream->SetPushMode(push_mode);
            }
            else
            {
                OnIntervalTimeout(interval_timer);
            }            
        }
        
        if (output || rx_cache_path)
        {
            // StartReceiver(bufferMax (per-sender))
            session->SetRxCacheMax(tx_cache_max);
            session->ReceiverSetUnicastNacks(unicast_nacks);
            session->ReceiverSetSilent(silent_receiver);
            if (preallocate_sender)
            {
                if (!session->PreallocateRemoteSender(rx_buffer_size, segment_size, ndata, nparity))
                {
                    PLOG(PL_FATAL, "NormApp::OnStartup() remote sender preallocation error!\n");
                    session_mgr.Destroy();
                    return false;
                }
            }
            if (low_delay)
                session->RcvrSetMaxDelay(1);
            else
                session->RcvrSetMaxDelay(-1); 
            if (realtime)
                session->RcvrSetRealtime(true);           
            session->SetRxRobustFactor(rx_robust_factor);
            session->ReceiverSetDefaultRepairBoundary(repair_boundary);
            if (!session->StartReceiver(rx_buffer_size))
            {
                PLOG(PL_FATAL, "NormApp::OnStartup() start receiver error!\n");
                session_mgr.Destroy();
                return false;
            }
            if (rx_sock_buffer_size > 0)
                session->SetRxSocketBuffer(rx_sock_buffer_size);
        }
        if (precise) dispatcher.SetPreciseTiming(true);
        if (boost) dispatcher.SetPriorityBoost(true);
        return true;
    }
    else
    {
        PLOG(PL_FATAL, "NormApp::OnStartup() new session error!\n");
        return false;
    }
}  // end NormApp::OnStartup()

void NormApp::OnShutdown()
{
    //DMSG(0, "NormApp::OnShutdown() ...\n");
    session_mgr.Destroy();
    if (input)
    {
        if (input_active) 
        {
#ifdef WIN32
            ASSERT(0);   // no Win32 support for stream i/o yet
#else
            dispatcher.RemoveGenericInput(fileno(input));
#endif // if/else WIN32/UNIX
            input_active = false;
        }
        if (stdin != input) fclose(input);
        input = NULL;
    }
    if (output) 
    {
        if (stdout != output) fclose(output);
        output = NULL;
    }
    if (post_processor)
    {
        delete post_processor;
        post_processor = NULL;   
    }
}  // end NormApp::OnShutdown()

// Out application instance 
PROTO_INSTANTIATE_APP(NormApp)

#ifdef UNIX
void NormApp::SignalHandler(int sigNum)
{
    switch(sigNum)
    {
        case SIGCHLD:
        {
            NormApp* app = static_cast<NormApp*>(ProtoApp::GetApp());
            if (app->post_processor) app->post_processor->OnDeath();
            // The use of "waitpid()" here is a work-around for
            // an IRIX SIGCHLD issue
            int status;
            while (waitpid(-1, &status, WNOHANG) > 0);
            signal(SIGCHLD, SignalHandler);
            break;
        }
            
        default:
            fprintf(stderr, "norm: Unexpected signal: %d\n", sigNum);
            break; 
    }  
}  // end NormApp::SignalHandler()
#endif // UNIX