Class: Modbus::Connection::TCPClient

Inherits:
Base
  • Object
show all
Defined in:
lib/modbus/connection/tcp_client.rb

Instance Method Summary collapse

Methods inherited from Base

#analyze_buffer, #receive_data, #reset_buffer

Constructor Details

#initialize(handler) ⇒ TCPClient

Initializes a new connection instance.

Parameters:

  • handler

    The managing handler object.



17
18
19
20
21
22
# File 'lib/modbus/connection/tcp_client.rb', line 17

def initialize(handler)
  super

  @transaction_ident    = 0
  @pending_transactions = []
end

Instance Method Details

#connection_completedObject

EM callback. Called when the TCP connection is sucessfully established.



27
28
29
# File 'lib/modbus/connection/tcp_client.rb', line 27

def connection_completed
  @handler.connected self
end

#next_transaction_identInteger

Creates a successor transaction identfication in the range of 0..65535

Returns:

  • (Integer)

    The transaction identification



43
44
45
# File 'lib/modbus/connection/tcp_client.rb', line 43

def next_transaction_ident
  @transaction_ident = @transaction_ident.succ.modulo 2**16
end

#pick_pending_transaction(transaction_ident) ⇒ Modbus::Transaction

Looks for a matching transaction in the internal store by a transaction ident and returns it if found.

Parameters:

  • transaction_ident (Integer)

    The transaction ident to match.

Returns:



64
65
66
67
68
# File 'lib/modbus/connection/tcp_client.rb', line 64

def pick_pending_transaction(transaction_ident)
  transaction = @pending_transactions.find { |r| r.transaction_ident == transaction_ident }
  @pending_transactions.delete transaction
  transaction
end

#track_transaction(transaction) ⇒ Object

Adds Transaction object to the list of tracked transactions

Parameters:



52
53
54
55
56
# File 'lib/modbus/connection/tcp_client.rb', line 52

def track_transaction(transaction)
  # TODO log "exception"
  # puts "Too many pending pending transactions: #{@pending_transactions.size}" if @pending_transactions.size > 100
  @pending_transactions << transaction
end

#unbindObject

EM callback. Called when the TCP connection is closed.



34
35
36
# File 'lib/modbus/connection/tcp_client.rb', line 34

def unbind
  @handler.disconnected self
end