Skip to content

Commit

Permalink
added test for serialization/deserialization to/from a file
Browse files Browse the repository at this point in the history
  • Loading branch information
mdorier committed Mar 28, 2024
1 parent e0b8091 commit e93282f
Show file tree
Hide file tree
Showing 5 changed files with 148 additions and 1,158 deletions.
13 changes: 13 additions & 0 deletions include/flock/flock-group.h
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,19 @@ flock_return_t flock_group_serialize(
void (*serializer)(void*, const char*, size_t),
void* context);

/**
* @brief Serialize the current group handle to a file.
* If the file exists, it will be overwritten.
*
* @param handle Group handle
* @param filename File name.
*
* @return FLOCK_SUCCESS or error code defined in flock-common.h
*/
flock_return_t flock_group_serialize_to_file(
flock_group_handle_t handle,
const char* filename);

/**
* @brief Get the size of the group.
*
Expand Down
3 changes: 0 additions & 3 deletions src/client.c
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,6 @@ static flock_return_t flock_group_update_view_cb(flock_request_t req)
}
ret = out.ret;

fprintf(stderr, "Received a group view with %lu members and %lu metadata\n",
out.view.members.size, out.view.metadata.size);

FLOCK_GROUP_VIEW_LOCK(&req->group_handle->view);
flock_group_view_clear(&req->group_handle->view);
FLOCK_GROUP_VIEW_MOVE(&out.view, &req->group_handle->view);
Expand Down
34 changes: 34 additions & 0 deletions src/group-handle.c
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,40 @@ flock_return_t flock_group_serialize(
return FLOCK_SUCCESS;
}

struct file_serializer_data {
const char* filename;
flock_return_t ret;
};

static void file_serializer(void* uargs, const char* content, size_t size)
{
struct file_serializer_data* data = (struct file_serializer_data*)uargs;
FILE* file = fopen(data->filename, "w");
if(!file) {
data->ret = FLOCK_ERR_ALLOCATION;
return;
}
size_t written = fwrite(content, 1, size, file);
if(written != size) {
data->ret = FLOCK_ERR_OTHER;
return;
}
fclose(file);
}

flock_return_t flock_group_serialize_to_file(
flock_group_handle_t handle,
const char* filename)
{
struct file_serializer_data context = {
.filename = filename,
.ret = FLOCK_SUCCESS
};
flock_return_t ret = flock_group_serialize(handle, file_serializer, &context);
if(ret != FLOCK_SUCCESS) return ret;
else return context.ret;
}

flock_return_t flock_group_size(
flock_group_handle_t handle,
size_t* size)
Expand Down
Loading

0 comments on commit e93282f

Please sign in to comment.