-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfitness_service.cpp
144 lines (113 loc) · 3.21 KB
/
fitness_service.cpp
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
#include "fitness_service.h"
#include <iostream>
#include <stdexcept>
FitnessService::FitnessService(const std::string address, const int port, Tuio *tuio, SharedData *shared_data)
: address(address)
, port(port)
, connection_listener(port)
, tuio(tuio)
, shared_data(shared_data)
, verbose (false)
{
}
FitnessService::~FitnessService()
{
}
void FitnessService::set_verbouse(bool verbouse)
{
this->verbose = verbouse;
}
void FitnessService::start_listen()
{
bool poweroff = false;
while (!poweroff) {
Connection client = connection_listener.accept();
int methodID = client.readInt4();
std::cout<<"method "<<methodID<<" called!"<<std::endl;
switch (methodID) {
case RPC_START:
rpc_start(client);
break;
case RPC_FITNESS:
rpc_fitness(client);
break;
case RPC_POSITION:
rpc_position(client);
break;
default:
std::cerr<<"id "<<methodID<<" not implemented"<<std::endl;
}
}
}
void FitnessService::action_start(const int id)
{
std::cout<<"start on id "<< id <<std::endl;
shared_data->create(id);
}
float FitnessService::action_fitness(const int id, const fitness_type type)
{
std::cout<<"fitness on id "<< id <<" fitness type "<<type<<std::endl;
RobotPath *path = shared_data->get(id);
float fitness = 0;
switch(type) {
case DISPLACEMENT:
fitness = path->calculate_displacement();
break;
case PATH:
fitness = path->calculate_path();
break;
default:
std::cerr<<"unsupported fitness type: "<<type<<std::endl;
throw std::invalid_argument("unsupported fitness type");
}
return fitness;
}
std::tuple<float, float> FitnessService::action_position(const int id)
{
std::cout<<"position on id "<< id <<std::endl;
return tuio->getPositionFromId(id);
}
void FitnessService::rpc_start(Connection& client)
{
int id = client.readInt4();
action_start(id);
client.writeInt4(SUCCESS);
return;
}
void FitnessService::rpc_fitness(Connection& client)
{
int id = client.readInt4();
fitness_type type = (fitness_type) client.readInt4();
float fitness;
try {
fitness = action_fitness(id, type);
} catch(std::invalid_argument &e) {
client.writeFloat4(0);
client.writeInt4(ERROR);
return;
} catch(std::out_of_range &e) {
client.writeFloat4(0);
client.writeInt4(ERROR);
return;
}
client.writeFloat4(fitness);
client.writeInt4(SUCCESS);
return;
}
void FitnessService::rpc_position(Connection& client)
{
int id = client.readInt4();
std::tuple<float, float> position;
try {
position = action_position(id);
} catch(IDNotFound &ex) {
client.writeFloat4(0.0);
client.writeFloat4(0.0);
client.writeInt4(ERROR);
return;
}
client.writeFloat4( std::get<0>(position) );
client.writeFloat4( std::get<1>(position) );
client.writeInt4(SUCCESS);
return;
}