Class: IRC::Server::Dispatcher

Inherits:
Object
  • Object
show all
Defined in:
lib/failirc/server/dispatcher.rb,
lib/failirc/server/dispatcher/event.rb,
lib/failirc/server/dispatcher/eventdispatcher.rb,
lib/failirc/server/dispatcher/connectiondispatcher.rb

Defined Under Namespace

Classes: ConnectionDispatcher, Event, EventDispatcher

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(server) ⇒ Dispatcher

Returns a new instance of Dispatcher.



30
31
32
33
34
35
36
37
38
# File 'lib/failirc/server/dispatcher.rb', line 30

def initialize (server)
    @server = server

    @connection = ConnectionDispatcher.new(self)
    @event      = EventDispatcher.new(self)

    @intervals = {}
    @timeouts  = {}
end

Instance Attribute Details

#connectionObject (readonly)

Returns the value of attribute connection.



28
29
30
# File 'lib/failirc/server/dispatcher.rb', line 28

def connection
  @connection
end

#eventObject (readonly)

Returns the value of attribute event.



28
29
30
# File 'lib/failirc/server/dispatcher.rb', line 28

def event
  @event
end

#serverObject (readonly)

Returns the value of attribute server.



28
29
30
# File 'lib/failirc/server/dispatcher.rb', line 28

def server
  @server
end

Instance Method Details

#alias(*args) ⇒ Object



200
201
202
# File 'lib/failirc/server/dispatcher.rb', line 200

def alias (*args)
    @event.alias(*args)
end

#clearInterval(interval) ⇒ Object



180
181
182
# File 'lib/failirc/server/dispatcher.rb', line 180

def clearInterval (interval)
    @intervals.delete(interval[:fiber])
end

#clearTimeout(timeout) ⇒ Object



167
168
169
# File 'lib/failirc/server/dispatcher.rb', line 167

def clearTimeout (timeout)
    @timeouts.delete(timeout[:fiber])
end

#connectionsObject



184
185
186
# File 'lib/failirc/server/dispatcher.rb', line 184

def connections
    @connection.connections
end

#disconnectingObject



196
197
198
# File 'lib/failirc/server/dispatcher.rb', line 196

def disconnecting
    @connection.disconnecting
end

#dispatch(*args) ⇒ Object



208
209
210
# File 'lib/failirc/server/dispatcher.rb', line 208

def dispatch (*args)
    @event.dispatch(*args)
end

#execute(*args) ⇒ Object



212
213
214
# File 'lib/failirc/server/dispatcher.rb', line 212

def execute (*args)
    @event.execute(*args)
end

#inputObject



188
189
190
# File 'lib/failirc/server/dispatcher.rb', line 188

def input
    @connection.input
end

#loopObject



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
150
151
152
153
154
155
156
157
# File 'lib/failirc/server/dispatcher.rb', line 108

def loop
    while true
        @defaults.each {|fiber|
            begin
                fiber.resume
            rescue FiberError
                self.debug 'Something went deeply wrong in the dispatcher, aborting.'
                Process::exit 23
            rescue Exception => e
                self.debug e
            end
        }

        @intervals.each {|fiber, meta|
            begin
                if !@intervals[fiber]
                    raise FiberError
                end

                if meta[:at] <= Time.now
                    fiber.resume

                    meta[:at] += meta[:offset]
                end
            rescue FiberError
                clearInterval meta
            rescue Exception => e
                self.debug e
            end
        }

        @timeouts.each {|fiber, meta|
            begin
                if !@timeouts[fiber]
                    raise FiberError
                end

                if meta[:at] <= Time.now
                    fiber.resume

                    clearTimeout meta
                end
            rescue FiberError
                clearTimeout meta
            rescue Exception => e
                self.debug e
            end
        }
    end
end

#outputObject



192
193
194
# File 'lib/failirc/server/dispatcher.rb', line 192

def output
    @connection.output
end

#register(*args) ⇒ Object



204
205
206
# File 'lib/failirc/server/dispatcher.rb', line 204

def register (*args)
    @event.register(*args)
end

#setInterval(fiber, time) ⇒ Object



171
172
173
174
175
176
177
178
# File 'lib/failirc/server/dispatcher.rb', line 171

def setInterval (fiber, time)
    @intervals[fiber] = {
        :fiber  => fiber,
        :offset => time,
        :at     => Time.now + time,
        :on     => Time.now,
    }
end

#setTimeout(fiber, time) ⇒ Object



159
160
161
162
163
164
165
# File 'lib/failirc/server/dispatcher.rb', line 159

def setTimeout (fiber, time)
    @timeouts[fiber] = {
        :fiber => fiber,
        :at    => Time.now + time,
        :on    => Time.now,
    }
end

#startObject



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
# File 'lib/failirc/server/dispatcher.rb', line 40

def start
    @started = true

    @listening = Fiber.new {
        while true
            if !@connection.connections.empty?
                timeout = 0
            else
                timeout = 2
            end

            @connection.accept timeout

            Fiber.yield
        end
    }

    @reading = Fiber.new {
        while true
            @connection.read

            Fiber.yield
        end
    }

    @cleaning = Fiber.new {
        while true
            @connection.clean

            Fiber.yield
        end
    }

    @handling = Fiber.new {
        while true
            @connection.handle

            Fiber.yield
        end
    }

    @writing = Fiber.new {
        while true
            @connection.write

            Fiber.yield
        end
    }

    @defaults = [@listening, @cleaning, @reading, @handling, @writing]
    
    self.loop
end

#stopObject



94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/failirc/server/dispatcher.rb', line 94

def stop
    if !@started
        return
    end

    @started  = false
    @stopping = true

    @event.finalize
    @connection.finalize

    @stopping = false
end