Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
119 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -1,8 +1,37 @@ | |||
module Puggernaut | module Puggernaut | ||
class Client | 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 | def logger | ||
Puggernaut.logger | Puggernaut.logger | ||
end | end | ||
|
|||
def say(messages) | |||
@connections.each do |connection| | |||
connection.say messages | |||
end | |||
end | |||
end | end | ||
end | end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters