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.



161
162
163
164
165
166
# File 'lib/content_server/file_streamer.rb', line 161

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



300
301
302
# File 'lib/content_server/file_streamer.rb', line 300

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

.write_string_to_file(str, file) ⇒ Object



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

def self.write_string_to_file(str, file)
  bytes_to_write = str.bytesize
  Log.debug1("writing to file: #{file.to_s}, #{bytes_to_write} bytes.")
  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



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

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?
    # Handle the case of backup empty file.
    handle_new_stream(file_checksum, 0) if !@streams.key?(file_checksum)
    # Finalize the file copy.
    handle_last_chunk(file_checksum)
    return false
  else
    Log.warning("Unexpected receive chuck message. file_checksum:#{file_checksum}, " \
                  "content.nil?:#{content.nil?}, content_checksum:#{content_checksum}")
    return false
  end
end