Skip to content

Commit

Permalink
Header file cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
willemt committed Feb 2, 2015
1 parent 2a50ff7 commit 5e9e46e
Showing 1 changed file with 50 additions and 52 deletions.
102 changes: 50 additions & 52 deletions include/raft.h
Expand Up @@ -5,14 +5,15 @@
/**
* Copyright (c) 2013, Willem-Hendrik Thiart
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
* found in the LICENSE file.
*
* @file
* @author Willem Thiart himself@willemthiart.com
* @version 0.1
*/

typedef struct {
typedef struct
{
/** The ID that this node used to have.
* So that we can tell which nodes were removed/added when the
* configuration changes */
Expand All @@ -25,7 +26,8 @@ typedef struct {
void* udata_address;
} raft_node_configuration_t;

typedef struct {
typedef struct
{
/* candidate's term */
int term;

Expand All @@ -39,7 +41,8 @@ typedef struct {
int last_log_term;
} msg_requestvote_t;

typedef struct {
typedef struct
{
/* the entry's unique ID */
unsigned int id;

Expand All @@ -50,23 +53,26 @@ typedef struct {
unsigned int len;
} msg_entry_t;

typedef struct {
typedef struct
{
/* the entry's unique ID */
unsigned int id;

/* whether or not the entry was committed */
int was_committed;
} msg_entry_response_t;

typedef struct {
typedef struct
{
/* currentTerm, for candidate to update itself */
int term;

/* true means candidate received vote */
int vote_granted;
} msg_requestvote_response_t;

typedef struct {
typedef struct
{
int term;
int leader_id;
int prev_log_idx;
Expand All @@ -76,7 +82,8 @@ typedef struct {
int leader_commit;
} msg_appendentries_t;

typedef struct {
typedef struct
{
/* currentTerm, for leader to update itself */
int term;

Expand All @@ -96,7 +103,8 @@ typedef struct {
typedef void* raft_server_t;
typedef void* raft_node_t;

typedef enum {
typedef enum
{
RAFT_MSG_REQUESTVOTE,
RAFT_MSG_REQUESTVOTE_RESPONSE,
RAFT_MSG_APPENDENTRIES,
Expand All @@ -105,112 +113,100 @@ typedef enum {
RAFT_MSG_ENTRY_RESPONSE,
} raft_message_type_e;


typedef int (
*func_send_f
) (
raft_server_t* raft,
void *udata,
int node,
raft_message_type_e msg_type,
const unsigned char *send_data,
const int len
);

/**
* @param raft The Raft server making this callback
* @param udata User data that is passed from Raft server
* @param node The peer's ID that we are sending this message to
* @return 0 on error */
typedef int (
*func_send_requestvote_f
*func_send_requestvote_f
) (
raft_server_t* raft,
void *udata,
int node,
msg_requestvote_t* msg
);
);

/**
* @param raft The Raft server making this callback
* @param udata User data that is passed from Raft server
* @param node The peer's ID that we are sending this message to
* @return 0 on error */
typedef int (
*func_send_requestvote_response_f
*func_send_requestvote_response_f
) (
raft_server_t* raft,
void *udata,
int node,
msg_requestvote_response_t* msg
);
);

/**
* @param raft The Raft server making this callback
* @param udata User data that is passed from Raft server
* @param node The peer's ID that we are sending this message to
* @return 0 on error */
typedef int (
*func_send_appendentries_f
*func_send_appendentries_f
) (
raft_server_t* raft,
void *udata,
int node,
msg_appendentries_t* msg
);
);

/**
* @param raft The Raft server making this callback
* @param udata User data that is passed from Raft server
* @param node The peer's ID that we are sending this message to
* @return 0 on error */
typedef int (
*func_send_appendentries_response_f
*func_send_appendentries_response_f
) (
raft_server_t* raft,
void *udata,
int node,
msg_appendentries_response_t* msg
);
);

/**
* @param raft The Raft server making this callback
* @param udata User data that is passed from Raft server
* @param node The peer's ID that we are sending this message to
* @return 0 on error */
typedef int (
*func_send_entries_f
*func_send_entries_f
) (
raft_server_t* raft,
void *udata,
int node,
msg_entry_t* msg
);
);

/**
* @param raft The Raft server making this callback
* @param udata User data that is passed from Raft server
* @param node The peer's ID that we are sending this message to
* @return 0 on error */
typedef int (
*func_send_entries_response_f
*func_send_entries_response_f
) (
raft_server_t* raft,
void *udata,
int node,
msg_entry_response_t* msg
);
);

#ifndef HAVE_FUNC_LOG
#define HAVE_FUNC_LOG
typedef void (
*func_log_f
*func_log_f
) (
raft_server_t* raft,
void *udata,
const char *buf,
...
);
);
#endif

/**
Expand All @@ -221,15 +217,16 @@ typedef void (
* @param len Length in bytes of data to be applied
* @return 0 on error */
typedef int (
*func_applylog_f
*func_applylog_f
) (
raft_server_t* raft,
void *udata,
const unsigned char *data,
const int len
);
);

typedef struct {
typedef struct
{
func_send_requestvote_f send_requestvote;
func_send_requestvote_response_f send_requestvote_response;
func_send_appendentries_f send_appendentries;
Expand All @@ -240,7 +237,8 @@ typedef struct {
func_applylog_f applylog;
} raft_cbs_t;

typedef struct {
typedef struct
{
/* entry's term */
unsigned int term;
/* the entry's unique ID */
Expand Down Expand Up @@ -272,15 +270,15 @@ void raft_free(raft_server_t* me_);
* Callbacks need to be set by the user for CRaft to work.
*
* @param funcs Callbacks
* @param udata The context that we include when making a callback */
* @param udata "User data" - user's context that's included in a callback */
void raft_set_callbacks(raft_server_t* me, raft_cbs_t* funcs, void* udata);

/**
* Set configuration
* @param nodes Array of nodes. End of array is marked by NULL entry
* @param my_idx Index of the node that refers to this Raft server */
void raft_set_configuration(raft_server_t* me_,
raft_node_configuration_t* nodes, int my_idx);
raft_node_configuration_t* nodes, int my_idx);

/**
* Set election timeout
Expand All @@ -302,40 +300,40 @@ int raft_periodic(raft_server_t* me, int msec_elapsed);

/**
* Receive an appendentries message
* @param node Index of the node who sent us this message
* @param ae The appendentries message
* @param node Index of the node who sent us this message
* @param ae The appendentries message
* @return 0 on error */
int raft_recv_appendentries(raft_server_t* me, int node,
msg_appendentries_t* ae);
msg_appendentries_t* ae);

/**
* Receive a response from an appendentries message we sent
* @param node Index of the node who sent us this message
* @param node Index of the node who sent us this message
* @param r The appendentries response message
* @return 0 on error */
int raft_recv_appendentries_response(raft_server_t* me_,
int node, msg_appendentries_response_t* r);
int node, msg_appendentries_response_t* r);
/**
* Receive a requestvote message
* @param node Index of the node who sent us this message
* @param node Index of the node who sent us this message
* @param vr The requestvote message
* @return 0 on error */
int raft_recv_requestvote(raft_server_t* me, int node,
msg_requestvote_t* vr);
msg_requestvote_t* vr);

/**
* Receive a response from a requestvote message we sent
* @param node Index of the node who sent us this message
* @param node Index of the node who sent us this message
* @param r The requestvote response message
* @param node The node this response was sent by */
int raft_recv_requestvote_response(raft_server_t* me, int node,
msg_requestvote_response_t* r);
msg_requestvote_response_t* r);

/**
* Receive an entry message from client.
* Append the entry to the log
* Send appendentries to followers
* @param node Index of the node who sent us this message
* Send appendentries to followers
* @param node Index of the node who sent us this message
* @param e The entry message */
int raft_recv_entry(raft_server_t* me, int node, msg_entry_t* e);

Expand Down

0 comments on commit 5e9e46e

Please sign in to comment.