Class: NREPL::Connection

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

Constant Summary collapse

@@debug_counter =
0

Instance Method Summary collapse

Constructor Details

#initialize(input, debug: false, out: input, watches: NREPL.class_variable_get(:@@watches)) ⇒ Connection



5
6
7
8
9
10
11
12
# File 'lib/nrepl/connection.rb', line 5

def initialize(input, debug: false, out: input, watches: NREPL.class_variable_get(:@@watches))
  @debug = debug
  @in = input
  @out = out
  @pending_evals = {}
  @watches = watches
  @counter = 0
end

Instance Method Details

#send_exception(msg, e) ⇒ Object



205
206
207
# File 'lib/nrepl/connection.rb', line 205

def send_exception(msg, e)
  send_msg(response_for(msg, { 'ex' => e.message, 'status' => ['done', 'error'] }))
end

#send_msg(msg) ⇒ Object



209
210
211
212
213
# File 'lib/nrepl/connection.rb', line 209

def send_msg(msg)
  debug "Sending", msg
  @out.write(msg.bencode)
  @out.flush
end

#treat_messages!Object



14
15
16
17
18
19
20
21
22
23
24
# File 'lib/nrepl/connection.rb', line 14

def treat_messages!
  bencode = BEncode::Parser.new(@in)
  loop do
    break if @in.eof?
    msg = bencode.parse!
    debug "Received", msg
    next unless msg
    treat_msg(msg)
  end
  @pending_evals.each { |(i, _)| clear_eval!(i) }
end

#treat_msg(msg) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
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
# File 'lib/nrepl/connection.rb', line 26

def treat_msg(msg)
  case msg['op']
  when 'clone'
    register_session(msg)
  when 'describe'
    describe_msg(msg)
  when 'eval'
    eval_op(msg, false)
  when 'eval_pause'
    eval_op(msg, true)
  when 'eval_resume'
    msg['id'] ||= "eval_#{@counter += 1}"
    stop_id = msg['stop_id']
    clear_eval!(stop_id)

    send_msg(response_for(msg, {
      'status' => ['done'],
      'op' => msg['op']
    }))
  when 'unwatch'
    msg['id'] ||= "eval_#{@counter += 1}"
    watch_id = msg['watch_id']
    @watches.delete(watch_id)

    send_msg(response_for(msg, {
      'status' => ['done'],
      'op' => msg['op']
    }))
  when 'interrupt'
    id = if(msg['interrupt-id'])
      msg['interrupt-id']
    else
      @pending_evals.keys.first
    end
    pending = @pending_evals[id] || {}
    thread = pending[:thread]
    msg['id'] ||= (id || 'unknown')

    if(thread)
      thread.kill
      clear_eval!(id)
      send_msg(response_for(msg, {
        'status' => ['done', 'interrupted'],
        'op' => msg['op']
      }))

    else
      send_msg(response_for(msg, {
        'status' => ['done'],
        'op' => msg['op']
      }))
    end

  else
    send_msg(response_for(msg, {
      'op' => msg['op'],
      'status' => ['done', 'error'],
      'error' => "unknown operation: #{msg['op'].inspect}"
    }))
  end
end