UltraGrid
UltraGrid Low-Latency High-Resolution Transmission System
Loading...
Searching...
No Matches
Participant_manager Class Reference
Collaboration diagram for Participant_manager:
Collaboration graph

Public Member Functions

 Participant_manager (module &mod, const char *compress)
 
 ~Participant_manager ()
 
void run_async (std::shared_ptr< socket_udp > server_sock)
 
void tick (sockaddr_storage &sin, socklen_t addrlen)
 

Private Member Functions

void worker ()
 

Private Attributes

std::string compression
 
struct module & mod
 
std::vector< Conf_participant > participants
 
std::atomic< bool > should_stop = false
 
std::shared_ptr< socket_udp > sock
 
std::thread worker_thread
 

Detailed Description

Definition at line 868 of file hd-rum-translator.cpp.

Constructor & Destructor Documentation

◆ Participant_manager()

Participant_manager::Participant_manager ( module & mod,
const char * compress )
inline

Definition at line 870 of file hd-rum-translator.cpp.

870: mod(mod), compression(compress ? compress : "") { }

◆ ~Participant_manager()

Participant_manager::~Participant_manager ( )
inline

Definition at line 871 of file hd-rum-translator.cpp.

871 {
872 should_stop.store(true, std::memory_order_relaxed);
873 if(worker_thread.joinable())
874 worker_thread.join();
875 }
std::atomic< bool > should_stop

References should_stop, and worker_thread.

Member Function Documentation

◆ run_async()

void Participant_manager::run_async ( std::shared_ptr< socket_udp > server_sock)
inline

Definition at line 940 of file hd-rum-translator.cpp.

940 {
941 sock = std::move(server_sock);
942 worker_thread = std::thread(&Participant_manager::worker, this);
943 }
std::shared_ptr< socket_udp > sock

References sock, worker(), and worker_thread.

Referenced by main().

Here is the call graph for this function:

◆ tick()

void Participant_manager::tick ( sockaddr_storage & sin,
socklen_t addrlen )
inline

Definition at line 877 of file hd-rum-translator.cpp.

877 {
878 struct timeval t;
879 gettimeofday(&t, NULL);
880 bool seen = false;
881 for(auto it = participants.begin(); it != participants.end();){
882 if(addrlen > 0 && sockaddr_equal(&it->addr, &sin)){
883 it->last_recv = t;
884 seen = true;
885 }
886 const double participant_timeout = 10.0;
887 if(tv_diff(t, it->last_recv) > participant_timeout){
888 log_msg(LOG_LEVEL_NOTICE, "Removing participant\n");
889 std::string msg = "delete-port ";
890 auto addr = reinterpret_cast<struct sockaddr *>(&it->addr);
891 char addr_str[128];
892 get_sockaddr_addr_str(addr, addr_str, sizeof(addr_str));
893 char *replica_name = get_replica_mod_name(addr_str, get_sockaddr_addr_port(addr));
894 msg += replica_name;
895 free(replica_name);
896
897 std::swap(*it, participants.back());
898 participants.pop_back();
899
900 struct msg_universal *m = (struct msg_universal *) new_message(sizeof(struct msg_universal));
901 strncpy(m->text, msg.c_str(), sizeof(m->text) - 1);
902 log_msg(LOG_LEVEL_NOTICE, "Msg: %s\n", m->text);
903 struct response *r = send_message_to_receiver(&mod, (struct message *) m);
905 log_msg(LOG_LEVEL_ERROR, "Couldn't remove participant (error %d)!\n", response_get_status(r));
906 }
907 free_response(r);
908 } else {
909 it++;
910 }
911 }
912
913 if(!seen && addrlen > 0){
915 p.addr = sin;
916 p.addrlen = addrlen;
917 p.last_recv = t;
918
919 log_msg(LOG_LEVEL_NOTICE, "New participant\n");
920 std::string msg = "create-port ";
921 msg += get_sockaddr_str(reinterpret_cast<sockaddr *>(&sin));
922 if(!compression.empty()){
923 msg += " ";
924 msg += compression;
925 }
926
927 struct msg_universal *m = (struct msg_universal *) new_message(sizeof(struct msg_universal));
928 strncpy(m->text, msg.c_str(), sizeof(m->text) - 1);
929 log_msg(LOG_LEVEL_NOTICE, "Msg: %s\n", m->text);
930 struct response *r = send_message_to_receiver(&mod, (struct message *) m);
932 log_msg(LOG_LEVEL_ERROR, "Cannot add new participant (error %d)!\n", response_get_status(r));
933 } else{
934 participants.push_back(p);
935 }
936 free_response(r);
937 }
938 }
std::vector< Conf_participant > participants
static void new_message(struct module *m)
void log_msg(int level, const char *format,...)
Definition debug.cpp:130
#define LOG_LEVEL_NOTICE
information that may be interesting
Definition debug.h:61
#define LOG_LEVEL_ERROR
general errors
Definition debug.h:59
static char * get_replica_mod_name(const char *addr, uint16_t tx_port)
static bool sockaddr_equal(struct sockaddr_storage *a, struct sockaddr_storage *b)
int response_get_status(struct response *r)
struct response * send_message_to_receiver(struct module *receiver, struct message *msg)
void free_response(struct response *r)
#define RESPONSE_ACCEPTED
Definition messaging.h:58
unsigned get_sockaddr_addr_port(struct sockaddr *sa)
Definition net.c:414
void get_sockaddr_addr_str(struct sockaddr *sa, char *buf, size_t n)
Definition net.c:427
const char * get_sockaddr_str(struct sockaddr *sa)
Definition net.c:450
struct timeval last_recv
struct sockaddr_storage addr
char text[8192]
Definition messaging.h:157
double tv_diff(struct timeval curr_time, struct timeval prev_time)
Definition tv.c:97

References Conf_participant::addr, Conf_participant::addrlen, compression, free_response(), get_replica_mod_name(), get_sockaddr_addr_port(), get_sockaddr_addr_str(), get_sockaddr_str(), Conf_participant::last_recv, LOG_LEVEL_ERROR, LOG_LEVEL_NOTICE, log_msg(), mod, new_message(), participants, RESPONSE_ACCEPTED, response_get_status(), send_message_to_receiver(), sockaddr_equal(), msg_universal::text, and tv_diff().

Referenced by main(), and worker().

Here is the call graph for this function:

◆ worker()

void Participant_manager::worker ( )
inlineprivate

Definition at line 953 of file hd-rum-translator.cpp.

953 {
954 struct timeval timeout = { 1, 0 };
955 char dummy_buf[256];
956
957 while(!should_stop.load(std::memory_order_relaxed)){
958 struct sockaddr_storage sin = {};
959 socklen_t addrlen = sizeof(sin);
960 int size = udp_recvfrom_timeout(sock.get(), dummy_buf, std::size(dummy_buf), &timeout, (sockaddr *) &sin, &addrlen);
961
962 if(size > 0)
963 tick(sin, addrlen);
964 }
965 }
void tick(sockaddr_storage &sin, socklen_t addrlen)
int udp_recvfrom_timeout(socket_udp *s, char *buffer, int buflen, struct timeval *timeout, struct sockaddr *src_addr, socklen_t *addrlen)
Definition net_udp.c:1715

References should_stop, sock, tick(), and udp_recvfrom_timeout().

Referenced by run_async().

Here is the call graph for this function:

Member Data Documentation

◆ compression

std::string Participant_manager::compression
private

Definition at line 948 of file hd-rum-translator.cpp.

Referenced by tick().

◆ mod

struct module& Participant_manager::mod
private

Definition at line 947 of file hd-rum-translator.cpp.

Referenced by tick().

◆ participants

std::vector<Conf_participant> Participant_manager::participants
private

Definition at line 946 of file hd-rum-translator.cpp.

Referenced by tick().

◆ should_stop

std::atomic<bool> Participant_manager::should_stop = false
private

Definition at line 951 of file hd-rum-translator.cpp.

Referenced by ~Participant_manager(), and worker().

◆ sock

std::shared_ptr<socket_udp> Participant_manager::sock
private

Definition at line 949 of file hd-rum-translator.cpp.

Referenced by run_async(), and worker().

◆ worker_thread

std::thread Participant_manager::worker_thread
private

Definition at line 950 of file hd-rum-translator.cpp.

Referenced by ~Participant_manager(), and run_async().


The documentation for this class was generated from the following file: