Class: EventMachine::FileStreamer

Inherits:
Object
  • Object
show all
Includes:
Deferrable
Defined in:
lib/em/streamer.rb

Constant Summary collapse

MappingThreshold =
16384
BackpressureLevel =
50000
ChunkSize =
16384

Instance Method Summary collapse

Methods included from Deferrable

#callback, #cancel_timeout, #errback, #fail, future, #set_deferred_failure, #set_deferred_status, #set_deferred_success, #succeed, #timeout

Constructor Details

#initialize(connection, filename, args) ⇒ FileStreamer

Returns a new instance of FileStreamer.



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/em/streamer.rb', line 34

def initialize connection, filename, args
	@connection = connection
	@http_chunks = args[:http_chunks]

	if File.exist?(filename)
		@size = File.size?(filename)
		if @size <= MappingThreshold
			stream_without_mapping filename
		else
			stream_with_mapping filename
		end
	else
		fail "file not found"
	end
end

Instance Method Details

#stream_one_chunkObject



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/em/streamer.rb', line 71

def stream_one_chunk
	loop {
		if @position < @size
			if @connection.get_outbound_data_size > BackpressureLevel
				EventMachine::next_tick {stream_one_chunk}
				break
			else
				len = @size - @position
				len = ChunkSize if (len > ChunkSize)

				@connection.send_data( "#{len.to_s(16)}\r\n" ) if @http_chunks
				@connection.send_data( @mapping.get_chunk( @position, len ))
				@connection.send_data("\r\n") if @http_chunks

				@position += len
			end
		else
			@connection.send_data "0\r\n\r\n" if @http_chunks
			@mapping.close
			succeed
			break
		end
	}
end