Class: ModSpox::Socket

Inherits:
Pool
  • Object
show all
Defined in:
lib/mod_spox/Socket.rb

Instance Attribute Summary collapse

Attributes inherited from Pool

#proc, #queue

Instance Method Summary collapse

Methods inherited from Pool

#destroy, #start_pool

Constructor Details

#initialize(bot, server, port, delay = 2, burst_in = 2, burst = 4) ⇒ Socket

factory

MessageFactory to parse messages

server

Server to connect to

port

Port number to connect to

delay

Number of seconds to delay between bursts

burst_in

Number of seconds allowed to burst

burst

Number of lines allowed to be sent within the burst_in time limit

Create a new Socket



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/mod_spox/Socket.rb', line 27

def initialize(bot, server, port, delay=2, burst_in=2, burst=4)
    super()
    @factory = bot.factory
    @pipeline = bot.pipeline
    @server = server
    @port = port.to_i
    @sent = 0
    @received = 0
    @delay = delay.to_f > 0 ? delay.to_f : 2.0
    @burst = burst.to_i > 0 ? burst.to_i : 4
    @burst_in = 2
    @kill = false
    @reader_thread = nil
    @time_check = nil
    @check_burst = 0
    @pause = false
    @sendq = @queue
    @lock = Mutex.new
    start_pool
end

Instance Attribute Details

#burstObject

Returns the value of attribute burst.



14
15
16
# File 'lib/mod_spox/Socket.rb', line 14

def burst
  @burst
end

#burst_inObject

Returns the value of attribute burst_in.



15
16
17
# File 'lib/mod_spox/Socket.rb', line 15

def burst_in
  @burst_in
end

#delayObject

Returns the value of attribute delay.



16
17
18
# File 'lib/mod_spox/Socket.rb', line 16

def delay
  @delay
end

#portObject (readonly)

Returns the value of attribute port.



18
19
20
# File 'lib/mod_spox/Socket.rb', line 18

def port
  @port
end

#receivedObject (readonly)

Returns the value of attribute received.



13
14
15
# File 'lib/mod_spox/Socket.rb', line 13

def received
  @received
end

#sentObject (readonly)

Returns the value of attribute sent.



12
13
14
# File 'lib/mod_spox/Socket.rb', line 12

def sent
  @sent
end

#serverObject (readonly)

Returns the value of attribute server.



17
18
19
# File 'lib/mod_spox/Socket.rb', line 17

def server
  @server
end

Instance Method Details

#<<(message) ⇒ Object

message

String to be sent to server

Queues a message up to be sent to the IRC server



123
124
125
# File 'lib/mod_spox/Socket.rb', line 123

def <<(message)
    @sendq << message
end

#connectObject

Connects to the IRC server



49
50
51
52
53
54
55
56
57
58
# File 'lib/mod_spox/Socket.rb', line 49

def connect
    Logger.log("Establishing connection to #{@server}:#{@port}", 10)
    @socket = TCPSocket.new(@server, @port)
    @socket.sync = true
    server = Models::Server.find_or_create(:host => @server, :port => @port)
    server.connected = true
    server.save
    Logger.log("Created new send queue: #{@sendq}", 10)
    reader
end

#getsObject

Retrieves a string from the server



100
101
102
# File 'lib/mod_spox/Socket.rb', line 100

def gets
    read
end

#processorObject

Starts the thread for sending messages to the server



128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/mod_spox/Socket.rb', line 128

def processor
    @lock.synchronize do
        write(@sendq.pop)
        if((Time.now.to_i - @time_check) > @burst_in)
            @time_check = nil
            @check_burst = 0
        elsif((Time.now.to_i - @time_check) <= @burst_in && @check_burst >= @burst)
            Logger.log("Burst limit hit. Output paused for: #{@delay} seconds", 70)
            sleep(@delay)
            @time_check = nil
            @check_burst = 0
        end
    end
end

#puts(message) ⇒ Object

message

String to send to server

Sends a string to the IRC server



83
84
85
# File 'lib/mod_spox/Socket.rb', line 83

def puts(message)
    write(message)
end

#readObject

Retrieves a string from the server



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/mod_spox/Socket.rb', line 105

def read
    message = @socket.gets
    if(message.nil? || @socket.closed?) # || message =~ /^ERROR/)
        @pipeline << Messages::Internal::Disconnected.new
        shutdown
        server = Models::Server.find_or_create(:host => @server, :port => @port)
        server.connected = false
        server.save
        return
    end
    Logger.log(">> #{message}", 5)
    @received += 1
    message.strip!
    return message
end

#readerObject

Starts the thread for reading messages from the server



144
145
146
147
148
149
150
151
# File 'lib/mod_spox/Socket.rb', line 144

def reader
    @reader_thread = Thread.new do
        until @kill do
            Kernel.select([@socket], nil, nil, nil)
            @factory << read
        end
    end
end

#shutdown(restart = false) ⇒ Object

restart

Reconnect after closing connection

Closes connection to IRC server



155
156
157
158
159
160
161
162
163
164
# File 'lib/mod_spox/Socket.rb', line 155

def shutdown(restart=false)
    @socket.close unless @socket.closed?
    @kill = true
    server = Models::Server.find_or_create(:host => @server, :port => @port)
    server.connected = false
    server.save
    sleep(0.1)            
    @reader_thread.kill if @reader_thread.alive?
    connect if restart
end

#write(message) ⇒ Object

message

String to send to server

Sends a string to the IRC server



89
90
91
92
93
94
95
96
97
# File 'lib/mod_spox/Socket.rb', line 89

def write(message)
    return if message.nil?
    @socket.puts(message + "\n") #send(message + "\n", 0)
    Logger.log("<< #{message}", 5)
    @last_send = Time.new
    @sent += 1
    @check_burst += 1
    @time_check = Time.now.to_i if @time_check.nil?
end