Class: NREPL::Server

Inherits:
Object
  • Object
show all
Defined in:
lib/nrepl-lazuli/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, binding: nil, pwd: Dir.pwd, tracing: true) ⇒ Server

Returns a new instance of Server.



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

def initialize(
  port: DEFAULT_PORT,
  host: DEFAULT_HOST,
  debug: false,
  binding: nil,
  pwd: Dir.pwd,
  tracing: true
)
  @port  = port
  @pwd = pwd
  @host  = host
  @debug = debug
  @connections = Set.new
  @binding = binding
  @bindings = {}
  @bindings_by_id = {}
  @tracing = tracing
  Thread.current[:nrepl_server] = self
  NREPL.class_variable_set(:@@connections, @connections)
  NREPL.class_variable_set(:@@bindings, @bindings)
  NREPL.class_variable_set(:@@bindings_by_id, @bindings_by_id)
end

Instance Attribute Details

#debugObject (readonly) Also known as: debug?

Returns the value of attribute debug.



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

def debug
  @debug
end

#hostObject (readonly)

Returns the value of attribute host.



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

def host
  @host
end

#portObject (readonly)

Returns the value of attribute port.



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

def port
  @port
end

Class Method Details

.bind!(binding, **kwargs) ⇒ Object



28
29
30
# File 'lib/nrepl-lazuli/server.rb', line 28

def self.bind!(binding, **kwargs)
  new(**kwargs.merge(binding: binding)).start
end

.spawn(args) ⇒ Object



16
17
18
19
20
21
22
# File 'lib/nrepl-lazuli/server.rb', line 16

def self.spawn(args)
  t = Thread.new {
    new(**args).start
  }
  sleep 0.1
  t[:nrepl_server]
end

.start(**kwargs) ⇒ Object



24
25
26
# File 'lib/nrepl-lazuli/server.rb', line 24

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

Instance Method Details

#auto_create_bindings!Object



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/nrepl-lazuli/server.rb', line 96

def auto_create_bindings!
  dir_regex = Regexp.new("^#{Regexp.escape(@pwd)}")
  @call_trace = TracePoint.new(:class, :call) do |tp|
    id = "#{tp.path}:#{tp.lineno}"
    b = tp.binding
    @bindings_by_id[id] = {binding: b, file: tp.path, row: tp.lineno-1}
    @bindings[tp.path] ||= {}
    @bindings[tp.path][tp.lineno-1] ||= {}
    @bindings[tp.path][tp.lineno-1][id] = b
    if tp.path =~ dir_regex
      @connections.each do |connection|
        connection.send_msg(
          'op' => 'hit_auto_watch',
          'file' => tp.path,
          'line' => tp.lineno-1,
          'status' => ['done']
        )
      end
    end
  end
  @call_trace.enable

  @ex_trace = TracePoint.new(:raise) do |tp|
    e = tp.raised_exception
    @@last_exception = [:error, e.inspect.sub(/\>$/, ' stack=' + e.backtrace.inspect+'>')]
  end
  @ex_trace.enable
end

#startObject



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/nrepl-lazuli/server.rb', line 61

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

  @old_out, @old_err = $stdout, $stderr
  $stdout = FakeStdout.new(@connections, STDOUT, "out")
  $stderr = FakeStdout.new(@connections, STDERR, "err")
  auto_create_bindings! if @tracing

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

  @socket = TCPServer.new(host, port)
  loop do
    break if @socket.closed?
    Thread.start(@socket.accept) do |client|
      connection = Connection.new(
        client,
        debug: debug?,
        binding: @binding,
        bindings_by_id: @bindings_by_id,
        bindings: @bindings
      )
      @connections << connection
      connection.treat_messages!
      @connections.delete(connection)
    end
  end
rescue IOError
  puts "Server closed" if debug?
ensure
  File.unlink(PORT_FILENAME)
end

#stopObject



125
126
127
128
129
# File 'lib/nrepl-lazuli/server.rb', line 125

def stop
  @connections = []
  $stdout, $stderr = @old_out, @old_err
  @socket.close
end