Class: Fix::Engine::Client

Inherits:
Object
  • Object
show all
Includes:
Logger
Defined in:
lib/fix/engine/client.rb

Overview

Represents a connected client

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Logger

#log, log, logger

Constructor Details

#initialize(ip, port, connection) ⇒ Client

Returns a new instance of Client.



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

def initialize(ip, port, connection)
  @ip         = ip
  @port       = port
  @connection = connection
 
  self.class.instance_variable_get(:@clients)[key] = self 
end

Instance Attribute Details

#connectionObject

Returns the value of attribute connection.



13
14
15
# File 'lib/fix/engine/client.rb', line 13

def connection
  @connection
end

#ipObject

Returns the value of attribute ip.



13
14
15
# File 'lib/fix/engine/client.rb', line 13

def ip
  @ip
end

#portObject

Returns the value of attribute port.



13
14
15
# File 'lib/fix/engine/client.rb', line 13

def port
  @port
end

#usernameObject

Returns the value of attribute username.



13
14
15
# File 'lib/fix/engine/client.rb', line 13

def username
  @username
end

Class Method Details

.countFixnum

Returns the count of currently connected clients

Returns:

  • (Fixnum)

    The client count



42
43
44
# File 'lib/fix/engine/client.rb', line 42

def self.count
  @clients.count
end

.delete(ip, port) ⇒ Object

Removes a client from the currently connected ones

Parameters:

  • ip (String)

    The client’s remote IP

  • port (Fixnum)

    The client’s port



52
53
54
# File 'lib/fix/engine/client.rb', line 52

def self.delete(ip, port)
  @clients.delete(key(ip, port))
end

.get(ip, port, connection = nil) ⇒ Fix::Engine::Client

Returns a client instance from its connection IP

Parameters:

  • ip (String)

    The connection IP

  • port (Fixnum)

    The connection port

  • connection (FE::Connection) (defaults to: nil)

    Optionnally the connection which will used to create an instance if none exists

Returns:



33
34
35
# File 'lib/fix/engine/client.rb', line 33

def self.get(ip, port, connection = nil)
  @clients[key(ip, port)] || Client.new(ip, port, connection)
end

.key(ip, port) ⇒ String

Returns an identifier for the given IP and port

Parameters:

  • ip (String)

    The client’s remote IP

  • port (Fixnum)

    The client’s port

Returns:

  • (String)

    An identifier



80
81
82
# File 'lib/fix/engine/client.rb', line 80

def self.key(ip, port)
  "#{ip}:#{port}"
end

Instance Method Details

#deleteObject

Removes the current client from the array of connected ones



68
69
70
# File 'lib/fix/engine/client.rb', line 68

def delete
  self.class.delete(ip, port)
end

#keyString

Returns an identifier for the current client

Returns:

  • (String)

    An identifier



61
62
63
# File 'lib/fix/engine/client.rb', line 61

def key
  self.class.key(ip, port)
end