Class: McProtocolE::Client
- Inherits:
-
Object
- Object
- McProtocolE::Client
- Defined in:
- lib/mc_protocol_e/client.rb
Overview
This client shows a MC protocol client.
Defined Under Namespace
Classes: NotStartedError
Constant Summary collapse
- DEFAULT_OPEN_TIMEOUT =
3
- DEFAULT_READ_TIMEOUT =
3
Class Method Summary collapse
-
.start(address:, port:, open_timeout: DEFAULT_OPEN_TIMEOUT, read_timeout: DEFAULT_READ_TIMEOUT) {|client| ... } ⇒ Object
Starts MC protocol communication.
Instance Method Summary collapse
-
#close ⇒ Object
Closes MC protocol communication.
-
#initialize(address:, port:, open_timeout: DEFAULT_OPEN_TIMEOUT, read_timeout: DEFAULT_READ_TIMEOUT) ⇒ Client
constructor
Constructor.
-
#request(req) ⇒ Object
Sends request.
-
#start ⇒ Object
Starts MC protocol communication.
-
#started? ⇒ Boolean
Returns true if communication has started.
Constructor Details
#initialize(address:, port:, open_timeout: DEFAULT_OPEN_TIMEOUT, read_timeout: DEFAULT_READ_TIMEOUT) ⇒ Client
Constructor.
19 20 21 22 23 24 25 |
# File 'lib/mc_protocol_e/client.rb', line 19 def initialize(address:, port:, open_timeout: DEFAULT_OPEN_TIMEOUT, read_timeout: DEFAULT_READ_TIMEOUT) @address = address @port = port @open_timeout = open_timeout @read_timeout = read_timeout @socket = nil end |
Class Method Details
.start(address:, port:, open_timeout: DEFAULT_OPEN_TIMEOUT, read_timeout: DEFAULT_READ_TIMEOUT) {|client| ... } ⇒ Object
Starts MC protocol communication.
33 34 35 36 37 38 39 40 41 |
# File 'lib/mc_protocol_e/client.rb', line 33 def self.start(address:, port:, open_timeout: DEFAULT_OPEN_TIMEOUT, read_timeout: DEFAULT_READ_TIMEOUT, &block) client = new(address: address, port: port, open_timeout: open_timeout, read_timeout: read_timeout) if block_given? client.start(&block) else client end end |
Instance Method Details
#close ⇒ Object
Closes MC protocol communication.
44 45 46 |
# File 'lib/mc_protocol_e/client.rb', line 44 def close socket.close if started? end |
#request(req) ⇒ Object
Sends request.
52 53 54 55 56 |
# File 'lib/mc_protocol_e/client.rb', line 52 def request(req) raise NotStartedError, "not started" unless started? req.exec(socket, read_timeout) end |
#start ⇒ Object
Starts MC protocol communication.
59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/mc_protocol_e/client.rb', line 59 def start @socket = Socket.tcp(address, port, connect_timeout: open_timeout) unless started? if block_given? begin yield self ensure close end else self end end |
#started? ⇒ Boolean
Returns true if communication has started.
74 75 76 |
# File 'lib/mc_protocol_e/client.rb', line 74 def started? socket && !socket.closed? end |