TimeBox order code complete on server.

This commit is contained in:
Pedro Pinto committed 2018-03-19 09:28:19 -04:00
1 parent 81941cba4e
commit fcf4c9f187
2 files changed
+125 -27

No files matched your search

@@ -59,8 +59,7 @@ void TimeBoxOrder::on_init_recovered(api::orderman::OrderRecord const &rec,
me->state = rec.state();
switch (me->state.status()) {
case ORDER_STATUS_WORKING:
case ORDER_STATUS_DISCONNECTED: {
case ORDER_STATUS_WORKING: {
auto msg = "System restarted with active time box order";
me->state.set_last_msg(msg);
transition(ORDER_STATUS_ERROR, msg);
@@ -73,6 +72,8 @@ void TimeBoxOrder::on_init_recovered(api::orderman::OrderRecord const &rec,
case ORDER_STATUS_CANCELED:
break;
// impossible to reach states for this order type
case ORDER_STATUS_DISCONNECTED:
case ORDER_STATUS_PENDING_WORKING:
case ORDER_STATUS_INACTIVE:
case OrderStatus_INT_MIN_SENTINEL_DO_NOT_USE_:
@@ -88,6 +89,7 @@ void TimeBoxOrder::on_activate(Price p, Quantity q, OrderServices &services) {
me->child_builder.activate(services);
switch (me->state.status()) {
case ORDER_STATUS_INACTIVE:
me->state.set_price(p.value());
me->state.set_quantity(q.value());
@@ -97,20 +99,19 @@ void TimeBoxOrder::on_activate(Price p, Quantity q, OrderServices &services) {
transition(ORDER_STATUS_WORKING);
break;
// these are not possible since the on_init_recovered code
// would have transitioned us to error already
case ORDER_STATUS_WORKING:
case ORDER_STATUS_PENDING_WORKING:
case ORDER_STATUS_DISCONNECTED: {
bts_assert(false);
break;
}
// terminal states
case ORDER_STATUS_ERROR:
case ORDER_STATUS_REJECTED_BY_SPARK:
case ORDER_STATUS_COMPLETED:
case ORDER_STATUS_CANCELED:
break;
// impossible to reach states at this stage (on recovery from disk
// we would have transitioned to error if not already in a terminal
// state)
case ORDER_STATUS_WORKING:
case ORDER_STATUS_PENDING_WORKING:
case ORDER_STATUS_DISCONNECTED:
case OrderStatus_INT_MIN_SENTINEL_DO_NOT_USE_:
case OrderStatus_INT_MAX_SENTINEL_DO_NOT_USE_:
bts_assert(false);
@@ -124,19 +125,30 @@ void TimeBoxOrder::on_update(Price p, Quantity q) {
switch (me->state.status()) {
case ORDER_STATUS_WORKING:
// what is state of our child order?
switch (me->child_state.status()) {
case ORDER_STATUS_INACTIVE:
// we have yet to launch it
me->state.set_price(p.value());
me->state.set_quantity(q.value());
break;
case ORDER_STATUS_WORKING:
// we launched our child, everything is nominal
me->state.set_price(p.value());
me->state.set_quantity(q.value());
me->child.update(p,q);
break;
case ORDER_STATUS_PENDING_WORKING:
// we launched our child but we are not yet able
// to update it
transition(me->state.status(), "Cannot update order");
break;
// if the child was in any of these states we couldn't be
// in the working state
case ORDER_STATUS_DISCONNECTED:
case ORDER_STATUS_ERROR:
case ORDER_STATUS_REJECTED_BY_SPARK:
@@ -148,15 +160,18 @@ void TimeBoxOrder::on_update(Price p, Quantity q) {
break;
}
break;
case ORDER_STATUS_DISCONNECTED:
case ORDER_STATUS_PENDING_WORKING:
case ORDER_STATUS_REJECTED_BY_SPARK:
case ORDER_STATUS_COMPLETED:
case ORDER_STATUS_CANCELED:
case ORDER_STATUS_ERROR:
transition(me->state.status(), "Cannot update order");
break;
// these are all unreacheable for this order type at this point
case ORDER_STATUS_INACTIVE:
case ORDER_STATUS_DISCONNECTED:
case ORDER_STATUS_PENDING_WORKING:
case OrderStatus_INT_MIN_SENTINEL_DO_NOT_USE_:
case OrderStatus_INT_MAX_SENTINEL_DO_NOT_USE_:
bts_assert(false);
@@ -172,17 +187,27 @@ void TimeBoxOrder::on_cancel() {
switch (me->state.status()) {
case ORDER_STATUS_WORKING:
// how is our child doing?
switch (me->child_state.status()) {
case ORDER_STATUS_INACTIVE:
// we have not yet launched the child, so nothing to do
transition(ORDER_STATUS_CANCELED);
break;
case ORDER_STATUS_WORKING:
// child is running, we need to send it a cancel
me->child.cancel();
transition(ORDER_STATUS_CANCELED);
break;
case ORDER_STATUS_PENDING_WORKING:
// child is running but it cannot be canceled at this point
transition(me->state.status(), "Cannot cancel order");
break;
// child cannot be in any of these if we are in the working state
case ORDER_STATUS_DISCONNECTED:
case ORDER_STATUS_ERROR:
case ORDER_STATUS_REJECTED_BY_SPARK:
@@ -194,14 +219,17 @@ void TimeBoxOrder::on_cancel() {
break;
}
break;
case ORDER_STATUS_PENDING_WORKING:
case ORDER_STATUS_DISCONNECTED:
case ORDER_STATUS_REJECTED_BY_SPARK:
case ORDER_STATUS_COMPLETED:
case ORDER_STATUS_CANCELED:
case ORDER_STATUS_ERROR:
transition(me->state.status(), "Cannot cancel order");
break;
// these are unreacheable for this order type
case ORDER_STATUS_PENDING_WORKING:
case ORDER_STATUS_DISCONNECTED:
case ORDER_STATUS_INACTIVE:
case OrderStatus_INT_MIN_SENTINEL_DO_NOT_USE_:
case OrderStatus_INT_MAX_SENTINEL_DO_NOT_USE_:
@@ -220,14 +248,20 @@ void TimeBoxOrder::transition(api::orderman::OrderStatus new_status,
me->state.set_status(new_status);
}
void TimeBoxOrder::report_state() { me->services->report_state(me->state); }
void TimeBoxOrder::on_time_change() {
auto now = utils::pb::pb_now().seconds();
switch (me->state.status()) {
case ORDER_STATUS_WORKING:
// what is the state of our child?
switch (me->child_state.status()) {
case ORDER_STATUS_INACTIVE:
if (utils::pb::pb_now().seconds() > me->start_time.seconds()) {
case ORDER_STATUS_INACTIVE: {
// we haven't yet launched the child, check the time and
// launch it if appropriate
if ( now >= me->start_time.seconds() && now < me->stop_time.seconds()) {
LOG(OrderMan, Info, "TimeBox order {} launching child {}",
key().value(), me->child.key().value());
me->child.activate(Price(me->state.price()),
@@ -236,16 +270,22 @@ void TimeBoxOrder::on_time_change() {
transition(ORDER_STATUS_WORKING);
}
break;
}
case ORDER_STATUS_WORKING:
if (utils::pb::pb_now().seconds() > me->stop_time.seconds()) {
// child is already running, could it be time to stop it?
if (now >= me->stop_time.seconds()) {
LOG(OrderMan, Info, "TimeBox order {} canceling child {}",
key().value(), me->child.key().value());
me->child.cancel();
transition(ORDER_STATUS_CANCELED);
}
break;
case ORDER_STATUS_PENDING_WORKING:
break;
// child cannot be in any of these states if we are in the working state
case ORDER_STATUS_DISCONNECTED:
case ORDER_STATUS_ERROR:
case ORDER_STATUS_REJECTED_BY_SPARK:
@@ -256,14 +296,64 @@ void TimeBoxOrder::on_time_change() {
bts_assert(false);
break;
}
case ORDER_STATUS_PENDING_WORKING:
case ORDER_STATUS_DISCONNECTED:
case ORDER_STATUS_REJECTED_BY_SPARK:
break;
case ORDER_STATUS_COMPLETED:
case ORDER_STATUS_CANCELED:
case ORDER_STATUS_ERROR:
break;
case ORDER_STATUS_INACTIVE:
case ORDER_STATUS_REJECTED_BY_SPARK:
case ORDER_STATUS_PENDING_WORKING:
case ORDER_STATUS_DISCONNECTED:
case OrderStatus_INT_MIN_SENTINEL_DO_NOT_USE_:
case OrderStatus_INT_MAX_SENTINEL_DO_NOT_USE_:
bts_assert(false);
break;
}
report_state();
}
void TimeBoxOrder::on_child_order_update(OrderStateUpdateEvent const&ev)
{
me->child_state = ev.state;
switch (me->state.status()) {
case ORDER_STATUS_WORKING:
switch (ev.state.status()) {
case ORDER_STATUS_WORKING:
case ORDER_STATUS_PENDING_WORKING:
// nothing to do but report new state
break;
case ORDER_STATUS_COMPLETED:
transition(ORDER_STATUS_COMPLETED);
break;
case ORDER_STATUS_DISCONNECTED:
case ORDER_STATUS_REJECTED_BY_SPARK:
case ORDER_STATUS_CANCELED:
case ORDER_STATUS_ERROR:
transition(ORDER_STATUS_ERROR, "Unexpected child order state");
break;
case ORDER_STATUS_INACTIVE:
case OrderStatus_INT_MIN_SENTINEL_DO_NOT_USE_:
case OrderStatus_INT_MAX_SENTINEL_DO_NOT_USE_:
bts_assert(false);
break;
}
break;
case ORDER_STATUS_CANCELED:
case ORDER_STATUS_ERROR:
case ORDER_STATUS_COMPLETED:
break;
case ORDER_STATUS_DISCONNECTED:
case ORDER_STATUS_PENDING_WORKING:
case ORDER_STATUS_REJECTED_BY_SPARK:
case ORDER_STATUS_INACTIVE:
case OrderStatus_INT_MIN_SENTINEL_DO_NOT_USE_:
case OrderStatus_INT_MAX_SENTINEL_DO_NOT_USE_:
@@ -273,8 +363,14 @@ void TimeBoxOrder::on_time_change() {
report_state();
}
void TimeBoxOrder::on_child_order_update(OrderStateUpdateEvent const&)
{}
void TimeBoxOrder::report_state() {
auto merged_state = me->state;
merged_state.set_filled_quantity (me->child_state.filled_quantity());
merged_state.set_last_price ( me->child_state.last_price());
merged_state.set_avg_price ( me->child_state.avg_price());
me->services->report_state(merged_state);
}
}
}