Class: NREPL::Server

Inherits:
Object
  • Object
show all
Defined in:
lib/nrepl/server.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(port: DEFAULT_PORT, host: DEFAULT_HOST, debug: false) ⇒ Server



20
21
22
23
24
25
26
# File 'lib/nrepl/server.rb', line 20

def initialize(port: DEFAULT_PORT, host: DEFAULT_HOST, debug: false)
  @port  = port
  @host  = host
  @debug = debug
  @connections = Set.new
  NREPL.class_variable_set(:@@connections, @connections)
end

Instance Attribute Details

#debugObject (readonly) Also known as: debug?

Returns the value of attribute debug.



13
14
15
# File 'lib/nrepl/server.rb', line 13

def debug
  @debug
end

#hostObject (readonly)

Returns the value of attribute host.



13
14
15
# File 'lib/nrepl/server.rb', line 13

def host
  @host
end

#portObject (readonly)

Returns the value of attribute port.



13
14
15
# File 'lib/nrepl/server.rb', line 13

def port
  @port
end

Class Method Details

.start(**kwargs) ⇒ Object



16
17
18
# File 'lib/nrepl/server.rb', line 16

def self.start(**kwargs)
  new(**kwargs).start
end

Instance Method Details

#startObject



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/nrepl/server.rb', line 34

def start
  puts "nREPL server started on port #{port} on host #{host} - nrepl://#{host}:#{port}"
  puts "Running in debug mode" if debug?
  record_port

  $stdout = FakeStdout.new(@connections, STDOUT, "out")
  $stderr = FakeStdout.new(@connections, STDERR, "err")

  Signal.trap("INT") { stop }
  Signal.trap("TERM") { stop }

  s = TCPServer.new(host, port)
  loop do
    Thread.start(s.accept) do |client|
      connection = Connection.new(client, debug: debug?)
      @connections << connection
      connection.treat_messages!
      @connections.delete(connection)
    end
  end
ensure
  File.unlink(PORT_FILENAME)
end

#stopObject



58
59
60
61
# File 'lib/nrepl/server.rb', line 58

def stop
  Thread.exit
  exit(0)
end