Class: IRC::Client::Dispatcher

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

Defined Under Namespace

Classes: ConnectionDispatcher, Event, EventDispatcher

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client) ⇒ Dispatcher

Returns a new instance of Dispatcher.



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

def initialize (client)
    @client = client

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

    @intervals = {}
    @timeouts  = {}
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



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

def client
  @client
end

#connectionObject (readonly)

Returns the value of attribute connection.



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

def connection
  @connection
end

#eventObject (readonly)

Returns the value of attribute event.



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

def event
  @event
end

Instance Method Details

#alias(*args) ⇒ Object



190
191
192
# File 'lib/failirc/client/dispatcher.rb', line 190

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

#clearInterval(interval) ⇒ Object



166
167
168
# File 'lib/failirc/client/dispatcher.rb', line 166

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

#clearTimeout(timeout) ⇒ Object



153
154
155
# File 'lib/failirc/client/dispatcher.rb', line 153

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

#disconnectingObject



186
187
188
# File 'lib/failirc/client/dispatcher.rb', line 186

def disconnecting
    @connection.disconnecting
end

#dispatch(*args) ⇒ Object



198
199
200
# File 'lib/failirc/client/dispatcher.rb', line 198

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

#execute(*args) ⇒ Object



202
203
204
# File 'lib/failirc/client/dispatcher.rb', line 202

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

#inputObject



178
179
180
# File 'lib/failirc/client/dispatcher.rb', line 178

def input
    @connection.input
end

#loopObject



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

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



182
183
184
# File 'lib/failirc/client/dispatcher.rb', line 182

def output
    @connection.output
end

#register(*args) ⇒ Object



194
195
196
# File 'lib/failirc/client/dispatcher.rb', line 194

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

#server(identifier) ⇒ Object



174
175
176
# File 'lib/failirc/client/dispatcher.rb', line 174

def server (identifier)
    @connection.server identifier
end

#serversObject



170
171
172
# File 'lib/failirc/client/dispatcher.rb', line 170

def servers
    @connection.servers
end

#setInterval(fiber, time) ⇒ Object



157
158
159
160
161
162
163
164
# File 'lib/failirc/client/dispatcher.rb', line 157

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

#setTimeout(fiber, time) ⇒ Object



145
146
147
148
149
150
151
# File 'lib/failirc/client/dispatcher.rb', line 145

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

def start
    @started = true

    @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 = [@cleaning, @reading, @handling, @writing]
    
    self.loop
end

#stopObject



80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/failirc/client/dispatcher.rb', line 80

def stop
    if !@started
        return
    end

    @started  = false
    @stopping = true

    @event.finalize
    @connection.finalize

    @stopping = false
end