Class: StompingGround::Connection
- Inherits:
-
Object
- Object
- StompingGround::Connection
- Defined in:
- lib/stomping_ground/connection.rb
Instance Attribute Summary collapse
-
#frame ⇒ Object
Returns the value of attribute frame.
-
#frame_info ⇒ Object
Returns the value of attribute frame_info.
-
#server ⇒ Object
Returns the value of attribute server.
-
#socket ⇒ Object
Returns the value of attribute socket.
Instance Method Summary collapse
- #connect_frame ⇒ Object
- #disconnect_frame ⇒ Object
- #handle ⇒ Object
-
#initialize(server, socket) ⇒ Connection
constructor
A new instance of Connection.
- #send_frame ⇒ Object
- #subscribe_frame ⇒ Object
- #write(data) ⇒ Object
Constructor Details
#initialize(server, socket) ⇒ Connection
Returns a new instance of Connection.
5 6 7 8 |
# File 'lib/stomping_ground/connection.rb', line 5 def initialize(server, socket) @server = server @socket = socket end |
Instance Attribute Details
#frame ⇒ Object
Returns the value of attribute frame.
3 4 5 |
# File 'lib/stomping_ground/connection.rb', line 3 def frame @frame end |
#frame_info ⇒ Object
Returns the value of attribute frame_info.
3 4 5 |
# File 'lib/stomping_ground/connection.rb', line 3 def frame_info @frame_info end |
#server ⇒ Object
Returns the value of attribute server.
3 4 5 |
# File 'lib/stomping_ground/connection.rb', line 3 def server @server end |
#socket ⇒ Object
Returns the value of attribute socket.
3 4 5 |
# File 'lib/stomping_ground/connection.rb', line 3 def socket @socket end |
Instance Method Details
#connect_frame ⇒ Object
37 38 39 40 41 42 43 |
# File 'lib/stomping_ground/connection.rb', line 37 def connect_frame write "CONNECTED\n" write "version:1.1\n" write "heart-beat:0,0\n" if frame_info[:'heart-beat'] write "\n" write "\0" end |
#disconnect_frame ⇒ Object
59 60 61 62 63 64 65 66 |
# File 'lib/stomping_ground/connection.rb', line 59 def disconnect_frame if frame_info[:'receipt-id'] write "RECEIPT\n" write "receipt-id:#{frame_info[:'receipt-id']}\n" write "\0" end @socket.close end |
#handle ⇒ Object
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/stomping_ground/connection.rb', line 10 def handle loop do break if @socket.closed? data = @socket.readpartial 4096 append_data(data) each_frame do |frame| @frame = frame @frame_info = parse(frame) case frame_info[:command] when "CONNECT" connect_frame when "SUBSCRIBE" subscribe_frame when "DISCONNECT" disconnect_frame when "SEND" send_frame end end end rescue EOFError @socket.close end |
#send_frame ⇒ Object
68 69 70 71 72 73 74 75 |
# File 'lib/stomping_ground/connection.rb', line 68 def send_frame filename = (frame) if filename dirname = File.dirname(filename) FileUtils.mkdir_p(dirname) if !File.exists?(dirname) File.open(filename, "w") { |file| file.write(frame) } end end |
#subscribe_frame ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/stomping_ground/connection.rb', line 45 def subscribe_frame if @server.queue_name.nil? || frame_info[:destination] == @server.queue_name = @server. || "hello" write "MESSAGE\n" write "subscription:#{frame_info[:id]}\n" write "message-id:007\n" write "destination:#{frame_info[:destination]}\n" write "content-type:text/plain\n" write "content-length:#{.length}\n" write "\n" write "#{}\0" end end |
#write(data) ⇒ Object
77 78 79 |
# File 'lib/stomping_ground/connection.rb', line 77 def write(data) @socket.write data end |