Skip to content

Instantly share code, notes, and snippets.

@JulianBirch
Created January 13, 2011 17:42
Show Gist options
  • Save JulianBirch/778249 to your computer and use it in GitHub Desktop.
Save JulianBirch/778249 to your computer and use it in GitHub Desktop.
Dusty.rb
### This assumes that appsettings configurationpath was equal to delta.rb
ext "fixConnection" do
senderCompId "IOM"
targetCompId "JEFNET"
heartbeatInterval 60
reconnectInterval 1
protocolVersion "FIX.4.2"
shouldCheckLatency false
isInitiator true
host "localhost"
portId 12345
shouldLogMessages true
end
static void Dusty(IWindsorContainer container, string fileName, Action<string> log)
{
var engine = IronRuby.Ruby.CreateEngine();
var directory = GetExecutingDirectory();
// Set the search path to include the calling program, so that you can import it with the python code
var paths = engine.GetSearchPaths().ToList();
paths.Add(directory);
engine.SetSearchPaths(paths);
var scriptScope = new ScriptScope(engine, new Scope());
engine.SetVariable(scriptScope, "container", container);
engine.SetVariable(scriptScope, "directory", directory);
engine.SetVariable(scriptScope, "log", log);
using (var reader = new StreamReader(Path.Combine(directory, fileName))) {
try
{
var text = reader.ReadToEnd();
engine.Execute(text, scriptScope);
}catch(Microsoft.Scripting.SyntaxErrorException e)
{
throw new Exception("Ruby code error: " + e.ToString() + "\nIn range "+ e.RawSpan + "\n of Code:\n"+ e.SourceCode);
}
catch (IronRuby.Builtins.SyntaxError e)
{
throw new Exception("Iron Ruby Syntax error\n"+e.Message + "\nSource:"+e.Source+"\nData:"+e.Data+"\n");
}
}
}
private static string GetExecutingDirectory() {
var codebase = Assembly.GetExecutingAssembly().CodeBase;
var uri = new Uri(codebase);
var fileName = uri.LocalPath + uri.Fragment;
return Path.GetDirectoryName(fileName);
}
require "Castle.Windsor"
require 'System.Configuration, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'
require "mscorlib"
# And require anything else you need
include System
include System::Collections::Generic
include System::Configuration
include ColourCoding::Junction
# And any other namespaces you're going to use
##############################################################
## This is the Dusty code. Copy and past to where you want it
##############################################################
$directory = directory
$log = log
def register(&block)
dusty = Dusty.new()
dusty.instance_eval &block
dusty._register(container)
end
class Dusty
include Castle::MicroKernel::Registration
attr_accessor :components
class WorkingComponent
def initialize(c, name)
@c = c
@so = Array.new
@do = Hash.new # System::Collections::Generic::Dictionary[System::String, System::Object]
@name = name
$log.Invoke "CREATE " + name
end
def _registration()
$log.Invoke "REGISTER " + @name
@c
end
def _apply(&block)
@c ||= yield @c
end
def _transient
_apply {|c| c.LifeStyle.Transient}
end
def __prepare(value)
if value.instance_of?(Array)
System::Collections::ArrayList.new(value.map {|x| __prepare x })
elsif value.instance_of?(Symbol)
value.to_s.to_clr_string
elsif value.instance_of?(Integer) || value.instance_of?(Fixnum)
System::Convert.ToInt32(value)
elsif value.instance_of?(TrueClass) || value.instance_of?(FalseClass)
System::Convert.ToBoolean(value)
else
value.to_s.to_clr_string
end
end
def _param(name, value)
isArray = value.instance_of?(Array)
isServiceOverride = isArray ? (value.length > 0 && value[0].instance_of?(Symbol)) : value.instance_of?(Symbol)
v = __prepare value
if isServiceOverride
so = Castle::MicroKernel::Registration::ServiceOverride.ForKey(name.to_clr_string)
so2 = so.Eq(isArray ? System::Array[System::String].new(v) : v)
@c = @c.ServiceOverrides(so2)
else
h = Hash.new
h[name.to_clr_string] = v
h = System::Collections::Hashtable.new h
@c = @c.DependsOn(h)
end
end
def method_missing(sym, *args, &block)
name = sym.to_s
_param name, args[0]
end
end
def initialize()
@components = {}
end
def ext(name, &block)
wc = @components[name]
wc.instance_eval &block unless block.nil?
end
def component(name, serviceType = nil, implementationType = nil, &block)
nameSpecified = name.instance_of?(String)
if !nameSpecified
implementationType = serviceType
serviceType = name
end
implementationType ||= serviceType
if !nameSpecified
name = implementationType.to_clr_type.FullName
end
wc = @components[name]
if wc.nil?
c = Component.For(serviceType.to_clr_type).ImplementedBy(implementationType.to_clr_type).Named(name)
wc = WorkingComponent.new(c, name)
@components[name] = wc
end
wc.instance_eval &block unless block.nil?
end
def includeDelta(fileName)
f = File.new(File.expand_path(fileName, $directory))
code = f.read()
eval code
end
def _register(container)
components.each {|k, v| container.Register(v._registration()) }
end
end
#####################
### End of Dusty Code
#####################
# This is how to use it
register do
# You can run normal ironruby statements
junctionLogFolder = ConfigurationManager.AppSettings["Junction Log Folder"]
installDirectory = System::IO::Path.GetDirectoryName(System::Reflection::Assembly.GetEntryAssembly().Location)
# Simple configuration. You don't need to provide the name
component "Service", IServiceHost, ServiceHost
# You can configure Microsoft Data Services, if you must...
component "TrackerDB", Database, Sql::SqlDatabase do
# You can set constructor parameters
connectionString ConfigurationManager.ConnectionStrings["Tracker"].ConnectionString
end
# This component needed to be transient
component "shards", ShardAssignment, ShardAssignment do
_transient
supportRisklessPrincipal true
end
# This component needed a service reference.
component IProcessor, ParallelProcessor do
statusUpdatesIntervalInMilliseconds 1000
monitor :SQLTracker
end
component "SQLTracker", IProcessingMonitor, MonitoringPersister do
recycleCount 5000
sessionFactory :SQLLog
end
# Generics
component IBroadcaster[IJunctionControlServer], JunctionServerBroadcaster
# This component will get extended in the delta file
component "fixConnection", Connectivity::IFixSession, Connectivity::Connection do
handler :fixHandler
logFolder junctionLogFolder
end
#####################
### Call The Delta
#####################
$log.Invoke "Environmental Delta"
includeDelta ConfigurationManager.AppSettings["ConfigurationPath"]
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment