From 28e98259f697ffb297082918bceff8b35cbb93e2 Mon Sep 17 00:00:00 2001 From: Eric Harding Date: Fri, 7 Feb 2020 22:20:42 -0500 Subject: [PATCH] Add client sdk example --- .../client/getting-started/services.md | 36 +++++++++---------- .../examples/client_sdk/.devcontainer.json | 20 +++++++++++ src/sdk/examples/client_sdk/.gitignore | 1 + .../client_sdk/.vscode/c_cpp_properties.json | 21 +++++++++++ .../examples/client_sdk/.vscode/launch.json | 27 ++++++++++++++ .../examples/client_sdk/.vscode/tasks.json | 29 +++++++++++++++ src/sdk/examples/client_sdk/build.sh | 24 +++++++++++++ .../client_sdk/client_sdk_example.cpp | 23 ++++++++++++ 8 files changed, 162 insertions(+), 19 deletions(-) create mode 100644 src/sdk/examples/client_sdk/.devcontainer.json create mode 100644 src/sdk/examples/client_sdk/.gitignore create mode 100644 src/sdk/examples/client_sdk/.vscode/c_cpp_properties.json create mode 100644 src/sdk/examples/client_sdk/.vscode/launch.json create mode 100644 src/sdk/examples/client_sdk/.vscode/tasks.json create mode 100644 src/sdk/examples/client_sdk/build.sh create mode 100644 src/sdk/examples/client_sdk/client_sdk_example.cpp diff --git a/doc/api/content/client/getting-started/services.md b/doc/api/content/client/getting-started/services.md index 97e5b27d3..43807a656 100644 --- a/doc/api/content/client/getting-started/services.md +++ b/doc/api/content/client/getting-started/services.md @@ -5,11 +5,21 @@ weight = 20 The Spark Client SDK uses [Google Protocol Buffers](https://developers.google.com/protocol-buffers/) to define the interface for each service in a language independent manner. Services have both functions which return a single result and streams of data which return values continuously while subscribed. This page describes the mapping from the Protocol Buffer definition to the concrete definition in each supported language. -## ServiceContext +# ServiceContext To connect your client to the Spark Server you need to get or create a `ServiceContext` instance. A `ServiceContext` represents a connection to the Spark Server and a background thread for handling messages on that connection. Generally there should be a single `ServiceContext` per process. All of the methods on the `ServiceContext` are safe thread safe. -To get a proxy for a given service call ServiceContext.locate() with an optional qualifier. The locate call cannot fail but it may return a disconnected proxy instance. When you make a call on the proxy check the call result to see if the method call succeeded. +Construct a ServiceContext by calling the default constructor. The ServiceContext will find the Spark Server by loading the server configuration over an HTTP connection. To determine the URL it will search the following locations and use the first one that is present. + +* A command line argument `-configserver ` +* An environment variable `BTSCONFIG` +* `%APPDATA%/bts/conf/spark.conf` (Windows) +* /sparkdata/conf/spark.conf (Linux) +* $HOME/sparkdata/conf/spark.conf (Linux) + +If your application is running on a system with the BTS Client installed this will probably work without any further configuration. If you plan to run your client application on a separate machine we recommend setting the `BTSCONFIG` environment variable. + +To get a proxy for a given service call ServiceContext.locate<T>("qualifier") where T is the type of the service and "qualifier" is an optional qualifier used to disambiguate between 2 instances of the same service. The locate call cannot fail but it may return a disconnected proxy. When you make a call on the proxy you should check the call result to see if the method call succeeded. # RPC @@ -47,8 +57,9 @@ To call the resulting service locate a proxy using the [ServiceContext](#service ```c# var ctx = new ServiceContext(); var orders = ctx.locate(); -var response = await orders.create_order_async( - new Create { price = 2.25, quantity = 42 }); +var response = + await orders.create_order_async( + new Create { price = 2.25, quantity = 42 }); if (response.success) { Console.WriteLine($"success: {response.value}"); } @@ -94,7 +105,7 @@ service OrderService { var sc = new ServiceContext(); var orders = sc.locate(); var fills_observable = orders.subscribei_fills(); - var unsub1 = fills_observable.Subscribe(update => { + var unsub = fills_observable.Subscribe(update => { Console.WriteLine( "Added {0} items, Removed {1} items, Updated {2} items. Total items {3}", update.added.Count(), @@ -102,22 +113,9 @@ service OrderService { update.changes.Count(), update.all.Count()); }); - // Use extenion methods to transform the data stream - var unsub2 = - fills_observable - // Filter the fill steam based on some criteria - .Filter(f => f.Side == Side.Buy) - // Build a Dictionary for fast access by id - .Table() - .Subscribe(update => { - if (update.state.TryGetValue("1234", out FillImmutable fill)) { - Console.WriteLine($"Found fill id 1234. LastPrice = {fill.LastPrice}"); - } - }); Thread.Sleep(TimeSpan.FromMinutes(5)); - unsub1.Dispose(); - unsub2.Dispose(); + unsub.Dispose(); ``` ### C++ Example ```cpp diff --git a/src/sdk/examples/client_sdk/.devcontainer.json b/src/sdk/examples/client_sdk/.devcontainer.json new file mode 100644 index 000000000..e4d6e291a --- /dev/null +++ b/src/sdk/examples/client_sdk/.devcontainer.json @@ -0,0 +1,20 @@ +{ + "name": "Spark sdk container", + "image": "spark_sdk", + "runArgs": [ + "--shm-size", "2048m", + "--cap-add=SYS_PTRACE", "--security-opt", "seccomp=unconfined", + "-u", "bts" + ], + "settings": { + "terminal.integrated.shell.linux": "/bin/bash", + "C_Cpp.default.cppStandard": "c++20" + }, + // use 9000 to avoid conflict with typical bts dev setup + "appPort": [9000,9001], + "postCreateCommand": "", + "extensions": [ + "ms-vscode.cpptools" + ] +} + diff --git a/src/sdk/examples/client_sdk/.gitignore b/src/sdk/examples/client_sdk/.gitignore new file mode 100644 index 000000000..4eaedcca4 --- /dev/null +++ b/src/sdk/examples/client_sdk/.gitignore @@ -0,0 +1 @@ +client_sdk_example diff --git a/src/sdk/examples/client_sdk/.vscode/c_cpp_properties.json b/src/sdk/examples/client_sdk/.vscode/c_cpp_properties.json new file mode 100644 index 000000000..5fea8714c --- /dev/null +++ b/src/sdk/examples/client_sdk/.vscode/c_cpp_properties.json @@ -0,0 +1,21 @@ +{ + "configurations": [ + { + "name": "Linux", + "includePath": [ + "/usr/include", + "/usr/local/include", + "${workspaceFolder}/**", + "/sdk/include/", + "/sdk/include/spark", + "/sdk/externals/include" + ], + "defines": [], + "compilerPath": "/usr/bin/clang", + "cStandard": "c11", + "cppStandard": "c++17", + "intelliSenseMode": "clang-x64" + } + ], + "version": 4 +} \ No newline at end of file diff --git a/src/sdk/examples/client_sdk/.vscode/launch.json b/src/sdk/examples/client_sdk/.vscode/launch.json new file mode 100644 index 000000000..c78882e49 --- /dev/null +++ b/src/sdk/examples/client_sdk/.vscode/launch.json @@ -0,0 +1,27 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "name": "(gdb) Launch", + "type": "cppdbg", + "request": "launch", + "program": "/sparkbin/sparkd", + "args": ["run"], + "stopAtEntry": false, + "cwd": "/sparkbin", + "environment": [], + "externalConsole": false, + "MIMode": "gdb", + "setupCommands": [ + { + "description": "Enable pretty-printing for gdb", + "text": "-enable-pretty-printing", + "ignoreFailures": true + } + ] + } + ] +} \ No newline at end of file diff --git a/src/sdk/examples/client_sdk/.vscode/tasks.json b/src/sdk/examples/client_sdk/.vscode/tasks.json new file mode 100644 index 000000000..6771fef6a --- /dev/null +++ b/src/sdk/examples/client_sdk/.vscode/tasks.json @@ -0,0 +1,29 @@ +{ + // See https://go.microsoft.com/fwlink/?LinkId=733558 + // for the documentation about the tasks.json format + // variables: ${workspaceRoot} + "version": "2.0.0", + "tasks": [ + { + "label": "build", + "type": "shell", + "command": "./build.sh", + "group": { + "kind": "build", + "isDefault": true + } + }, + { + "label": "Start Sparkd", + "type": "shell", + "command": "cd /sparkbin; ./sparkd run", + "isBackground": true, + "problemMatcher": [] + }, + { + "label": "Stop Sparkd", + "type": "shell", + "command": "pkill -f sparkd" + } + ] +} diff --git a/src/sdk/examples/client_sdk/build.sh b/src/sdk/examples/client_sdk/build.sh new file mode 100644 index 000000000..0fcdfabea --- /dev/null +++ b/src/sdk/examples/client_sdk/build.sh @@ -0,0 +1,24 @@ +#!/bin/bash +set -e + +SDK_ROOT=${SDK_ROOT:-../..} +NAME=client_sdk_example + +g++ -isystem $SDK_ROOT/externals/include \ + -I $SDK_ROOT/include \ + -I $SDK_ROOT/include/spark \ + -std=c++1z \ + -fPIC \ + -g \ + -o $NAME \ + $NAME.cpp \ + -Wl,-rpath,$SDK_ROOT/ \ + -L $SDK_ROOT/lib \ + -l spark + + +if [ "${SDK_VERSION}" != "" ]; then + ln -s /sparkbin/libspark.so +fi + + diff --git a/src/sdk/examples/client_sdk/client_sdk_example.cpp b/src/sdk/examples/client_sdk/client_sdk_example.cpp new file mode 100644 index 000000000..7f1e804ef --- /dev/null +++ b/src/sdk/examples/client_sdk/client_sdk_example.cpp @@ -0,0 +1,23 @@ +#include "lib/api/public/ServiceContext.h" +#include "lib/api/services/HelloService.client.pb.h" +#include "spark/lib/domain_model/Security.h" +// #include "lib/api/services/MarketDataRelay.client.pb.h" +#include "lib/api/services/AdminService.client.pb.h" + +#include +#include + +using namespace bts::api; + +std::vector get_securities(ServiceContext& ctx) { + std::vector result; + return result; +} + +std::vector get_users(ServiceContext& ctx) { + // auto admin = ctx; +} + +int main(int argc, char** argv) { + return 0; +}