Module: ShootingStar::Server
- Defined in:
- lib/shooting_star/server.rb
Overview
The module which will be included by servant who was born in the Asteroid. This idea is from EventMachine.
Constant Summary collapse
- @@servers =
{}
- @@uids =
{}
{}
- @@executings =
{}
Instance Attribute Summary collapse
-
#signature ⇒ Object
readonly
Returns the value of attribute signature.
Class Method Summary collapse
-
.[](signature) ⇒ Object
an accessor which maps signatures to servers.
Instance Method Summary collapse
-
#commit ⇒ Object
perform buffered executions.
-
#executed(id) ⇒ Object
noticed execution and remove the command from execution buffer.
-
#post_init ⇒ Object
initialize servant waked up.
-
#receive_data(data) ⇒ Object
receive the data sent from client.
-
#respond(id, params) ⇒ Object
respond to an execution command.
- #tag ⇒ Object
- #uid ⇒ Object
-
#unbind ⇒ Object
detect disconnection from the client and clean it up.
-
#update(uid, tag) ⇒ Object
update current status of servant.
Instance Attribute Details
#signature ⇒ Object (readonly)
Returns the value of attribute signature.
12 13 14 |
# File 'lib/shooting_star/server.rb', line 12 def signature @signature end |
Class Method Details
.[](signature) ⇒ Object
an accessor which maps signatures to servers.
95 96 97 |
# File 'lib/shooting_star/server.rb', line 95 def self.[](signature) @@servers[signature] end |
Instance Method Details
#commit ⇒ Object
perform buffered executions.
59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/shooting_star/server.rb', line 59 def commit return false if @unbound @executing.each{|id, params| execute(id, params)} return false if @execution.empty? send_data "HTTP/1.1 200 OK\nContent-Type: text/javascript\n\n" send_data @execution @committed_at = Time.now @waiting = nil @execution = '' @executing = Hash.new @@executings.delete(@signature) write_and_close true end |
#executed(id) ⇒ Object
noticed execution and remove the command from execution buffer.
75 76 77 78 |
# File 'lib/shooting_star/server.rb', line 75 def executed(id) @executing = @@executings[@signature] ||= Hash.new @executing.delete(id) end |
#post_init ⇒ Object
initialize servant waked up.
19 20 21 22 |
# File 'lib/shooting_star/server.rb', line 19 def post_init @execution = '' @data = '' end |
#receive_data(data) ⇒ Object
receive the data sent from client.
25 26 27 28 |
# File 'lib/shooting_star/server.rb', line 25 def receive_data(data) @data += data response if @data[-4..-1] == "\r\n\r\n" end |
#respond(id, params) ⇒ Object
respond to an execution command. it’ll be buffered.
48 49 50 51 52 53 54 55 56 |
# File 'lib/shooting_star/server.rb', line 48 def respond(id, params) return unbind && false unless @waiting || !session_timeout? @executing = @@executings[@signature] ||= Hash.new if params[:tag] && !params[:tag].empty? && !@tag.empty? return false if (params[:tag] & @tag).empty? end @executing[id] = params @waiting end |
#tag ⇒ Object
92 |
# File 'lib/shooting_star/server.rb', line 92 def tag; @@tags[@signature] end |
#uid ⇒ Object
91 |
# File 'lib/shooting_star/server.rb', line 91 def uid; @@uids[@signature] end |
#unbind ⇒ Object
detect disconnection from the client and clean it up.
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/shooting_star/server.rb', line 31 def unbind @unbound = true if channel = Channel[@channel] channel.leave(self) notify(:event => :leave, :uid => @uid, :tag => @tag) end @@servers.delete(@signature) @@uids.delete(@signature) @@tags.delete(@signature) @@executings.delete(@signature) log "Disconnected: #{@uid}:#{@signature}" if Channel.cleanup(@channel) log "Channel closed: #{@channel}" end end |
#update(uid, tag) ⇒ Object
update current status of servant.
81 82 83 84 85 86 87 88 89 |
# File 'lib/shooting_star/server.rb', line 81 def update(uid, tag) if @uid != uid || @tag != tag notify(:event => :leave, :uid => @uid, :tag => @tag) @@uids[@signature] = @uid = uid @@tags[@signature] = @tag = tag notify(:event => :enter, :uid => @uid, :tag => @tag) end log "Update: #{@uid}:#{@tag.join(',')}" end |