Class: Networking::TCPClient

Inherits:
Object
  • Object
show all
Defined in:
lib/networking/tcp.rb

Overview

TCPServer

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host, port, obj_clb = nil, reconnected_clb = nil) ⇒ TCPClient

Returns a new instance of TCPClient.



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/networking/tcp.rb', line 133

def initialize(host, port, obj_clb=nil, reconnected_clb=nil)
  @host = host
  @port = port
  @tcp_socket = nil
  @obj_clb = obj_clb
  @reconnected_clb = reconnected_clb
  Log.debug3("Start TCPClient initialize  with @obj_clb: #{@obj_clb}")
  if @obj_clb != nil
    @tcp_thread = start_reading
    @tcp_thread.abort_on_exception = true
  end
  # Variable to signal when remote server is ready.
  @remote_server_available = ConditionVariable.new
  @remote_server_available_mutex = Mutex.new
  open_socket unless socket_good?
  Log.debug3("End TCPClient initialize.")
end

Instance Attribute Details

#tcp_threadObject (readonly)

Returns the value of attribute tcp_thread.



131
132
133
# File 'lib/networking/tcp.rb', line 131

def tcp_thread
  @tcp_thread
end

Instance Method Details

#open_socketObject

This function may be executed only from one thread!!! or in synchronized manner. private



166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/networking/tcp.rb', line 166

def open_socket
  Log.debug1("Connecting to content server #{@host}:#{@port}.")
  begin
    @tcp_socket = TCPSocket.new(@host, @port)
  rescue Errno::ECONNREFUSED
    Log.warning('Connection refused')
  end
  Log.debug3("Reconnect clb: '#{@reconnected_clb.nil? ? 'nil' : @reconnected_clb}'")
  if socket_good?
    @remote_server_available_mutex.synchronize {
      @remote_server_available.signal
    }
    @reconnected_clb.call if @reconnected_clb != nil && socket_good?
  end
end

#send_obj(obj) ⇒ Object



151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/networking/tcp.rb', line 151

def send_obj(obj)
  Log.debug1('send_obj')
  open_socket unless socket_good?
  Log.debug1('after open')
  if !socket_good?
    Log.warning('Socket not opened for writing, skipping send.')
    return false
  end
  Log.debug2("writing... socket port: #{@tcp_socket.peeraddr}")
  bytes_written = Networking.write_to_stream(@tcp_socket, obj)
  return bytes_written
end