Class: BBFS::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.



140
141
142
143
144
145
# File 'lib/content_server/file_streamer.rb', line 140

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



274
275
276
# File 'lib/content_server/file_streamer.rb', line 274

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

.write_string_to_file(str, file) ⇒ Object



263
264
265
266
267
268
269
# File 'lib/content_server/file_streamer.rb', line 263

def self.write_string_to_file(str, file)
  bytes_to_write = str.bytesize
  Log.info("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



147
148
149
150
151
152
153
154
155
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
# File 'lib/content_server/file_streamer.rb', line 147

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_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