class RJR::Nodes::TCPConnection
@private Helper class intialized by eventmachine encapsulating a tcp socket connection
Attributes
host[R]
port[R]
Public Class Methods
new(args = {})
click to toggle source
TCPConnection
intializer
Specify the TCP
Node
establishing the connection and optionaly remote host/port which this connection is connected to
# File lib/rjr/nodes/tcp.rb, line 29 def initialize(args = {}) @rjr_node = args[:rjr_node] @host = args[:host] @port = args[:port] @send_lock = Mutex.new @data = "" @rjr_node.add_connection(self) end
Public Instance Methods
post_init()
click to toggle source
# File lib/rjr/nodes/tcp.rb, line 39 def post_init @rjr_node.send(:connection_event, :opened, self) end
receive_data(data)
click to toggle source
EventMachine::Connection#receive_data callback, handle request / response messages
# File lib/rjr/nodes/tcp.rb, line 44 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 = JSONParser.extract_json_from(@data) msg, @data = *extracted @rjr_node.send(:handle_message, msg, self) # XXX private method end end
send_msg(data)
click to toggle source
Send data safely using local connection
# File lib/rjr/nodes/tcp.rb, line 56 def send_msg(data) @send_lock.synchronize{ TCP.em.schedule { send_data(data) } } end
unbind()
click to toggle source
# File lib/rjr/nodes/tcp.rb, line 62 def unbind @rjr_node.remove_connection(self) @rjr_node.send(:connection_event, :closed, self) end