Class: Flare::Net::Connection
- Inherits:
-
Object
- Object
- Flare::Net::Connection
- Includes:
- Util::Logging, Util::Result
- Defined in:
- lib/flare/net/connection.rb
Overview
Description
Constant Summary
Constants included from Util::Result
Util::Result::ClientError, Util::Result::Deleted, Util::Result::End, Util::Result::Error, Util::Result::Exists, Util::Result::Found, Util::Result::None, Util::Result::NotFound, Util::Result::NotStored, Util::Result::Ok, Util::Result::ServerError, Util::Result::Stored
Instance Attribute Summary collapse
-
#host ⇒ Object
readonly
Returns the value of attribute host.
-
#port ⇒ Object
readonly
Returns the value of attribute port.
-
#received_size ⇒ Object
readonly
Returns the value of attribute received_size.
-
#sent_size ⇒ Object
readonly
Returns the value of attribute sent_size.
Instance Method Summary collapse
- #close ⇒ Object
- #closed? ⇒ Boolean
- #getline ⇒ Object
-
#initialize(host, port, uplink_limit = nil, downlink_limit = nil) ⇒ Connection
constructor
A new instance of Connection.
- #last_sent ⇒ Object
- #read(length = nil) ⇒ Object
- #reconnect ⇒ Object
- #send(cmd) ⇒ Object
- #to_s ⇒ Object
Methods included from Util::Result
result_of_string, string_of_result
Methods included from Util::Logging
#debug, #error, #fatal, #info, logger, #puts, set_logger, #trace, #warn
Constructor Details
#initialize(host, port, uplink_limit = nil, downlink_limit = nil) ⇒ Connection
Returns a new instance of Connection.
23 24 25 26 27 28 29 30 31 32 |
# File 'lib/flare/net/connection.rb', line 23 def initialize(host, port, uplink_limit = nil, downlink_limit = nil) @host = host @port = port @socket = TCPSocket.open(host, port) @last_sent = "" @sent_size = 0 @received_size = 0 @uplink_limit = Flare::Util::Bwlimit.new(uplink_limit) @downlink_limit = Flare::Util::Bwlimit.new(downlink_limit) end |
Instance Attribute Details
#host ⇒ Object (readonly)
Returns the value of attribute host.
34 35 36 |
# File 'lib/flare/net/connection.rb', line 34 def host @host end |
#port ⇒ Object (readonly)
Returns the value of attribute port.
34 35 36 |
# File 'lib/flare/net/connection.rb', line 34 def port @port end |
#received_size ⇒ Object (readonly)
Returns the value of attribute received_size.
35 36 37 |
# File 'lib/flare/net/connection.rb', line 35 def received_size @received_size end |
#sent_size ⇒ Object (readonly)
Returns the value of attribute sent_size.
35 36 37 |
# File 'lib/flare/net/connection.rb', line 35 def sent_size @sent_size end |
Instance Method Details
#close ⇒ Object
37 38 39 |
# File 'lib/flare/net/connection.rb', line 37 def close @socket.close unless @socket.closed? end |
#closed? ⇒ Boolean
41 42 43 |
# File 'lib/flare/net/connection.rb', line 41 def closed? @socket.closed? end |
#getline ⇒ Object
69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/flare/net/connection.rb', line 69 def getline ret = @socket.gets return nil if ret.nil? if $DEBUG puts "<-- #{ret.chomp}" end size = ret.size @received_size += size @downlink_limit.inc size @downlink_limit.wait ret end |
#last_sent ⇒ Object
65 66 67 |
# File 'lib/flare/net/connection.rb', line 65 def last_sent @last_sent end |
#read(length = nil) ⇒ Object
82 83 84 85 86 87 88 89 90 |
# File 'lib/flare/net/connection.rb', line 82 def read(length = nil) ret = @socket.read(length) return nil if ret.nil? size = ret.size @received_size += size @downlink_limit.inc size @downlink_limit.wait ret end |
#reconnect ⇒ Object
45 46 47 48 49 50 51 |
# File 'lib/flare/net/connection.rb', line 45 def reconnect if @socket.closed? @socket = nil @socket = TCPSocket.open(@host, @port) end @socket end |
#send(cmd) ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/flare/net/connection.rb', line 53 def send(cmd) if $DEBUG puts "--> server=[#{self}] cmd=[#{cmd.chomp}]" end size = cmd.size @sent_size += size @socket.write cmd @last_sent = cmd @uplink_limit.inc size @uplink_limit.wait end |
#to_s ⇒ Object
92 93 94 |
# File 'lib/flare/net/connection.rb', line 92 def to_s "#{@host}:#{@port}" end |