From 3b6c998f674f0afaf815e7a9f99f85478d5fe618 Mon Sep 17 00:00:00 2001 From: Kevin Darby Date: Thu, 20 Sep 2018 18:20:40 -0400 Subject: [PATCH 1/3] spark now has an excel api --- spark_client.sln | 92 ++++++++++++------- src/client/spark_excel/Class1.cs | 20 ++++ .../spark_excel/Properties/AssemblyInfo.cs | 17 ++++ .../Properties/ExcelDna.Build.props | 49 ++++++++++ src/client/spark_excel/packages.config | 5 + src/client/spark_excel/spark_excel-addin.dna | 20 ++++ src/client/spark_excel/spark_excel.csproj | 68 ++++++++++++++ 7 files changed, 236 insertions(+), 35 deletions(-) create mode 100644 src/client/spark_excel/Class1.cs create mode 100644 src/client/spark_excel/Properties/AssemblyInfo.cs create mode 100644 src/client/spark_excel/Properties/ExcelDna.Build.props create mode 100644 src/client/spark_excel/packages.config create mode 100644 src/client/spark_excel/spark_excel-addin.dna create mode 100644 src/client/spark_excel/spark_excel.csproj diff --git a/spark_client.sln b/spark_client.sln index f8aa20aaf..1624dd1a5 100644 --- a/spark_client.sln +++ b/spark_client.sln @@ -4,7 +4,7 @@ VisualStudioVersion = 15.0.27130.2024 MinimumVisualStudioVersion = 15.0.26124.0 Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "spark_api", "src\client\spark_api\spark_api.csproj", "{DC9A8975-8211-4FC9-A9BA-C8B9D26AD481}" EndProject -Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "libspark", "build\src\lib\libspark.vcxproj", "{69D73CF1-9D7A-38CA-9817-0D0C3E3BCB3C}" +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "libspark", "build\src\lib\libspark.vcxproj", "{3335AAF0-489D-359D-8305-9B7A44E2C760}" EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Spark", "src\client\spark\Spark.csproj", "{9A4D4D07-1AB6-4B2B-B111-A4F34D6C267E}" EndProject @@ -12,15 +12,17 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "sparkcmd", "src\client\spar EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "spark_core", "src\client\spark_core\spark_core.csproj", "{6E449EB6-20AC-4FA6-B8D9-10F4BB7E1FEE}" EndProject -Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "ZERO_CHECK", "build\ZERO_CHECK.vcxproj", "{33E7E6C5-04E3-36A8-A751-639BE5E72B9B}" +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "ZERO_CHECK", "build\ZERO_CHECK.vcxproj", "{2256015B-E7D5-3614-8F39-814CA68A4444}" EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "spark_sdk", "src\client\spark_sdk\spark_sdk.csproj", "{6963C314-83C4-4667-B0D0-C2F7AE208C29}" ProjectSection(ProjectDependencies) = postProject - {69D73CF1-9D7A-38CA-9817-0D0C3E3BCB3C} = {69D73CF1-9D7A-38CA-9817-0D0C3E3BCB3C} + {3335AAF0-489D-359D-8305-9B7A44E2C760} = {3335AAF0-489D-359D-8305-9B7A44E2C760} EndProjectSection EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "spark_test", "src\client\spark_test\spark_test.csproj", "{ECD78CC5-138F-4809-A764-2358C2AC2E82}" EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "spark_excel", "src\client\spark_excel\spark_excel.csproj", "{BE9F27C3-B583-47DF-95FA-1F8BBC89DCCB}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -57,22 +59,22 @@ Global {DC9A8975-8211-4FC9-A9BA-C8B9D26AD481}.RelWithDebInfo|x64.Build.0 = Release|x64 {DC9A8975-8211-4FC9-A9BA-C8B9D26AD481}.RelWithDebInfo|x86.ActiveCfg = Release|x64 {DC9A8975-8211-4FC9-A9BA-C8B9D26AD481}.RelWithDebInfo|x86.Build.0 = Release|x64 - {69D73CF1-9D7A-38CA-9817-0D0C3E3BCB3C}.Debug|Any CPU.ActiveCfg = Debug|x64 - {69D73CF1-9D7A-38CA-9817-0D0C3E3BCB3C}.Debug|x64.ActiveCfg = Debug|x64 - {69D73CF1-9D7A-38CA-9817-0D0C3E3BCB3C}.Debug|x64.Build.0 = Debug|x64 - {69D73CF1-9D7A-38CA-9817-0D0C3E3BCB3C}.Debug|x86.ActiveCfg = Debug|x64 - {69D73CF1-9D7A-38CA-9817-0D0C3E3BCB3C}.MinSizeRel|Any CPU.ActiveCfg = MinSizeRel|x64 - {69D73CF1-9D7A-38CA-9817-0D0C3E3BCB3C}.MinSizeRel|x64.ActiveCfg = MinSizeRel|x64 - {69D73CF1-9D7A-38CA-9817-0D0C3E3BCB3C}.MinSizeRel|x64.Build.0 = MinSizeRel|x64 - {69D73CF1-9D7A-38CA-9817-0D0C3E3BCB3C}.MinSizeRel|x86.ActiveCfg = MinSizeRel|x64 - {69D73CF1-9D7A-38CA-9817-0D0C3E3BCB3C}.Release|Any CPU.ActiveCfg = Release|x64 - {69D73CF1-9D7A-38CA-9817-0D0C3E3BCB3C}.Release|x64.ActiveCfg = Release|x64 - {69D73CF1-9D7A-38CA-9817-0D0C3E3BCB3C}.Release|x64.Build.0 = Release|x64 - {69D73CF1-9D7A-38CA-9817-0D0C3E3BCB3C}.Release|x86.ActiveCfg = Release|x64 - {69D73CF1-9D7A-38CA-9817-0D0C3E3BCB3C}.RelWithDebInfo|Any CPU.ActiveCfg = RelWithDebInfo|x64 - {69D73CF1-9D7A-38CA-9817-0D0C3E3BCB3C}.RelWithDebInfo|x64.ActiveCfg = RelWithDebInfo|x64 - {69D73CF1-9D7A-38CA-9817-0D0C3E3BCB3C}.RelWithDebInfo|x64.Build.0 = RelWithDebInfo|x64 - {69D73CF1-9D7A-38CA-9817-0D0C3E3BCB3C}.RelWithDebInfo|x86.ActiveCfg = RelWithDebInfo|x64 + {3335AAF0-489D-359D-8305-9B7A44E2C760}.Debug|Any CPU.ActiveCfg = Debug|x64 + {3335AAF0-489D-359D-8305-9B7A44E2C760}.Debug|x64.ActiveCfg = Debug|x64 + {3335AAF0-489D-359D-8305-9B7A44E2C760}.Debug|x64.Build.0 = Debug|x64 + {3335AAF0-489D-359D-8305-9B7A44E2C760}.Debug|x86.ActiveCfg = Debug|x64 + {3335AAF0-489D-359D-8305-9B7A44E2C760}.MinSizeRel|Any CPU.ActiveCfg = MinSizeRel|x64 + {3335AAF0-489D-359D-8305-9B7A44E2C760}.MinSizeRel|x64.ActiveCfg = MinSizeRel|x64 + {3335AAF0-489D-359D-8305-9B7A44E2C760}.MinSizeRel|x64.Build.0 = MinSizeRel|x64 + {3335AAF0-489D-359D-8305-9B7A44E2C760}.MinSizeRel|x86.ActiveCfg = MinSizeRel|x64 + {3335AAF0-489D-359D-8305-9B7A44E2C760}.Release|Any CPU.ActiveCfg = Release|x64 + {3335AAF0-489D-359D-8305-9B7A44E2C760}.Release|x64.ActiveCfg = Release|x64 + {3335AAF0-489D-359D-8305-9B7A44E2C760}.Release|x64.Build.0 = Release|x64 + {3335AAF0-489D-359D-8305-9B7A44E2C760}.Release|x86.ActiveCfg = Release|x64 + {3335AAF0-489D-359D-8305-9B7A44E2C760}.RelWithDebInfo|Any CPU.ActiveCfg = RelWithDebInfo|x64 + {3335AAF0-489D-359D-8305-9B7A44E2C760}.RelWithDebInfo|x64.ActiveCfg = RelWithDebInfo|x64 + {3335AAF0-489D-359D-8305-9B7A44E2C760}.RelWithDebInfo|x64.Build.0 = RelWithDebInfo|x64 + {3335AAF0-489D-359D-8305-9B7A44E2C760}.RelWithDebInfo|x86.ActiveCfg = RelWithDebInfo|x64 {9A4D4D07-1AB6-4B2B-B111-A4F34D6C267E}.Debug|Any CPU.ActiveCfg = Debug|x64 {9A4D4D07-1AB6-4B2B-B111-A4F34D6C267E}.Debug|x64.ActiveCfg = Debug|x64 {9A4D4D07-1AB6-4B2B-B111-A4F34D6C267E}.Debug|x64.Build.0 = Debug|x64 @@ -137,22 +139,22 @@ Global {6E449EB6-20AC-4FA6-B8D9-10F4BB7E1FEE}.RelWithDebInfo|x64.Build.0 = Release|x64 {6E449EB6-20AC-4FA6-B8D9-10F4BB7E1FEE}.RelWithDebInfo|x86.ActiveCfg = Release|x64 {6E449EB6-20AC-4FA6-B8D9-10F4BB7E1FEE}.RelWithDebInfo|x86.Build.0 = Release|x64 - {33E7E6C5-04E3-36A8-A751-639BE5E72B9B}.Debug|Any CPU.ActiveCfg = Debug|x64 - {33E7E6C5-04E3-36A8-A751-639BE5E72B9B}.Debug|x64.ActiveCfg = Debug|x64 - {33E7E6C5-04E3-36A8-A751-639BE5E72B9B}.Debug|x64.Build.0 = Debug|x64 - {33E7E6C5-04E3-36A8-A751-639BE5E72B9B}.Debug|x86.ActiveCfg = Debug|x64 - {33E7E6C5-04E3-36A8-A751-639BE5E72B9B}.MinSizeRel|Any CPU.ActiveCfg = MinSizeRel|x64 - {33E7E6C5-04E3-36A8-A751-639BE5E72B9B}.MinSizeRel|x64.ActiveCfg = MinSizeRel|x64 - {33E7E6C5-04E3-36A8-A751-639BE5E72B9B}.MinSizeRel|x64.Build.0 = MinSizeRel|x64 - {33E7E6C5-04E3-36A8-A751-639BE5E72B9B}.MinSizeRel|x86.ActiveCfg = MinSizeRel|x64 - {33E7E6C5-04E3-36A8-A751-639BE5E72B9B}.Release|Any CPU.ActiveCfg = Release|x64 - {33E7E6C5-04E3-36A8-A751-639BE5E72B9B}.Release|x64.ActiveCfg = Release|x64 - {33E7E6C5-04E3-36A8-A751-639BE5E72B9B}.Release|x64.Build.0 = Release|x64 - {33E7E6C5-04E3-36A8-A751-639BE5E72B9B}.Release|x86.ActiveCfg = Release|x64 - {33E7E6C5-04E3-36A8-A751-639BE5E72B9B}.RelWithDebInfo|Any CPU.ActiveCfg = RelWithDebInfo|x64 - {33E7E6C5-04E3-36A8-A751-639BE5E72B9B}.RelWithDebInfo|x64.ActiveCfg = RelWithDebInfo|x64 - {33E7E6C5-04E3-36A8-A751-639BE5E72B9B}.RelWithDebInfo|x64.Build.0 = RelWithDebInfo|x64 - {33E7E6C5-04E3-36A8-A751-639BE5E72B9B}.RelWithDebInfo|x86.ActiveCfg = RelWithDebInfo|x64 + {2256015B-E7D5-3614-8F39-814CA68A4444}.Debug|Any CPU.ActiveCfg = Debug|x64 + {2256015B-E7D5-3614-8F39-814CA68A4444}.Debug|x64.ActiveCfg = Debug|x64 + {2256015B-E7D5-3614-8F39-814CA68A4444}.Debug|x64.Build.0 = Debug|x64 + {2256015B-E7D5-3614-8F39-814CA68A4444}.Debug|x86.ActiveCfg = Debug|x64 + {2256015B-E7D5-3614-8F39-814CA68A4444}.MinSizeRel|Any CPU.ActiveCfg = MinSizeRel|x64 + {2256015B-E7D5-3614-8F39-814CA68A4444}.MinSizeRel|x64.ActiveCfg = MinSizeRel|x64 + {2256015B-E7D5-3614-8F39-814CA68A4444}.MinSizeRel|x64.Build.0 = MinSizeRel|x64 + {2256015B-E7D5-3614-8F39-814CA68A4444}.MinSizeRel|x86.ActiveCfg = MinSizeRel|x64 + {2256015B-E7D5-3614-8F39-814CA68A4444}.Release|Any CPU.ActiveCfg = Release|x64 + {2256015B-E7D5-3614-8F39-814CA68A4444}.Release|x64.ActiveCfg = Release|x64 + {2256015B-E7D5-3614-8F39-814CA68A4444}.Release|x64.Build.0 = Release|x64 + {2256015B-E7D5-3614-8F39-814CA68A4444}.Release|x86.ActiveCfg = Release|x64 + {2256015B-E7D5-3614-8F39-814CA68A4444}.RelWithDebInfo|Any CPU.ActiveCfg = RelWithDebInfo|x64 + {2256015B-E7D5-3614-8F39-814CA68A4444}.RelWithDebInfo|x64.ActiveCfg = RelWithDebInfo|x64 + {2256015B-E7D5-3614-8F39-814CA68A4444}.RelWithDebInfo|x64.Build.0 = RelWithDebInfo|x64 + {2256015B-E7D5-3614-8F39-814CA68A4444}.RelWithDebInfo|x86.ActiveCfg = RelWithDebInfo|x64 {6963C314-83C4-4667-B0D0-C2F7AE208C29}.Debug|Any CPU.ActiveCfg = Debug|x64 {6963C314-83C4-4667-B0D0-C2F7AE208C29}.Debug|x64.ActiveCfg = Debug|x64 {6963C314-83C4-4667-B0D0-C2F7AE208C29}.Debug|x64.Build.0 = Debug|x64 @@ -197,6 +199,26 @@ Global {ECD78CC5-138F-4809-A764-2358C2AC2E82}.RelWithDebInfo|x64.Build.0 = Release|Any CPU {ECD78CC5-138F-4809-A764-2358C2AC2E82}.RelWithDebInfo|x86.ActiveCfg = Release|Any CPU {ECD78CC5-138F-4809-A764-2358C2AC2E82}.RelWithDebInfo|x86.Build.0 = Release|Any CPU + {BE9F27C3-B583-47DF-95FA-1F8BBC89DCCB}.Debug|Any CPU.ActiveCfg = Debug|x64 + {BE9F27C3-B583-47DF-95FA-1F8BBC89DCCB}.Debug|x64.ActiveCfg = Debug|x64 + {BE9F27C3-B583-47DF-95FA-1F8BBC89DCCB}.Debug|x64.Build.0 = Debug|x64 + {BE9F27C3-B583-47DF-95FA-1F8BBC89DCCB}.Debug|x86.ActiveCfg = Debug|x64 + {BE9F27C3-B583-47DF-95FA-1F8BBC89DCCB}.MinSizeRel|Any CPU.ActiveCfg = Release|x64 + {BE9F27C3-B583-47DF-95FA-1F8BBC89DCCB}.MinSizeRel|Any CPU.Build.0 = Release|x64 + {BE9F27C3-B583-47DF-95FA-1F8BBC89DCCB}.MinSizeRel|x64.ActiveCfg = Release|x64 + {BE9F27C3-B583-47DF-95FA-1F8BBC89DCCB}.MinSizeRel|x64.Build.0 = Release|x64 + {BE9F27C3-B583-47DF-95FA-1F8BBC89DCCB}.MinSizeRel|x86.ActiveCfg = Release|x64 + {BE9F27C3-B583-47DF-95FA-1F8BBC89DCCB}.MinSizeRel|x86.Build.0 = Release|x64 + {BE9F27C3-B583-47DF-95FA-1F8BBC89DCCB}.Release|Any CPU.ActiveCfg = Release|x64 + {BE9F27C3-B583-47DF-95FA-1F8BBC89DCCB}.Release|x64.ActiveCfg = Release|x64 + {BE9F27C3-B583-47DF-95FA-1F8BBC89DCCB}.Release|x64.Build.0 = Release|x64 + {BE9F27C3-B583-47DF-95FA-1F8BBC89DCCB}.Release|x86.ActiveCfg = Release|x64 + {BE9F27C3-B583-47DF-95FA-1F8BBC89DCCB}.RelWithDebInfo|Any CPU.ActiveCfg = Release|x64 + {BE9F27C3-B583-47DF-95FA-1F8BBC89DCCB}.RelWithDebInfo|Any CPU.Build.0 = Release|x64 + {BE9F27C3-B583-47DF-95FA-1F8BBC89DCCB}.RelWithDebInfo|x64.ActiveCfg = Release|x64 + {BE9F27C3-B583-47DF-95FA-1F8BBC89DCCB}.RelWithDebInfo|x64.Build.0 = Release|x64 + {BE9F27C3-B583-47DF-95FA-1F8BBC89DCCB}.RelWithDebInfo|x86.ActiveCfg = Release|x64 + {BE9F27C3-B583-47DF-95FA-1F8BBC89DCCB}.RelWithDebInfo|x86.Build.0 = Release|x64 EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/src/client/spark_excel/Class1.cs b/src/client/spark_excel/Class1.cs new file mode 100644 index 000000000..e6d22417e --- /dev/null +++ b/src/client/spark_excel/Class1.cs @@ -0,0 +1,20 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +using ExcelDna.Integration; + + +namespace bts.excel +{ + public static class MyFunctions + { + [ExcelFunction(Description = "My first .NET function")] + public static string SayHello(string name) + { + return "Hello " + name; + } + } +} diff --git a/src/client/spark_excel/Properties/AssemblyInfo.cs b/src/client/spark_excel/Properties/AssemblyInfo.cs new file mode 100644 index 000000000..95ebd4d2e --- /dev/null +++ b/src/client/spark_excel/Properties/AssemblyInfo.cs @@ -0,0 +1,17 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +[assembly: InternalsVisibleTo("spark")] +[assembly: InternalsVisibleTo("spark_core")] +[assembly: InternalsVisibleTo("sparkcmd")] +[assembly: InternalsVisibleTo("spark_test")] + + +// Setting ComVisible to false makes the types in this assembly not visible +// to COM components. If you need to access a type in this assembly from +// COM, set the ComVisible attribute to true on that type. +[assembly: ComVisible(false)] + +// The following GUID is for the ID of the typelib if this project is exposed to COM +[assembly: Guid("b319f835-7390-47e5-8093-461ed0dfce63")] diff --git a/src/client/spark_excel/Properties/ExcelDna.Build.props b/src/client/spark_excel/Properties/ExcelDna.Build.props new file mode 100644 index 000000000..cb4657bfb --- /dev/null +++ b/src/client/spark_excel/Properties/ExcelDna.Build.props @@ -0,0 +1,49 @@ + + + + + + + + + false + true + + + + 64 + + + + + + true + + + -packed + + + diff --git a/src/client/spark_excel/packages.config b/src/client/spark_excel/packages.config new file mode 100644 index 000000000..b38fa8ac2 --- /dev/null +++ b/src/client/spark_excel/packages.config @@ -0,0 +1,5 @@ + + + + + \ No newline at end of file diff --git a/src/client/spark_excel/spark_excel-addin.dna b/src/client/spark_excel/spark_excel-addin.dna new file mode 100644 index 000000000..37e6fa6d2 --- /dev/null +++ b/src/client/spark_excel/spark_excel-addin.dna @@ -0,0 +1,20 @@ + + + + + + diff --git a/src/client/spark_excel/spark_excel.csproj b/src/client/spark_excel/spark_excel.csproj new file mode 100644 index 000000000..5371a08ad --- /dev/null +++ b/src/client/spark_excel/spark_excel.csproj @@ -0,0 +1,68 @@ + + + + net471 + Library + + + Blue Trading Systems + Blue Trading Systems + Spark + + true + x64 + AnyCPU + true + + 1.0.0 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + From 6582065be0925a36e2b63fc7ac12281fc7c3d210 Mon Sep 17 00:00:00 2001 From: Kevin Darby Date: Fri, 21 Sep 2018 09:38:53 -0400 Subject: [PATCH 2/3] moving excel code over --- src/client/spark_excel/Class1.cs | 20 -- src/client/spark_excel/ExcelAddin.cs | 60 ++++ src/client/spark_excel/rtd/DataParser.cs | 313 ++++++++++++++++++ src/client/spark_excel/rtd/RTDServer.cs | 114 +++++++ src/client/spark_excel/rtd/Subscribe.cs | 93 ++++++ src/client/spark_excel/rtd/Topic.cs | 69 ++++ src/client/spark_excel/utils/APIDispatch.cs | 125 +++++++ .../spark_excel/utils/ReflectionExtensions.cs | 108 ++++++ src/client/spark_excel/utils/Utilities.cs | 247 ++++++++++++++ 9 files changed, 1129 insertions(+), 20 deletions(-) delete mode 100644 src/client/spark_excel/Class1.cs create mode 100644 src/client/spark_excel/ExcelAddin.cs create mode 100644 src/client/spark_excel/rtd/DataParser.cs create mode 100644 src/client/spark_excel/rtd/RTDServer.cs create mode 100644 src/client/spark_excel/rtd/Subscribe.cs create mode 100644 src/client/spark_excel/rtd/Topic.cs create mode 100644 src/client/spark_excel/utils/APIDispatch.cs create mode 100644 src/client/spark_excel/utils/ReflectionExtensions.cs create mode 100644 src/client/spark_excel/utils/Utilities.cs diff --git a/src/client/spark_excel/Class1.cs b/src/client/spark_excel/Class1.cs deleted file mode 100644 index e6d22417e..000000000 --- a/src/client/spark_excel/Class1.cs +++ /dev/null @@ -1,20 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -using ExcelDna.Integration; - - -namespace bts.excel -{ - public static class MyFunctions - { - [ExcelFunction(Description = "My first .NET function")] - public static string SayHello(string name) - { - return "Hello " + name; - } - } -} diff --git a/src/client/spark_excel/ExcelAddin.cs b/src/client/spark_excel/ExcelAddin.cs new file mode 100644 index 000000000..dbca9748b --- /dev/null +++ b/src/client/spark_excel/ExcelAddin.cs @@ -0,0 +1,60 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Reflection; + +using ExcelDna.Integration; +using System.IO; +using System.Security.Policy; +using System.Diagnostics; + +namespace bts.excel +{ + public class TestMethod + { + [ExcelFunction] + static public string bts_excel_test(string x) { return "Hello " + x; } + } + + [System.Runtime.InteropServices.ComVisible(true)] + public class ContextTeardown : ExcelComAddIn + { + private api.core.ServiceContext _context; + public ContextTeardown(api.core.ServiceContext context) + { + _context = context; + } + //This is called on application shutdown + public override void OnBeginShutdown(ref Array custom) + { + _context.Dispose(); + _context = null; + } + } + + [System.Runtime.InteropServices.ComVisible(true)] + public class ExcelAddin : IExcelAddIn + { + public void AutoClose() { /*don't do anything here, it's only called when the addin is removed, not on shutdown*/ } + public void AutoOpen() //This is called on EXCEL startup, when the addin is loaded + { + ExcelIntegration.RegisterUnhandledExceptionHandler(ex => ex.ToString()); + + load_assemblies(); + ExcelComAddInHelper.LoadComAddIn(new ContextTeardown(APIDispatch.service_context.Value)); + } + + private void load_assemblies() + { + var config = System.Configuration.ConfigurationManager.AppSettings["load_assemblies"]; + if (config != null) + { + var asses = config.Split(new char[] { ';', ',', ' ' }, StringSplitOptions.RemoveEmptyEntries); + foreach (var ass in asses) + Utilities.bts_load_assembly(ass); + } + } + } +} \ No newline at end of file diff --git a/src/client/spark_excel/rtd/DataParser.cs b/src/client/spark_excel/rtd/DataParser.cs new file mode 100644 index 000000000..e049ff18d --- /dev/null +++ b/src/client/spark_excel/rtd/DataParser.cs @@ -0,0 +1,313 @@ +using System; +using System.Collections.Generic; +using System.Reflection; +using System.Linq; +using bts.excel.extensions; +using System.Text.RegularExpressions; + +namespace bts.excel +{ + public class DataParser + { + private static Type analyze_type(Type type, out bool is_array) + { + Type element_type = type; + is_array = false; + if (element_type.IsArray) + { + is_array = true; + element_type = element_type.GetElementType(); + } + return element_type; + } + + public static bool supported_primitive(Type t) + { + bool dummy; + return supported_primitive(t, out dummy); + } + + public static bool supported_primitive(Type t, out bool is_array) + { + is_array = t.IsArray; + if (is_array) + t = t.GetElementType(); + return t.IsPrimitive || t.IsEnum || t == typeof(string) || t == typeof(DateTime); + } + + private static bool excluded_member(MemberInfo s) + { + return s.Name.ToLower() == "extensionobject" || + s.Name.ToLower() == "proto" || + s.Name.ToLower() == "descriptor" || + + s.Name.Contains(".") + ; + } + + private static void analyze_fields(Type element_type, + out List superduperfields, out List superfields, out List fields, + out List superdupernames, out List supernames, out List names, out List combined) + { + var members = ReflectionExtensions.get_fields_or_properties(element_type); + superduperfields = new List(); + superfields = new List(); + fields = new List(); + superdupernames = new List(); + supernames = new List(); + names = new List(); + combined = new List(); + foreach (var r in members) + { + if (excluded_member(r)) + continue; + if (supported_primitive(ReflectionExtensions.member_type(r))) + { + superduperfields.Add(r); + superdupernames.Add(r.Name); + superfields.Add(r); + supernames.Add(r.Name); + fields.Add(r); + names.Add(r.Name); + combined.Add(r.Name); + } + else + { + var sub = ReflectionExtensions.get_fields_or_properties(ReflectionExtensions.member_type(r)); + foreach (var s in sub) + { + if (excluded_member(s)) + continue; + if (supported_primitive(ReflectionExtensions.member_type(s))) + { + superduperfields.Add(r); + superdupernames.Add(r.Name); + superfields.Add(r); + supernames.Add(r.Name); + fields.Add(s); + names.Add(s.Name); + combined.Add(r.Name + "." + s.Name); + } + else + { + var subsub = ReflectionExtensions.get_fields_or_properties(ReflectionExtensions.member_type(s)); + foreach (var ss in subsub) + { + if (excluded_member(ss) || !supported_primitive(ReflectionExtensions.member_type(ss))) + continue; + superduperfields.Add(r); + superdupernames.Add(r.Name); + superfields.Add(s); + supernames.Add(s.Name); + fields.Add(ss); + names.Add(ss.Name); + combined.Add(r.Name + "." + s.Name + "." + ss.Name); + } + } + } + } + } + } + + static public object gather_data(Type type, object[,] data) + { + if (type == null) + return null; + bool is_array; + Type element_type = analyze_type(type, out is_array); + + bool field_name_headers = true; + List superduperfields, superfields, fields; + List superdupernames, supernames, names, combined; + analyze_fields(element_type, out superduperfields, out superfields, out fields, + out superdupernames, out supernames, out names, out combined); + + int object_len = data.GetLength(1) - (field_name_headers ? 1 : 0); + Array objects = Array.CreateInstance(element_type, object_len); + for (int j = 0; j < objects.Length; j++) + { + object target = Activator.CreateInstance(element_type); + for (int zz = 0; zz < superduperfields.Count; zz++) + { + if (Regex.Matches(combined[zz], @"\.").Count == 2) + ReflectionExtensions.set_value(superduperfields[zz], target, Activator.CreateInstance(ReflectionExtensions.member_type(superduperfields[zz]))); + } + for (int z = 0; z < superfields.Count; z++) + { + if (combined[z].Contains(".")) + ReflectionExtensions.set_value(superfields[z], target, Activator.CreateInstance(ReflectionExtensions.member_type(superfields[z]))); + } + for (int i = 0; i < data.GetLength(0); i++) + { + string name = data[i, 0].ToString(); + int idx = combined.IndexOf(name); + if (idx < 0 || idx >= combined.Count) + continue; + + MemberInfo fi = fields[idx]; + object val = val = data[i, j + (field_name_headers ? 1 : 0)]; + val = read_data(ReflectionExtensions.member_type(fi), val); + int dotcount = Regex.Matches(combined[i], @"\.").Count; + if (dotcount == 2) + { + object subsubtarget = ReflectionExtensions.get_value(superduperfields[i], target); + if (subsubtarget != null) + { + object subtarget = ReflectionExtensions.get_value(superfields[i], subsubtarget); + if (subtarget != null) + ReflectionExtensions.set_value(fi, subtarget, val); + } + } + else if (dotcount == 1) + { + object subtarget = ReflectionExtensions.get_value(superfields[i], target); + if (subtarget != null) + ReflectionExtensions.set_value(fi, subtarget, val); + } + else if (dotcount == 0) + ReflectionExtensions.set_value(fi, target, val); + } + objects.SetValue(target, j); + } + + if (is_array) + return objects; + else if (objects.Length > 0) + return objects.GetValue(0); + else + return null; + } + + public static object read_data(Type field_type, object val) + { + if (typeof(Array).IsAssignableFrom(field_type)) + val = unstringify_array(val, field_type); + else if (field_type == typeof(DateTime)) + val = DateTime.FromOADate(Convert.ToDouble(val)); + else if (field_type.IsEnum) + val = Enum.Parse(field_type, val.ToString()); + else + val = Convert.ChangeType(val, field_type); + return val; + } + + public static object[,] scatter_data(object data, bool include_field_names = true, Type type_override = null) + { + if (data == null) + return null; + + bool is_array; + Type type = type_override ?? data.GetType(); + if (type.IsGenericType && type.GetGenericTypeDefinition().Equals(typeof(core.TableUpdate<,>))) + { + Tuple tup = core.ObservableUpdateExtensions.key_value_tuple((dynamic)data); + return scatter_data(tup.Item2, include_field_names, tup.Item2.FirstOrDefault()?.GetType().MakeArrayType()); + } + Type element_type = analyze_type(type, out is_array); + + List superduperfields, superfields, fields; + List superdupernames, supernames, names, combined; + analyze_fields(element_type, out superduperfields, out superfields, out fields, + out superdupernames, out supernames, out names, out combined); + + Array the_data = (is_array) ? (Array)data : new object[] { data }; + int len = the_data.GetLength(0); + + var ret = new object[fields.Count, len + (include_field_names ? 1 : 0)]; + for (int j = 0; j < ret.GetLength(1); j++) + { + for (int i = 0; i < ret.GetLength(0); i++) + { + object dat = ""; + int dotcount = Regex.Matches(combined[i], @"\.").Count; + if (dotcount == 2) + { + if (j == 0 && include_field_names) + dat = combined[i]; + else + { + object subsub = ReflectionExtensions.get_value(superduperfields[i], the_data.GetValue(j - (include_field_names ? 1 : 0))); + if (subsub != null) + { + object sub = ReflectionExtensions.get_value(superfields[i], subsub); + if (sub != null) + dat = ReflectionExtensions.get_value(fields[i], sub); + } + } + } + else if (dotcount == 1) + { + if (j == 0 && include_field_names) + dat = combined[i]; + else + { + object sub = ReflectionExtensions.get_value(superfields[i], the_data.GetValue(j - (include_field_names ? 1 : 0))); + if (sub != null) + dat = ReflectionExtensions.get_value(fields[i], sub); + } + } + else if (dotcount == 0) + { + if (j == 0 && include_field_names) + dat = names[i]; + else + dat = ReflectionExtensions.get_value(fields[i], the_data.GetValue(j - (include_field_names ? 1 : 0))); + } + else + dat = "Unknown value: " + combined[i]; + write_data(out ret[i, j], dat); + } + } + return ret; + } + + public static void write_data(out object ret, object dat) + { + ret = dat; + if (dat is DateTime) + ret = ((DateTime)dat).ToOADate(); + else if (dat is Enum) + ret = dat.ToString(); + else if (dat is Array) + { + Array ary = (Array)dat; + ret = stringify_array(ary); + } + } + + public static string stringify_array(Array array) + { + string stringify = "{ "; + int idx = 0; + foreach (object d in array) + { + object o; + write_data(out o, d); + stringify += o.ToString() + ((idx++ == array.Length - 1) ? string.Empty : ","); + } + stringify += " }"; + return stringify; + } + + public static Array unstringify_array(object val, Type field_type = null) + { + if (field_type == null) + field_type = typeof(string[]); + + string[] data = val.ToString().Split(new char[] { ',', '{', '}' }, StringSplitOptions.RemoveEmptyEntries); + Type dt = field_type.GetElementType(); + Array ary = Array.CreateInstance(dt, data.Length); + for (int i = 0; i < data.Length; i++) + { + object entry = null; + try + { + entry = read_data(dt, Convert.ChangeType(data[i], dt)); + } + catch (FormatException) { } + ary.SetValue(entry, i); + } + return ary; + } + } +} \ No newline at end of file diff --git a/src/client/spark_excel/rtd/RTDServer.cs b/src/client/spark_excel/rtd/RTDServer.cs new file mode 100644 index 000000000..f11438684 --- /dev/null +++ b/src/client/spark_excel/rtd/RTDServer.cs @@ -0,0 +1,114 @@ +using System; +using System.Collections.Generic; +using System.Collections.Concurrent; +using System.Linq; + +using ExcelDna.Integration; +using ExcelDna.Integration.Rtd; +using bts.excel.extensions; + +namespace bts.excel.rtd +{ + [System.Runtime.InteropServices.ComVisible(true)] + public class RTDServer : ExcelRtdServer + { + readonly Dictionary> _subs = new Dictionary>(); + readonly List _disps = new List(); + + readonly static ConcurrentDictionary data_store = new ConcurrentDictionary(); + + public RTDServer() + { + } + + static public bool retrieve(string key, out object data) + { + return data_store.TryGetValue(key, out data); + } + + protected override bool ServerStart() { return true; } + + protected override void ServerTerminate() { } + + protected override RtdServer.Topic CreateTopic(int topicId, IList topicInfo) + { + string[] tagvals = new string[topicInfo.Count - 2]; + Array.Copy(topicInfo.ToArray(), 2, tagvals, 0, tagvals.Length); + var bt = new Topic(this, topicId, topicInfo[0], topicInfo[1], tagvals); + return bt; + } + + protected override object ConnectData(Topic topic, IList topicInfo, ref bool newValues) + { + //return ExcelErrorUtil.ToComError(ExcelError.ExcelErrorNA); + newValues = true; + Topic bt = (Topic)topic; + IObservable obs = null; + if (bt.service_type == null) + return "Unknown ServiceType: " + bt.service_type_string; + if (bt.request_type == null) + return "Unknown MethodName: " + bt.method_name; + try + { + obs = subscribe(bt); + if (obs == null) + return "Something went haywire in Service/Request reflection: " + bt.service_type_string + bt.method_name; + _subs[topic.TopicId] = obs; + } + catch (Exception e) + { + return "PLEASE RESUBSCRIBE! Error: " + e.ToString(); + } + _disps.Add(obs.Subscribe((data) => + { + string gstring = bt.id.ToString(); + + data_store[gstring] = data; + bt.UpdateValue(gstring); + bt.UpdateNotify(); + }, + (error) => + { + topic.UpdateValue(error.ToString()); + }, + () => { })); + return ExcelErrorUtil.ToComError(ExcelError.ExcelErrorNA); //success! + } + + protected override void DisconnectData(ExcelRtdServer.Topic topic) + { + IObservable sub; + if (_subs.TryGetValue(topic.TopicId, out sub)) + { + IDisposable disp = sub as IDisposable; + if (disp != null) + disp.Dispose(); + _subs.Remove(topic.TopicId); + } + } + + protected override int Heartbeat() + { + return base.Heartbeat(); + } + + + IObservable subscribe(Topic topic) + { + var service = APIDispatch.locate_service(topic.service_type); + var request = APIDispatch.generate_request(topic.service_type, topic.method_name, topic.parameters[0]); + IObservable stream = null; + if (service != null) //request can be null here + { + var subscription = APIDispatch.invoke_service_call(service, topic.method_name, request); + stream = subscription as IObservable; + if (stream != null) + { + dynamic dtable = core.ObservableUpdateExtensions.table((dynamic)subscription); + stream = dtable as IObservable; + } + } + return stream; + } + } +} \ No newline at end of file diff --git a/src/client/spark_excel/rtd/Subscribe.cs b/src/client/spark_excel/rtd/Subscribe.cs new file mode 100644 index 000000000..dcd6d163d --- /dev/null +++ b/src/client/spark_excel/rtd/Subscribe.cs @@ -0,0 +1,93 @@ +using System; +using System.Collections.Generic; +using System.Linq; + +using ExcelDna.Integration; +using ExcelDna.Integration.Rtd; + +namespace bts.excel.rtd +{ + public class Subscribe + { + //[ExcelFunction] + //public static object bts_subscribe_deprecated(string service_type, string request_type, object parameters) + //{ + // IList topic = Utilities.get_tokens(parameters); + // topic.Insert(0, request_type); + // topic.Insert(0, service_type); + // object val = XlCall.RTD(typeof(APIRTDServer).FullName, null, topic.ToArray()); + + // if(val is string) + // { + // //I can't marshal just any old object through the excel machinery, so in order to retain a 'registration free' + // //model, I have to pass this string-guid from the RTDServer, up through excel and back here. + // object data; + // if(APIRTDServer.retrieve(val.ToString(), out data)) + // { + // object[,] ret = DataParser.scatter_data(data); + // return ret; + // } + // } + // return val; + //} + + [ExcelFunction] + public static object bts_subscribe(string service_type, string method_name, object parameters) + { + IList topic = Utilities.get_tokens(parameters); + topic.Insert(0, method_name); + topic.Insert(0, service_type); + object val = XlCall.RTD(typeof(RTDServer).FullName, null, topic.ToArray()); + return val; + } + + [ExcelFunction] + public static object bts_unpack(object reference) + { + object data; + if (RTDServer.retrieve(reference.ToString(), out data)) + { + object[,] ret = DataParser.scatter_data(data); + return ret; + } + else + return ExcelErrorUtil.ToComError(ExcelError.ExcelErrorNA); + } + + #region marketdata + [ExcelFunction] + public static object bts_subscribe_bbo(string symbol, string security_type, string exchange) + { + MarketDataRequest req = new MarketDataRequest(); + req.SecurityName = symbol; + + SecurityRequestType type; + if (Enum.TryParse(security_type, true, out type)) + req.SecurityType = type; + else + return "Can't Parse Security Type: " + security_type; + + Venue ven; + if (Enum.TryParse(exchange, true, out ven)) + req.SecurityVenue = ven; + else + return "Can't Parse Exchange: " + exchange; + req.MdType = MarketDataType.Bbo; + + string json = Google.Protobuf.JsonFormatter.Default.Format(req); + return bts_subscribe(typeof(MarketDataService).FullName, nameof(MarketDataService.subscribe_marketdata), json); + } + + [ExcelFunction] + public static object bts_subscribe_securities() + { + SecurityRequest req = new SecurityRequest(); + + string json = Google.Protobuf.JsonFormatter.Default.Format(req); + return bts_subscribe(typeof(MarketDataService).FullName, nameof(MarketDataService.subscribe_securities), json); + } + #endregion + + + } +} \ No newline at end of file diff --git a/src/client/spark_excel/rtd/Topic.cs b/src/client/spark_excel/rtd/Topic.cs new file mode 100644 index 000000000..18513f846 --- /dev/null +++ b/src/client/spark_excel/rtd/Topic.cs @@ -0,0 +1,69 @@ +using System; +using System.Linq; + +using ExcelDna.Integration; +using ExcelDna.Integration.Rtd; +using bts.excel.extensions; +using System.Reflection; + +namespace bts.excel.rtd +{ + class Topic : ExcelRtdServer.Topic + { + public Guid id { get; private set; } + public Type service_type { get; private set; } + public Type request_type { get; private set; } + public string method_name { get; private set; } + public string[] parameters { get; private set; } + + public string service_type_string { get; private set; } + + public Topic(ExcelRtdServer server, int topicid, + string serv_type, string methodname, string[] param) : base(server, topicid) + { + service_type_string = serv_type; + method_name = methodname; + id = Guid.NewGuid(); + service_type = find_api_type(service_type_string, null, typeof(core.IApiClient)); + request_type = APIDispatch.find_request_type(service_type, method_name); + parameters = param; + } + + Type find_api_type(string type, string assem_regex = null, Type assignable = null) + { + return Utilities.find_full_type_in_all_assemblies(type, assem_regex, assignable); + } + + public override int GetHashCode() + { + var h = service_type?.GetHashCode() ^ request_type?.GetHashCode() ^ all_params().GetHashCode(); + if (h.HasValue) + return h.Value; + else + return -1; + } + + string all_params() + { + if (parameters != null) + return parameters.Aggregate((a, b) => a + b); + else + return string.Empty; + } + + + + public override bool Equals(object obj) + { + Topic top = (obj as Topic); + if (top == null) + return false; + else + { + return top.service_type.Equals(this.service_type) && + top.request_type.Equals(this.request_type) && + top.all_params().Equals(this.all_params()); + } + } + } +} \ No newline at end of file diff --git a/src/client/spark_excel/utils/APIDispatch.cs b/src/client/spark_excel/utils/APIDispatch.cs new file mode 100644 index 000000000..da704fb10 --- /dev/null +++ b/src/client/spark_excel/utils/APIDispatch.cs @@ -0,0 +1,125 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Reflection; +using ExcelDna.Integration; + +using bts.api.core; +using bts.excel.extensions; + +using Google.Protobuf; +using Google.Protobuf.Reflection; + +namespace bts.excel +{ + public class APIDispatch + { + public static Lazy service_context = new Lazy(() => new core.ServiceContext(), true); + static MethodInfo _locate_meth; + readonly static Dictionary _services = new Dictionary(); + const string descriptor_property_name = "Descriptor"; + + static MethodInfo locate_method + { + get + { + if (_locate_meth == null) + { + _locate_meth = service_context.Value.GetType().find_method((_) => + { + return _.Name == nameof(core.ServiceContext.locate) && _.IsGenericMethod; + })[0]; + } + return _locate_meth; + } + } + + static public object api_call_internal(Type service_type, string method_name, IMessage parameters) + { + object response = null; + if (service_type != null && method_name != null) + { + object service = locate_service(service_type); + response = invoke_service_call(service, method_name, parameters); + } + return response; + } + + //static public object invoke_service_call(object service, Type request_type, IMessage request) + //{ + // var sub_method = service.GetType().GetMethods().Where(meth => + // { + // var par = meth.GetParameters().FirstOrDefault(); + // return (par != null && par.ParameterType.IsAssignableFrom(request_type)); + // }).FirstOrDefault(); + // object subscription = null; + // if(sub_method != null) + // subscription = sub_method.Invoke(service, new[] { request }); + // return subscription; + //} + + static public object invoke_service_call(object service, string method_name, IMessage request) + { + var sub_method = service.GetType().GetMethod(method_name); + object subscription = null; + if (sub_method != null) + subscription = sub_method.Invoke(service, new[] { request }); + return subscription; + } + + public static Type find_request_type(Type service_type, string method_name) + { + return service_type?.GetMethod(method_name)?.GetParameters().FirstOrDefault()?.ParameterType; + } + + static public IMessage generate_request(Type request_type, string request_json) + { + IMessage request = null; + var descproperty = request_type?.get_static_field_or_property(descriptor_property_name); + if (descproperty != null) + { + MessageDescriptor desc = (MessageDescriptor)descproperty.get_value(null); + if (!string.IsNullOrEmpty(request_json) && request_json != "{ }" && request_json != "{}") + request = JsonParser.Default.Parse(request_json, desc); + else + request = null; //TODO this is a workaround for Eric's bug that requires 'empty' requests to pass null instead + } + return request; + } + + static public IMessage generate_request(Type service_type, string method_name, string request_json) + { + Type request_type = find_request_type(service_type, method_name); + return generate_request(request_type, request_json); + } + + static public object locate_service(Type service_type) + { + //object svc = null; + //if(service_type != null && !_services.TryGetValue(service_type, out svc)) + //{ + var method = locate_method?.MakeGenericMethod(service_type); + object svc = method?.Invoke(service_context.Value, new object[] { null }); + if (svc != null) + _services[svc] = null; + return svc; + } + + + [ExcelFunction] + static public object bts_api_call(string service_type, string method_name, object parameters) + { + Type svc = Utilities.find_full_type_in_all_assemblies(service_type, ".*api.*"); + + var tok = Utilities.get_tokens(parameters); + string json = tok.FirstOrDefault(); + IMessage request = generate_request(svc, method_name, json); + object resp = api_call_internal(svc, method_name, request); + if (resp != null) + return DataParser.scatter_data(resp); + else + return ExcelErrorUtil.ToComError(ExcelError.ExcelErrorNA); + } + + } +} \ No newline at end of file diff --git a/src/client/spark_excel/utils/ReflectionExtensions.cs b/src/client/spark_excel/utils/ReflectionExtensions.cs new file mode 100644 index 000000000..0ea5cbf28 --- /dev/null +++ b/src/client/spark_excel/utils/ReflectionExtensions.cs @@ -0,0 +1,108 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Reflection; + +namespace bts.excel.extensions +{ + public static class ReflectionExtensions + { + public static void set_value(this MemberInfo info, object target, object value) + { + if (info is FieldInfo) + ((FieldInfo)info).SetValue(target, value); + else if (info is PropertyInfo) + ((PropertyInfo)info).SetValue(target, value); + } + + public static object get_value(this MemberInfo info, object target) + { + if (info is FieldInfo) + return ((FieldInfo)info).GetValue(target); + else if (info is PropertyInfo) + return ((PropertyInfo)info).GetValue(target); + else + return null; + } + + public static MemberInfo[] get_fields_or_properties(this Type t) + { + MemberInfo[] mi = t.GetMembers(BindingFlags.Instance | BindingFlags.Public + | BindingFlags.NonPublic).Where(m => (m.MemberType == MemberTypes.Field || m.MemberType == MemberTypes.Property)).ToArray(); + return mi; + } + + public static MemberInfo[] get_fields(this Type t) + { + MemberInfo[] mi = t.GetMembers(BindingFlags.Instance | BindingFlags.Public + | BindingFlags.NonPublic).Where(m => (m.MemberType == MemberTypes.Field)).ToArray(); + return mi; + } + + public static MemberInfo[] get_properties(this Type t) + { + MemberInfo[] mi = t.GetMembers(BindingFlags.Instance | BindingFlags.Public + | BindingFlags.NonPublic).Where(m => (m.MemberType == MemberTypes.Property)).ToArray(); + return mi; + } + + public static MemberInfo get_field_or_property(this Type t, string name) + { + MemberInfo[] mi = t.GetMembers(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic) + .Where(m => (m.MemberType == MemberTypes.Field || m.MemberType == MemberTypes.Property)) + .Where(m => (m.Name == name)) + .ToArray(); + if (mi != null && mi.Length > 0) + return mi[0]; + else + return null; + } + + public static MemberInfo get_static_field_or_property(this Type t, string name) + { + MemberInfo[] mi = t.GetMembers(BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic) + .Where(m => (m.MemberType == MemberTypes.Field || m.MemberType == MemberTypes.Property)) + .Where(m => (m.Name == name)) + .ToArray(); + if (mi != null && mi.Length > 0) + return mi[0]; + else + return null; + } + + public static MemberInfo[] search_field_or_property(this Type t, Func criteria) + { + MemberInfo[] mi = t.GetMembers(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic) + .Where(m => (m.MemberType == MemberTypes.Field || m.MemberType == MemberTypes.Property)) + .Where(criteria) + .ToArray(); + if (mi != null && mi.Length > 0) + return mi; + else + return new MemberInfo[0]; + } + + + public static Type member_type(this MemberInfo info) + { + if (info is FieldInfo) + return ((FieldInfo)info).FieldType; + else if (info is PropertyInfo) + return ((PropertyInfo)info).PropertyType; + else + return null; + } + + public static MethodInfo[] find_method(this Type t, Func criteria) + { + var mi = t.GetMethods(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic) + .Where(criteria).ToArray(); + if (mi != null && mi.Length > 0) + return mi; + else + return new MethodInfo[0]; + } + } +} \ No newline at end of file diff --git a/src/client/spark_excel/utils/Utilities.cs b/src/client/spark_excel/utils/Utilities.cs new file mode 100644 index 000000000..0ae2b9a4c --- /dev/null +++ b/src/client/spark_excel/utils/Utilities.cs @@ -0,0 +1,247 @@ +using System; +using System.Collections.Concurrent; +using System.Collections.Generic; +using System.Reflection; +using System.Text.RegularExpressions; +using ExcelDna.Integration; + +namespace bts.excel +{ + public class Utilities + { + static ConcurrentDictionary _types = new ConcurrentDictionary(); + public static Type find_full_type_in_all_assemblies(string type, string assembly_regex = null, Type assignable_from = null) + { + if (assignable_from == null) + assignable_from = typeof(object); + if (type == null) + return null; + Type res = null; + if (!_types.TryGetValue(type, out res)) + { + int idx = type.IndexOf("<"); + if (idx > 0) + { + try + { + string[] zargs = type.Substring(idx + 1, type.Length - idx - 1).Replace(">", string.Empty).Split(','); + string gtstring = type.Substring(0, idx) + "`" + zargs.Length.ToString(); + var gt = find_full_type_in_all_assemblies(gtstring, assembly_regex); + Type[] args = new Type[zargs.Length]; + for (int i = 0; i < zargs.Length; i++) + args[i] = find_full_type_in_all_assemblies(zargs[i], assembly_regex); + res = gt.MakeGenericType(args); + } + catch (Exception) { /*TODO log?*/ } + } + else + { + List alltypes = find_all_types_in_all_assemblies(type, assembly_regex); + foreach (Type at in alltypes) + { + if (!assignable_from.IsAssignableFrom(at)) + continue; + res = at; + _types[type] = res; + } + } + } + return res; + } + + static public List find_all_types_in_all_assemblies(string type, string assembly_regex = null) + { + List res = new List(); + var asses = AppDomain.CurrentDomain.GetAssemblies(); + foreach (Assembly a in asses) + { + if (!string.IsNullOrEmpty(assembly_regex)) + { + if (!Regex.Match(a.GetName().Name, assembly_regex).Success) + continue; + } + Type t = a.GetType(type, false, true); + if (t != null) + res.Add(t); + } + return res; + } + + static public T[,] transpose_keep_dimension(T[,] array) + { + var res = new T[array.GetLength(1), array.GetLength(0)]; + for (int i = 0; i < array.GetLength(1); i++) + for (int j = 0; j < array.GetLength(0); j++) + res[i, j] = array[j, i]; + return res; + } + + static public T[,] transpose(T[,] array) + { + var res = new T[array.Length, 1]; + for (int i = 0; i < array.Length; i++) + res[i, 0] = array[0, i]; + return res; + } + + static public T[,] transpose(T[] array) + { + var res = new T[array.Length, 1]; + for (int i = 0; i < array.Length; i++) + res[i, 0] = array[i]; + return res; + } + + static public object[,] transpose(Array array) + { + var res = new object[array.Length, 1]; + for (int i = 0; i < array.Length; i++) + res[i, 0] = array.GetValue(i); + return res; + } + + static public T[,] make_column_vector(T[] row_array) + { + return transpose(row_array); + } + + static public T[] make_row_vector(T[,] col_array) + { + var res = new List(); + for (int i = 0; i < col_array.GetLength(0); i++) + { + for (int j = 0; j < col_array.GetLength(1); j++) + { + res.Add(col_array[i, j]); + } + } + return res.ToArray(); + } + + + static public Array make_row_vector(Type type, object[,] col_array) + { + var res = Array.CreateInstance(type, col_array.Length); + int len = col_array.GetLength(1); + for (int i = 0; i < col_array.GetLength(0); i++) + { + for (int j = 0; j < len; j++) + { + var obj = col_array[i, j]; + if (type.IsAssignableFrom(obj.GetType())) + res.SetValue(obj, i * len + j); + } + } + return res; + } + + [ExcelFunction] + static public string bts_stringify_array_typed(object[,] data, string typename) + { + Type type = find_full_type_in_all_assemblies(typename); + object array = DataParser.gather_data(type, data); + + object ret; + DataParser.write_data(out ret, array); + return ret.ToString(); + } + + [ExcelFunction] + static public string bts_stringify_array(object[] array) + { + object ret; + DataParser.write_data(out ret, array); + return ret.ToString(); + } + + [ExcelFunction] + static public object[,] bts_read_stringified_array(object array, string typename) + { + Type type = find_full_type_in_all_assemblies(typename); + object data = DataParser.read_data(type, array); + object[,] ret = DataParser.scatter_data(data); + + return ret; + } + + [ExcelFunction] + static public object[,] bts_read_double_array(object array) + { + object data = DataParser.read_data(typeof(double[]), array); + object[,] ret = DataParser.scatter_data(data, false); + var t = transpose(ret); + return t; + } + + [ExcelFunction] + static public object[,] bts_read_string_array(object array) + { + object data = DataParser.read_data(typeof(string[]), array); + object[,] ret = DataParser.scatter_data(data, false); + var t = transpose(ret); + return t; + } + + + static public T[,] combine_arrays_vertically(IEnumerable arrays) + { + int rows = 0, cols = 0; + foreach (var a in arrays) + { + if (a.GetLength(1) > cols) + cols = a.GetLength(1); + rows += a.GetLength(0); + } + T[,] result = new T[rows, cols]; int idx = 0; + foreach (T[,] a in arrays) + { + for (int i = 0; i < a.GetLength(0); i++) + { + for (int j = 0; j < a.GetLength(1); j++) + result[idx, j] = (T)a.GetValue(i, j); + idx++; + } + } + return result; + } + + static private Dictionary _ass = new Dictionary(); + [ExcelFunction(IsMacroType = true)] + public static string bts_load_assembly(string assembly_path) + { + //XlCall.Excel(XlCall.xlfVolatile, false); + System.Reflection.Assembly ass; + if (!_ass.TryGetValue(assembly_path, out ass)) + { + ass = System.Reflection.Assembly.Load(assembly_path); + _ass[assembly_path] = ass; + } + return (ass == null) ? "ERROR: Couldn't load assembly " + assembly_path : "Assembly Loaded: " + assembly_path + " " + DateTime.Now.ToLongTimeString(); + } + + static public IList get_tokens(object tag) + { + List list = new List(); + if (tag is Array) + { + Array array = (Array)tag; + if (array.Rank > 1) + { + for (int i = 0; i < array.GetLength(0); i++) + { + for (int j = 0; j < array.GetLength(1); j++) + list.Add(array.GetValue(i, j).ToString()); + } + } + else + { + for (int j = 0; j < array.Length; j++) + list.Add(array.GetValue(j).ToString()); + } + } + else + list.Add(tag.ToString()); + return list; + } + } +} \ No newline at end of file From 2eb3ff434782fbc76ac4d88efcc3ed3a0a451f33 Mon Sep 17 00:00:00 2001 From: Kevin Darby Date: Fri, 21 Sep 2018 11:21:08 -0400 Subject: [PATCH 3/3] builds --- src/client/spark_excel/rtd/DataParser.cs | 10 +++- src/client/spark_excel/rtd/RTDServer.cs | 11 ++-- src/client/spark_excel/rtd/Subscribe.cs | 56 ++++++++++----------- src/client/spark_excel/rtd/Topic.cs | 2 +- src/client/spark_excel/spark_excel.csproj | 1 + src/client/spark_excel/utils/APIDispatch.cs | 4 +- 6 files changed, 46 insertions(+), 38 deletions(-) diff --git a/src/client/spark_excel/rtd/DataParser.cs b/src/client/spark_excel/rtd/DataParser.cs index e049ff18d..1a415e6cb 100644 --- a/src/client/spark_excel/rtd/DataParser.cs +++ b/src/client/spark_excel/rtd/DataParser.cs @@ -4,6 +4,7 @@ using System.Reflection; using System.Linq; using bts.excel.extensions; using System.Text.RegularExpressions; +using bts.api.core; namespace bts.excel { @@ -191,6 +192,11 @@ namespace bts.excel return val; } + public static Tuple key_value_tuple(TableUpdate source) where TKey : IEquatable + { + return Tuple.Create(source.state.Keys.Cast().ToArray(), source.state.Values.Cast().ToArray()); + } + public static object[,] scatter_data(object data, bool include_field_names = true, Type type_override = null) { if (data == null) @@ -198,9 +204,9 @@ namespace bts.excel bool is_array; Type type = type_override ?? data.GetType(); - if (type.IsGenericType && type.GetGenericTypeDefinition().Equals(typeof(core.TableUpdate<,>))) + if (type.IsGenericType && type.GetGenericTypeDefinition().Equals(typeof(TableUpdate<,>))) { - Tuple tup = core.ObservableUpdateExtensions.key_value_tuple((dynamic)data); + Tuple tup = key_value_tuple((dynamic)data); return scatter_data(tup.Item2, include_field_names, tup.Item2.FirstOrDefault()?.GetType().MakeArrayType()); } Type element_type = analyze_type(type, out is_array); diff --git a/src/client/spark_excel/rtd/RTDServer.cs b/src/client/spark_excel/rtd/RTDServer.cs index f11438684..161aa7cde 100644 --- a/src/client/spark_excel/rtd/RTDServer.cs +++ b/src/client/spark_excel/rtd/RTDServer.cs @@ -6,6 +6,7 @@ using System.Linq; using ExcelDna.Integration; using ExcelDna.Integration.Rtd; using bts.excel.extensions; +using bts.api.core; namespace bts.excel.rtd { @@ -30,11 +31,11 @@ namespace bts.excel.rtd protected override void ServerTerminate() { } - protected override RtdServer.Topic CreateTopic(int topicId, IList topicInfo) + protected override ExcelRtdServer.Topic CreateTopic(int topicId, IList topicInfo) { string[] tagvals = new string[topicInfo.Count - 2]; Array.Copy(topicInfo.ToArray(), 2, tagvals, 0, tagvals.Length); - var bt = new Topic(this, topicId, topicInfo[0], topicInfo[1], tagvals); + var bt = new bts.excel.rtd.Topic(this, topicId, topicInfo[0], topicInfo[1], tagvals); return bt; } @@ -42,7 +43,7 @@ namespace bts.excel.rtd { //return ExcelErrorUtil.ToComError(ExcelError.ExcelErrorNA); newValues = true; - Topic bt = (Topic)topic; + bts.excel.rtd.Topic bt = (bts.excel.rtd.Topic)topic; IObservable obs = null; if (bt.service_type == null) return "Unknown ServiceType: " + bt.service_type_string; @@ -93,7 +94,7 @@ namespace bts.excel.rtd } - IObservable subscribe(Topic topic) + IObservable subscribe(bts.excel.rtd.Topic topic) { var service = APIDispatch.locate_service(topic.service_type); var request = APIDispatch.generate_request(topic.service_type, topic.method_name, topic.parameters[0]); @@ -104,7 +105,7 @@ namespace bts.excel.rtd stream = subscription as IObservable; if (stream != null) { - dynamic dtable = core.ObservableUpdateExtensions.table((dynamic)subscription); + dynamic dtable = ObservableUpdateExtensions.Table((dynamic)subscription); stream = dtable as IObservable; } } diff --git a/src/client/spark_excel/rtd/Subscribe.cs b/src/client/spark_excel/rtd/Subscribe.cs index dcd6d163d..4f9986133 100644 --- a/src/client/spark_excel/rtd/Subscribe.cs +++ b/src/client/spark_excel/rtd/Subscribe.cs @@ -54,39 +54,39 @@ namespace bts.excel.rtd return ExcelErrorUtil.ToComError(ExcelError.ExcelErrorNA); } - #region marketdata - [ExcelFunction] - public static object bts_subscribe_bbo(string symbol, string security_type, string exchange) - { - MarketDataRequest req = new MarketDataRequest(); - req.SecurityName = symbol; + // #region marketdata + // [ExcelFunction] + // public static object bts_subscribe_bbo(string symbol, string security_type, string exchange) + // { + // MarketDataRequest req = new MarketDataRequest(); + // req.SecurityName = symbol; - SecurityRequestType type; - if (Enum.TryParse(security_type, true, out type)) - req.SecurityType = type; - else - return "Can't Parse Security Type: " + security_type; + // SecurityRequestType type; + // if (Enum.TryParse(security_type, true, out type)) + // req.SecurityType = type; + // else + // return "Can't Parse Security Type: " + security_type; - Venue ven; - if (Enum.TryParse(exchange, true, out ven)) - req.SecurityVenue = ven; - else - return "Can't Parse Exchange: " + exchange; - req.MdType = MarketDataType.Bbo; + // Venue ven; + // if (Enum.TryParse(exchange, true, out ven)) + // req.SecurityVenue = ven; + // else + // return "Can't Parse Exchange: " + exchange; + // req.MdType = MarketDataType.Bbo; - string json = Google.Protobuf.JsonFormatter.Default.Format(req); - return bts_subscribe(typeof(MarketDataService).FullName, nameof(MarketDataService.subscribe_marketdata), json); - } + // string json = Google.Protobuf.JsonFormatter.Default.Format(req); + // return bts_subscribe(typeof(MarketDataService).FullName, nameof(MarketDataService.subscribe_marketdata), json); + // } - [ExcelFunction] - public static object bts_subscribe_securities() - { - SecurityRequest req = new SecurityRequest(); + // [ExcelFunction] + // public static object bts_subscribe_securities() + // { + // SecurityRequest req = new SecurityRequest(); - string json = Google.Protobuf.JsonFormatter.Default.Format(req); - return bts_subscribe(typeof(MarketDataService).FullName, nameof(MarketDataService.subscribe_securities), json); - } - #endregion + // string json = Google.Protobuf.JsonFormatter.Default.Format(req); + // return bts_subscribe(typeof(MarketDataService).FullName, nameof(MarketDataService.subscribe_securities), json); + // } + // #endregion } diff --git a/src/client/spark_excel/rtd/Topic.cs b/src/client/spark_excel/rtd/Topic.cs index 18513f846..7340a5665 100644 --- a/src/client/spark_excel/rtd/Topic.cs +++ b/src/client/spark_excel/rtd/Topic.cs @@ -24,7 +24,7 @@ namespace bts.excel.rtd service_type_string = serv_type; method_name = methodname; id = Guid.NewGuid(); - service_type = find_api_type(service_type_string, null, typeof(core.IApiClient)); + service_type = find_api_type(service_type_string, null, typeof(bts.api.core.IApiClient)); request_type = APIDispatch.find_request_type(service_type, method_name); parameters = param; } diff --git a/src/client/spark_excel/spark_excel.csproj b/src/client/spark_excel/spark_excel.csproj index 5371a08ad..1865703d6 100644 --- a/src/client/spark_excel/spark_excel.csproj +++ b/src/client/spark_excel/spark_excel.csproj @@ -33,6 +33,7 @@ + diff --git a/src/client/spark_excel/utils/APIDispatch.cs b/src/client/spark_excel/utils/APIDispatch.cs index da704fb10..710780f0f 100644 --- a/src/client/spark_excel/utils/APIDispatch.cs +++ b/src/client/spark_excel/utils/APIDispatch.cs @@ -14,7 +14,7 @@ namespace bts.excel { public class APIDispatch { - public static Lazy service_context = new Lazy(() => new core.ServiceContext(), true); + public static Lazy service_context = new Lazy(() => new ServiceContext(), true); static MethodInfo _locate_meth; readonly static Dictionary _services = new Dictionary(); const string descriptor_property_name = "Descriptor"; @@ -27,7 +27,7 @@ namespace bts.excel { _locate_meth = service_context.Value.GetType().find_method((_) => { - return _.Name == nameof(core.ServiceContext.locate) && _.IsGenericMethod; + return _.Name == nameof(ServiceContext.locate) && _.IsGenericMethod; })[0]; } return _locate_meth;