class TextProtocols::Server

Attributes

bind[R]
port[R]
protocol[R]

Public Class Methods

config(port, bind, &block) click to toggle source
# File lib/text_protocols.rb, line 36
def self.config port, bind, &block
  server = self.new bind, port, block
end
new(bind, port, block) click to toggle source
Calls superclass method
# File lib/text_protocols.rb, line 40
def initialize bind, port, block
  @bind     = bind
  @port     = port
  @protocol = Protocol.new 
  @protocol.instance_eval &block
  super(port, bind)
  
  puts "Text Protocols #{TextProtocols::VERSION} - PID: #{Process.pid.to_s}, Port: #{port}" 
  $stdout.flush
end

Public Instance Methods

parse_request(request) click to toggle source
# File lib/text_protocols.rb, line 65
def parse_request request
  parts = request.split
  params = {}
  parts[1..-1].each do |e| 
    k, v = e.split '='
    params[k.to_sym] = v
  end
  [parts[0], params]
end
serve(io) click to toggle source
# File lib/text_protocols.rb, line 51
def serve io
  loop do
    request = io.readline
    
    if request
      command, params = parse_request request
      io.puts @protocol.handle command, params
    else
      io.close
      break
    end
  end
end