Class: StompingGround::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/stomping_ground/connection.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#frameObject

Returns the value of attribute frame.



3
4
5
# File 'lib/stomping_ground/connection.rb', line 3

def frame
  @frame
end

#frame_infoObject

Returns the value of attribute frame_info.



3
4
5
# File 'lib/stomping_ground/connection.rb', line 3

def frame_info
  @frame_info
end

#serverObject

Returns the value of attribute server.



3
4
5
# File 'lib/stomping_ground/connection.rb', line 3

def server
  @server
end

#socketObject

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_frameObject



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_frameObject



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

#handleObject



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_frameObject



68
69
70
71
72
73
74
75
# File 'lib/stomping_ground/connection.rb', line 68

def send_frame
  filename = sent_message_filename_for(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_frameObject



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
    message = @server.message_body || "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:#{message.length}\n"
    write "\n"
    write "#{message}\0"
  end
end

#write(data) ⇒ Object



77
78
79
# File 'lib/stomping_ground/connection.rb', line 77

def write(data)
  @socket.write data
end