Class: PackServ::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/packserv/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(proto = nil) ⇒ Client

Returns a new instance of Client.



5
6
7
8
9
10
11
12
13
14
# File 'lib/packserv/client.rb', line 5

def initialize(proto = nil)
  @proto = proto || DefaultProtocol

  @handler = ->(_) {}
  @event_queue = Queue.new
  @response_queue = Queue.new
  @outgoing_queue = Queue.new

  @threads = ThreadGroup.new
end

Instance Attribute Details

#handlerObject

Returns the value of attribute handler.



3
4
5
# File 'lib/packserv/client.rb', line 3

def handler
  @handler
end

Instance Method Details

#connect(host, port) ⇒ Object



16
17
18
19
20
21
22
23
# File 'lib/packserv/client.rb', line 16

def connect(host, port)
  @server = TCPSocket.new(host, port)

  @threads.add(Thread.new { _connect(@server) })
  @threads.add(Thread.new { dispatch_events })

  self
end

#disconnectObject



25
26
27
28
# File 'lib/packserv/client.rb', line 25

def disconnect
  @threads.list.map(&:exit)
  @server.close
end

#send(obj) ⇒ Object



30
31
32
33
34
# File 'lib/packserv/client.rb', line 30

def send(obj)
  @outgoing_queue.push(obj)

  @response_queue.pop
end