Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Everything working (unspec'd)

  • Loading branch information...
commit 589212be4bbcea20d05c70560b71da3eb0bd195e 1 parent dd672bd
Winton Welsh authored
1  .gitignore
View
@@ -1,5 +1,6 @@
.DS_Store
*.gem
coverage
+log/*
pkg
tmp
58 lib/puggernaut/client.rb
View
@@ -1,13 +1,27 @@
+require 'socket'
+#require 'puggernaut/client/tcp'
+
module Puggernaut
class Client
+ attr_accessor :connections
+
def initialize(env='development', servers={})
Puggernaut.env = env
-
- EM.epoll if EM.epoll?
- @connections = servers.inject([]) do |array, (host, port)|
- logger.info "Starting TCP client for #{host}:#{port}"
- array << EM.connect(host, port, Tcp)
+ @connections = {}
+ @retry = []
+ @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
@@ -16,8 +30,38 @@ def logger
end
def say(messages)
- @connections.each do |connection|
- connection.say messages
+ messages.each do |room, message|
+ message =
+ if message.is_a?(::Array)
+ message.collect { |m| "#{room}|#{m}" }.join("\n")
+ else
+ "#{room}|#{message}"
+ end
+ @servers.each do |host, port|
+ send host, port, message
+ end
+ end
+ end
+
+ private
+
+ def send(host, port, data, try_again=true)
+ if try_again
+ @retry.length.times do
+ host, port, data = @retry.shift
+ @connections.delete("#{host}:#{port}")
+ send host, port, data, false
+ end
+ end
+ begin
+ logger.info "Client#send - #{host}:#{port} - #{data}"
+ connection = connect(host, port)
+ connection.print(data)
+ raise 'not ok' unless connection.gets.include?('OK')
+ rescue Exception => e
+ logger.info "Client#send - Exception - #{e.message} - #{host}:#{port} - #{data}"
+ @retry << [ host, port, data ]
+ @retry.shift if @retry.length > 10
end
end
end
42 lib/puggernaut/client/tcp.rb
View
@@ -1,42 +0,0 @@
-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 "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
8 lib/puggernaut/server.rb
View
@@ -18,17 +18,17 @@ def initialize(env='development', port=8000)
self.class.rooms = {}
EM.epoll if EM.epoll?
EM.run do
- logger.info "Starting HTTP server on port #{port}"
+ logger.info "Server#initialize - Starting HTTP - #{port}"
EM.start_server '0.0.0.0', port, Http
- logger.info "Starting TCP server on port #{port+1}"
+ logger.info "Server#initialize - Starting TCP - #{port+1}"
EM.start_server '0.0.0.0', port + 1, Tcp
end
rescue Interrupt
- logger.info "Shuting down server..."
+ logger.info "Server#initialize - Shutting down"
exit
rescue
- logger.error "Error: " + $!.message
+ logger.error "Server#initialize - Error - #{$!.message}"
logger.error "\t" + $!.backtrace.join("\n\t")
end
end
14 lib/puggernaut/server/http.rb
View
@@ -13,12 +13,12 @@ def receive_data(data)
method, request, version = lines.shift.split(' ', 3)
if request.nil?
- logger.error "Strange request: #{[method, request, version].inspect}"
+ logger.error "Server::Http#receive_data - Strange request - #{[method, request, version].inspect}"
close_connection
return
else
path, query = request.split('?', 2)
- logger.info "Request on #{path} with #{query}"
+ logger.info "Server::Http#receive_data - Request - #{path} - #{query}"
query = CGI.parse(query) if not query.nil?
end
@@ -31,7 +31,7 @@ def receive_data(data)
if query['last'] && !query['last'].empty?
last = query['last'].dup
last = @rooms.inject([]) { |array, room|
- array += room.all_messages_after(last.pop)
+ array += room.all_messages_after(last.shift)
array
}.join("\n")
end
@@ -40,7 +40,7 @@ def receive_data(data)
else
EM::Timer.new(30) { respond }
@subscription_ids = @rooms.collect do |room|
- logger.info "Waiting for message from room #{room}"
+ logger.info "Server::Http#receive_data - Subscribed - #{room}"
room.subscribe { |str| respond str }
end
end
@@ -53,7 +53,7 @@ def receive_data(data)
end
def respond(body='', status=200, content_type='text/plain; charset=utf-8')
- logger.info "Response #{status}: #{body}"
+ logger.info "Server::Http#respond - #{status} - #{body}"
response = [
"HTTP/1.1 %d Puggernaut",
"Content-length: %d",
@@ -69,7 +69,9 @@ def respond(body='', status=200, content_type='text/plain; charset=utf-8')
def unbind
if @subscription_ids
@subscription_ids.each do |id|
- @rooms.pop.unsubscribe(id)
+ room = @rooms.shift
+ room.unsubscribe(id)
+ logger.info "Sever::Http#unbind - #{room} - #{id}"
end
end
end
9 lib/puggernaut/server/room.rb
View
@@ -17,13 +17,18 @@ def all_messages_after(identifier)
found
}[1..-1].collect { |message|
"#{@room}|#{message.join '|'}"
- }.join("\n")
+ }
+ end
+
+ def logger
+ Puggernaut.logger
end
def say(message)
message = [ rand.to_s[2..-1], message ]
@messages << message
- @messages.pop if @messages.length > 100
+ @messages.shift if @messages.length > 100
+ logger.info "Server::Room#say - #{@room} - #{message[0]} - #{message[1]}"
push "#{@room}|#{message.join '|'}"
message[0]
end
6 lib/puggernaut/server/tcp.rb
View
@@ -8,10 +8,10 @@ def logger
def receive_data(data)
data.split("\n").each do |line|
- room, message = line.split('|', 1)
- room = Puggernaut.rooms[room] ||= Room.new(room)
+ room, message = line.split('|', 2)
+ room = Puggernaut::Server.rooms[room] ||= Room.new(room)
id = room.say message
- logger.info "Message #{id} sent to #{room.room}"
+ send_data "OK\n"
end
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.