Module: AMQP::Server

Defined in:
lib/amqp/server.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.start(host = 'localhost', port = 5672) ⇒ Object



79
80
81
# File 'lib/amqp/server.rb', line 79

def self.start host = 'localhost', port = 5672
  EM.start_server host, port, self
end

Instance Method Details

#post_initObject



5
6
7
8
9
# File 'lib/amqp/server.rb', line 5

def post_init
  @buf = ''
  @channels = {}
  @started = false
end

#process_frame(frame) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/amqp/server.rb', line 45

def process_frame frame
  channel = frame.channel

  case method = frame.payload
  when Protocol::Connection::StartOk
    @user = method.response[:LOGIN]
    @pass = method.response[:PASSWORD]
    send Protocol::Connection::Tune.new(0, 131072, 0)

  when Protocol::Connection::TuneOk
    # mnnk

  when Protocol::Connection::Open
    @vhost = method.virtual_host
    send Protocol::Connection::OpenOk.new('localhost')

  when Protocol::Channel::Open
    @channels[channel] = []
    send Protocol::Channel::OpenOk.new, :channel => channel

  when Protocol::Access::Request
    send Protocol::Access::RequestOk.new(101)
  end
end

#receive_data(data) ⇒ Object



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
36
37
38
39
40
41
42
43
# File 'lib/amqp/server.rb', line 11

def receive_data data
  @buf << data

  unless @started
    if @buf.size >= 8
      if @buf.slice!(0,8) == "AMQP\001\001\b\000"
        send Protocol::Connection::Start.new(
          8,
          0,
          {
            :information => 'Licensed under the Ruby license. See http://github.com/tmm1/amqp',
            :copyright => 'Copyright (c) 2008-2009 Aman Gupta',
            :platform => 'Ruby/EventMachine',
            :version => '0.6.1',
            :product => 'SquirrelMQ'
          },
          'PLAIN AMQPLAIN',
          'en_US'
        )
      else
        close_connection
        return
      end
      @started = true
    else
      return
    end
  end

  while frame = Frame.parse(@buf)
    process_frame frame
  end
end

#send(data, opts = {}) ⇒ Object



70
71
72
73
74
75
76
77
# File 'lib/amqp/server.rb', line 70

def send data, opts = {}
  channel = opts[:channel] ||= 0
  data = data.to_frame(channel) unless data.is_a? Frame
  data.channel = channel

  log 'send', data
  send_data data.to_s
end