Class: ContentServer::FileReceiver

Inherits:
Object
  • Object
show all
Defined in:
lib/content_server/file_streamer.rb

Overview

Start implementing as dummy, no self thread for now. Later when we need it to response and send aborts, timeouts, ect, it will need self thread.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file_done_clb = nil, file_abort_clb = nil, reset_copy = nil) ⇒ FileReceiver

Returns a new instance of FileReceiver.



149
150
151
152
153
154
# File 'lib/content_server/file_streamer.rb', line 149

def initialize(file_done_clb=nil, file_abort_clb=nil, reset_copy=nil)
  @file_done_clb = file_done_clb
  @file_abort_clb = file_abort_clb
  @reset_copy = reset_copy
  @streams = {}
end

Class Method Details

.destination_filename(folder, sha1) ⇒ Object

Creates destination filename for backup server, input is base folder and sha1. for example: folder:/mnt/hd1/bbbackup, sha1:d0be2dc421be4fcd0172e5afceea3970e2f3d940 dest filename: /mnt/hd1/bbbackup/d0/be/2d/d0be2dc421be4fcd0172e5afceea3970e2f3d940



298
299
300
# File 'lib/content_server/file_streamer.rb', line 298

def self.destination_filename(folder, sha1)
  File.join(folder, sha1[0,2], sha1[2,2], sha1)
end

.write_string_to_file(str, file) ⇒ Object



287
288
289
290
291
292
293
# File 'lib/content_server/file_streamer.rb', line 287

def self.write_string_to_file(str, file)
  bytes_to_write = str.bytesize
  Log.debug1("writing to file: %s, %s bytes.", file, bytes_to_write)
  while bytes_to_write > 0
    bytes_to_write -= file.write(str)
  end
end

Instance Method Details

#receive_chunk(file_checksum, offset, file_size, content, content_checksum) ⇒ Object



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
# File 'lib/content_server/file_streamer.rb', line 156

def receive_chunk(file_checksum, offset, file_size, content, content_checksum)
  # If standard chunk copy.
  if !content.nil? && !content_checksum.nil?
    received_content_checksum = FileIndexing::IndexAgent.get_content_checksum(content)
    comment = "Calculated received chunk with content checksum #{received_content_checksum}" \
                " vs message content checksum #{content_checksum}, " \
                "file checksum #{file_checksum}"
    Log.debug1(comment) if content_checksum == received_content_checksum
    # TODO should be here a kind of abort?
    if content_checksum != received_content_checksum
      Log.warning(comment)
      new_offset = 0
      if @streams.key?(file_checksum)
        new_offset = @streams[file_checksum].file.pos
      end
      @reset_copy.call(file_checksum, new_offset) unless @reset_copy.nil?
      return false
    end

    if !@streams.key?(file_checksum)
      handle_new_stream(file_checksum, file_size)
    end
    # We still check @streams has the key, because file can fail to open.
    if @streams.key?(file_checksum)
      return handle_new_chunk(file_checksum, offset, content)
    else
      Log.warning('Cannot handle chunk, stream does not exists, sending abort.')
      @file_abort_clb.call(file_checksum) unless @file_abort_clb.nil?
      return false
    end
    # If last chunk copy.
  elsif content.nil? && content_checksum.nil?
    Log.debug1('Handle the case of backup empty file or last chunk')
    # Handle the case of backup empty file or last chunk.
    handle_new_stream(file_checksum, 0) if !@streams.key?(file_checksum)
    # Finalize the file copy.
    handle_last_chunk(file_checksum)
    return true
  else
    Log.warning("Unexpected receive chuck message. file_checksum:#{file_checksum}, " \
                  "content.nil?:#{content.nil?}, content_checksum:#{content_checksum}")
    return false
  end
  Log.error('Code should never reach this point')
end