Class: Plc::Emulator::EmuPlcServer

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config = {}) ⇒ EmuPlcServer

Returns a new instance of EmuPlcServer.



45
46
47
48
# File 'lib/plc/emulator/emu_plc_server.rb', line 45

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

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



31
32
33
# File 'lib/plc/emulator/emu_plc_server.rb', line 31

def config
  @config
end

Class Method Details

.launch(config = {}) ⇒ Object



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

def launch config={}
  @server ||= begin
    server = new config
    server.run
    server
  end
end

Instance Method Details

#runObject



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# 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