Class: NetworkClipboard::ConnectionWrapper
- Inherits:
-
Object
- Object
- NetworkClipboard::ConnectionWrapper
- Defined in:
- lib/network_clipboard/client.rb
Instance Method Summary collapse
-
#initialize(client, connection) ⇒ ConnectionWrapper
constructor
A new instance of ConnectionWrapper.
- #join ⇒ Object
- #read_loop ⇒ Object
- #stop ⇒ Object
- #write_loop ⇒ Object
Constructor Details
#initialize(client, connection) ⇒ ConnectionWrapper
Returns a new instance of ConnectionWrapper.
14 15 16 17 18 19 20 21 |
# File 'lib/network_clipboard/client.rb', line 14 def initialize(client,connection) @client = client @connection = connection @read_thread = Thread.new{read_loop} @write_thread = Thread.new{write_loop} @running = true @value = nil end |
Instance Method Details
#join ⇒ Object
53 54 55 56 |
# File 'lib/network_clipboard/client.rb', line 53 def join @read_thread.join @write_thread.join end |
#read_loop ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/network_clipboard/client.rb', line 23 def read_loop while @running begin new_value = @connection.receive() rescue DisconnectedError if @running LOGGER.error("Client #{@connection.remote_client_id} went away") @running = false end break end next if @value == new_value LOGGER.info("Received new clipboard value from #{@connection.remote_client_id}") Clipboard.copy(@value = new_value) end @connection.close_read LOGGER.debug("Read loop completed") end |
#stop ⇒ Object
58 59 60 |
# File 'lib/network_clipboard/client.rb', line 58 def stop @running = false end |
#write_loop ⇒ Object
42 43 44 45 46 47 48 49 50 51 |
# File 'lib/network_clipboard/client.rb', line 42 def write_loop while @running new_value = Clipboard.paste (sleep(2); next) if new_value.nil? or new_value.empty? or @value == new_value LOGGER.info("Sending clipboard value to #{@connection.remote_client_id}") @connection.send(@value = new_value) end @connection.close_write LOGGER.debug("Write loop completed") end |