среда, 19 декабря 2007 г.

Простотак.

этот язык просто не может перестать нравиться мне все больше и больше! druby server:
require 'drb/drb'

# The URI for the server to connect to
URI="druby://localhost:8787"

class Server
  def initialize
    @msgs = []
  end
  def get_current_time
    return Time.now
  end
  def post_msg(msg)
    @msgs.push msg
  end
  def read_msgs
    return @msgs
  end
  def exec_on_srv( n )
    if @msgs[n]
      str = @msgs[n]
      str.untaint
      eval str
    end
  end
end

FRONT_OBJECT=Server.new
$SAFE = 1   # disable eval() and friends
DRb.start_service(URI, FRONT_OBJECT)
DRb.thread.join
client:
require 'drb/drb'

# The URI to connect to
SERVER_URI="druby://localhost:8787"
DRb.start_service

server = DRbObject.new_with_uri(SERVER_URI)
puts server.get_current_time

answer = ''
while answer != 'quit'
  print "cmd: "
  answer = gets.chomp
  tokens = answer.split
  cmd = tokens.shift
  if cmd == "msg"
    server.post_msg tokens.join(' ')
  elsif cmd == "read"
    arr = server.read_msgs
    n = tokens.first.to_i
    if n > 0
      puts "#{n} #{arr[n-1]}"
    else
      puts "msgs: "
      arr.each_index{ |c|
        puts "#{c+1} #{arr[c]}"
      }
      puts "total #{arr.size}"
    end
  elsif cmd == "exec"
    arr = server.read_msgs
    n = tokens.first.to_i
    if n > 0
      begin
        eval arr[n-1].to_s
      rescue
        puts "exec error: #{$!}"
      end
    else
      puts "statement number needed"
    end
  elsif cmd == "exec_on_srv"
    n = tokens.first.to_i
    if n > 0
      server.exec_on_srv(n-1)
    else
      puts "wrong or missing parametr"
    end
  end
end

Комментариев нет: