Class: RJR::Nodes::TCPConnection
- Inherits:
-
EventMachine::Connection
- Object
- EventMachine::Connection
- RJR::Nodes::TCPConnection
- Defined in:
- lib/rjr/nodes/tcp.rb
Overview
Helper class intialized by eventmachine encapsulating a tcp socket connection
Instance Attribute Summary collapse
-
#host ⇒ Object
readonly
Returns the value of attribute host.
-
#port ⇒ Object
readonly
Returns the value of attribute port.
Instance Method Summary collapse
-
#initialize(args = {}) ⇒ TCPConnection
constructor
TCPConnection intializer.
-
#receive_data(data) ⇒ Object
EventMachine::Connection#receive_data callback, handle request / response messages.
-
#send_msg(data) ⇒ Object
Send data safely using local connection.
Constructor Details
#initialize(args = {}) ⇒ TCPConnection
TCPConnection intializer
Specify the TCP Node establishing the connection and optionaly remote host/port which this connection is connected to
28 29 30 31 32 33 34 35 |
# File 'lib/rjr/nodes/tcp.rb', line 28 def initialize(args = {}) @rjr_node = args[:rjr_node] @host = args[:host] @port = args[:port] @send_lock = Mutex.new @data = "" end |
Instance Attribute Details
#host ⇒ Object (readonly)
Returns the value of attribute host.
21 22 23 |
# File 'lib/rjr/nodes/tcp.rb', line 21 def host @host end |
#port ⇒ Object (readonly)
Returns the value of attribute port.
22 23 24 |
# File 'lib/rjr/nodes/tcp.rb', line 22 def port @port end |
Instance Method Details
#receive_data(data) ⇒ Object
EventMachine::Connection#receive_data callback, handle request / response messages
38 39 40 41 42 43 44 45 46 47 |
# File 'lib/rjr/nodes/tcp.rb', line 38 def receive_data(data) # a large json-rpc message may be split over multiple packets # (invocations of receive_data) # and multiple messages may be concatinated into one packet @data += data while extracted = MessageUtil.retrieve_json(@data) msg, @data = *extracted @rjr_node.send(:handle_message, msg, self) # XXX private method end end |
#send_msg(data) ⇒ Object
Send data safely using local connection
50 51 52 53 54 |
# File 'lib/rjr/nodes/tcp.rb', line 50 def send_msg(data) @send_lock.synchronize{ TCP.em.schedule { send_data(data) } } end |