Class: TinyQ::Connection

Inherits:
EventMachine::Connection
  • Object
show all
Includes:
EventMachine::Protocols::LineText2
Defined in:
lib/tinyq/connection.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConnection

Returns a new instance of Connection.



10
11
12
# File 'lib/tinyq/connection.rb', line 10

def initialize
    set_delimiter TinyQ::DELIMITER
end

Instance Attribute Details

#ipObject (readonly)

Returns the value of attribute ip.



4
5
6
# File 'lib/tinyq/connection.rb', line 4

def ip
  @ip
end

#portObject (readonly)

Returns the value of attribute port.



5
6
7
# File 'lib/tinyq/connection.rb', line 5

def port
  @port
end

#serverObject

Returns the value of attribute server.



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

def server
  @server
end

Instance Method Details

#failed(errmsg) ⇒ Object



34
35
36
37
# File 'lib/tinyq/connection.rb', line 34

def failed errmsg
    response = {:Status => "Failed", :Message => errmsg}
    reply(response)
end

#okObject



29
30
31
32
# File 'lib/tinyq/connection.rb', line 29

def ok
    response = {:Status => "OK"}
    reply(response)
end

#post_initObject



14
15
16
17
18
19
# File 'lib/tinyq/connection.rb', line 14

def post_init
    @port, *ip_parts = get_peername[2,6].unpack "nC4"
    @ip = ip_parts.join('.')
    
    $LOG.info("#{@ip}:#{@port} connected")
end

#process_message(data) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/tinyq/connection.rb', line 40

def process_message data
    begin
        request = JSON.parse(data)
        $LOG.debug("Request: #{request}")
        if request.has_key? 'Command'
            $LOG.debug("Command - #{request['Command']}")

            case request['Command']
            when "PutMessages"
                # Put message in a bucket
                if request.has_key? 'Bucket'
                    bucket = @server.bucket(request['Bucket'])

                    if request['Message'].kind_of?(Array)
                        request['Message'].each do |message|
                            Journal.instance.event({:Event=>"PutMessage"})
                            bucket.put_message(message)
                        end
                    else
                        Journal.instance.event({:Event=>"PutMessage"})
                        bucket.put_message(request['Message'])
                    end

                    ok()
                else
                    failed("Parameter Bucket missing")
                end
            when "FeedFunnel"
                # Feed Bucket to Funnel
                if request.has_key? 'Bucket' and request.has_key? 'Funnel'
                    # Hook up a funnel to a bucket
                    bucket = @server.bucket(request['Bucket'])
                    funnel = @server.funnels[request['Funnel']]
                    if nil != funnel
                        bucket.feed_funnel(funnel)
                    else
                        if request.has_key? 'Broadcaster'
                            funnel = bucket.funnel(request['Funnel'], request['Broadcaster'])
                        else
                            funnel = bucket.funnel(request['Funnel'])
                        end
                        @server.funnels[request['Funnel']] = funnel
                    end

                    ok()
                else
                    failed("Parameter Bucket or Funnel missing")
                end
            when "GetMessages"
                # Get Messages from Funnel
                if request.has_key? 'Funnel'
                    funnel = @server.funnels[request['Funnel']]
                    if nil == funnel
                        $LOG.warn("Funnel was never fed...")
                        failed("Funnel was never fed")
                    else
                        if request.has_key? 'Ack'
                            ok()
                        end

                        if request.has_key? 'Count'
                            funnel.add_connection(self, request['Count'])
                        else
                            funnel.add_connection(self)
                        end
                    end
                else
                    failed("Parameter Funnel missing")
                end
            when "GetInfo"
                if request.has_key? 'Bucket'
                    bucket = @server.bucket(request['Bucket'])

                    response = {
                        :Status => 'OK',
                        :Bucket => {
                        :Name => bucket.name,
                        :MessageCount => bucket.messages.size,
                        :Funnels => [],
                    }
                    }

                    bucket.funnels.each do |n,funnel|
                        response[:Bucket][:Funnels].push({
                            :Name => funnel.name,
                            :MessageCount => funnel.messages.size,
                            :SubscriberCount => funnel.subscribers.size,
                        })
                    end

                    reply(response)

                elsif request.has_key? 'Funnel'
                else
                    failed("Parameter Funnel or Bucket missing")
                end
            else
                # Unsupported command
                failed("Unsupported command")
            end

        else
            # Did not understand
            failed("Parameter Command missing")
        end
    rescue Exception => e
        $LOG.error("Failed #{e}")
        failed("Internal error #{e}")
    end
end

#receive_line(line) ⇒ Object



21
22
23
# File 'lib/tinyq/connection.rb', line 21

def receive_line line
    process_message line
end

#reply(rsp) ⇒ Object



25
26
27
# File 'lib/tinyq/connection.rb', line 25

def reply rsp
    send_data("#{rsp.to_json}#{TinyQ::DELIMITER}")
end

#unbindObject



151
152
153
154
155
# File 'lib/tinyq/connection.rb', line 151

def unbind
    $LOG.info("#{@ip}:#{@port} disconnected")
 
    @server.remove_connection(self)
end