Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Ready to start testing

  • Loading branch information...
commit 49e431803e1e7ca8b07807948d7022e53d328567 1 parent 58db62b
@winton authored
View
16 lib/puggernaut.rb
@@ -11,5 +11,19 @@
require 'puggernaut/server'
module Puggernaut
- # snort snort
+ class <<self
+
+ attr_accessor :env
+
+ def logger
+ unless @logger
+ base = File.expand_path('../../', __FILE__)
+ FileUtils.mkdir_p("#{base}/log")
+ file = File.open("#{base}/log/#{env}.log", 'a')
+ file.sync = true
+ @logger = Logger.new(file)
+ end
+ @logger
+ end
+ end
end
View
29 lib/puggernaut/client.rb
@@ -1,8 +1,37 @@
module Puggernaut
class Client
+ def initialize(env, servers)
+ Puggernaut.env = env
+
+ loop do
+ begin
+ GC.start
+ EM.epoll if EM.epoll?
+ EM.run do
+ @connections = servers.inject([]) do |array, (host, port)|
+ logger.info "#{Time.now} Starting TCP client for #{host}:#{port}"
+ array << EM.connect(host, port, Tcp)
+ end
+ end
+ rescue Interrupt
+ logger.info "#{Time.now} Shuting down client..."
+ exit
+ rescue
+ logger.error "#{Time.now} Error: " + $!.message
+ logger.error "\t" + $!.backtrace.join("\n\t")
+ end
+ end
+ end
+
def logger
Puggernaut.logger
end
+
+ def say(messages)
+ @connections.each do |connection|
+ connection.say messages
+ end
+ end
end
end
View
42 lib/puggernaut/client/tcp.rb
@@ -0,0 +1,42 @@
+module Puggernaut
+ class Client
+ module Tcp
+
+ include EM::Deferrable
+
+ def connection_completed
+ @reconnecting = false
+ @connected = true
+ succeed
+ end
+
+ def logger
+ Puggernaut.logger
+ end
+
+ def say(messages)
+ messages.each do |room, messages|
+ messages.each do |message|
+ send_data "#{room}|#{message}"
+ logger.info "#{Time.now} Said #{message} to #{room}"
+ end
+ end
+ end
+
+ def send_data(data)
+ callback{ super(data) }
+ end
+
+ def unbind
+ @deferred_status = nil
+ if @connected || @reconnecting
+ EM.add_timer(1) { reconnect @host, @port }
+ @connected = false
+ @reconnecting = true
+ else
+ raise 'Unable to connect to server'
+ end
+ end
+ end
+ end
+end
View
42 lib/puggernaut/server.rb
@@ -5,23 +5,37 @@
module Puggernaut
class Server
- def initialize(env='development')
- self.class.env = env
- end
-
class <<self
- attr_accessor :env, :rooms
-
- def logger
- unless @logger
- base = File.expand_path('../../', __FILE__)
- FileUtils.mkdir_p("#{base}/log")
- file = File.open("#{base}/log/#{env}.log", 'a')
- file.sync = true
- @logger = Logger.new(file)
+ attr_accessor :rooms
+ end
+
+ def initialize(env='development', port=8000)
+ Puggernaut.env = env
+
+ loop do
+ begin
+ GC.start
+ self.class.rooms = {}
+ EM.epoll if EM.epoll?
+ EM.run do
+ logger.info "#{Time.now} Starting HTTP server on port #{port}"
+ EM.start_server '0.0.0.0', port, Http
+
+ logger.info "#{Time.now} Starting TCP server on port #{port+1}"
+ EM.start_server '0.0.0.0', port + 1, Tcp
+ end
+ rescue Interrupt
+ logger.info "#{Time.now} Shuting down server..."
+ exit
+ rescue
+ logger.error "#{Time.now} Error: " + $!.message
+ logger.error "\t" + $!.backtrace.join("\n\t")
end
- @logger
end
end
+
+ def logger
+ Puggernaut.logger
+ end
end
end
View
8 lib/puggernaut/server/http.rb
@@ -11,19 +11,19 @@ def receive_data(data)
method, request, version = lines.shift.split(' ', 3)
if request.nil?
- logger.error "#{Time.now} strange request: #{[method, request, version].inspect}"
+ logger.error "#{Time.now} Strange request: #{[method, request, version].inspect}"
close_connection
return
else
path, query = request.split('?', 2)
- logger.info "#{Time.now} request on #{path} with #{query}"
+ logger.info "#{Time.now} Request on #{path} with #{query}"
query = CGI.parse(query) if not query.nil?
end
if path == '/'
if query && query['rooms']
@rooms = query['rooms'].split(',').inject([]) do |array, room|
- array << (Puggernaut.rooms[room] ||= Room.new(room))
+ array << (Puggernaut::Server.rooms[room] ||= Room.new(room))
array
end
if query['last']
@@ -33,7 +33,7 @@ def receive_data(data)
array
}.join("\n")
else
- EventMachine::Timer.new(30) { respond }
+ EM::Timer.new(30) { respond }
@subscription_ids = @rooms.collect do |room|
room.subscribe { |str| respond str }
end
View
2  lib/puggernaut/server/tcp.rb
@@ -11,7 +11,7 @@ def receive_data(data)
room, message = line.split('|', 1)
room = Puggernaut.rooms[room] ||= Room.new(room)
id = room.say message
- logger.info "#{Time.now} message #{id} sent to #{room.room}"
+ logger.info "#{Time.now} Message #{id} sent to #{room.room}"
end
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.