Class: NREPL::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/nrepl-lazuli/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(port: DEFAULT_PORT, host: DEFAULT_HOST) ⇒ Client

Returns a new instance of Client.



10
11
12
13
14
15
16
# File 'lib/nrepl-lazuli/client.rb', line 10

def initialize(port: DEFAULT_PORT, host: DEFAULT_HOST)
  @port       = port
  @host       = host
  @socket = TCPSocket.new(@host, @port)
  @bencode = BEncode.new(@socket)
  @last_id = 0
end

Instance Attribute Details

#hostObject (readonly)

Returns the value of attribute host.



8
9
10
# File 'lib/nrepl-lazuli/client.rb', line 8

def host
  @host
end

#portObject (readonly)

Returns the value of attribute port.



8
9
10
# File 'lib/nrepl-lazuli/client.rb', line 8

def port
  @port
end

Instance Method Details

#closed?Boolean

Returns:

  • (Boolean)


62
63
64
# File 'lib/nrepl-lazuli/client.rb', line 62

def closed?
  @socket.nil?
end

#readObject



50
51
52
# File 'lib/nrepl-lazuli/client.rb', line 50

def read
  @bencode.parse!
end

#register_sessionObject

Raises:

  • (ArgumentError)


54
55
56
57
58
59
60
# File 'lib/nrepl-lazuli/client.rb', line 54

def register_session
  write('op' => 'clone')
  msg = read!

  raise ArgumentError, "failed to create session" unless msg['new_session']
  msg['new_session']
end

#rpc(msg) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/nrepl-lazuli/client.rb', line 26

def rpc(msg)
  id = msg["id"] || msg[:id]
  if(!id)
    @last_id += 1
    id = "autogen_#@last_id"
    msg = msg.merge('id' => id)
  end

  write(msg)
  Timeout.timeout(10) do
    loop do
      ret = read
      if(ret["id"] == id)
        break ret
      end
    end
  end
end

#stopObject



18
19
20
21
22
23
24
# File 'lib/nrepl-lazuli/client.rb', line 18

def stop
  unless closed?
    @socket.close
    @socket = nil
    @bencode = nil
  end
end

#write(msg) ⇒ Object



45
46
47
48
# File 'lib/nrepl-lazuli/client.rb', line 45

def write(msg)
  @socket.write(BEncode.encode(msg))
  @socket.flush
end