Skip to content

Commit

Permalink
added development mode with verbose output to log
Browse files Browse the repository at this point in the history
  • Loading branch information
keith@dailymugshot.com committed Jan 9, 2010
1 parent 1144760 commit 4f2303b
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 11 deletions.
3 changes: 3 additions & 0 deletions README.rdoc
Expand Up @@ -38,6 +38,9 @@ All you need to do on the server is:
#start her up!
ActiveSocketServer.new(4001)

#or optionally start in development mode...(verbose info to log)
ActiveSocketServer.new(4001,:development)

Here is a blog post describing an authentication server (with source code zipped at bottom): http://www.activesocket.org/2009/06/authentication-with-activesocket.html

== Use Case
Expand Down
11 changes: 5 additions & 6 deletions common/logger.rb
@@ -1,11 +1,10 @@
module ActiveSocket
def log

@log ||= Logger.new(File.dirname(__FILE__) + "/../debug.log")

# Replace the above line with the following to use Rails logging:
# @log ||= Rails.logger

unless @log
@log = Logger.new(File.dirname(__FILE__) + "/../log.log")
@log.level = (ActiveSocket::ENV == :development) ? Logger::DEBUG : Logger::WARN
end
@log
end
module_function :log
end
5 changes: 4 additions & 1 deletion common/protocol.rb
Expand Up @@ -28,18 +28,21 @@ def self.unpack(str)
begin
obj = Marshal::load(marshalled_response)
rescue
ActiveSocket.log.debug "failure: could not Marshal::load: #{$!}"
ActiveSocket.log.error "failure: could not Marshal::load: #{$!}"
obj = "failure: could not Marshal::load: #{$!}"
end
ActiveSocket.log.debug("unpacked: #{obj.inspect}")
return obj
end

# Pack: takes an object and package it up
# object -> string via marshal -> array of chars -> array of hex values -> string of concat hexs
def self.pack(obj)
begin
ActiveSocket.log.debug("packing object #{obj.inspect}")
str = Marshal::dump(obj)
rescue
ActiveSocket.log.error("Failure: could not Marshal::dump")
str = Marshal::dump("Failure: could not Marshal::dump")
end
m = []
Expand Down
9 changes: 5 additions & 4 deletions server/server.rb
Expand Up @@ -3,11 +3,12 @@ class ActiveSocketServer

include Processors

def initialize(port)
def initialize(port, environment = :production)
ActiveSocket.const_set("ENV", environment)
@serverSocket = TCPServer.new(port)
@serverSocket.setsockopt( Socket::SOL_SOCKET, Socket::SO_REUSEADDR, 1 )
ActiveSocket.log.debug("ActiveSocket Server started on port #{port.to_s}")
puts "ActiveSocket Server started on port #{port.to_s}"
ActiveSocket.log.debug("ActiveSocket Server started on port #{port.to_s} in #{ActiveSocket::ENV} mode")
puts "ActiveSocket Server started on port #{port.to_s} in #{ActiveSocket::ENV} mode"
while 1
wait_for_client
end
Expand All @@ -33,7 +34,7 @@ def help_client

def wait_for_message
if @session.eof?
ActiveSocket.log.debug("they left :(")
ActiveSocket.log.debug("client disconnected :(")
@session.close
return
else
Expand Down

0 comments on commit 4f2303b

Please sign in to comment.