File: faye.rb

package info (click to toggle)
ruby-faye 1.2.4-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 608 kB
  • sloc: ruby: 1,871; makefile: 3
file content (124 lines) | stat: -rw-r--r-- 3,369 bytes parent folder | download
1
2
3
4
5
6
7
8
9
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
36
37
38
39
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
require 'cgi'
require 'cookiejar'
require 'digest/sha1'
require 'em-http'
require 'em-http/version'
require 'eventmachine'
require 'faye/websocket'
require 'forwardable'
require 'multi_json'
require 'rack'
require 'securerandom'
require 'set'
require 'time'
require 'uri'

module Faye
  VERSION = '1.2.4'

  ROOT = File.expand_path(File.dirname(__FILE__))

  autoload :Deferrable,   File.join(ROOT, 'faye', 'mixins', 'deferrable')
  autoload :Logging,      File.join(ROOT, 'faye', 'mixins', 'logging')
  autoload :Publisher,    File.join(ROOT, 'faye', 'mixins', 'publisher')
  autoload :Timeouts,     File.join(ROOT, 'faye', 'mixins', 'timeouts')

  autoload :Namespace,    File.join(ROOT, 'faye', 'util', 'namespace')

  autoload :Engine,       File.join(ROOT, 'faye', 'engines', 'proxy')

  autoload :Channel,      File.join(ROOT, 'faye', 'protocol', 'channel')
  autoload :Client,       File.join(ROOT, 'faye', 'protocol', 'client')
  autoload :Dispatcher,   File.join(ROOT, 'faye', 'protocol', 'dispatcher')
  autoload :Scheduler,    File.join(ROOT, 'faye', 'protocol', 'scheduler')
  autoload :Extensible,   File.join(ROOT, 'faye', 'protocol', 'extensible')
  autoload :Grammar,      File.join(ROOT, 'faye', 'protocol', 'grammar')
  autoload :Publication,  File.join(ROOT, 'faye', 'protocol', 'publication')
  autoload :Server,       File.join(ROOT, 'faye', 'protocol', 'server')
  autoload :Subscription, File.join(ROOT, 'faye', 'protocol', 'subscription')

  autoload :Error,        File.join(ROOT, 'faye', 'error')
  autoload :Transport,    File.join(ROOT, 'faye', 'transport', 'transport')

  autoload :RackAdapter,  File.join(ROOT, 'faye', 'adapters', 'rack_adapter')
  autoload :StaticServer, File.join(ROOT, 'faye', 'adapters', 'static_server')

  BAYEUX_VERSION   = '1.0'
  JSONP_CALLBACK   = 'jsonpcallback'
  CONNECTION_TYPES = %w[long-polling cross-origin-long-polling callback-polling websocket eventsource in-process]

  MANDATORY_CONNECTION_TYPES = %w[long-polling callback-polling in-process]

  class << self
    attr_accessor :logger
  end

  def self.ensure_reactor_running!
    Engine.ensure_reactor_running!
  end

  def self.random(*args)
    Engine.random(*args)
  end

  def self.client_id_from_messages(messages)
    first = [messages].flatten.find { |m| m['channel'] == '/meta/connect' }
    first && first['clientId']
  end

  def self.copy_object(object)
    case object
    when Hash
      clone = {}
      object.each { |k,v| clone[k] = copy_object(v) }
      clone
    when Array
      clone = []
      object.each { |v| clone << copy_object(v) }
      clone
    else
      object
    end
  end

  def self.to_json(value)
    case value
      when Hash, Array then MultiJson.dump(value)
      when String, NilClass then value.inspect
      else value.to_s
    end
  end

  def self.async_each(list, iterator, callback)
    n       = list.size
    i       = -1
    calls   = 0
    looping = false

    loop, resume = nil, nil

    iterate = lambda do
      calls -= 1
      i += 1
      if i == n
        callback.call if callback
      else
        iterator.call(list[i], resume)
      end
    end

    loop = lambda do
      unless looping
        looping = true
        iterate.call while calls > 0
        looping = false
      end
    end

    resume = lambda do
      calls += 1
      loop.call
    end
    resume.call
  end
end