Removing the --socket-mem eal parameter
[samplevnf.git] / VNFs / DPPD-PROX / tools / flow_extract / stream3.cpp
1 /*
2 // Copyright (c) 2010-2017 Intel Corporation
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 //     http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 */
16
17 #include <iostream>
18 #include <fstream>
19
20 using namespace std;
21
22 #include "stream3.hpp"
23
24 Stream3::Stream3(uint32_t id, PcapPkt::L4Proto proto)
25         : m_id(id), m_proto(proto), m_pktCount(0), m_flushCount(0)
26 {
27 }
28
29 void Stream3::writeHeader(ofstream *outputFile) const
30 {
31         outputFile->write(reinterpret_cast<const char *>(&m_id), sizeof(m_id));
32         outputFile->write(reinterpret_cast<const char *>(&m_flushCount), sizeof(m_flushCount));
33 }
34
35 void Stream3::writePackets(ofstream *outputFile) const
36 {
37         for (size_t i  = 0; i < m_pkts.size(); ++i)
38                 m_pkts[i]->toFile(outputFile);
39 }
40
41 void Stream3::clearPackets()
42 {
43         for (size_t i = 0; i < m_pkts.size(); ++i)
44                 delete m_pkts[i];
45         m_pkts.clear();
46         m_flushCount = 0;
47 }
48
49 void Stream3::flush(ofstream *outputFile)
50 {
51         writeHeader(outputFile);
52         writePackets(outputFile);
53         clearPackets();
54 }
55
56 void Stream3::addPkt(const PcapPkt& pkt)
57 {
58         m_pkts.push_back(new PcapPkt(pkt));
59         m_pktCount++;
60         m_flushCount++;
61 }
62
63 Timestamp Stream3::getTimeout() const
64 {
65         uint32_t timeoutMinutes = m_proto == PcapPkt::PROTO_UDP? 10 : 5;
66
67         return Timestamp(timeoutMinutes * 60, 0);
68 }
69
70 uint32_t Stream3::getIDFromMem(uint8_t *mem)
71 {
72         return *reinterpret_cast<uint32_t *>(mem);
73 }
74
75 void Stream3::addFromMemory(uint8_t *mem, size_t *len)
76 {
77         uint32_t n_pkts;
78
79         mem += sizeof(m_id);
80         n_pkts = *reinterpret_cast<uint32_t *>(mem);
81         mem += sizeof(n_pkts);
82
83         *len = sizeof(m_id) + sizeof(n_pkts);
84         for (uint32_t i = 0; i < n_pkts; ++i) {
85                 addPkt(PcapPkt(mem));
86                 mem += m_pkts.back()->memSize();
87                 *len += m_pkts.back()->memSize();
88         }
89 }
90
91 void Stream3::removeAllPackets()
92 {
93         clearPackets();
94         m_pktCount = 0;
95 }