Permalink
Browse files

Allow bursts of multiple messages per poll request

  • Loading branch information...
1 parent b13098f commit 43270896fd62a98bf2f684e9cbf81211c7c52a3c @winton committed Jan 17, 2011
View
@@ -8,21 +8,10 @@
$:.unshift File.dirname(__FILE__)
+require 'puggernaut/logger'
require 'puggernaut/client'
require 'puggernaut/server'
module Puggernaut
- class <<self
-
- def logger
- unless @logger
- base = Dir.pwd
- FileUtils.mkdir_p("#{base}/log")
- file = File.open("#{base}/log/puggernaut.log", 'a')
- file.sync = true
- @logger = Logger.new(file)
- end
- @logger
- end
- end
+ # snort snort
end
@@ -1,9 +1,10 @@
require 'socket'
-#require 'puggernaut/client/tcp'
module Puggernaut
class Client
+ include Logger
+
attr_accessor :connections
def initialize(servers={})
@@ -12,22 +13,13 @@ def initialize(servers={})
@servers = servers
end
- def connect(host, port)
- host_port = "#{host}:#{port}"
- @connections[host_port] ||= TCPSocket.open(host, port)
- end
-
def close
@connections.each do |host_port, connection|
connection.close
logger.info "Client#close - #{http_port}"
end
end
- def logger
- Puggernaut.logger
- end
-
def say(messages)
messages.each do |room, message|
message =
@@ -47,13 +39,14 @@ def say(messages)
def send(host, port, data, try_retry=true)
try if try_retry
begin
- logger.info "Client#send - #{host}:#{port} - #{data}"
- connection = connect(host, port)
+ host_port = "#{host}:#{port}"
+ logger.info "Client#send - #{host_port} - #{data}"
+ connection = @connections[host_port] ||= TCPSocket.open(host, port)
connection.print(data)
response = connection.gets
raise 'not ok' if !response || !response.include?('OK')
rescue Exception => e
- logger.info "Client#send - Exception - #{e.message} - #{host}:#{port} - #{data}"
+ logger.info "Client#send - Exception - #{e.message} - #{host_port} - #{data}"
@retry << [ host, port, data ]
@retry.shift if @retry.length > 10
end
@@ -0,0 +1,22 @@
+module Puggernaut
+ module Logger
+
+ def logger
+ Puggernaut::Logger.logger
+ end
+
+ class <<self
+
+ def logger
+ unless @logger
+ base = Dir.pwd
+ FileUtils.mkdir_p("#{base}/log")
+ file = File.open("#{base}/log/puggernaut.log", 'a')
+ file.sync = true
+ @logger = ::Logger.new(file)
+ end
+ @logger
+ end
+ end
+ end
+end
@@ -5,6 +5,8 @@
module Puggernaut
class Server
+ include Logger
+
class <<self
attr_accessor :rooms
end
@@ -31,9 +33,5 @@ def initialize(port=8000)
end
end
end
-
- def logger
- Puggernaut.logger
- end
end
end
@@ -4,9 +4,7 @@ module Puggernaut
class Server
module Http
- def logger
- Puggernaut.logger
- end
+ include Logger
def receive_data(data)
lines = data.split(/[\r\n]+/)
@@ -2,6 +2,8 @@ module Puggernaut
class Server
class Room < EM::Channel
+ include Logger
+
attr_reader :messages, :room
def initialize(room)
@@ -22,18 +24,15 @@ def all_messages_after(identifier)
"#{@room}|#{message.join '|'}"
}
end
-
- def logger
- Puggernaut.logger
- end
- def say(message)
- message = [ rand.to_s[2..-1], message ]
- @messages << message
- @messages.shift if @messages.length > 100
- logger.info "Server::Room#say - #{@room} - #{message[0]} - #{message[1]}"
- push "#{@room}|#{message.join '|'}"
- message[0]
+ def say(messages)
+ push messages.split("\n").collect { |message|
+ message = [ rand.to_s[2..-1], message ]
+ @messages << message
+ @messages.shift if @messages.length > 100
+ logger.info "Server::Room#say - #{@room} - #{message[0]} - #{message[1]}"
+ "#{@room}|#{message.join '|'}"
+ }.join("\n")
end
end
end
@@ -2,17 +2,20 @@ module Puggernaut
class Server
module Tcp
- def logger
- Puggernaut.logger
- end
+ include Logger
def receive_data(data)
- data.split("\n").each do |line|
+ messages = data.split("\n").inject({}) do |hash, line|
room, message = line.split('|', 2)
+ hash[room] ||= []
+ hash[room] << message
+ hash
+ end
+ messages.each do |room, messages|
room = Puggernaut::Server.rooms[room] ||= Room.new(room)
- id = room.say message
- send_data "OK\n"
+ room.say messages.join("\n")
end
+ send_data "OK\n"
end
end
end

0 comments on commit 4327089

Please sign in to comment.