-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: leonard.kosta <[email protected]>
- Loading branch information
1 parent
7ad9a59
commit ba9d617
Showing
10 changed files
with
135 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/** | ||
* @brief Contains functions for the peer discovery thread. | ||
*/ | ||
|
||
#ifndef INCLUDE_PEER_DISCOVERY_THREAD_H_ | ||
#define INCLUDE_PEER_DISCOVERY_THREAD_H_ | ||
|
||
#include <pthread.h> | ||
#include <stdatomic.h> | ||
#ifdef _WIN32 | ||
#include <winsock2.h> | ||
#include <ws2tcpip.h> | ||
#else | ||
#include <arpa/inet.h> | ||
#include <netdb.h> | ||
#include <netinet/in.h> | ||
#include <sys/socket.h> | ||
#include <sys/types.h> | ||
#include <unistd.h> | ||
#endif | ||
#include "include/return_codes.h" | ||
#include "include/linked_list.h" | ||
|
||
// TODO docstring. Look at mining_thread.h, because the synchronization stuff is the same | ||
typedef struct discover_peers_args_t { | ||
struct sockaddr_in6 peer_discovery_bootstrap_server_addr; | ||
uint64_t communication_interval_seconds; | ||
linked_list_t *peer_info_list; // TODO shared between threads. Initially empty. | ||
pthread_mutex_t peer_info_list_mutex; | ||
bool print_progress; | ||
atomic_bool *should_stop; | ||
bool *exit_ready; | ||
pthread_cond_t exit_ready_cond; | ||
pthread_mutex_t exit_ready_mutex; | ||
} discover_peers_args_t; | ||
|
||
// TODO docstrings | ||
return_code_t *discover_peers(discover_peers_args_t *args); | ||
|
||
#endif // INCLUDE_PEER_DISCOVERY_THREAD_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
/** | ||
* @brief Contains functions for cross-platform sleep (pause the program). | ||
*/ | ||
|
||
#ifndef INCLUDE_SLEEP_H_ | ||
#define INCLUDE_SLEEP_H_ | ||
|
||
#include <stdint.h> | ||
#include "include/return_codes.h" | ||
|
||
/** | ||
* @brief Pauses the program for the given number of microseconds. | ||
* | ||
* @return return_code_t A return code indicating success or failure. | ||
*/ | ||
return_code_t sleep_microseconds(uint64_t microseconds); | ||
|
||
#endif // INCLUDE_SLEEP_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
/** | ||
* @brief Runs the peer discovery client. | ||
*/ | ||
|
||
#include "include/peer_discovery_thread.h" | ||
#include "include/return_codes.h" | ||
|
||
int main(int argc, char **argv) { | ||
return_code_t return_code = SUCCESS; | ||
discover_peers_args_t args = {0}; | ||
// TODO read bootstrap server address from argv | ||
// TODO read communication interval from argv | ||
// TODO launch thread | ||
end: | ||
return return_code; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
// TODO registers with peer discovery server and maintains peer list. | ||
|
||
#include "include/peer_discovery_thread.h" | ||
#include "include/sleep.h" | ||
|
||
return_code_t *discover_peers(discover_peers_args_t *args) { | ||
return_code_t return_code = SUCCESS; | ||
bool should_stop = *args->should_stop; | ||
while (!should_stop) { | ||
if (args->print_progress) { | ||
printf("Attempting to connect to peer discovery server.\n"); | ||
} | ||
// TODO connect to peer discovery server | ||
|
||
// TODO register self as peer | ||
// TODO request peer list | ||
|
||
sleep_microseconds(args->communication_interval_seconds * 1000000); | ||
should_stop = *args->should_stop; | ||
} | ||
if (should_stop) { | ||
return_code = FAILURE_STOPPED_EARLY; | ||
if (args->print_progress) { | ||
printf("Stopping peer discovery.\n"); | ||
} | ||
} | ||
pthread_mutex_lock(&args->exit_ready_mutex); | ||
*args->exit_ready = true; | ||
pthread_cond_signal(&args->exit_ready_cond); | ||
pthread_mutex_unlock(&args->exit_ready_mutex); | ||
end: | ||
return_code_t *return_code_ptr = malloc(sizeof(return_code_t)); | ||
*return_code_ptr = return_code; | ||
return return_code_ptr; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
#include <time.h> | ||
#include "include/sleep.h" | ||
|
||
// TODO test this function | ||
return_code_t sleep_microseconds(uint64_t microseconds) { | ||
return_code_t return_code = SUCCESS; | ||
struct timespec ts; | ||
ts.tv_sec = microseconds / 1000000; | ||
ts.tv_nsec = (microseconds % 1000000) * 1000; | ||
int result = nanosleep(&ts, NULL); | ||
if (0 != result) { | ||
return_code = FAILURE_SLEEP; | ||
goto end; | ||
} | ||
end: | ||
return return_code; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters