Payload.hpp 5.06 KB
Newer Older
1
2
3
4
5
6
7
/*
 * @project        The CERN Tape Archive (CTA)
 * @copyright      Copyright(C) 2021 CERN
 * @license        This program is free software: you can redistribute it and/or modify
 *                 it under the terms of the GNU General Public License as published by
 *                 the Free Software Foundation, either version 3 of the License, or
 *                 (at your option) any later version.
8
 *
9
10
11
12
 *                 This program is distributed in the hope that it will be useful,
 *                 but WITHOUT ANY WARRANTY; without even the implied warranty of
 *                 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *                 GNU General Public License for more details.
13
 *
14
15
16
 *                 You should have received a copy of the GNU General Public License
 *                 along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
17
#include <zlib.h>
18
#include "disk/DiskFile.hpp"
19
#include "castor/tape/tapeserver/file/File.hpp"
20
#include "common/exception/MemException.hpp"
21
#include "common/exception/EndOfFile.hpp"
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#pragma once 

namespace castor {
namespace tape {
namespace tapeserver {
namespace daemon {
  
/**
 * Class managing a fixed size payload buffer. Some member functions also
 * allow read
 * @param capacity Size of the payload buffer in bytes
 */
class Payload
{
36
37
  Payload(const Payload&);
  Payload& operator=(const Payload&);
38
39
public:
  Payload(size_t capacity):
40
41
  m_data(new (std::nothrow) unsigned char[capacity]),m_totalCapacity(capacity),m_size(0) {
    if(NULL == m_data) {
42
      throw cta::exception::MemException("Failed to allocate memory for a new MemBlock!");
43
44
    }
  }
45
  
46
  ~Payload(){
47
    delete[] m_data;
48
49
50
51
52
53
54
  }
  
  /** Amount of data present in the payload buffer */
  size_t size() const {
    return m_size;
  }
  
55
56
57
58
59
  /** Reset the internal counters of the payload */
  void reset() {
    m_size = 0;
  }
  
60
61
62
63
64
65
66
67
68
69
70
71
  /** Remaining free space in the payload buffer */
  size_t remainingFreeSpace() const {
    return m_totalCapacity - m_size;
  }
  
  /** Total size of the payload block */
  size_t totalCapacity() const {
    return m_totalCapacity;
  }
    
  /** Returns a pointer to the beginning of the payload block */
  unsigned char* get(){
72
    return m_data;
73
74
75
76
  }
  
  /** Returns a pointer to the beginning of the payload block (readonly version) */
  unsigned char const*  get() const {
77
    return m_data;
78
79
80
81
82
83
  }
  
  /** 
   * Reads all the buffer in one go from a diskFile::ReadFile object 
   * @param from reference to the diskFile::ReadFile
   */
84
  size_t read(cta::disk::ReadFile& from){
85
    m_size = from.read(m_data,m_totalCapacity);
86
87
    return m_size;
  }
David COME's avatar
David COME committed
88

89
90
91
92
93
94
  /**
   * Reads one block from a tapeFile::readFile
   * @throws castor::tape::daemon::Payload::EOF
   * @param from reference to the tapeFile::ReadFile
   * @return whether another tape block will fit in the memory block.
   */
95
  bool append(castor::tape::tapeFile::ReadFile & from){
96
97
98
99
100
    if (from.getBlockSize() > remainingFreeSpace()) {
      std::stringstream err;
      err << "Trying to read a tape file block with too little space left: BlockSize="
       << from.getBlockSize() << " remainingFreeSpace=" << remainingFreeSpace()
              << " (totalSize=" << m_totalCapacity << ")"; 
101
      throw cta::exception::MemException(err.str());
102
103
104
    }
    size_t readSize;
    try {
105
      readSize = from.read(m_data + m_size, from.getBlockSize());
106
    } catch (castor::tape::tapeFile::EndOfFile&) {
107
      throw cta::exception::EndOfFile("In castor::tape::tapeserver::daemon::Payload::append: reached end of file");
108
109
110
111
112
113
114
115
116
    }
    m_size += readSize;
    return  from.getBlockSize() <= remainingFreeSpace();
  }
  
  /**
   * Write the complete buffer to a diskFile::WriteFile
   * @param to reference to the diskFile::WriteFile
   */
117
  void write(cta::disk::WriteFile& to){
118
    to.write(m_data,m_size);
119
120
121
122
123
124
125
126
127
128
129
130
  }
  
  /**
   * Write the complete buffer to a tapeFile::WriteFile, tape block by
   * tape block
   * @param to reference to the tapeFile::WriteFile
   */
  void write(tape::tapeFile::WriteFile& to) {
    size_t blockSize = to.getBlockSize();
    size_t writePosition = 0;
    // Write all possible full tape blocks
    while (m_size - writePosition > blockSize) {
131
      to.write(m_data + writePosition, blockSize);
132
133
134
135
      writePosition += blockSize;
    }
    // Write a remainder, if any
    if (m_size - writePosition) {
136
      to.write(m_data + writePosition, m_size - writePosition);
137
138
    }
  }
David COME's avatar
David COME committed
139
140
141
142
143
144
145
146
147
148
149
  
  /*
   Example for the Adler32
   * unsigned long chck = Pyaload::zeroAdler32();
   * while() {
   *   payload.read(from);
   *   chck = payload.adler32(chck);
   * }
   */
  
  
150
151
152
153
154
155
   /**
    * Compute adler32 checksum on the current data hold.
    * @param previous The previous adler32 checksum from all previous datablock
    * @return the updated checksum
    */
  unsigned long  adler32(unsigned long previous){
156
    return ::adler32(previous,m_data,m_size);
157
  }
David COME's avatar
David COME committed
158
159
160
161
  
  /**
   * Return the initial value for computing Adler32 checksum
   */
David COME's avatar
David COME committed
162
163
164
  static unsigned long zeroAdler32() {
     return  ::adler32(0L,Z_NULL,0);
   }
165
private:
166
  unsigned char* m_data;
167
168
169
170
171
  size_t m_totalCapacity;
  size_t m_size;
};

}}}}