Class: TextProtocols::Server

Inherits:
GServer
  • Object
show all
Defined in:
lib/text_protocols.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(bind, port, block) ⇒ Server

Returns a new instance of Server.



40
41
42
43
44
45
46
47
48
49
# 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

Instance Attribute Details

#bindObject (readonly)

Returns the value of attribute bind.



34
35
36
# File 'lib/text_protocols.rb', line 34

def bind
  @bind
end

#portObject (readonly)

Returns the value of attribute port.



34
35
36
# File 'lib/text_protocols.rb', line 34

def port
  @port
end

#protocolObject (readonly)

Returns the value of attribute protocol.



34
35
36
# File 'lib/text_protocols.rb', line 34

def protocol
  @protocol
end

Class Method Details

.config(port, bind, &block) ⇒ Object



36
37
38
# File 'lib/text_protocols.rb', line 36

def self.config port, bind, &block
  server = self.new bind, port, block
end

Instance Method Details

#parse_request(request) ⇒ Object



65
66
67
68
69
70
71
72
73
# 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) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
# 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