class NetworkClipboard::Client

Public Class Methods

new() click to toggle source
# File lib/network_clipboard/client.rb, line 71
def initialize
  @config = Config.new
  @discovery = Discovery.new(@config)

  @tcp_server = TCPServer.new(@config.port)

  @connections = {}
  @connections_mutex = Mutex.new

  @running = true
end
run() click to toggle source
# File lib/network_clipboard/client.rb, line 65
def self.run
  c = Client.new
  Signal.trap('INT'){c.stop}
  c.loop
end

Public Instance Methods

announce_loop() click to toggle source
# File lib/network_clipboard/client.rb, line 96
def announce_loop
  while @running
    LOGGER.debug("Announcing")
    @discovery.announce
    LOGGER.debug("Announced")
    sleep(15)
  end
end
discover_loop() click to toggle source
# File lib/network_clipboard/client.rb, line 105
def discover_loop
  while @running
    LOGGER.debug("Discovering")
    remote_client_id,address = @discovery.get_peer_announcement
    LOGGER.debug("Found #{remote_client_id} on #{address}")

    @connections_mutex.synchronize do
      next if @connections[remote_client_id]

      aes_connection = AESConnection.new(@config,TCPSocket.new(address,@config.port))

      if aes_connection.remote_client_id != remote_client_id
        LOGGER.error("Client Id #{aes_connection.remote_client_id} doesn't match original value #{remote_client_id}")
        aes_connection.close
        next
      end
        
      if @connections[aes_connection.remote_client_id]
        LOGGER.error("Duplicate connections #{aes_connection} and #{@connections[aes_connection.remote_client_id]}")
        aes_connection.close
        next
      end

      LOGGER.info("New Peer -> #{remote_client_id}")
      @connections[aes_connection.remote_client_id] = ConnectionWrapper.new(self,aes_connection)
    end
  end
end
incoming_loop() click to toggle source
# File lib/network_clipboard/client.rb, line 134
def incoming_loop
  while @running
    incoming = @tcp_server.accept
    aes_connection = AESConnection.new(@config,incoming)

    LOGGER.debug("Incoming #{aes_connection.remote_client_id} from #{incoming.peeraddr(false)[-1]}")

    @connections_mutex.synchronize do
      if @connections[aes_connection.remote_client_id]
        LOGGER.info("Connection already established to #{aes_connection.remote_client_id}, dropping.")
        aes_connection.close
        next
      end

      LOGGER.info("New Peer <- #{aes_connection.remote_client_id}")

      @connections[aes_connection.remote_client_id] = ConnectionWrapper.new(self,aes_connection)
    end
  end
end
loop() click to toggle source
# File lib/network_clipboard/client.rb, line 83
def loop
  Thread.abort_on_exception = true
  @announce_thread = Thread.new{announce_loop}
  @discover_thread = Thread.new{discover_loop}
  @incoming_loop = Thread.new{incoming_loop}

  @announce_thread.join

  @connections.values.each do |connection|
    connection.join
  end
end
run_connection(connection) click to toggle source
# File lib/network_clipboard/client.rb, line 155
def run_connection(connection)
  begin
    while @running
      Clipboard.copy(connection.receive())
    end
  ensure
    connection.close

    @connections_mutex.synchronize do
      @connections.delete(connection.remote_client_id)
    end
  end
end
stop() click to toggle source
# File lib/network_clipboard/client.rb, line 169
def stop
  @running = false
  @connections.values.each(&:stop)
end