class Plc::Emulator::EmuPlcServer

Attributes

config[R]

Public Class Methods

launch(config={}) click to toggle source
# File lib/plc/emulator/emu_plc_server.rb, line 35
def launch config={}
  @server ||= begin
    server = new config
    server.run
    server
  end
end
new(config = {}) click to toggle source
# File lib/plc/emulator/emu_plc_server.rb, line 45
def initialize config = {}
  @port = config[:port] || 5555
  @plc = EmuPlc.new config
end

Public Instance Methods

run() click to toggle source
# File lib/plc/emulator/emu_plc_server.rb, line 50
def run
  @plc.run
  Thread.new do
    server = TCPServer.open @port
    puts "launching emulator ... "
    launched = false
    loop do
      Thread.start(server.accept) do |socket|
        puts "done launching" unless launched
        launched ||= true
        while line = socket.gets
          begin
            r = @plc.execute_console_commands line
            socket.puts r
          rescue => e
            socket.puts "E0 #{e}\r\n"
          end
        end
      end
    end
  end
end