Changeset 345

Show
Ignore:
Timestamp:
06/01/07 08:30:16 (2 years ago)
Author:
blackhedd
Message:

Added EventMachine#next_tick, and associated tests.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • version_0/ChangeLog

    r342 r345  
    292931May07: Added a preliminary implementation of EventMachine#popen. 
    303001Jun07: Added EM, a "pseudo-alias" for EventMachine. 
     3101Jun07: Added EM#next_tick. 
  • version_0/lib/eventmachine.rb

    r344 r345  
    803803  end 
    804804 
    805   # A wrapper over the setuid system call. Particularly useful when opening a network 
    806   # server on a privileged port because you can use this call to drop privileges 
    807   # after opening the port. 
    808   # This method has no effective implementation on Windows or in the pure-Ruby 
    809   # implementation of EventMachine. 
    810   # Call #set_effective_user by passing it a string containing the effective name 
    811   # of the user whose privilege-level your process should attain. 
    812   # This method is intended for use in enforcing security requirements, consequently 
    813   # it will throw a fatal error and end your program if it fails. 
    814   def self::set_effective_user username 
    815       EventMachine::setuid_string username 
    816   end 
    817  
    818   #-- 
    819   # 
    820   def self::popen cmd, mode="r", handler=nil 
    821         klass = if (handler and handler.is_a?(Class)) 
    822                 handler 
    823         else 
    824                 Class.new( Connection ) {handler and include handler} 
    825         end 
    826  
    827         s = invoke_popen cmd, mode 
    828         c = klass.new s 
    829         @conns[s] = c 
    830         yield(c) if block_given? 
    831         c 
    832   end 
     805        # A wrapper over the setuid system call. Particularly useful when opening a network 
     806        # server on a privileged port because you can use this call to drop privileges 
     807        # after opening the port. 
     808        # This method has no effective implementation on Windows or in the pure-Ruby 
     809        # implementation of EventMachine. 
     810        # Call #set_effective_user by passing it a string containing the effective name 
     811        # of the user whose privilege-level your process should attain. 
     812        # This method is intended for use in enforcing security requirements, consequently 
     813        # it will throw a fatal error and end your program if it fails. 
     814        # 
     815        def self::set_effective_user username 
     816                EventMachine::setuid_string username 
     817        end 
     818 
     819 
     820 
     821        # Schedules a proc for execution immediately after the next "turn" through the reactor 
     822        # core. An advanced technique, this can be useful for improving memory management and/or 
     823        # application responsiveness, especially when scheduling large amounts of data for 
     824        # writing to a network connection. TODO, we need a FAQ entry on this subject. 
     825        # 
     826        # #next_tick takes either a single argument (which must be a Proc) or a block. 
     827        # And I'm taking suggestions for a better name for this method. 
     828        # 
     829        def self::next_tick pr=nil, &block 
     830                raise "no argument or block given" unless ((pr && pr.respond_to?(:call)) or block) 
     831                (@next_tick_procs ||= []) << (pr || block) 
     832                if @next_tick_procs.length == 1 
     833                        add_timer(0) { 
     834                                @next_tick_procs.each {|t| t.call} 
     835                                @next_tick_procs.clear 
     836                        } 
     837                end 
     838        end 
     839 
     840        # TODO, must document popen. At this moment, it's only available on Unix, and it's half-duplex. 
     841        # Both of these limitations are expected to go away. 
     842        #-- 
     843        # 
     844        def self::popen cmd, mode="r", handler=nil 
     845                klass = if (handler and handler.is_a?(Class)) 
     846                        handler 
     847                else 
     848                        Class.new( Connection ) {handler and include handler} 
     849                end 
     850 
     851                s = invoke_popen cmd, mode 
     852                c = klass.new s 
     853                @conns[s] = c 
     854                yield(c) if block_given? 
     855                c 
     856        end 
     857 
     858 
    833859 
    834860  private 
  • version_0/Rakefile

    r339 r345  
    214214end 
    215215 
     216desc "Test Next Tick" 
     217task :test_next_tick do |t| 
     218  run_tests t, :extension, "test_next_tick*.rb" 
     219end 
     220 
    216221 
    217222desc "Build everything"