Class: TinyQ::Server

Inherits:
Object
  • Object
show all
Defined in:
lib/tinyq/server.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeServer

Returns a new instance of Server.



8
9
10
11
12
# File 'lib/tinyq/server.rb', line 8

def initialize
    @connections = []
    @buckets = {}
    @funnels = {}
end

Instance Attribute Details

#bucketsObject

Returns the value of attribute buckets.



5
6
7
# File 'lib/tinyq/server.rb', line 5

def buckets
  @buckets
end

#connectionsObject

Returns the value of attribute connections.



3
4
5
# File 'lib/tinyq/server.rb', line 3

def connections
  @connections
end

#funnelsObject

Returns the value of attribute funnels.



6
7
8
# File 'lib/tinyq/server.rb', line 6

def funnels
  @funnels
end

Class Method Details

.startObject



47
48
49
# File 'lib/tinyq/server.rb', line 47

def self.start
    new().start
end

Instance Method Details

#bucket(name) ⇒ Object



85
86
87
88
89
90
91
92
93
94
# File 'lib/tinyq/server.rb', line 85

def bucket(name)
    bucket = @buckets[name]
    if nil == bucket
        $LOG.info("Server - Creating bucket #{name}")
        bucket = Bucket.new(name)
        @buckets[name] = bucket
    end

    bucket
end

#main(args) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/tinyq/server.rb', line 24

def main(args)
    opt = Getopt::Std.getopts("bp:i:dh")

    if opt['h']
        usage
        exit
    end

    @ip = opt['i'] || '127.0.0.1'
    @port = opt['p'] || 64321

    if opt['d']
        $LOG.level = Logger::DEBUG
    end

    if opt['b']
        puts "Going into background"
        Daemons.daemonize
    end
    
    start
end

#remove_connection(c) ⇒ Object



96
97
98
99
100
101
# File 'lib/tinyq/server.rb', line 96

def remove_connection(c)
    @connections.delete(c)
    @funnels.each do |name,funnel|
        funnel.remove_connection(c)
    end
end

#startObject



51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/tinyq/server.rb', line 51

def start
    EventMachine::run {
        trap("TERM") { stop }
        trap("INT") { stop }
        EventMachine.epoll
        @signature = EventMachine.start_server(@ip, @port, Connection) do |con|
            con.server = self
            # We actually do not want to wait on clients
            #@connections.push(con)
        end
        $LOG.info("TinyQ listening on 0.0.0.0:64321")
    }
end

#stopObject



66
67
68
69
70
71
72
73
# File 'lib/tinyq/server.rb', line 66

def stop
    $LOG.info("TinyQ Exiting")
    EventMachine.stop_server(@signature)

    unless wait_for_connections_and_stop
        EventMachine.add_periodic_timer(1) { wait_for_connections_and_stop }
    end
end

#usageObject



14
15
16
17
18
19
20
21
22
# File 'lib/tinyq/server.rb', line 14

def usage
    puts "#{$0}"
    puts "version: #{TinyQ::VERSION}"
    puts " -b go into background/daemonize"
    puts " -p <port> port number to listen to (default: 64321)"
    puts " -i <ip> ip to bind to (default: 127.0.0.1)"
    puts " -d turn on debug"
    puts " -h help, this message"
end

#wait_for_connections_and_stopObject



75
76
77
78
79
80
81
82
83
# File 'lib/tinyq/server.rb', line 75

def wait_for_connections_and_stop
    if @connections.empty?
        EventMachine.stop
        true
    else
        $LOG.info("Waiting for #{@connections.size} connection(s) to finish...")
        false
    end
end