commit
2dd2b4e9b8
17 files changed
+139
-46
No files matched your search
@@ -70,7 +70,6 @@ namespace mbts.ui.shell
|
||||
{
|
||||
Log.Gen.info("Application.OnExit");
|
||||
if (_runtime != null) _runtime.Dispose();
|
||||
utils.BTSNativeMethods.finit_spark_lib();
|
||||
base.OnExit(e);
|
||||
Environment.Exit(0);
|
||||
}
|
||||
|
||||
@@ -13,6 +13,7 @@ namespace bts.api.core {
|
||||
AuthToken _token;
|
||||
|
||||
public ServiceContext() {
|
||||
BTS.init();
|
||||
_qbus = ApiNativeMethods.api_create_quickbus(null, 0);
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,42 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
|
||||
namespace bts
|
||||
{
|
||||
public static class BTS
|
||||
{
|
||||
static int _needs_init_spark_lib = 0;
|
||||
public static void init() {
|
||||
var args = Environment.GetCommandLineArgs();
|
||||
utils.BTSNativeMethods.provide_command_line(args, args.Length);
|
||||
|
||||
var buffer = new StringBuilder(4096);
|
||||
bool config_ok = utils.BTSNativeMethods.initialize_nothrow(buffer, buffer.Capacity);
|
||||
if (!config_ok) {
|
||||
throw new ApplicationException(buffer.ToString());
|
||||
}
|
||||
|
||||
if (Interlocked.CompareExchange(ref _needs_init_spark_lib,1,0) == 0) {
|
||||
utils.BTSNativeMethods.init_spark_lib();
|
||||
}
|
||||
|
||||
// This seems like a good idea but it crashes due to thread shutdown order
|
||||
// AppDomain.CurrentDomain.ProcessExit += (o, e) => {
|
||||
// utils.BTSNativeMethods.finit_spark_lib();
|
||||
// };
|
||||
}
|
||||
|
||||
internal static IEnumerable<string> remove_config_arg(string[] args) {
|
||||
int skip = 0;
|
||||
foreach (var arg in args) {
|
||||
if (arg.ToLower() == "-configserver") {
|
||||
skip = 2;
|
||||
}
|
||||
if (skip > 0) { skip -= 1; }
|
||||
else { yield return arg; }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -4,7 +4,7 @@ using System.Linq;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Text;
|
||||
|
||||
namespace mbts.utils
|
||||
namespace bts.utils
|
||||
{
|
||||
internal static class BTSNativeMethods
|
||||
{
|
||||
@@ -25,5 +25,8 @@ namespace mbts.utils
|
||||
|
||||
[DllImport("kernel32.dll")]
|
||||
public static extern bool SetConsoleTitle(string lpConsoleTitle);
|
||||
|
||||
[DllImport("libspark")]
|
||||
public static extern bool initialize_nothrow(StringBuilder buffer, int length);
|
||||
}
|
||||
}
|
||||
@@ -4,6 +4,7 @@ using System.Linq;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using bts.utils;
|
||||
|
||||
namespace mbts.utils
|
||||
{
|
||||
|
||||
@@ -6,6 +6,7 @@ using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Threading;
|
||||
using System.Linq;
|
||||
using bts.utils;
|
||||
|
||||
namespace mbts.config
|
||||
{
|
||||
@@ -24,38 +25,37 @@ namespace mbts.config
|
||||
return ProtoBuf.Serializer.Deserialize<T>(stream);
|
||||
}
|
||||
|
||||
private static bool initialize(out string error) {
|
||||
StringBuilder buffer = new StringBuilder(4096);
|
||||
bool success = initialize_nothrow(buffer, buffer.Capacity);
|
||||
error = buffer.ToString();
|
||||
return success;
|
||||
}
|
||||
|
||||
public static void initialize() {
|
||||
if (_state == State.OK) return;
|
||||
string[] args = Environment.GetCommandLineArgs();
|
||||
utils.BTSNativeMethods.provide_command_line(args, args.Length);
|
||||
// Considered doing this async on a background thread
|
||||
// but it wouldn't do any good. The next guy down the line to ask for config
|
||||
// will just block until this completes.
|
||||
string error;
|
||||
if (!initialize(out error)) {
|
||||
try {
|
||||
bts.BTS.init();
|
||||
} catch(ApplicationException) {
|
||||
_state = State.Error;
|
||||
throw new ApplicationException(error);
|
||||
throw;
|
||||
}
|
||||
_state = State.OK;
|
||||
utils.BTSNativeMethods.init_spark_lib();
|
||||
}
|
||||
|
||||
public static void initialize_for_test() {
|
||||
reset();
|
||||
initialize();
|
||||
public static void initialize_for_test(string config="") {
|
||||
raw_initialize_for_test(config);
|
||||
// still need init_spark_lib
|
||||
bts.BTS.init();
|
||||
_state = State.OK;
|
||||
}
|
||||
|
||||
public static bool try_initialize(out string error)
|
||||
{
|
||||
public static bool try_initialize(out string error) {
|
||||
reset();
|
||||
return initialize(out error);
|
||||
try {
|
||||
bts.BTS.init();
|
||||
}catch(ApplicationException e) {
|
||||
error = e.Message;
|
||||
return false;
|
||||
}
|
||||
error = string.Empty;
|
||||
return true;
|
||||
}
|
||||
|
||||
////////////
|
||||
@@ -67,6 +67,10 @@ namespace mbts.config
|
||||
|
||||
[DllImport("libspark")]
|
||||
private static extern bool initialize_nothrow(StringBuilder buffer, int length);
|
||||
|
||||
[DllImport("libspark", EntryPoint="initialize_for_test")]
|
||||
private static extern void raw_initialize_for_test(string buffer);
|
||||
|
||||
[DllImport("libspark")]
|
||||
private static extern void reset();
|
||||
|
||||
|
||||
@@ -89,6 +89,10 @@ namespace bts.sdk
|
||||
cr.NewOrder.SubType = OrderSubType.None;
|
||||
cr.NewOrder.TimeInForce = TimeInForce.Day;
|
||||
var admin = await _admin.Value.get_admin_data_async(new VoidMessage());
|
||||
if (!admin.success) {
|
||||
Console.Error.WriteLine(admin.error_message);
|
||||
return;
|
||||
}
|
||||
BTSUser user = admin.value.Users.FirstOrDefault( x=> username != null ? x.Name.ToLowerInvariant() == username.ToLowerInvariant() : true );
|
||||
if (user != null)
|
||||
{
|
||||
|
||||
@@ -26,6 +26,7 @@ namespace bts.sdk
|
||||
}
|
||||
public async static Task Main(string[] args)
|
||||
{
|
||||
BTS.init();
|
||||
Log.ClientSdk.info("Welcome to the BTS Client Side SDK example");
|
||||
|
||||
string u = args.FirstOrDefault(x => x.Contains("--user="));
|
||||
|
||||
@@ -9,6 +9,7 @@ using mbts.config;
|
||||
using System.Linq;
|
||||
using CommandLine;
|
||||
using Newtonsoft.Json;
|
||||
using bts;
|
||||
|
||||
namespace sparkcmd {
|
||||
|
||||
@@ -41,6 +42,12 @@ namespace sparkcmd {
|
||||
}
|
||||
|
||||
public static int Run(TestCommandOptions o) {
|
||||
if (o.type == TestType.Unit) {
|
||||
// Skip loading any local config
|
||||
Configuration.initialize_for_test();
|
||||
}
|
||||
else Configuration.initialize();
|
||||
|
||||
Console.WriteLine(JsonConvert.SerializeObject(o));
|
||||
|
||||
if (o.list) {
|
||||
|
||||
@@ -9,6 +9,7 @@ using bts;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq.Expressions;
|
||||
using System.Reflection;
|
||||
using bts.utils;
|
||||
|
||||
namespace sparkcmd {
|
||||
class Program {
|
||||
@@ -23,7 +24,11 @@ namespace sparkcmd {
|
||||
// return 3;
|
||||
// }
|
||||
static int Main(string[] args) {
|
||||
mbts.utils.BTSNativeMethods.provide_command_line(args, args.Length);
|
||||
// Initializing config is up to each individual command
|
||||
// We call provide_command_line here in case config is first
|
||||
// initialized from c++ instead of c#
|
||||
BTSNativeMethods.provide_command_line(args, args.Length);
|
||||
|
||||
// note: this can be made to infer the options type list from the list of delegates
|
||||
return Parser.Default
|
||||
.ParseArguments<
|
||||
@@ -35,7 +40,7 @@ namespace sparkcmd {
|
||||
TestCommandOptions,
|
||||
OrderStressOptions,
|
||||
MBOCommandOptions
|
||||
>(args)
|
||||
>(BTS.remove_config_arg(args))
|
||||
.MapResult<
|
||||
ConfigcheckOptions,
|
||||
ScriptClientOptions,
|
||||
|
||||
@@ -140,14 +140,23 @@ void reply_error(ApiError error, raw_response_callback cb, void* extra) {
|
||||
}
|
||||
|
||||
|
||||
CAPI void api_client_send(ApiClient* pclient, const char* buffer, int buffer_length, raw_response_callback cb, void* extra) {
|
||||
int32_t rpc_connect_timeout_ms() {
|
||||
static int32_t t = config::get<config::RPCConfig>().rpc_connect_timeout_seconds() * 1000;
|
||||
return t;
|
||||
}
|
||||
void api_client_send_internal(ApiClient* pclient, qbus::RPCRequest&& request, raw_response_callback cb, int32_t delay, void* extra) {
|
||||
bts_assert(pclient);
|
||||
std::string strbuf(buffer, buffer_length);
|
||||
qbus::RPCRequest request(move(strbuf));
|
||||
pclient->rpc.wait_until_connected(1);
|
||||
LOG(Api,Diag,"send rpc {}",((int64_t)buffer));
|
||||
bool connected = pclient->rpc.wait_until_connected(0);
|
||||
if (!connected && delay > 0) {
|
||||
// This branch is the uncommon 'slow path' where connection is taking longer than we expect
|
||||
LOG(Api,Diag,"rpc call delay waiting for connection {}",((int64_t)request.value().c_str()));
|
||||
pclient->qbus.post([=, request{std::move(request)}]() mutable {
|
||||
api_client_send_internal(pclient, std::move(request), cb, delay - 100, extra);
|
||||
}, 100);
|
||||
return;
|
||||
}
|
||||
pclient->rpc.send(std::move(request), [=](qbus::RPCError e, const qbus::RPCReplyPtr& reply) {
|
||||
LOG(Api,Diag,"rpc reply {}",((int64_t)buffer));
|
||||
LOG(Api,Diag,"rpc reply {}",((int64_t)request.value().c_str()));
|
||||
switch (e) {
|
||||
case qbus::RPCError::RPC_OK:
|
||||
safe_call_fn([&]() {
|
||||
@@ -165,6 +174,12 @@ CAPI void api_client_send(ApiClient* pclient, const char* buffer, int buffer_len
|
||||
}
|
||||
});
|
||||
}
|
||||
CAPI void api_client_send(ApiClient* pclient, const char* buffer, int buffer_length, raw_response_callback cb, void* extra) {
|
||||
LOG(Api,Diag,"send rpc {}",((int64_t)buffer));
|
||||
std::string strbuf(buffer, buffer_length);
|
||||
qbus::RPCRequest request(move(strbuf));
|
||||
api_client_send_internal(pclient, std::move(request), cb, rpc_connect_timeout_ms(), extra);
|
||||
}
|
||||
|
||||
CAPI bool api_client_is_on_qb_thread(ApiClient* pclient) {
|
||||
return pclient->qbus.on_qb_thread();
|
||||
|
||||
@@ -11,6 +11,7 @@
|
||||
#include "lib/utils/bts_assert.h"
|
||||
#include "lib/utils/is_unittest_environment.h"
|
||||
#include "lib/utils/bts_array_size.h"
|
||||
#include "lib/utils/defer.h"
|
||||
#include "config.h"
|
||||
|
||||
#include "lib/config/protos/system.pb.h"
|
||||
@@ -348,11 +349,13 @@ void ConfigurationState::initialize_settings()
|
||||
|
||||
std::string settings = settings_path();
|
||||
std::string fallback = get_command_argument("-ConfigFallback");
|
||||
load(settings, fallback, log);
|
||||
|
||||
initialize_builtins();
|
||||
initialize_macros();
|
||||
_deferred_queue->process();
|
||||
DEFER(// Do this even if load() fails
|
||||
initialize_builtins();
|
||||
initialize_macros();
|
||||
_deferred_queue->process();
|
||||
);
|
||||
load(settings, fallback, log);
|
||||
}
|
||||
|
||||
void ConfigurationState::reset()
|
||||
|
||||
@@ -89,7 +89,10 @@ namespace internal {
|
||||
// Functions used in testing
|
||||
//////////////////////////////////////////
|
||||
|
||||
extern "C" BTS_EXPORT void initialize_for_test(const std::string& values, bool print_errors)
|
||||
extern "C" BTS_EXPORT void initialize_for_test(const char* config) {
|
||||
initialize_for_test(std::string(config), false);
|
||||
}
|
||||
void initialize_for_test(const std::string& values, bool print_errors)
|
||||
{
|
||||
config_state.reset();
|
||||
config_state.load_string(values, print_errors);
|
||||
|
||||
@@ -56,7 +56,8 @@ namespace config {
|
||||
{
|
||||
// Initialize configuration state from a string instead of from the filesystem. defaults.conf will still be loaded.
|
||||
// Calling this a second time clears all values from previous initialization
|
||||
extern "C" BTS_EXPORT void initialize_for_test(const std::string& configuration, bool print_errors=true);
|
||||
extern "C" BTS_EXPORT void initialize_for_test(const char* config);
|
||||
void initialize_for_test(const std::string& configuration, bool print_errors=true);
|
||||
|
||||
// Releases all resources and returns config to factory defaults
|
||||
extern "C" BTS_EXPORT void reset();
|
||||
|
||||
@@ -1,24 +1,24 @@
|
||||
Logging {
|
||||
rule: "System.build_type == 'debug' && !('testorderman' in System.cmd_args) && !('dump' in System.cmd_args) && !('sparkcmd' in System.cmd_args) && !('dotnet' in System.cmd_args)"
|
||||
value
|
||||
{
|
||||
console { default_level: "Info" }
|
||||
syslog { default_level: "Info" }
|
||||
file { default_level: "Info" }
|
||||
}
|
||||
}
|
||||
|
||||
Logging {
|
||||
rule: "System.platform == 'windows'"
|
||||
value {
|
||||
console { default_level: "Error" }
|
||||
syslog { default_level: "Info" }
|
||||
file { default_level: "Info" }
|
||||
initial_queue_size: 1024
|
||||
initial_queue_size: 128
|
||||
max_queue_growth: 100
|
||||
}
|
||||
}
|
||||
|
||||
Logging {
|
||||
rule: "System.build_type = 'debug'"
|
||||
value
|
||||
{
|
||||
console { default_level: "Info" }
|
||||
syslog { default_level: "Info" }
|
||||
file { default_level: "Info" }
|
||||
}
|
||||
}
|
||||
|
||||
Logging
|
||||
{
|
||||
value
|
||||
|
||||
@@ -18,3 +18,6 @@ message QuickbusCfg {
|
||||
optional uint32 max_missed_hbs = 6 [default=3];
|
||||
}
|
||||
|
||||
message RPCConfig {
|
||||
optional uint32 rpc_connect_timeout_seconds = 1 [default = 10];
|
||||
}
|
||||
@@ -4,4 +4,5 @@ setlocal
|
||||
cd /d %~dp0/../
|
||||
setlocal
|
||||
set path=%spark_build_python_path%;%path%
|
||||
touch src\lib\CMakeLists.txt
|
||||
msbuild /m /nologo /p:debugtype=portable /p:platform=x64 /p:GenerateFullPaths=true /clp:verbosity=minimal /clp:summary %* spark_client.sln
|
||||
Reference in new issue
Block a user