-
Notifications
You must be signed in to change notification settings - Fork 2
/
client.cpp
executable file
·396 lines (332 loc) · 10.9 KB
/
client.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
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
/*
Client connects to a node on its local machine & gets simulation parameters
Then it begins controlling robots
*/
#define IS_CLIENT
#include <map>
#include <dlfcn.h>
#include "controller.cpp"
using namespace std;
Controller *ctlr;
string node_ipc_id;
int my_id;
int sleep_time;
int num_robots;
double home_radius;
Home *my_home;
// local socket to control sock of node on our machine
zmq::socket_t *node_req_sock;
// socket to node's rep sync sock (also used for initialization)
zmq::socket_t *node_sync_req_sock;
// socket to sync pub sock of node on our machine (also used for initialization)
zmq::socket_t *node_sub_sock;
// construct some protobuf messages here so we don't call constructor needlessly
antixtransfer::control_message sense_req_msg;
antixtransfer::control_message control_msg;
antixtransfer::sense_data sense_msg;
// used for wait_for_next_turn()
antixtransfer::done node_done_msg;
void
synchronize_sub_sock() {
antixtransfer::node_master_sync sync_msg;
sync_msg.set_my_id( my_id );
string s;
while (s != "cli_sync")
s = antix::recv_str(node_sub_sock);
antix::recv_blank(node_sub_sock);
antix::send_pb(node_sync_req_sock, &sync_msg);
antix::recv_blank(node_sync_req_sock);
}
/*
Node has sent us the following sense data with at least one robot
Decide what to do and send a response
Decision logic from rtv's Antix
*/
void
controller(zmq::socket_t *node, antixtransfer::sense_data *sense_msg) {
// Message that gets sent as a request containing multiple robots
control_msg.clear_robot();
// For each robot in the sense data from this node, build a decision
int robot_size = sense_msg->robot_size();
for (int i = 0; i < robot_size; i++) {
// First we create a Controller with this robot's state
// XXX
// we copy seen_pucks to a vector to give a nicer interface
// this costs cpu... perhaps undesirable. but otherwise controller
// must look at protobuf...
ctlr->seen_pucks.clear();
int seen_puck_size = sense_msg->robot(i).seen_puck_size();
for (int j = 0; j < seen_puck_size; j++) {
ctlr->seen_pucks.push_back(
CSeePuck(sense_msg->robot(i).seen_puck(j).held(),
sense_msg->robot(i).seen_puck(j).range(),
sense_msg->robot(i).seen_puck(j).bearing())
);
}
ctlr->puck_action = PUCK_ACTION_NONE;
ctlr->x = sense_msg->robot(i).x();
ctlr->y = sense_msg->robot(i).y();
ctlr->a = sense_msg->robot(i).a();
ctlr->id = sense_msg->robot(i).id();
ctlr->last_x = sense_msg->robot(i).last_x();
ctlr->last_y = sense_msg->robot(i).last_y();
ctlr->home = my_home;
ctlr->has_puck = sense_msg->robot(i).has_puck();
#if DEBUG
cout << "Running controller for robot " << ctlr->id << " on turn " << antix::turn << endl;
cout << " at " << ctlr->x << ", " << ctlr->y << " and with ";
cout << ctlr->last_x << ", " << ctlr->last_y << " as last_x/y" << endl;
#endif
ctlr->v = 0.0;
ctlr->w = 0.0;
ctlr->doubles.clear();
ctlr->ints.clear();
ctlr->collided = sense_msg->robot(i).collided();
// Update this robot's memory
int doubles_size = sense_msg->robot(i).doubles_size();
for (int j = 0; j < doubles_size; j++) {
ctlr->doubles.push_back( sense_msg->robot(i).doubles(j) );
}
int ints_size = sense_msg->robot(i).ints_size();
for (int j = 0; j < ints_size; j++) {
ctlr->ints.push_back( sense_msg->robot(i).ints(j) );
}
#if DEBUG
cout << "Got last x " << sense_msg->robot(i).last_x();
cout << " and last y " << sense_msg->robot(i).last_y();
cout << " on turn " << antix::turn << endl;
#endif
// then run the controller
ctlr->controller();
// then add robot's new data to response protobuf
antixtransfer::control_message::Robot *r = control_msg.add_robot();
r->set_id( ctlr->id );
r->set_last_x( ctlr->last_x );
r->set_last_y( ctlr->last_y );
r->set_v( ctlr->v );
r->set_w( ctlr->w );
if (ctlr->puck_action == PUCK_ACTION_PICKUP)
r->set_puck_action(antixtransfer::control_message::PICKUP);
else if (ctlr->puck_action == PUCK_ACTION_DROP)
r->set_puck_action(antixtransfer::control_message::DROP);
else
r->set_puck_action(antixtransfer::control_message::NONE);
vector<double>::const_iterator doubles_end = ctlr->doubles.end();
for (vector<double>::const_iterator it = ctlr->doubles.begin(); it != doubles_end; it++) {
r->add_doubles( *it );
}
vector<int>::const_iterator ints_end = ctlr->ints.end();
for (vector<int>::const_iterator it = ctlr->ints.begin(); it != ints_end; it++) {
r->add_ints( *it );
}
}
// send the decision for all of our robots to this node
antix::send_pb(node, &control_msg);
}
/*
Request from local node what our robots can see
Make a decision based on this & send it back
*/
void
sense_and_controller() {
#if DEBUG_SYNC
cout << "Sync: Requesting sense data from node for my team: " << my_id << "..." << endl;
#endif
// Ask local node what the robots from our team sees
antix::send_pb(node_req_sock, &sense_req_msg);
#if DEBUG_SYNC
cout << "Sync: Awaiting sense data response..." << endl;
#endif
// Get the sense data back from the node
int rc = antix::recv_pb(node_req_sock, &sense_msg, 0);
assert(rc == 1);
#if DEBUG_SYNC
cout << "Sync: Got sense data with " << sense_msg.robot_size() << " robots." << endl;
#endif
// if there's at least one robot in the response, we will be sending a command
if (sense_msg.robot_size() > 0) {
controller(node_req_sock, &sense_msg);
#if DEBUG_SYNC
cout << "Sync: Awaiting responses from node we sent commands to..." << endl;
#endif
// get response back since REQ sock
antix::recv_blank(node_req_sock);
}
#if DEBUG_SYNC
cout << "Sync: Sensing & controlling done." << endl;
#endif
}
/*
Look through the list of homes from init response & find our own
*/
Home *
find_our_home(antixtransfer::connect_init_response *init_response) {
for (int i = 0; i < init_response->home_size(); i++) {
if (init_response->home(i).team() == my_id)
return new Home( init_response->home(i).x(), init_response->home(i).y(), home_radius, init_response->home(i).team() );
}
return NULL;
}
int
main(int argc, char **argv) {
GOOGLE_PROTOBUF_VERIFY_VERSION;
zmq::context_t context(1);
srand( time(NULL) );
srand48( time(NULL) );
if (argc != 5) {
cerr << "Usage: " << argv[0] << " <# of robots> <client id> <node IPC id> <AI library.so>" << endl;
return -1;
}
assert(atoi(argv[1]) > 0);
num_robots = atoi(argv[1]);
my_id = atoi(argv[2]);
node_ipc_id = string(argv[3]);
string ai_library = string(argv[4]);
// If non absolute path, assume cwd for AI.so path
if (ai_library[0] != '/')
ai_library = "./" + ai_library;
// Load AI dynamically
// From http://stackoverflow.com/questions/496664/c-dynamic-shared-library-on-linux
void* handle = dlopen(ai_library.c_str(), RTLD_NOW | RTLD_GLOBAL);
if (handle == NULL) {
cerr << "Error: could not load AI library: " << dlerror() << endl;
exit(-1);
}
Controller* (*create)();
void (*destroy)(Controller*);
create = (Controller* (*)())dlsym(handle, "create_object");
destroy = (void (*)(Controller*))dlsym(handle, "destroy_object");
ctlr = (Controller*) create();
// initialize some protobufs that do not change
sense_req_msg.set_team(my_id);
control_msg.set_team(my_id);
node_done_msg.set_my_id(my_id);
node_done_msg.set_type( antixtransfer::done::CLIENT );
cout << "Connecting to local node..." << endl;
string node_ipc_prefix = IPC_PREFIX;
// node sync req sock
while (1) {
try {
node_sync_req_sock = new zmq::socket_t(context, ZMQ_REQ);
} catch (zmq::error_t e) {
cout << "Error: Node sync req new: " << e.what() << endl;
delete node_sync_req_sock;
antix::sleep(1000);
continue;
}
break;
}
while (1) {
try {
node_sync_req_sock->connect(antix::make_endpoint_ipc(node_ipc_prefix + node_ipc_id + "r"));
} catch (zmq::error_t e) {
cout << "Error: Node sync req connect: " << e.what() << endl;
antix::sleep(1000);
continue;
}
break;
}
// node sync sub sock
while (1) {
try {
node_sub_sock = new zmq::socket_t(context, ZMQ_SUB);
} catch (zmq::error_t e) {
cout << "Error: Node sync sub new: " << e.what() << endl;
delete node_sub_sock;
antix::sleep(1000);
continue;
}
break;
}
while (1) {
try {
node_sub_sock->setsockopt(ZMQ_SUBSCRIBE, "", 0);
node_sub_sock->connect(antix::make_endpoint_ipc(node_ipc_prefix + node_ipc_id + "p"));
} catch (zmq::error_t e) {
cout << "Error: Node sync sub connect: " << e.what() << endl;
antix::sleep(1000);
continue;
}
break;
}
// node control
while (1) {
try {
node_req_sock = new zmq::socket_t(context, ZMQ_REQ);
} catch (zmq::error_t e) {
cout << "Error: Node req new: " << e.what() << endl;
delete node_req_sock;
antix::sleep(1000);
continue;
}
break;
}
while (1) {
try {
node_req_sock->connect(antix::make_endpoint_ipc(node_ipc_prefix + node_ipc_id + "c"));
} catch (zmq::error_t e) {
cout << "Error: Node req connect: " << e.what() << endl;
antix::sleep(1000);
continue;
}
break;
}
cout << "Connected to local node. Telling it of our existence..." << endl;
// Identify ourself & specify num robots we want
antixtransfer::connect_init_client init_req;
init_req.set_num_robots( num_robots );
init_req.set_id( my_id );
antix::send_pb(node_sync_req_sock, &init_req);
// Get back blank in response since REQ sock
antix::recv_blank(node_sync_req_sock);
// Make sure we are synchronized with node's pub sock
synchronize_sub_sock();
cout << "Waiting for signal for simulation begin..." << endl;
// Wait for response containing simulation params / home location
// This also indicates simulation begin
antixtransfer::connect_init_response init_response;
// we may get messages on sub sock from node syncing other clients. ignore
string s;
while (s != "cli_begin")
s = antix::recv_str(node_sub_sock);
int rc = antix::recv_pb(node_sub_sock, &init_response, 0);
assert(rc == 1);
home_radius = init_response.home_radius();
sleep_time = init_response.sleep_time();
Robot::pickup_range = init_response.pickup_range();
antix::world_size = init_response.world_size();
ctlr->set_static_vars(antix::world_size, Robot::pickup_range);
// set our own home
my_home = find_our_home(&init_response);
assert(my_home != NULL);
cout << "Beginning simulation..." << endl;
// response from node on sync sock
string response;
// enter main loop
while (1) {
// sense, then decide & send what commands for each robot
sense_and_controller();
// XXX it's possible we should use a different function than this
// as this includes score data definition (done msg) for node which
// may waste cpu depending on protobuf impl.
response = antix::wait_for_next_turn(node_sync_req_sock, node_sub_sock, &node_done_msg);
if (response == "s")
// leave loop
break;
#if DEBUG
antix::turn++;
#endif
#if SLEEP
antix::sleep(sleep_time);
#endif
}
cout << "Received shutdown message from node. Shutting down..." << endl;
delete my_home;
delete node_sync_req_sock;
delete node_sub_sock;
delete node_req_sock;
destroy(ctlr);
dlclose(handle);
google::protobuf::ShutdownProtobufLibrary();
return 0;
}