ServerWrapper.ftl 4.26 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
<#include "/Common.ftl">
#include <chrono>
#include <iostream>
#include <memory>
#include <string>

#include "spdlog/spdlog.h"
#include <armadillo>
#include <grpcpp/grpcpp.h>
#include "model.grpc.pb.h"
#include "${viewModel.mainModelName}.h"

using arma::mat;
using grpc::Server;
using grpc::ServerBuilder;
using grpc::ServerContext;
using grpc::Status;
using model::Actuation;
using model::Model;
using model::ModelInputs;
using model::ModelOutputs;
using model::SensorData;
using model::Trajectory;
using std::chrono::duration_cast;
using std::chrono::high_resolution_clock;
using std::chrono::milliseconds;
using std::chrono::time_point;

Actuation make_actuation(double engine, double steering_angle, double brakes)
{
  Actuation p;
  p.set_engine(engine);
  p.set_steering_angle(steering_angle);
  p.set_brakes(brakes);
  return p;
}

class ServiceImplementation final : public Model::Service
{
private:
  time_point<high_resolution_clock> start_time_;
  int number_of_calls_;
  const int calls_stats_frequency_;

  std::shared_ptr<spdlog::logger> logger_;

  ${viewModel.mainModelName} model_;

  void pass_inputs_to_model_(const ModelInputs *request)
  {
    model_.timeIncrement = request->time_increment();
    if (request->has_sensor_data())
    {
      pass_sensor_data_(request->sensor_data());
    }
    else
    {
      logger_->warn("no sensor data is provided");
    }
    if (request->has_actuation())
    {
      pass_actuation_(request->actuation());
    }
    else
    {
      logger_->warn("no actuation is provided");
    }
    if (request->has_trajectory())
    {
      pass_trajectory_(request->trajectory());
    }
    else
    {
      logger_->warn("no trajectory is provided");
    }
  }

  void pass_sensor_data_(const SensorData &sensor_data)
  {
    model_.currentVelocity = sensor_data.velocity();
    if (sensor_data.has_position())
    {
      auto p = sensor_data.position();
      model_.x = p.x();
      model_.y = p.y();
    }
    else
    {
      logger_->warn("no position is provided");
    }
    model_.compass = sensor_data.compass();
  }

  void pass_actuation_(const Actuation &actuation)
  {
    model_.currentEngine = actuation.engine();
    model_.currentSteering = actuation.steering_angle();
    model_.currentBrakes = actuation.brakes();
  }

  void pass_trajectory_(const Trajectory &trajectory)
  {
    auto len = trajectory.points_size();
    model_.trajectory_length = len;
    if (len > 0)
    {
      mat &xs = model_.trajectory_x;
      mat &ys = model_.trajectory_y;
      for (auto i = 0; i < len; i++)
      {
        auto p = trajectory.points(i);
        xs(0, i) = p.x();
        ys(0, i) = p.y();
      }
    }
    else
    {
      logger_->warn("trajectory is empty");
    }
  }

  void log_call_()
  {
    number_of_calls_++;
    if (number_of_calls_ % calls_stats_frequency_ == 0)
    {
      auto duration = duration_cast<milliseconds>(high_resolution_clock::now() - start_time_);
      logger_->info("average response time = {} milliseconds", duration.count());
      number_of_calls_ = 0;
      start_time_ = high_resolution_clock::now();
    }
  }

public:
  ServiceImplementation() : calls_stats_frequency_(10000), start_time_(high_resolution_clock::now())
  {
    number_of_calls_ = 0;
    logger_ = spdlog::basic_logger_mt("ServiceImplementation", "/var/www/html/ServiceImplementation.log");
    model_.init();
  }

  Status Execute(ServerContext *context, const ModelInputs *request, ModelOutputs *response) override
  {
    log_call_();
    pass_inputs_to_model_(request);
    model_.execute();
    auto engine = model_.engine;
    auto steering_angle = model_.steering;
    auto brakes = model_.brakes;
    response->mutable_actuation()->CopyFrom(make_actuation(engine, steering_angle, brakes));
    return Status::OK;
  }
};

void run_server()
{
  std::cout << "initializing..." << std::endl
            << std::flush;
  ServerBuilder builder;
  std::string server_address("0.0.0.0:10247");
  builder.AddListeningPort(server_address, grpc::InsecureServerCredentials());
  ServiceImplementation service;
  builder.RegisterService(&service);
  std::unique_ptr<Server> server(builder.BuildAndStart());
  std::cout << "server is listening on " << server_address << std::endl
            << std::flush;
  server->Wait();
}

int main(int argc, char **argv)
{
  run_server();
  return 0;
}