class MonoTalk::Server

Public Class Methods

new(servicename,routine) click to toggle source
# File lib/module/server.rb, line 8
def initialize(servicename,routine)
  @io = STDERR
  @servicename = servicename.to_sym
  @routine = routine

  @io.puts Color.cyan "Command List"
  @routine.each do |key,value|
    @io.puts "#{key} -> #{value}"
  end
end

Public Instance Methods

accept() click to toggle source
# File lib/module/server.rb, line 19
def accept
  @server = TCPServer.open(Mono::PORT[@servicename])
  @io.puts 'Acception start, waiting for data...'
  @sock = @server.accept
  @server.close
  begin
    hash = JSON.parse @sock.gets
    @io.puts hash
    @sock.write routing(hash)
  rescue => e
    @io.puts Color.red 'GIVE UP! SOMETHING BAD HAPPEN!'
    @io.puts Color.yellow hash
    @io.puts Color.magenta e 
  ensure
    @sock.close
    @io.puts 'Socket was closed.'
  end
end
close() click to toggle source
# File lib/module/server.rb, line 55
def close
  @server.close
  @io.puts 'Server closed.'
end
routing(hash) click to toggle source
# File lib/module/server.rb, line 50
def routing(hash)
  return "Undefined command, ABORT -> #{hash['command']}" unless @routine[hash['command']]
  return @routine[hash['command']].call(hash['params'])
end
test(hash) click to toggle source
# File lib/module/server.rb, line 38
def test(hash)
  @io.puts Color.yellow "Starting test..."
  @server = TCPServer.open(Mono::PORT[@servicename])
  STDERR.puts 'Acception start, waiting for data...'
  @server.close
  @io.puts hash
  ret = routing(hash)
  @io.puts ret
  @io.puts 'Test was closed.'
  return ret
end