Add client sdk example

This commit is contained in:
Eric Harding committed 2020-02-07 22:20:42 -05:00
1 parent 82bc402216
commit 28e98259f6
8 files changed
+162 -19

No files matched your search

@@ -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<T>() 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 <url>`
* 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&lt;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<OrderService>();
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<OrderService>();
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<fill_id, Fill> 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
@@ -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"
]
}
+1
View File
@@ -0,0 +1 @@
client_sdk_example
@@ -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
}
+27
View File
@@ -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
}
]
}
]
}
+29
View File
@@ -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"
}
]
}
+24
View File
@@ -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
@@ -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 <string>
#include <vector>
using namespace bts::api;
std::vector<Security> get_securities(ServiceContext& ctx) {
std::vector<Security> result;
return result;
}
std::vector<std::string> get_users(ServiceContext& ctx) {
// auto admin = ctx;
}
int main(int argc, char** argv) {
return 0;
}