RubyConf 2006 Lightning Talk

Here's the code for my lightning talk. First my drb session:

irb(main):001:0> DRbObject.new_with_uri("druby://localhost:9000").write('the best!', 'green')
=> 1
irb(main):002:0>

The Rails controller:

require 'drb'

class BetaBriteController < ApplicationController
  def index
    if params[:q]
      Thread.new(params[:q], params[:c]) { |q,c|
        obj = DRbObject.new_with_uri("druby://localhost:9000")
        obj.write(q, c || 'red')
      }
    end
  end
end


Finally, the DRb server:

$SAFE = 1

require 'drb'
require 'serialport'
require 'monitor'

require 'rubygems'
require 'betabrite'

class Server < Monitor
  def write(string, color)
    synchronize do

      # Open the serial port
      sp = SerialPort.new('/dev/tty.PL2303-151', 9600, 8, 1, SerialPort::NONE)

      BetaBrite.new { |bb|
        # Set up a string file labeled '1'
        bb.add BetaBrite::StringFile.new('1') { |s|

          # Add the string to the file
          s.write BetaBrite::String.new(string) { |a|
            a.set_color color
          }
        }

      # Write to the serial port.
      }.write { |text|
        sp.write text
      }
      sleep 1
    end
  end
end

server = Server.new
DRb.start_service('druby://localhost:9000', server)
DRb.thread.join

Post a Comment

Your email is never shared. Required fields are marked *

*
*
Check Spelling
Activate Spell Check while Typing