18#ifdef FREECIV_JSON_CONNECTION
31#ifdef FREECIV_HAVE_SYS_TYPES_H
34#ifdef HAVE_SYS_SOCKET_H
35#include <sys/socket.h>
37#ifdef HAVE_NETINET_IN_H
38#include <netinet/in.h>
40#ifdef HAVE_ARPA_INET_H
63static bool dio_get_bool8_json_internal(json_t *json_packet,
70static CURL *get_curl(
void)
72 static CURL *curl_easy_handle = NULL;
74 if (curl_easy_handle == NULL) {
75 curl_easy_handle = curl_easy_init();
78 curl_easy_reset(curl_easy_handle);
81 return curl_easy_handle;
84static void plocation_write_data(json_t *
item,
91static void plocation_write_field(json_t *
item,
96 json_object_set_new(
item, location->
name, data);
98 plocation_write_data(json_object_get(
item, location->
name),
106static void plocation_write_elem(json_t *
item,
111 json_array_set_new(
item, location->
number, data);
113 plocation_write_data(json_array_get(
item, location->
number),
122static void plocation_write_data(json_t *
item,
126 switch (location->
kind) {
128 plocation_write_field(
item, location, data);
131 plocation_write_elem(
item, location, data);
134 log_error(
"Unknown packet part location kind.");
139static json_t *plocation_read_data(json_t *
item,
145static json_t *plocation_read_field(json_t *
item,
149 return json_object_get(
item, location->
name);
151 return plocation_read_data(json_object_get(
item, location->
name),
159static json_t *plocation_read_elem(json_t *
item,
163 return json_array_get(
item, location->
number);
165 return plocation_read_data(json_array_get(
item, location->
number),
173static json_t *plocation_read_data(json_t *
item,
176 switch (location->
kind) {
178 return plocation_read_field(
item, location);
180 return plocation_read_elem(
item, location);
182 log_error(
"Unknown packet part location kind.");
195 plocation_write_data(dout->
json, location, json_integer(value));
209 plocation_write_data(dout->
json, location, json_integer(value));
219 const struct plocation *location,
int value)
222 plocation_write_data(dout->
json, location, json_integer(value));
232 const struct plocation *location,
int value)
235 plocation_write_data(dout->
json, location, json_integer(value));
249 json_t *obj = json_object();
250 json_t *min_surplus = json_array();
251 json_t *factor = json_array();
254 for (i = 0; i <
O_LAST; i++) {
255 json_array_append_new(min_surplus,
257 json_array_append_new(factor,
258 json_integer(param->
factor[i]));
261 json_object_set_new(obj,
"minimal_surplus", min_surplus);
262 json_object_set_new(obj,
"factor", factor);
263 json_object_set_new(obj,
"max_growth", json_boolean(param->
max_growth));
264 json_object_set_new(obj,
"require_happy",
266 json_object_set_new(obj,
"allow_disorder",
268 json_object_set_new(obj,
"allow_specialists",
270 json_object_set_new(obj,
"happy_factor",
272 plocation_write_data(dout->
json, location, obj);
286 json_t *obj = json_object();
287 json_object_set_new(obj,
"order", json_integer(order->
order));
288 json_object_set_new(obj,
"activity", json_integer(order->
activity));
289 json_object_set_new(obj,
"target", json_integer(order->
target));
290 json_object_set_new(obj,
"sub_target", json_integer(order->
sub_target));
291 json_object_set_new(obj,
"action", json_integer(order->
action));
292 if (order->
dir == -1) {
294 json_object_set_new(obj,
"dir", json_integer(-1));
296 json_object_set_new(obj,
"dir", json_integer(order->
dir));
298 plocation_write_data(dout->
json, location, obj);
320 for (i = 0; i <
size; i++) {
326 json_object_set_new(
universal,
"kind", json_integer(pcp->
kind));
342static bool dio_get_uint8_json_internal(json_t *json_packet,
346 json_t *pint = plocation_read_data(json_packet, location);
352 *dest = json_integer_value(pint);
366 const struct plocation *location,
int *dest)
369 return dio_get_uint8_json_internal(pc->json_packet, location, dest);
379 const struct plocation *location,
int *dest)
382 json_t *pint = plocation_read_data(pc->json_packet, location);
388 *dest = json_integer_value(pint);
404static bool dio_get_uint32_json_internal(json_t *json_packet,
408 json_t *pint = plocation_read_data(json_packet, location);
414 *dest = json_integer_value(pint);
428 const struct plocation *location,
int *dest)
431 return dio_get_uint32_json_internal(pc->json_packet, location, dest);
441 const struct plocation *location,
int *dest)
444 return dio_get_uint32_json_internal(pc->json_packet, location, dest);
464 log_packet(
"Corrupt cm_parameter.max_growth");
471 log_packet(
"Corrupt cm_parameter.require_happy");
478 log_packet(
"Corrupt cm_parameter.allow_disorder");
485 log_packet(
"Corrupt cm_parameter.allow_specialists");
493 log_packet(
"Corrupt cm_parameter.happy_factor");
500 for (i = 0; i <
O_LAST; i++) {
511 for (i = 0; i <
O_LAST; i++) {
515 log_packet(
"Corrupt cm_parameter.minimal_surplus");
540 int iorder, iactivity, idir;
544 while (
loc->sub_location) {
555 loc->sub_location->name =
"activity";
562 loc->sub_location->name =
"target";
569 loc->sub_location->name =
"sub_target";
576 loc->sub_location->name =
"action";
583 loc->sub_location->name =
"dir";
593 order->
order = iorder;
615 const json_t *wlist = plocation_read_data(pc->json_packet, location);
619 if (!json_is_array(wlist)) {
625 length = json_array_size(wlist);
633 for (i = 0; i < length; i++) {
641 if (!dio_get_uint8_json_internal(pc->json_packet, location, &
kind)) {
649 if (!dio_get_uint8_json_internal(pc->json_packet, location, &
value)) {
677 int **values,
int stop_value)
681 log_warn(
"Received unimplemeted data type uint8_vec8.");
699 log_warn(
"Received unimplemeted data type uint16_vec8.");
716 bool survives, present, quiet;
721 json_t *
requirement = plocation_read_data(pc->json_packet, location);
731 log_error(
"ERROR: Unable to get part of requirement from location: %s",
736 req_field->
name =
"value";
737 if (!dio_get_uint32_json_internal(
requirement, req_field, &value)) {
738 log_error(
"ERROR: Unable to get part of requirement from location: %s",
743 req_field->
name =
"range";
745 log_error(
"ERROR: Unable to get part of requirement from location: %s",
750 req_field->
name =
"survives";
751 if (!dio_get_bool8_json_internal(
requirement, req_field, &survives)) {
752 log_error(
"ERROR: Unable to get part of requirement from location: %s",
757 req_field->
name =
"present";
758 if (!dio_get_bool8_json_internal(
requirement, req_field, &present)) {
759 log_error(
"ERROR: Unable to get part of requirement from location: %s",
764 req_field->
name =
"quiet";
765 if (!dio_get_bool8_json_internal(
requirement, req_field, &quiet)) {
766 log_error(
"ERROR: Unable to get part of requirement from location: %s",
793 json_t *action_probability = plocation_read_data(pc->json_packet, location);
795 if (!action_probability) {
796 log_error(
"ERROR: Unable to get action probability from location: %s",
804 if (!dio_get_uint8_json_internal(action_probability, ap_field, &prob->
min)) {
805 log_error(
"ERROR: Unable to get part of action probability "
811 ap_field->
name =
"max";
812 if (!dio_get_uint8_json_internal(action_probability, ap_field, &prob->
max)) {
813 log_error(
"ERROR: Unable to get part of action probability "
835 json_t *farray = json_array();
839 for (i = 0; i <
size; i++) {
840 json_array_append_new(farray, json_null());
843 plocation_write_data(dout->
json, location, farray);
853 const struct plocation *location,
int value)
856 plocation_write_data(dout->
json, location, json_integer(value));
866 const struct plocation *location,
int value)
869 plocation_write_data(dout->
json, location, json_integer(value));
879 const struct plocation *location,
bool value)
882 plocation_write_data(dout->
json, location, value ? json_true() : json_false());
892 const struct plocation *location,
bool value)
895 plocation_write_data(dout->
json, location, value ? json_true() : json_false());
906 float value,
int float_factor)
909 plocation_write_data(dout->
json, location, json_real(value));
920 float value,
int float_factor)
923 plocation_write_data(dout->
json, location, json_real(value));
934 int *values,
int stop_value)
938 log_error(
"Tried to send unimplemeted data type uint8_vec8.");
948 const struct plocation *location,
int *values,
953 log_error(
"Tried to send unimplemeted data type uint16_vec8.");
974 for (i = 0; i <
size; i++) {
978 ((
unsigned char *)value)[i]);
995 plocation_write_data(dout->
json, location, json_string(value));
1009 char *escaped_value;
1012 escaped_value = curl_easy_escape(get_curl(), value, 0);
1018 curl_free(escaped_value);
1033 bool survives, present, quiet;
1044 json_object_set_new(
requirement,
"value", json_integer(value));
1048 json_object_set_new(
requirement,
"survives", json_boolean(survives));
1049 json_object_set_new(
requirement,
"present", json_boolean(present));
1050 json_object_set_new(
requirement,
"quiet", json_boolean(quiet));
1068 json_t *action_probability = json_object();
1072 json_object_set_new(action_probability,
"min", json_integer(prob->
min));
1073 json_object_set_new(action_probability,
"max", json_integer(prob->
max));
1076 plocation_write_data(dout->
json, location, action_probability);
1085static bool dio_get_bool8_json_internal(json_t *json_packet,
1089 json_t *pbool = plocation_read_data(json_packet, location);
1095 *dest = json_is_true(pbool);
1109 const struct plocation *location,
bool *dest)
1111 if (pc->json_mode) {
1112 return dio_get_bool8_json_internal(pc->json_packet, location, dest);
1122 const struct plocation *location,
bool *dest)
1124 if (pc->json_mode) {
1125 json_t *pbool = plocation_read_data(pc->json_packet, location);
1131 *dest = json_is_true(pbool);
1149 float *dest,
int float_factor)
1151 if (pc->json_mode) {
1152 json_t *preal = plocation_read_data(pc->json_packet, location);
1158 *dest = json_real_value(preal);
1171 float *dest,
int float_factor)
1173 if (pc->json_mode) {
1174 json_t *preal = plocation_read_data(pc->json_packet, location);
1180 *dest = json_real_value(preal);
1192 const struct plocation *location,
int *dest)
1194 if (pc->json_mode) {
1195 json_t *pint = plocation_read_data(pc->json_packet, location);
1201 *dest = json_integer_value(pint);
1218 const struct plocation *location,
int *dest)
1220 if (pc->json_mode) {
1221 json_t *pint = plocation_read_data(pc->json_packet, location);
1227 *dest = json_integer_value(pint);
1245 void *dest,
size_t dest_size)
1247 if (pc->json_mode) {
1252 for (i = 0; i < dest_size; i++) {
1257 if (!dio_get_uint8_json_internal(pc->json_packet, location, &val)) {
1261 ((
unsigned char *)dest)[i] = val;
1275static bool dio_get_string_json_internal(json_t *json_packet,
1277 char *dest,
size_t max_dest_size)
1279 json_t *pstring = plocation_read_data(json_packet, location);
1280 const char *result_str;
1287 result_str = json_string_value(pstring);
1303 char *dest,
size_t max_dest_size)
1305 if (pc->json_mode) {
1306 return dio_get_string_json_internal(pc->json_packet, location,
1307 dest, max_dest_size);
1320 char *dest,
size_t max_dest_size)
1322 if (pc->json_mode) {
1323 char *escaped_value;
1324 char *unescaped_value;
1327 escaped_value =
fc_malloc(max_dest_size);
1329 if (!dio_get_string_json_internal(pc->json_packet, location,
1330 escaped_value, max_dest_size)) {
1336 unescaped_value = curl_easy_unescape(get_curl(), escaped_value, 0, NULL);
1342 memcpy(dest, unescaped_value,
1344 MIN(max_dest_size, strlen(unescaped_value) + 1));
1347 curl_free(unescaped_value);
1350 dest[max_dest_size - 1] =
'\0';
void cm_init_parameter(struct cm_parameter *dest)
void dio_put_sint8_json(struct json_data_out *dout, const struct plocation *location, int value)
void dio_put_uint32_json(struct json_data_out *dout, const struct plocation *location, int value)
bool dio_get_uint16_json(struct connection *pc, struct data_in *din, const struct plocation *location, int *dest)
bool dio_get_estring_json(struct connection *pc, struct data_in *din, const struct plocation *location, char *dest, size_t max_dest_size)
void dio_put_sfloat_json(struct json_data_out *dout, const struct plocation *location, float value, int float_factor)
bool dio_get_worklist_json(struct connection *pc, struct data_in *din, struct plocation *location, struct worklist *pwl)
void dio_put_sint16_json(struct json_data_out *dout, const struct plocation *location, int value)
bool dio_get_sint16_json(struct connection *pc, struct data_in *din, const struct plocation *location, int *dest)
bool dio_get_sint32_json(struct connection *pc, struct data_in *din, const struct plocation *location, int *dest)
void dio_put_uint8_vec8_json(struct json_data_out *dout, const struct plocation *location, int *values, int stop_value)
bool dio_get_sint8_json(struct connection *pc, struct data_in *din, const struct plocation *location, int *dest)
void dio_put_string_json(struct json_data_out *dout, const struct plocation *location, const char *value)
bool dio_get_bool32_json(struct connection *pc, struct data_in *din, const struct plocation *location, bool *dest)
bool dio_get_uint32_json(struct connection *pc, struct data_in *din, const struct plocation *location, int *dest)
bool dio_get_string_json(struct connection *pc, struct data_in *din, const struct plocation *location, char *dest, size_t max_dest_size)
void dio_put_bool8_json(struct json_data_out *dout, const struct plocation *location, bool value)
bool dio_get_requirement_json(struct connection *pc, struct data_in *din, const struct plocation *location, struct requirement *preq)
void dio_put_uint16_json(struct json_data_out *dout, const struct plocation *location, int value)
bool dio_get_uint8_vec8_json(struct connection *pc, struct data_in *din, const struct plocation *location, int **values, int stop_value)
void dio_put_uint16_vec8_json(struct json_data_out *dout, const struct plocation *location, int *values, int stop_value)
bool dio_get_cm_parameter_json(struct connection *pc, struct data_in *din, struct plocation *location, struct cm_parameter *param)
bool dio_get_sfloat_json(struct connection *pc, struct data_in *din, const struct plocation *location, float *dest, int float_factor)
bool dio_get_uint8_json(struct connection *pc, struct data_in *din, const struct plocation *location, int *dest)
void dio_put_worklist_json(struct json_data_out *dout, struct plocation *location, const struct worklist *pwl)
bool dio_get_memory_json(struct connection *pc, struct data_in *din, struct plocation *location, void *dest, size_t dest_size)
bool dio_get_unit_order_json(struct connection *pc, struct data_in *din, struct plocation *location, struct unit_order *order)
void dio_put_cm_parameter_json(struct json_data_out *dout, struct plocation *location, const struct cm_parameter *order)
bool dio_get_ufloat_json(struct connection *pc, struct data_in *din, const struct plocation *location, float *dest, int float_factor)
void dio_put_bool32_json(struct json_data_out *dout, const struct plocation *location, bool value)
void dio_put_requirement_json(struct json_data_out *dout, const struct plocation *location, const struct requirement *preq)
void dio_put_unit_order_json(struct json_data_out *dout, struct plocation *location, const struct unit_order *order)
void dio_put_memory_json(struct json_data_out *dout, struct plocation *location, const void *value, size_t size)
bool dio_get_action_probability_json(struct connection *pc, struct data_in *din, const struct plocation *location, struct act_prob *prob)
void dio_put_sint32_json(struct json_data_out *dout, const struct plocation *location, int value)
void dio_put_action_probability_json(struct json_data_out *dout, const struct plocation *location, const struct act_prob *prob)
bool dio_get_bool8_json(struct connection *pc, struct data_in *din, const struct plocation *location, bool *dest)
void dio_put_estring_json(struct json_data_out *dout, const struct plocation *location, const char *value)
void dio_put_ufloat_json(struct json_data_out *dout, const struct plocation *location, float value, int float_factor)
bool dio_get_uint16_vec8_json(struct connection *pc, struct data_in *din, const struct plocation *location, int **values, int stop_value)
void dio_put_uint8_json(struct json_data_out *dout, const struct plocation *location, int value)
void dio_put_farray_json(struct json_data_out *dout, const struct plocation *location, int size)
void dio_put_cm_parameter_raw(struct raw_data_out *dout, const struct cm_parameter *param)
void dio_put_action_probability_raw(struct raw_data_out *dout, const struct act_prob *aprob)
void dio_put_unit_order_raw(struct raw_data_out *dout, const struct unit_order *order)
bool dio_get_requirement_raw(struct data_in *din, struct requirement *preq)
bool dio_get_sint8_raw(struct data_in *din, int *dest)
void dio_put_ufloat_raw(struct raw_data_out *dout, float value, int float_factor)
struct plocation * plocation_field_new(char *name)
bool dio_get_worklist_raw(struct data_in *din, struct worklist *pwl)
bool dio_get_action_probability_raw(struct data_in *din, struct act_prob *aprob)
void dio_put_bool8_raw(struct raw_data_out *dout, bool value)
void dio_put_uint16_raw(struct raw_data_out *dout, int value)
void dio_put_sfloat_raw(struct raw_data_out *dout, float value, int float_factor)
bool dataio_get_conv_callback(char *dst, size_t ndst, const char *src, size_t nsrc)
bool dio_get_sfloat_raw(struct data_in *din, float *dest, int float_factor)
void dio_put_uint8_raw(struct raw_data_out *dout, int value)
void dio_put_sint16_raw(struct raw_data_out *dout, int value)
bool dio_get_uint32_raw(struct data_in *din, int *dest)
bool dio_get_sint16_raw(struct data_in *din, int *dest)
bool dio_get_unit_order_raw(struct data_in *din, struct unit_order *order)
bool dio_get_sint32_raw(struct data_in *din, int *dest)
void dio_put_sint32_raw(struct raw_data_out *dout, int value)
bool dio_get_cm_parameter_raw(struct data_in *din, struct cm_parameter *param)
bool dio_get_uint8_vec8_raw(struct data_in *din, int **values, int stop_value)
void dio_put_uint32_raw(struct raw_data_out *dout, int value)
bool dio_get_uint16_raw(struct data_in *din, int *dest)
bool dio_get_memory_raw(struct data_in *din, void *dest, size_t dest_size)
void dio_put_memory_raw(struct raw_data_out *dout, const void *value, size_t size)
void dio_put_uint16_vec8_raw(struct raw_data_out *dout, int *values, int stop_value)
bool dio_get_bool32_raw(struct data_in *din, bool *dest)
bool dio_get_ufloat_raw(struct data_in *din, float *dest, int float_factor)
void dio_put_worklist_raw(struct raw_data_out *dout, const struct worklist *pwl)
void dio_put_requirement_raw(struct raw_data_out *dout, const struct requirement *preq)
bool dio_get_uint8_raw(struct data_in *din, int *dest)
bool dio_get_uint16_vec8_raw(struct data_in *din, int **values, int stop_value)
void dio_put_uint8_vec8_raw(struct raw_data_out *dout, int *values, int stop_value)
struct plocation * plocation_elem_new(int number)
bool dio_get_bool8_raw(struct data_in *din, bool *dest)
void dio_put_string_raw(struct raw_data_out *dout, const char *value)
bool dio_get_string_raw(struct data_in *din, char *dest, size_t max_dest_size)
const char * plocation_name(const struct plocation *loc)
void dio_put_bool32_raw(struct raw_data_out *dout, bool value)
void dio_put_sint8_raw(struct raw_data_out *dout, int value)
#define dio_get_estring_raw
#define dio_put_estring_raw
#define log_warn(message,...)
#define log_error(message,...)
void req_get_values(const struct requirement *req, int *type, int *range, bool *survives, bool *present, bool *quiet, int *value)
struct requirement req_from_values(int type, int range, bool survives, bool present, bool quiet, int value)
struct universal universal_by_number(const enum universals_n kind, const int value)
int universal_number(const struct universal *source)
int minimal_surplus[O_LAST]
struct plocation * sub_location
enum unit_activity activity
struct universal entries[MAX_LEN_WORKLIST]
void worklist_init(struct worklist *pwl)
bool worklist_append(struct worklist *pwl, const struct universal *prod)
int worklist_length(const struct worklist *pwl)