[Gridflow-cvs] [svn] commit: r3293 - /trunk/format/main.rb

gridflow-cvs at artengine.ca gridflow-cvs at artengine.ca
Tue Mar 25 17:10:03 EDT 2008


Author: matju
Date: Tue Mar 25 17:10:03 2008
New Revision: 3293

Log:
removed classes [#in grid], [#out grid], EventIO. (the first two are now written in C++)

Modified:
    trunk/format/main.rb

Modified: trunk/format/main.rb
==============================================================================
--- trunk/format/main.rb (original)
+++ trunk/format/main.rb Tue Mar 25 17:10:03 2008
@@ -276,114 +276,6 @@
 	alias unpack unpack2
 end
 
-# adding event-driven IO to a Format class
-module EventIO
-	def read_wait?; !!@action; end
-	def initialize(*)
-		@acceptor = nil
-		@buffer = nil
-		@action = nil
-		@chunksize = nil
-		@rewind_redefined = false
-		@clock = Clock.new self
-		@delay = 100 # ms
-		super
-	end
-	def call() try_read end
-	def on_read(n,&action)
-		@action = action
-		@chunksize = n
-	end
-	def try_accept
-		#!@#$ use setsockopt(SO_REUSEADDR) here???
-		@acceptor.nonblock = true
-		@stream = @acceptor.accept
-		@stream.nonblock = true
-		@stream.sync = true
-		@clock.unset
-#		send_out 0, :accept # does not work
-	rescue Errno::EAGAIN
-	end
-	def try_read(dummy=nil)
-		n = @chunksize-(if @buffer then @buffer.length else 0 end)
-		t = @stream.read(n) # or raise EOFError
-		if not t
-			raise "heck" if not @stream.eof?
-			rewind
-			t = @stream.read(n) or raise "can't read any of #{n} bytes?"
-		end
-		if @buffer then @buffer << t else @buffer = t end
-		if @buffer.length == @chunksize
-			action,buffer = @action, at buffer
-			@action, at buffer = nil,""
-			@clock.unset
-			action.call buffer
-		end
-	rescue Errno::EAGAIN
-		post "read would block"
-	end
-	def raw_open_gzip_in(filename)
-		r,w = IO.pipe
-		if pid=fork
-			GridFlow.subprocesses[pid]=true
-			w.close
-			@stream = r
-		else
-			r.close
-			STDOUT.reopen w
-			STDIN.reopen filename, "r"
-			exec "gzip", "-dc"
-		end
-	end
-	def raw_open_gzip_out(filename)
-		r,w = IO.pipe
-		if pid=fork
-			GridFlow.subprocesses[pid]=true
-			r.close
-			@stream = w
-		else
-			w.close
-			STDIN.reopen r
-			STDOUT.reopen filename, "w"
-			exec "gzip", "-c"
-		end
-	end
-	def raw_open(mode,source,*args)
-		@raw_open_args = mode,source,*args
-		fmode = case mode
-			when :in; "r"
-			when :out; "w"
-			else raise "bad mode" end
-		@stream.close if @stream
-		case source
-		when :file
-			filename = args[0].to_s
-			filename = GridFlow.find_file filename if mode==:in
-			@stream = File.open filename, fmode
-		when :gzfile
-			filename = args[0].to_s
-			filename = GridFlow.find_file filename if mode==:in
-			if mode==:in then
-				raw_open_gzip_in filename
-			else
-				raw_open_gzip_out filename
-			end
-			def self.rewind
-				raw_open(*@raw_open_args)
-				@frame = 0
-			end unless @rewind_redefined
-			@rewind_redefined = true
-		else
-			raise "unknown access method '#{source}'"
-		end
-	end
-	def close
-		@acceptor.close if @acceptor
-		@stream.close if @stream
-		GridFlow.hunt_zombies
-	end
-end
-
 Format.subclass("#io:file",1,1) {
 	def self.new(mode,file)
 		file=file.to_s
@@ -396,176 +288,6 @@
 		h.new(*a)
 	end
 	@comment="format autodetection proxy"
-}
-
-=begin
-	This is the Grid format I defined:
-	1 uint8: 0x7f
-	4 uint8: "GRID" big endian | "grid" little endian
-	1 uint8: type {
-		number of bits in 8,16,32,64, plus one of: 1:unsigned 2:float
-		but float8,float16 are not allowed (!)
-	}
-	1 uint8: reserved (supported: 0)
-	1 uint8: number of dimensions N (supported: at least 0..4)
-	N uint32: number of elements per dimension D[0]..D[N-1]
-	raw data goes there.
-=end
-Format.subclass("#io:grid",1,1) {
-	include EventIO
-	install_rgrid 0
-	@comment = "GridFlow file format"
-	suffixes_are "grid"
-	# bits per value: 32 only
-	attr_accessor :bpv # Fixnum: bits-per-value
-	# endianness
-	# attr_accessor :endian # ENDIAN_LITTLE or ENDIAN_BIG
-	# IO or File
-	attr_reader :stream
-	# nil=headerful; array=assumed dimensions of received grids
-	#attr_accessor :headerless
-	def initialize(mode,source,*args)
-		super
-		@bpv = 32
-		@headerless = nil
-		@endian = OurByteOrder
-		raw_open mode,source,*args
-	end
-	# rewinding and starting
-	def frame
-		raise "can't get frame when there is no connection" if not @stream
-		raise "already waiting for input" if read_wait?
-		return false if eof?
-		if @headerless then
-			@n_dim=@headerless.length
-			@dim = @headerless
-			@dex = 0
-			set_bufsize
-			send_out_grid_begin 0, @dim
-			on_read(bufsize) {|data| frame3 data }
-		else
-			on_read(8) {|data| frame1 data }
-		end
-		try_read nil while read_wait?
-		super
-	end
-	def set_bufsize
-		@prod = 1
-		@dim.each {|x| @prod *= x }
-		n = @prod/@dim[0]
-		k = GridFlow.max_packet / n
-		k=1 if k<1
-		@bufsize = k*n*@bpv/8
-		@bufsize = @prod if @bufsize > @prod
-	end
-	# the header
-	def frame1 data
-		head, at bpv,reserved, at n_dim = data.unpack "a5ccc"
-		@endian = case head
-			when "\x7fGRID"; ENDIAN_BIG
-			when "\x7fgrid"; ENDIAN_LITTLE
-			else raise "grid header: invalid (#{data.inspect})" end
-		case bpv
-		when 8, 16, 32; # ok
-		else raise "unsupported bpv (#{@bpv})"
-		end
-		if reserved!=0
-			raise "reserved field is not zero"
-		end
-		if @n_dim > GridFlow.max_rank
-			raise "too many dimensions (#{@n_dim})"
-		end
-		on_read(4*@n_dim) {|data| frame2 data }
-	end
-	# the dimension list
-	def frame2 data
-		@dim = data.unpack(if @endian==ENDIAN_LITTLE then "V*" else "N*" end)
-		set_bufsize
-		if @prod > GridFlow.max_size
-			raise "dimension list: invalid prod (#{@prod})"
-		end
-		send_out_grid_begin 0, @dim, @cast
-
-		on_read(bufsize) {|data| frame3 data }
-		@dex = 0
-	end
-	attr_reader :bufsize
-	# for each slice of the body
-	def frame3 data
-		n = data.length
-		nn = n*8/@bpv
-		# is/was there a problem with the size of the data being read?
-		case @bpv
-		when 8
-			@bp = BitPacking.new(@endian,1,[0xff])
-			send_out_grid_flow(0, @bp.unpack(data))
-			@dex += data.length
-		when 16
-			@bp = BitPacking.new(@endian,2,[0xffff])
-			send_out_grid_flow(0, @bp.unpack(data))
-			@dex += data.length/2
-		when 32
-			data.swap32! if @endian!=OurByteOrder
-			send_out_grid_flow 0, data
-			@dex += data.length/4
-		end
-		if @dex >= @prod
-			@clock.unset
-		else
-			on_read(bufsize) {|data| frame3 data }
-		end
-	end
-	def _0_rgrid_begin
-		if not @stream
-			raise "can't send frame when there is no connection"
-		end
-		@dim = inlet_dim 0
-		return if @headerless
-		# header
-		@stream.write(
-			[if @endian==ENDIAN_LITTLE then "\x7fgrid" else "\x7fGRID" end,
-			 @bpv,0, at dim.length].pack("a5ccc"))
-		# dimension list
-		@stream.write(
-			@dim.to_a.pack(if @endian==ENDIAN_LITTLE then "V*" else "N*" end))
-	end
-	def _0_rgrid_flow data
-		case @bpv
-		when 8, 16
-			@stream.write @bp.pack(data)
-		when 32
-			data.swap32! if GridFlow::OurByteOrder != @endian
-			@stream.write data
-		end
-	end
-	def _0_rgrid_end; @stream.flush end
-	def endian(a)
-		@endian = case a
-		when :little; ENDIAN_LITTLE
-		when :big;    ENDIAN_BIG
-		when :same;   ENDIAN_SAME
-		else raise "argh"
-		end
-	end
-	def headerless(*args)
-		args=args[0] if Array===args[0]
-		args.map! {|a|
-			Numeric===a or raise "expecting dimension list..."
-			a.to_i
-		}
-		@headerless = args
-	end
-	def headerful; @headerless = nil end
-	#!@#$ method name conflict ?
-	def type(nt)
-		#!@#$ bug: should not be able to modify this _during_ a transfer
-		case nt
-		when :uint8; @bpv= 8; @bp=BitPacking.new(ENDIAN_LITTLE,1,[0xff])
-		when :int16; @bpv=16; @bp=BitPacking.new(ENDIAN_LITTLE,1,[0xffff])
-		when :int32; @bpv=32; @bp=nil
-		else raise "unsupported number type"
-		end
-	end
 }
 
 =begin



More information about the Gridflow-cvs mailing list