Permalink
Browse files

Merge "add Router Registrar that will be used to register the dashboa…

…rd to the router"
  • Loading branch information...
2 parents 9de73ab + ecadb91 commit 27a3561c4be866d7a70f5baf235f297428143c00 Vadim Spivak committed with Gerrit Code Review Aug 27, 2011
View
@@ -0,0 +1,6 @@
+source :rubygems
+
+gem "eventmachine"
+gem "nats"
+gem "vcap_logging"
+gem "yajl-ruby"
@@ -0,0 +1,21 @@
+GEM
+ remote: http://rubygems.org/
+ specs:
+ daemons (1.1.4)
+ eventmachine (0.12.10)
+ json_pure (1.5.3)
+ nats (0.4.10)
+ daemons (>= 1.1.0)
+ eventmachine (>= 0.12.10)
+ json_pure (>= 1.5.1)
+ vcap_logging (0.1.0)
+ yajl-ruby (0.8.2)
+
+PLATFORMS
+ ruby
+
+DEPENDENCIES
+ eventmachine
+ nats
+ vcap_logging
+ yajl-ruby
@@ -0,0 +1,11 @@
+#!/usr/bin/env ruby
+$:.unshift(File.expand_path("../lib", File.dirname(__FILE__)))
+
+require "router_registrar"
+
+config_file = ENV["CONFIG_FILE"] || File.expand_path("../config/config.yml", File.dirname(__FILE__))
+RouterRegistrar::Config.configure(YAML.load_file(config_file))
+
+EM.run do
+ RouterRegistrar::RouterRegistrar.new
+end
@@ -0,0 +1,9 @@
+---
+logging:
+ level: info
+mbus: nats://localhost:4222
+uri: test.vcap.me
+host: localhost
+port: 80
+tags:
+ foo: bar
@@ -0,0 +1,63 @@
+$:.unshift(File.expand_path(".", File.dirname(__FILE__)))
+
+require "rubygems"
+require "bundler/setup"
+
+require "eventmachine"
+require "nats/client"
+require "vcap/logging"
+
+module RouterRegistrar
+
+ class Config
+ class << self
+ [:logger, :nats_uri, :uri, :host, :port, :tags].each { |option| attr_accessor option }
+
+ def configure(config)
+ VCAP::Logging.setup_from_config(config["logging"])
+ @logger = VCAP::Logging.logger("router_registrar")
+
+ @nats_uri = config["mbus"]
+
+ @uri = config["uri"]
+ @host = config["host"]
+ @port = config["port"]
+ @tags = config["tags"]
+ end
+ end
+ end
+
+ class RouterRegistrar
+ def initialize
+ @logger = Config.logger
+
+ @registration_message = Yajl::Encoder.encode({
+ :host => Config.host,
+ :port => Config.port,
+ :uris => [Config.uri],
+ :tags => Config.tags
+ })
+
+ NATS.on_error do |e|
+ @logger.fatal("Exiting, NATS error")
+ @logger.fatal(e)
+ exit
+ end
+
+ @nats = NATS.connect(:uri => Config.nats_uri) do
+ @logger.info("Connected to NATS")
+ @nats.subscribe("router.start") do
+ send_registration_message
+ end
+ send_registration_message
+ end
+
+ def send_registration_message
+ @logger.info("Sending registration: #{@registration_message}")
+ @nats.publish("router.register", @registration_message)
+ end
+ end
+
+ end
+
+end

0 comments on commit 27a3561

Please sign in to comment.