Class: BBFS::ContentServer::FileStreamer

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(send_chunk_clb, abort_streaming_clb = nil) ⇒ FileStreamer

Returns a new instance of FileStreamer.



49
50
51
52
53
54
55
56
57
# File 'lib/content_server/file_streamer.rb', line 49

def initialize(send_chunk_clb, abort_streaming_clb=nil)
  @send_chunk_clb = send_chunk_clb
  @abort_streaming_clb = abort_streaming_clb
  @stream_queue = Queue.new

  # Used from internal thread only.
  @streams = {}
  @thread = run
end

Instance Attribute Details

#threadObject (readonly)

Returns the value of attribute thread.



42
43
44
# File 'lib/content_server/file_streamer.rb', line 42

def thread
  @thread
end

Instance Method Details

#abort_streaming(checksum) ⇒ Object



67
68
69
# File 'lib/content_server/file_streamer.rb', line 67

def abort_streaming(checksum)
  @stream_queue << [:ABORT_STREAM, checksum]
end

#copy_another_chuck(checksum) ⇒ Object



59
60
61
# File 'lib/content_server/file_streamer.rb', line 59

def copy_another_chuck(checksum)
  @stream_queue << [:COPY_CHUNK, checksum]
end

#handle(message) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/content_server/file_streamer.rb', line 83

def handle(message)
  type, content = message
  if type == :NEW_STREAM
    checksum, path = content
    reset_stream(checksum, path, 0)
    @stream_queue << [:COPY_CHUNK, checksum] if @streams.key?(checksum)
  elsif type == :ABORT_STREAM
    checksum = content
    Stream.close_delete_stream(checksum, @streams)
  elsif type == :RESET_STREAM
    checksum, new_offset = content
    reset_stream(checksum, nil, new_offset)
    @stream_queue << [:COPY_CHUNK, checksum] if @streams.key?(checksum)
  elsif type == :COPY_CHUNK
    checksum = content
    if @streams.key?(checksum)
      offset = @streams[checksum].file.pos
      Log.debug1("Sending chunk for #{checksum}, offset #{offset}.")
      chunk = @streams[checksum].file.read(Params['streaming_chunk_size'])
      if chunk.nil?
        # No more to read, send end of file.
        @send_chunk_clb.call(checksum, offset, @streams[checksum].size, nil, nil)
        Stream.close_delete_stream(checksum, @streams)
      else
        chunk_checksum = FileIndexing::IndexAgent.get_content_checksum(chunk)
        @send_chunk_clb.call(checksum, offset, @streams[checksum].size, chunk, chunk_checksum)
      end
    else
      Log.info("No checksum found to copy chunk. #{checksum}.")
    end
  end

end

#reset_stream(checksum, path, offset) ⇒ Object



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/content_server/file_streamer.rb', line 117

def reset_stream(checksum, path, offset)
  if !@streams.key? checksum
    begin
      file = File.new(path, 'rb')
      if offset > 0
        file.seek(offset)
      end
      Log.info("File streamer: #{file.to_s}.")
    rescue IOError => e
      Log.warning("Could not stream local file #{path}. #{e.to_s}")
    end
    @streams[checksum] = Stream.new(checksum, path, file, file.size)
  else
    @streams[checksum].file.seek(offset)
  end
end

#reset_streaming(checksum, new_offset) ⇒ Object



71
72
73
# File 'lib/content_server/file_streamer.rb', line 71

def reset_streaming(checksum, new_offset)
  @stream_queue << [:RESET_STREAM, [checksum, new_offset]]
end

#runObject



75
76
77
78
79
80
81
# File 'lib/content_server/file_streamer.rb', line 75

def run
  return Thread.new do
    loop {
      checksum = handle(@stream_queue.pop)
    }
  end
end

#start_streaming(checksum, path) ⇒ Object



63
64
65
# File 'lib/content_server/file_streamer.rb', line 63

def start_streaming(checksum, path)
  @stream_queue << [:NEW_STREAM, [checksum, path]]
end