Class: Cod::TcpClient
Overview
Acts as a channel that connects to a tcp listening socket on the other end.
Defined Under Namespace
Classes: Connection, OtherEnd, RobustConnection
Class Method Summary collapse
-
._load(params) ⇒ Object
:nodoc:.
Instance Method Summary collapse
-
#_dump(level) ⇒ Object
:nodoc:.
- #client ⇒ Object
-
#close ⇒ Object
Closes all underlying connections.
-
#get(opts = {}) ⇒ Object
Receives a message.
-
#initialize(destination, serializer) ⇒ TcpClient
constructor
A new instance of TcpClient.
-
#put(obj) ⇒ Object
Sends an object to the other end of the channel, if it is connected.
-
#service ⇒ Object
——————————————————— service/client.
Methods inherited from Channel
Constructor Details
#initialize(destination, serializer) ⇒ TcpClient
Returns a new instance of TcpClient.
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/cod/tcp_client.rb', line 8 def initialize(destination, serializer) @serializer = serializer @destination = destination # TcpClient handles two cases: Construction via an url (destination is a # string) and construction via a connection that has been # preestablished (destination is a socket): if destination.respond_to?(:read) # destination seems to be a socket, wrap it with Connection @connection = Connection.new(destination) else @connection = RobustConnection.new(destination) end @work_queue = WorkQueue.new # The predicate for allowing sends: Is the connection up? @work_queue.predicate { # NOTE This will not be called unless we have some messages to send, # so no useless connections are made @connection.try_connect @connection.established? } end |
Class Method Details
._load(params) ⇒ Object
:nodoc:
96 97 98 99 100 101 |
# File 'lib/cod/tcp_client.rb', line 96 def self._load(params) # :nodoc: # Instead of a tcp client (no way to construct one at this point), we'll # insert a kind of marker in the object stream that will be replaced # with a valid client later on. (hopefully) OtherEnd.new(params) end |
Instance Method Details
#_dump(level) ⇒ Object
:nodoc:
93 94 95 |
# File 'lib/cod/tcp_client.rb', line 93 def _dump(level) # :nodoc: @destination end |
#client ⇒ Object
74 75 76 77 78 79 80 81 82 83 |
# File 'lib/cod/tcp_client.rb', line 74 def client # NOTE: Normally, it doesn't make sense to ask the client channel for # something for a service connection, since the service needs to know # where to send requests in addition to knowing where to receive # answers. In the case of sockets, this is different: The service will # send its answers back the same way it got the requests from, so this # is really ok: # Service::Client.new(self, self) end |
#close ⇒ Object
Closes all underlying connections. You should only call this if you don’t want to use the channel again, since it will also stop reconnection attempts.
37 38 39 40 |
# File 'lib/cod/tcp_client.rb', line 37 def close @work_queue.shutdown @connection.close end |
#get(opts = {}) ⇒ Object
Receives a message. opts may contain various options, see below. Options include:
64 65 66 67 |
# File 'lib/cod/tcp_client.rb', line 64 def get(opts={}) @connection.try_connect @connection.read(@serializer) end |
#put(obj) ⇒ Object
Sends an object to the other end of the channel, if it is connected. If it is not connected, objects sent will queue up and once the internal storage reaches the high watermark, they will be dropped silently.
Example:
channel.put :object
# Really, any Ruby object that the current serializer can turn into
# a string!
51 52 53 54 55 56 57 58 59 |
# File 'lib/cod/tcp_client.rb', line 51 def put(obj) # TODO high watermark check # NOTE: predicate will call #try_connect @work_queue.schedule { send(obj) } @work_queue.try_work end |
#service ⇒ Object
——————————————————— service/client
71 72 73 |
# File 'lib/cod/tcp_client.rb', line 71 def service fail "A tcp client cannot be a service." end |