2ff2538ba0f31818e66d255bc8805fdd3810374b
[senf.git] / Examples / psi2tsModule / psi2ts.cc
1 // $Id$
2 //
3 // Copyright (C) 2008
4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 // Competence Center NETwork research (NET), St. Augustin, GERMANY
6 //     Thorsten Horstmann <tho@berlios.de>
7 //
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 2 of the License, or
11 // (at your option) any later version.
12 //
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 // GNU General Public License for more details.
17 //
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the
20 // Free Software Foundation, Inc.,
21 // 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22
23 /** \file
24     \brief psi2ts.cc non-inline non-template implementation */
25
26 #include "psi2ts.hh"
27 //#include "psi2ts.ih"
28
29 // Custom includes
30
31 //#include "psi2ts.cc.mpp"
32
33 #define prefix_
34 ///////////////////////////////cc.p////////////////////////////////////////
35
36 namespace {
37     template <class InputIterator, class Distance>
38     void advance_max(InputIterator& i, Distance n, InputIterator& i_end)
39     {
40         Distance d = std::min( std::distance(i, i_end), n);
41         std::advance( i, d);
42     }
43 }
44
45
46 prefix_ Psi2TsModule::Psi2TsModule()
47 {
48     continuity_counter_ = 0;
49     state_ = IDLE;
50     route( input, output );
51     input.onRequest( &Psi2TsModule::onRequest );
52 }
53
54 prefix_ void Psi2TsModule::onRequest()
55 {
56     senf::PacketData & section = input.read().data();
57     iterator sec_end = section.end();
58     iterator begin = section.begin();
59     iterator end = section.begin();
60     advance_max( end, 184, sec_end);
61     
62     do {
63         senf::TransportPacket tsPacket (senf::TransportPacket::create(188));
64         tsPacket->continuity_counter() = next_continuity_counter();
65         if (state_ == IDLE) {
66             state_ = PROC;
67             tsPacket->pusi() = true;
68         }
69         senf::PacketData & payloadData (tsPacket.next().data());
70         std::copy( begin, end, payloadData.begin() );
71         tsPacket.finalize();
72         
73         output.write( tsPacket);
74         
75         advance_max( begin, 184, sec_end);
76         advance_max( end,   184, sec_end);
77     } while (begin != end);
78     state_ = IDLE;
79 }
80
81 prefix_ unsigned Psi2TsModule::next_continuity_counter()
82 {
83     continuity_counter_ = (continuity_counter_ + 1) % 16;
84     return continuity_counter_;
85 }
86
87
88 ///////////////////////////////cc.e////////////////////////////////////////
89 #undef prefix_
90 //#include "psi2ts.cc.mpp"
91
92 \f
93 // Local Variables:
94 // mode: c++
95 // fill-column: 100
96 // comment-column: 40
97 // c-file-style: "senf"
98 // indent-tabs-mode: nil
99 // ispell-local-dictionary: "american"
100 // compile-command: "scons -u test"
101 // End: