Skip to content

Commit

Permalink
json protocol: allow object creation in packet code
Browse files Browse the repository at this point in the history
See RM #496

Signed-off-by: Alina Lenk <[email protected]>
  • Loading branch information
alien-valkyrie committed Apr 21, 2024
1 parent 46cae50 commit 5d2dc07
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 20 deletions.
16 changes: 8 additions & 8 deletions common/generate_packets.py
Original file line number Diff line number Diff line change
Expand Up @@ -1476,19 +1476,19 @@ def _get_code_put_diff(self, location: Location) -> str:
{location.json_subloc}->number = -1;
/* Create the diff array element. */
e |= DIO_PUT(farray, &dout, &field_addr, 2);
e |= DIO_PUT(object, &dout, &field_addr);
/* Enter diff array element (start at the index address). */
{location.json_subloc}->number = count_{location.index}++;
{location.json_subloc}->sub_location = plocation_elem_new(0);
{location.json_subloc}->sub_location = plocation_field_new("index");
#endif /* FREECIV_JSON_CONNECTION */
/* Write the index */
{index_put}\
#ifdef FREECIV_JSON_CONNECTION
/* Content address. */
{location.json_subloc}->sub_location->number = 1;
{location.json_subloc}->sub_location->name = "data";
#endif /* FREECIV_JSON_CONNECTION */
{inner_put}\
Expand All @@ -1505,11 +1505,11 @@ def _get_code_put_diff(self, location: Location) -> str:
{location.json_subloc}->number = -1;
/* Create the terminating diff array element. */
e |= DIO_PUT(farray, &dout, &field_addr, 1);
e |= DIO_PUT(object, &dout, &field_addr);
/* Enter diff array element (start at the index address). */
{location.json_subloc}->number = count_{location.index};
{location.json_subloc}->sub_location = plocation_elem_new(0);
{location.json_subloc}->sub_location = plocation_field_new("index");
#endif /* FREECIV_JSON_CONNECTION */
/* Write the sentinel value */
Expand Down Expand Up @@ -1571,7 +1571,7 @@ def _get_code_get_diff(self, location: Location) -> str:
/* Enter array (start at initial element). */
{location.json_subloc} = plocation_elem_new(0);
/* Enter diff array element (start at the index address). */
{location.json_subloc}->sub_location = plocation_elem_new(0);
{location.json_subloc}->sub_location = plocation_field_new("index");
#endif /* FREECIV_JSON_CONNECTION */
while (TRUE) {{
Expand All @@ -1592,7 +1592,7 @@ def _get_code_get_diff(self, location: Location) -> str:
#ifdef FREECIV_JSON_CONNECTION
/* Content address. */
{location.json_subloc}->sub_location->number = 1;
{location.json_subloc}->sub_location->name = "data";
#endif /* FREECIV_JSON_CONNECTION */
{inner_get}\
Expand All @@ -1601,7 +1601,7 @@ def _get_code_get_diff(self, location: Location) -> str:
/* Move to the next diff array element. */
{location.json_subloc}->number++;
/* Back to the index address. */
{location.json_subloc}->sub_location->number = 0;
{location.json_subloc}->sub_location->name = "index";
#endif /* FREECIV_JSON_CONNECTION */
}}
Expand Down
31 changes: 19 additions & 12 deletions common/networking/dataio_json.c
Original file line number Diff line number Diff line change
Expand Up @@ -120,22 +120,12 @@ static int plocation_write_elem(json_t *item,
e = json_array_set_new(item, location->number, data);
}
} else {
// PTZ200718 handles last element get case...
// better here than all around put_array-diff algo ?
size_t n = (location->number == -1)
? (json_array_size(item) - 1)
: location->number;

json_t *sub_item = json_array_get(item, n);

if (json_is_array(sub_item)) {
e = plocation_write_data(sub_item,
location->sub_location, data);
} else {
log_error("ERROR:plocation_write_elem:array not found for sub location:"
"%s @[" SIZE_T_PRINTF "]",
json_dumps(item, JSON_DECODE_ANY), location->number);
}
e = plocation_write_data(json_array_get(item, n),
location->sub_location, data);
}

return e;
Expand Down Expand Up @@ -921,6 +911,23 @@ int dio_put_farray_json(struct json_data_out *dout,
return e;
}

/**********************************************************************//**
Create an empty JSON object.
**************************************************************************/
int dio_put_object_json(struct json_data_out *dout,
const struct plocation *location)
{
int e = 0;

if (dout->json) {
e |= plocation_write_data(dout->json, location, json_object());
} else {
/* No caller needs this */
}

return e;
}

/**********************************************************************//**
Insert uint32 value.
**************************************************************************/
Expand Down
2 changes: 2 additions & 0 deletions common/networking/dataio_json.h
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,8 @@ bool dio_get_uint16_vec8_json(struct connection *pc, struct data_in *din,
/* puts */
int dio_put_farray_json(struct json_data_out *dout,
const struct plocation *location, int size);
int dio_put_object_json(struct json_data_out *dout,
const struct plocation *location);

int dio_put_type_json(struct json_data_out *dout, enum data_type type,
const struct plocation *location,
Expand Down

0 comments on commit 5d2dc07

Please sign in to comment.