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



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

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.



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

def thread
  @thread
end

Instance Method Details

#abort_streaming(checksum) ⇒ Object



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

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

#copy_another_chuck(checksum) ⇒ Object



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

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

#handle(message) ⇒ Object



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

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.debug1("No checksum found to copy chunk. #{checksum}.")
    end
  end

end

#reset_stream(checksum, path, offset) ⇒ Object



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

def reset_stream(checksum, path, offset)
  if !@streams.key? checksum
    begin
      file = File.new(path, 'rb')
      if offset > 0
        file.seek(offset)
      end
      Log.debug1("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



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

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

#runObject



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

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

#start_streaming(checksum, path) ⇒ Object



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

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