Class: Plc::Emulator::EmuPlcServer

Inherits:
Object
  • Object
show all
Defined in:
lib/plc/emulator/emu_plc_server.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config = {}) ⇒ EmuPlcServer

Returns a new instance of EmuPlcServer.



43
44
45
46
# File 'lib/plc/emulator/emu_plc_server.rb', line 43

def initialize config = {}
  @port = config[:port] || 5555
  @plc = EmuPlc.new
end

Class Method Details

.launchObject



33
34
35
36
37
38
39
# File 'lib/plc/emulator/emu_plc_server.rb', line 33

def launch
  @server ||= begin
    server = new
    server.run
    server
  end
end

Instance Method Details

#runObject



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/plc/emulator/emu_plc_server.rb', line 48

def run
  @plc.run
  Thread.new do
    server = TCPServer.open @port
    puts "launching emulator ... "
    loop do
      socket = server.accept
      puts "done launching"
      while line = socket.gets
        begin
          r = @plc.execute_console_commands line
          socket.puts r
        rescue => e
          socket.puts "E0 #{e}\r"
        end
      end
    end
  end
end