using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Diagnostics; using System.Reflection; using BCG.Utilities; using BCG.Utilities.General; using Bts.DataStreamCore.Services; namespace BCG { /// Derive from this class to create a new proxy stream for a stream that /// should be computed out of process. In the proxy class add constructors /// that exactly mirror the ones you want to expose from the real stream. /// Each of the proxy constructors should just call the base constructor /// passing it a, possibly null VSDB, and the arguments it received in /// exactly the same order. Only non nullparameters of types string, int, bool or /// DateTime are supported. DateTime arguments will loose their time portion, /// string arguments can only have digits or letters and ints have to be positive. /// /// For example, if you real stream is: /// /// class TradeLog : ViewStream /// { /// public TradeLog(string forAccount, string forTrader) {....} /// } /// /// Then your proxy stream would like: /// /// class TradeLogProxy : ProxyStream /// { /// public TradeLogProxy(string forAccount, string forTrader) : base(null,forTrader, forAccount) {} /// } /// /// Unfortunatly, PubSub tags have a very limited alphabet, one that does not even support upper and lower /// case dinstinctions, so some special care needs to be taken by the proxy or the real stream when dealing /// with case sensitive arguments or with string arguments that can contain _ (the only non alpha numeric /// character allowed in a string). The proxy tries to help by supporting the following conventions: /// /// - When passing a symbol, mark the parameter with the kind of symbol you mean, option or /// underlyier. Example: /// public TheosProxy([Option] string symbol) : base(symbol) {} or /// public FuturePositionProxy([Underlyer] string future) : base(symbol) {} /// /// - When passing an exchange mark it with the Exchange attribute. Example: /// public QuoteWidthsProxy([Symbol] string symbol, /// [Exchange] string e) : base(e) {} /// /// - When passing a group name mark it with the group. Since group names can have _ and presumably numbers /// there can be only one group argument which has to be last one. /// attribute. Ex: /// public RiskTheosProxy([Group] string g) : base(g) {} /// /// - When passing an instrument type mark it as InstrumentType. Example: /// public SymbolsOfTypeProxy([InstrumentType] string it) : base(it) {} /// /// - When passing a trader mark it as trader. Example: /// public TraderTrades([trader] string joes) : base(joes) {} /// /// - When passing a string that can contain _, the string argument must /// be the last one in the constructor and should be marked as [LongString] /// public WorkerHealth([LongString] string machineName) : base(machineName) {} /// /// - When passing a symbol, instrument type combo combine both in one string /// with the symbol:InstrumentType format and market it with BCGSym: /// public PositionProxy([BCGSym] string bcgsym) : base(bcgsym) {} /// More complex cases should be handled by the real stream. public abstract class ProxyStream : ViewStream { public enum ProxyParameterType { clrType, optionSymbol, trader, underlyerSymbol, group, exchange, instrumentType, bcgSym } public class ProxyParameter { public ProxyParameterType type; public object value; } public class MarshallingException : SystemException { public MarshallingException (string format, params object [] args) : base(String.Format(format,args) ) {} } [AttributeUsage(AttributeTargets.Parameter, AllowMultiple = false, Inherited = false)] protected class ProxyParameterAttribute : Attribute {} protected class OptionAttribute : ProxyParameterAttribute { } protected class LongStringAttribute : ProxyParameterAttribute { } protected class UnderlyierAttribute : ProxyParameterAttribute { } protected class BCGSymAttribute : ProxyParameterAttribute { } protected class GroupAttribute : ProxyParameterAttribute { } protected class ExchangeAttribute : ProxyParameterAttribute { } protected class TraderAttribute: ProxyParameterAttribute { } protected class InstrumentTypeAttribute : ProxyParameterAttribute { } public string getTag() { return myTag; } private string myTag; protected ProxyStream(ITableAccumulatorFactory tableAccumulatorFactory, params object[] args) => myInit(null, tableAccumulatorFactory, args); protected ProxyStream(ViewStreamDatabase db, ITableAccumulatorFactory tableAccumulatorFactory, params object[] args) => myInit(db, tableAccumulatorFactory, args); class MyException : System.Exception { public MyException (string msg) : base (msg) { } } private static void myThrowIfFalse(bool condition, string msg) { if (!condition) throw new MyException(msg); } private void myInit(ViewStreamDatabase db, ITableAccumulatorFactory tableAccumulatorFactory, params object[] args) { try { ConstructorInfo ci = GetType().GetConstructor(args.Select(t => t.GetType()).ToArray()); foreach (object o in args) myThrowIfFalse(o != null, "Null arguments are not supported."); myTag = myGetTag(ci, args, db != null); mySetRealSource(TableStream.Create("prepub_" + myTag, tableAccumulatorFactory)); } catch (MyException e) { // BlueEventLog2.instance().logEvent(e.Message, BlueEventLog2Level.Fatal); mySetRealSource(TableStream.Create("error_tag", tableAccumulatorFactory)); } } public static string getProxyName(string tag) { return tag.Split('_')[0]; } public static List unmarshal(Type proxyType, string tag) { myThrowIfFalse (proxyType.IsSubclassOf(typeof(ProxyStream)), String.Format("{0} is not a subclass of ProxyStream", proxyType.Name)); myThrowIfFalse (tag.ToLower().StartsWith(proxyType.Name.ToLower()), String.Format("Tag {0} does not match proxy type {1}.", tag, proxyType.Name)); string encodedParameters = tag.Length == proxyType.Name.Length ? "" : tag.Substring((proxyType.Name + "_").Length); ConstructorInfo ci; if (myConstructorsNeedToBeEnconded(proxyType)) { List signature = myUnmarshalConstructorSignature(ref encodedParameters); ci = proxyType.GetConstructor(signature.ToArray()); if (ci == null) { signature.Insert(0, typeof(ViewStreamDatabase)); ci = proxyType.GetConstructor(signature.ToArray()); if (ci == null) throw new MarshallingException("Cannot find constructor for {0} matching signature {1}", proxyType.Name, encodedParameters); } } else ci = proxyType.GetConstructors()[0]; ParameterInfo[] parameters = ci.GetParameters(); List result = new List(); ParameterInfo [] pars = ci.GetParameters(); // skip VSDB if first parameter int i = pars.Length > 0 && pars[0].ParameterType.Equals(typeof(ViewStreamDatabase)) ? 1 : 0; for (;i < pars.Length; i++) result.Add(new ProxyParameter { type = myGetParameterType(pars[i]), value = myGetParameterValue(pars[i], ref encodedParameters) }); return result; } private static List myUnmarshalConstructorSignature(ref string encodedParameters) { List result = new List (); for (int i = 0; i < encodedParameters.Length; i++) if (encodedParameters[i] == 's') result.Add(typeof(string)); else if (encodedParameters[i] == 'd') result.Add(typeof(DateTime)); else if (encodedParameters[i] == 'i') result.Add(typeof(Int32)); else if (encodedParameters[i] == 'b') result.Add(typeof(bool)); else if (encodedParameters[i] == '_') break; else throw new MarshallingException("'{0}' does not start with a valid constructor signature", encodedParameters); if (result.Count > 0) encodedParameters = encodedParameters.Substring(result.Count + 1); return result; } private static ProxyParameterType myGetParameterType(ParameterInfo pi) { if (pi.ParameterType == typeof(string)) { ProxyParameterType result = ProxyParameterType.clrType; object[] attr = pi.GetCustomAttributes(false); if (attr.Length > 0) { foreach (Attribute a in attr) if (a is OptionAttribute) result = ProxyParameterType.optionSymbol; else if (a is ExchangeAttribute) result = ProxyParameterType.exchange; else if (a is TraderAttribute) result = ProxyParameterType.trader; else if (a is GroupAttribute) result = ProxyParameterType.group; else if (a is InstrumentTypeAttribute) result = ProxyParameterType.instrumentType; else if (a is UnderlyierAttribute) result = ProxyParameterType.underlyerSymbol; else if (a is BCGSymAttribute) result = ProxyParameterType.bcgSym; else if (a is LongStringAttribute) result = ProxyParameterType.clrType; } return result; } else if (pi.ParameterType == typeof(Int32) || pi.ParameterType == typeof(DateTime) || pi.ParameterType == typeof(bool)) return ProxyParameterType.clrType; else { myThrowIfFalse (false,String.Format( "{0} is not a supported proxy parameter type", pi.ParameterType.Name)); return ProxyParameterType.clrType; } } private static object myGetParameterValue(ParameterInfo pi, ref string encodedParameters) { if (pi.ParameterType == typeof(DateTime)) { string[] parts = encodedParameters.Split('_'); if (parts.Length < 3) throw new MarshallingException("{0} does not start with a valid date", encodedParameters); int year; if (!Int32.TryParse(parts[0], out year)) throw new MarshallingException("{0} is not a valid year string", parts[0]); int month; if (!Int32.TryParse(parts[1], out month)) throw new MarshallingException("{0} is not a valid month string", parts[1]); int day; if (!Int32.TryParse(parts[2], out day)) throw new MarshallingException("{0} is not a valid day string", parts[2]); DateTime result; try { result = new DateTime(year, month, day); } catch (ArgumentException) { throw new MarshallingException("{0}/{1}/{2} is not a valid date", year, month, day ); } if (parts.Length > 3) encodedParameters = encodedParameters.Substring(parts[0].Length + parts[1].Length + parts[2].Length + 3); else encodedParameters = ""; return result; } else { if (pi.ParameterType == typeof(string) && myIsBCGSym(pi)) { string[] parts = encodedParameters.Split('_'); if (parts.Length < 2) throw new MarshallingException("{0} does not start with a valid BCGSym string", encodedParameters); else { if (parts.Length > 2) encodedParameters = encodedParameters.Substring(parts[0].Length + parts[1].Length + 2); else encodedParameters = ""; return parts[0] + ":" + parts[1]; } } else if (pi.ParameterType == typeof(string) && myIsLongString(pi)) { string result = encodedParameters; encodedParameters = ""; return result; } else { string[] parts = encodedParameters.Split('_'); object result = null; if (pi.ParameterType == typeof(string)) result = parts[0]; else if (pi.ParameterType == typeof(Int32)) { int n; if (!Int32.TryParse(parts[0], out n)) throw new MarshallingException("{0} is not a valid int", parts[0]); result = n; } else if (pi.ParameterType == typeof(Boolean)) { bool b; if (!bool.TryParse(parts[0], out b)) throw new MarshallingException("{0} is not a valid bool", parts[0]); result = b; } else myThrowIfFalse (false, String.Format("{0} is not a supported proxy parameter type", pi.ParameterType.Name)); if (parts.Length > 1) encodedParameters = encodedParameters.Substring(parts[0].Length + 1); else encodedParameters = ""; return result; } } } private bool myValidateString(string s, bool allowUnderscore) { foreach (char c in s) if (!Char.IsDigit(c) && !Char.IsLetter(c) && !(allowUnderscore && c.Equals('_'))) return false; return true; } private static bool myHasAttribute(ParameterInfo pi, Type attrType) { object[] attr = pi.GetCustomAttributes(false); if (attr.Length > 0) foreach (Attribute at in attr) if (at.GetType().Equals(attrType)) return true; return false; } private static bool myIsBCGSym(ParameterInfo pi) {return myHasAttribute(pi, typeof(BCGSymAttribute));} private static bool myIsLongString(ParameterInfo pi) { return myHasAttribute(pi, typeof(LongStringAttribute)) || myHasAttribute(pi, typeof(GroupAttribute)); } private static bool myFirstParameterIsVSDB(ConstructorInfo ci) { return ci.GetParameters().Length > 0 && ci.GetParameters()[0].ParameterType.Equals(typeof(ViewStreamDatabase)); } private static bool myIsSameModuloVSDB(ConstructorInfo ci0, ConstructorInfo ci1) { if ((ci1.GetParameters().Length - ci0.GetParameters().Length) != 1) return false; if (!myFirstParameterIsVSDB(ci1)) return false; for (int i = 0; i < ci0.GetParameters().Length; i++) if (!ci1.GetParameters()[i + 1].ParameterType.Equals( ci0.GetParameters()[i].ParameterType)) return false; return true; } private static bool myConstructorsNeedToBeEnconded(Type t) { ConstructorInfo [] cis = t.GetConstructors(); if (cis.Length == 1) return false; else if (cis.Length > 2) return true; else { ConstructorInfo ci0 = cis[0]; ConstructorInfo ci1 = cis[1]; return !(myIsSameModuloVSDB(ci0, ci1) || myIsSameModuloVSDB(ci1, ci0)); } } private string myGetConstructorSignature(ConstructorInfo ci) { string result =""; int i = myFirstParameterIsVSDB(ci) ? 1 : 0; for (; i < ci.GetParameters().Length; i++) { Type t = ci.GetParameters()[i].ParameterType; if (t.Equals(typeof(string))) result += "s"; else if (t.Equals(typeof(DateTime))) result += "d"; else if (t.Equals(typeof(bool))) result += "b"; else if (t.Equals(typeof(Int32))) result += "i"; else myThrowIfFalse (false, String.Format("{0} is not a supported proxy parameter type", t.Name)); } return result; } private string myGetTag(ConstructorInfo ci, object[] args, bool firstArgIsVSDB) { Type t = ci.DeclaringType; string tag = t.Name; if (myConstructorsNeedToBeEnconded(t)) tag += "_" + myGetConstructorSignature(ci); int i = myFirstParameterIsVSDB(ci) ? 1 : 0; foreach (object a in args) { Debug.Assert(a != null); if (a.GetType() == typeof(DateTime)) { DateTime dt = (DateTime)a; tag += String.Format("_{0}_{1}_{2}", dt.Year, dt.Month, dt.Day); } else if (a.GetType() == typeof(string)) { if (myIsBCGSym(ci.GetParameters()[i])) { string[] parts = a.ToString().Split(':'); myThrowIfFalse (parts.Length == 2, String.Format("Invalid BCGSym string argument {0}. Use sym:InstrumentType", a)); if (!myValidateString(parts[0], false) || !myValidateString(parts[1], false)) { // BlueEventLog2.instance().logEvent( // String.Format( // "Invalid string argument {0}. Currently, only letters and digits are supported in string arguments", // a),BlueEventLog2Level.Error); return "error_tag"; } tag += String.Format("_{0}_{1}", parts[0], parts[1]); } else if (myIsLongString(ci.GetParameters()[i])) { // not a BCGSym if (!myValidateString(a.ToString(), true)) { // BlueEventLog2.instance().logEvent( // String.Format("Invalid string argument {0}. Currently, only letters, digits and _'s are supported in long string arguments", // a),BlueEventLog2Level.Error); return "error_tag"; } //can't do this-- ci.GetParameters() order is undefined //myThrowIfFalse (i.Equals(ci.GetParameters().Length - 1), // "Long string argument must be last"); tag += "_" + a.ToString(); } else { // not a BCGSym if (!myValidateString(a.ToString(), false)) { // BlueEventLog2.instance().logEvent(String.Format( // "Invalid string argument {0}. Currently, only letters and digits are supported in string arguments", a), // BlueEventLog2Level.Error); return "error_tag"; } tag += "_" + a.ToString(); } } else if (a.GetType() == typeof(Int32)) { myThrowIfFalse ((int) a >= 0, String.Format("Invalid int argument {0}. Currently only positive integers are supported.", a)); tag += "_" + a.ToString(); } else if (a.GetType() == typeof(bool)) tag += "_" + a.ToString(); else myThrowIfFalse (false, String.Format("{0} is not a supported proxy parameter type", a.GetType().Name)); i++; } return tag; } } }