Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Puggernaut::Server::Http working

  • Loading branch information...
commit dd672bd3dbd5e4d5be8475d17e13f3f55e1f9dd6 1 parent 4dc5d88
Winton Welsh authored
9  bin/puggernaut
... ...
@@ -1,3 +1,10 @@
1 1
 #!/usr/bin/env ruby
2 2
 
3  
-require File.expand_path(File.dirname(__FILE__) + "/../lib/puggernaut")
  3
+require File.expand_path(File.dirname(__FILE__) + "/../lib/puggernaut")
  4
+
  5
+env = ARGV.index('-e')
  6
+port = ARGV.index('-p')
  7
+env = ARGV[env + 1] if env
  8
+port = ARGV[port + 1].to_i if port
  9
+
  10
+Puggernaut::Server.new(*[ env || 'development', port ].compact)
1  lib/puggernaut.rb
@@ -4,6 +4,7 @@
4 4
 
5 5
 require 'eventmachine'
6 6
 require 'logger'
  7
+require 'fileutils'
7 8
 
8 9
 $:.unshift File.dirname(__FILE__)
9 10
 
2  lib/puggernaut/client.rb
@@ -6,7 +6,7 @@ def initialize(env='development', servers={})
6 6
       
7 7
       EM.epoll if EM.epoll?
8 8
       @connections = servers.inject([]) do |array, (host, port)|
9  
-        logger.info "#{Time.now} Starting TCP client for #{host}:#{port}"
  9
+        logger.info "Starting TCP client for #{host}:#{port}"
10 10
         array << EM.connect(host, port, Tcp)
11 11
       end
12 12
     end
2  lib/puggernaut/client/tcp.rb
@@ -18,7 +18,7 @@ def say(messages)
18 18
         messages.each do |room, messages|
19 19
           messages.each do |message|
20 20
             send_data "#{room}|#{message}"
21  
-            logger.info "#{Time.now} Said #{message} to #{room}"
  21
+            logger.info "Said #{message} to #{room}"
22 22
           end
23 23
         end
24 24
       end
8  lib/puggernaut/server.rb
@@ -18,17 +18,17 @@ def initialize(env='development', port=8000)
18 18
           self.class.rooms = {}
19 19
           EM.epoll if EM.epoll?
20 20
           EM.run do
21  
-            logger.info "#{Time.now} Starting HTTP server on port #{port}"
  21
+            logger.info "Starting HTTP server on port #{port}"
22 22
             EM.start_server '0.0.0.0', port, Http
23 23
             
24  
-            logger.info "#{Time.now} Starting TCP server on port #{port+1}"
  24
+            logger.info "Starting TCP server on port #{port+1}"
25 25
             EM.start_server '0.0.0.0', port + 1, Tcp
26 26
           end
27 27
         rescue Interrupt
28  
-          logger.info "#{Time.now} Shuting down server..."
  28
+          logger.info "Shuting down server..."
29 29
           exit
30 30
         rescue
31  
-          logger.error "#{Time.now} Error: " + $!.message
  31
+          logger.error "Error: " + $!.message
32 32
           logger.error "\t" + $!.backtrace.join("\n\t")
33 33
         end
34 34
       end
29  lib/puggernaut/server/http.rb
... ...
@@ -1,3 +1,5 @@
  1
+require 'cgi'
  2
+
1 3
 module Puggernaut
2 4
   class Server
3 5
     module Http
@@ -11,30 +13,34 @@ def receive_data(data)
11 13
         method, request, version = lines.shift.split(' ', 3)
12 14
 
13 15
         if request.nil?
14  
-          logger.error "#{Time.now} Strange request: #{[method, request, version].inspect}"
  16
+          logger.error "Strange request: #{[method, request, version].inspect}"
15 17
           close_connection
16 18
           return
17 19
         else
18 20
           path, query = request.split('?', 2)
19  
-          logger.info "#{Time.now} Request on #{path} with #{query}"
  21
+          logger.info "Request on #{path} with #{query}"
20 22
           query = CGI.parse(query) if not query.nil?
21 23
         end
22 24
 
23 25
         if path == '/'
24  
-          if query && query['rooms']
25  
-            @rooms = query['rooms'].split(',').inject([]) do |array, room|
  26
+          if query && !query['room'].empty?
  27
+            @rooms = query['room'].inject([]) do |array, room|
26 28
               array << (Puggernaut::Server.rooms[room] ||= Room.new(room))
27 29
               array
28 30
             end
29  
-            if query['last']
30  
-              last_ids = query['last'].split(',')
31  
-              respond @rooms.inject([]) { |array, room|
32  
-                array += room.all_messages_after(last_ids.pop)
  31
+            if query['last'] && !query['last'].empty?
  32
+              last = query['last'].dup
  33
+              last = @rooms.inject([]) { |array, room|
  34
+                array += room.all_messages_after(last.pop)
33 35
                 array
34 36
               }.join("\n")
  37
+            end
  38
+            if last && !last.empty?
  39
+              respond last
35 40
             else
36 41
               EM::Timer.new(30) { respond }
37 42
               @subscription_ids = @rooms.collect do |room|
  43
+                logger.info "Waiting for message from room #{room}"
38 44
                 room.subscribe { |str| respond str }
39 45
               end
40 46
             end
@@ -47,6 +53,7 @@ def receive_data(data)
47 53
       end
48 54
 
49 55
       def respond(body='', status=200, content_type='text/plain; charset=utf-8')
  56
+        logger.info "Response #{status}: #{body}"
50 57
         response = [
51 58
           "HTTP/1.1 %d Puggernaut",
52 59
           "Content-length: %d",
@@ -60,8 +67,10 @@ def respond(body='', status=200, content_type='text/plain; charset=utf-8')
60 67
       end
61 68
 
62 69
       def unbind
63  
-        @subscription_ids.each do |id|
64  
-          @rooms.pop.unsubscribe(id)
  70
+        if @subscription_ids
  71
+          @subscription_ids.each do |id|
  72
+            @rooms.pop.unsubscribe(id)
  73
+          end
65 74
         end
66 75
       end
67 76
     end
1  lib/puggernaut/server/room.rb
@@ -7,6 +7,7 @@ class Room < EM::Channel
7 7
       def initialize(room)
8 8
         @messages = []
9 9
         @room = room
  10
+        super()
10 11
       end
11 12
       
12 13
       def all_messages_after(identifier)
2  lib/puggernaut/server/tcp.rb
@@ -11,7 +11,7 @@ def receive_data(data)
11 11
           room, message = line.split('|', 1)
12 12
           room = Puggernaut.rooms[room] ||= Room.new(room)
13 13
           id = room.say message
14  
-          logger.info "#{Time.now} Message #{id} sent to #{room.room}"
  14
+          logger.info "Message #{id} sent to #{room.room}"
15 15
         end
16 16
       end
17 17
     end

0 notes on commit dd672bd

Please sign in to comment.
Something went wrong with that request. Please try again.