class Tkar::MessageStream::Ascii

Translate ascii command text to ruby method calls.

Constants

ARG_CONVERSION
NORMALIZE

Public Instance Methods

conv_angle(s) click to toggle source
    # File lib/tkar/stream.rb
 97 def conv_angle(s)
 98   r = flip_Float(s)
 99   @radians ? r : r * DEGREES_TO_RADIANS
100 end
conv_param(s) click to toggle source
   # File lib/tkar/stream.rb
88 def conv_param(s)
89    s.slice!(/\.0+$/) # so that floats can be used for colors
90   (Integer(s) rescue Float(s)) rescue s
91 end
flip_Float(s) click to toggle source
   # File lib/tkar/stream.rb
93 def flip_Float(s)
94   @flip ? -Float(s) : Float(s)
95 end
get_cmd() click to toggle source

Returns next command in pipe, in form [:meth, arg, arg, ...].

    # File lib/tkar/stream.rb
149 def get_cmd
150   begin
151     cmdline = get_line
152     raise StreamClosed, "Session ended" unless cmdline ## ?
153   end while cmdline =~ /^\s*(#|$)/
154   parse_cmd(cmdline)
155 rescue TryAgain
156   retry
157 rescue SystemCallError => ex
158   $stderr.puts ex.class, ex.message
159   raise StreamClosed, "Session ended"
160 end
get_line() click to toggle source
    # File lib/tkar/stream.rb
130 def get_line
131   line = @fd_in.gets
132   while line == nil and not @fd_in_stack.empty?
133     @fd_in = @fd_in_stack.pop
134     line = @fd_in.gets
135   end
136   if line
137     while line =~ /(.*)\\\r?$/ # to allow continuation of long lines
138       next_line = @fd_in.gets
139       break unless next_line
140       line = $1 + next_line
141     end
142   end
143   line
144 end
parse_cmd(cmdline) click to toggle source
    # File lib/tkar/stream.rb
162 def parse_cmd cmdline
163   $stderr.puts cmdline if @verbose
164   cmd, *args = cmdline.split
165   cmd = NORMALIZE[cmd] || (raise ArgumentError, "Bad command: #{cmd}")
166   
167   case cmd
168   when "param"
169     return [cmd, Integer(args.shift), Integer(args.shift),
170       conv_param(args.join(" "))]
171     ## hacky, and loses multiple spaces
172   when "title"
173     return [cmd, args.join(" ")]
174   when "echo"
175     str = args.join(" ")
176     ## hacky, and loses multiple spaces
177     put_msg(str)
178     raise TryAgain # hm...
179   when "load"
180     filename = args.join(" ")
181     ## hacky, and loses multiple spaces
182     begin
183       new_fd_in = File.open(filename)
184     rescue Errno::ENOENT # if not absolute, try local
185       raise unless @fd_in_dir
186       new_fd_in = File.open(File.join(@fd_in_dir, filename))
187     end
188     @fd_in_dir ||= File.dirname(new_fd_in.path)
189     @fd_in_stack.push(@fd_in)
190     @fd_in = new_fd_in
191     raise TryAgain # hm...
192   end
193   
194   conv = ARG_CONVERSION[cmd]
195   unless conv
196     raise "No argument conversion for command #{cmd.inspect}"
197   end
198   i = -1; last_i = conv.length - 1
199   args.map! do |arg|
200     i += 1 unless i == last_i # keep using the last conversion thereafter
201     (c = conv[i]) ? send(c, arg) : arg
202   end
203   [cmd, *args]
204 end
put_msg(msg) click to toggle source
    # File lib/tkar/stream.rb
206 def put_msg(msg)
207   @fd_out_mutex.synchronize do ## why necessary?
208     @fd_out.puts msg
209   end
210   @fd_out.flush
211 rescue Errno::ECONNABORTED
212 end