Class: HomeQ::SOBS::Server

Inherits:
Object
  • Object
show all
Includes:
Base::Configuration, Base::Logging
Defined in:
lib/homeq/sobs/server.rb

Overview

Server / Listener class

Constant Summary collapse

DEFAULT_PORT =
56000
DEFAULT_HOST =
'localhost'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Base::Configuration

#config

Methods included from Base::Logging

#logger

Constructor Details

#initialize(host = nil, port = nil, queuename = nil, handler = nil) ⇒ Server

Returns a new instance of Server.



573
574
575
576
577
578
579
580
581
582
583
# File 'lib/homeq/sobs/server.rb', line 573

def initialize(host=nil, port=nil, queuename=nil, handler=nil)
  @host 			= host
  @port 			= port
  @connections 		= []
  @foreman 		= nil
  @queue_name 		= queuename
  @total_connections 	= 0
  @started_at		= nil
  @stopped_at		= nil
  @handler		= handler
end

Instance Attribute Details

#connectionsObject

Returns the value of attribute connections.



546
547
548
# File 'lib/homeq/sobs/server.rb', line 546

def connections
  @connections
end

#foremanObject

Returns the value of attribute foreman.



547
548
549
# File 'lib/homeq/sobs/server.rb', line 547

def foreman
  @foreman
end

#queue_nameObject

Returns the value of attribute queue_name.



548
549
550
# File 'lib/homeq/sobs/server.rb', line 548

def queue_name
  @queue_name
end

#total_connectionsObject (readonly)

Returns the value of attribute total_connections.



549
550
551
# File 'lib/homeq/sobs/server.rb', line 549

def total_connections
  @total_connections
end

Class Method Details

.create_home_queue(queuename, handler = nil) ⇒ Object



556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
# File 'lib/homeq/sobs/server.rb', line 556

def self.create_home_queue(queuename, handler=nil)
  logger = HomeQ::Base::Logging::Logger.instance.logger
  config = HomeQ::Base::Configuration::Configuration.instance
  if queuename =~ /^__/
    return
  elsif !config.topology[queuename]
    Base::System.instance.die("No queue info for '#{queuename}'")
  end
  logger.info {
    "Starting home queue '#{queuename}'" +
    (handler ? ", handler: #{handler}" : '')
  }
  q, host, port, hostname = config.topology[queuename]
  server = HomeQ::SOBS::Server.new(host, port, queuename, handler)
  Base::System.instance.servers << server
end

Instance Method Details

#startObject



585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
# File 'lib/homeq/sobs/server.rb', line 585

def start
  @started_at		= Time.now
  @foreman 		||= Foreman.new(self)
  begin
    @signature =
      EventMachine.start_server(@host,
                                @port,
                                HomeQ::SOBS::ServerConnection) { |conn|
      conn.server  = self
      conn.handler = @handler
      @connections << conn
      @total_connections += 1
    }
  rescue Exception => e
    msg = "Error starting SOBS listener '#{@host}:#{@port}'." +  
      "Probably a config error. Original error: #{e.message}."
    Base::System.instance.die(msg)
  else
    logger.info {
      "SOBS server running on #{@host}:#{@port}"
    }
  end
end

#stopObject



609
610
611
612
613
614
615
616
617
618
# File 'lib/homeq/sobs/server.rb', line 609

def stop
  @stopped_at		= Time.now
  EventMachine.stop_server(@signature)
  unless wait_for_connections
    # Still some running
    EventMachine.add_periodic_timer(1) {
      wait_for_connections
    }
  end
end

#to_sObject



620
621
622
623
624
625
626
627
628
629
630
631
# File 'lib/homeq/sobs/server.rb', line 620

def to_s
  str = ''
  str << "#{self.class} Queuename: #{@queue_name}"
  str << " on #{@host}:#{@port}" if @host || @port
  str << "\n"
  str << "Started: #{@started_at} (#{Time.now - @started_at}s)\n"
  str << "Total connections: #{@total_connections} "
  str << "Active connections: #{connections.length}"
  str << ("\n" + connections.join("\n")).gsub(/\n/m, "\n  ")
  str << ("\n" + @foreman.to_s).gsub(/\n/m, "\n  ") if @foreman
  str
end