35 std::set<channel_t*, channel_t::compare_refrence>
channels;
127 if (send != transmitpackage.
size)
137 pthread_mutex_lock(&out_buffer_critical);
138 while(out_buffer.empty()){
140 pthread_cond_wait(&out_buffer_not_empty, &out_buffer_critical);
142 transmit_msg =
transmit(out_buffer.front());
144 log->
warning(
"transmit of: %d, to: %d, failed: %s", out_buffer.front().from_port, out_buffer.front().to_port,
150 pthread_mutex_unlock(&out_buffer_critical);
163 Link *bestPick =
nullptr;
164 unsigned int bestPickScore = 0;
166 if (driverCandidates.empty())
168 for (
auto driverCandidate : driverCandidates) {
170 if (s > bestPickScore) {
171 bestPick = driverCandidate;
176 return std::make_tuple(bestPick,
err_t::OK);
180 unsigned int score = 0;
200 log->
error(
"Failed to pick new driver: %s",
err_to_char.at(std::get<1>(dr)));
212 static bool registerd =
false;
221 uint16_t bytes_read = 0;
229 bytes_read = driver->
receive(&size, 1);
231 if (bytes_read && size >= 2) {
232 log->
verbose(
"new incoming connection, size: %i", size);
233 buff = (uint8_t *) malloc(size);
235 bytes_read = driver->
receive(&buff[1], (size - 1));
256 return start(std::get<0>(dr));
259 return std::get<1>(dr);
288 log->
error(
"failed to start transmissionQueueHandeler: %i", err);
296 log->
error(
"failed to start receiveHandeler: %i", err);
371 if (
channels.insert(channel).second) {
391 uint16_t packages = ceil((
double) data_size / (
double)
MAX_DATA_SIZE);
393 uint8_t n_to_wait_for = 0;
406 while (data_size > 0) {
408 tp[n_to_wait_for].to_port = to_port;
409 tp[n_to_wait_for].from_port = channel->
port;
410 tp[n_to_wait_for].n = 0;
411 tp[n_to_wait_for].notify_on_ack = wait_for_ack;
413 tp[n_to_wait_for].data_transmitting = &data[ds_p];
415 ds_l = (data_size >=
MAX_DATA_SIZE) ? MAX_DATA_SIZE : data_size;
417 tp[n_to_wait_for].data_lenght = ds_l;
422 log->
verbose(
"sending from: %d to: %d", tp[n_to_wait_for].from_port,
423 tp[n_to_wait_for].to_port);
467 if (channel->port == tp.
to_port) {
468 pthread_mutex_lock((pthread_mutex_t*)&channel->in_buffer_lock);
469 channel->in_buffer.push(tp);
470 pthread_mutex_unlock((pthread_mutex_t*)&channel->in_buffer_lock);
471 pthread_cond_broadcast((pthread_cond_t*)&channel->in_buffer_not_empty);
514 return comm::send(
this, to_port, data, data_size, wait_for_ack);
virtual uint32_t get_speed()=0
Get speed of link (bits per second)
std::set< channel_t *, channel_t::compare_refrence > channels
all endpoints
int debug(const char *format,...)
log debug message
static void transmitpackage_to_binary(transmitpackage_t transp, uint8_t *bin)
Get binary array of transmitpackage, for sending over a link.
void * transmissionQueueHandeler(void *)
Tramsmission handeler.
static constexpr size_t MAX_DATA_SIZE
pthread_mutex_t out_buffer_critical
static log::channel_t * log
const std::map< err_t, char * > err_to_char
pthread_mutex_t in_buffer_lock
void selectDriverTask()
start a task to select a driver, does not block.
err_t transmit(transmitpackage_t transmitpackage)
Transmit a package.
void setDriver(Link &driver)
set the driver
port_t to_port
to whom to send it to.
unsigned int rateDriver(Link &driver)
rate the driver on speed, errors, active connection, realtime, connection type etc.
pthread_t transmissionQueueHandeler_thread_handeler
unsigned int get_speed()
Get the speed of the link to the other hosts in bits per second.
err_t register_channel(channel_t *channel)
Register a new communication endpoint.
status_t status
stores the comm status
status_t get_status()
Get the running status of com.
bool is_connected()
Does the link have an active connection with a client?
err_t send(channel_t *channel, port_t to_port, uint8_t *data, size_t data_size, bool wait_for_ack)
Send data.
void set_status(status_t status)
set the comm status
static constexpr uint8_t HEADER_SIZE
pthread_t receiveHandeler_thread_handeler
uint8_t data_lenght
size of the data
err_t send(port_t to_port, uint8_t *data, size_t data_size, bool wait_for_ack=false)
size_t transmit(uint8_t *package, size_t package_size)
directly transmit a package on the link.
uint8_t size
total size of package (+ the size of the size variable)
std::queue< transmitpackage_t > out_buffer
Link * getDriver()
get the driver
void * receiveHandeler(void *)
Task to handle incomming connections.
err_t unregister_candidate_driver(Link *driver)
unregister a driver to be a comdiver candidate
err_t stop()
Stop the communication, free all queue's, channels and buffers.
err_t resume()
resume all communication.
Link * driver
stores the driver.
int verbose(const char *format,...)
log verbose message
err_t incoming_connection(uint8_t *package, uint8_t package_size)
Interrupt incomming connection handeler.
pthread_cond_t in_buffer_not_empty
bool is_connected()
Get the status of the comm link.
void * _selectDriverTask(void *)
pick a new best driver, dont call directly will delete your process.
err_t register_candidate_driver(Link *driver)
register a driver to be a comm driver candidate.
err_t start()
Start new communication.
endpoint type of a comm channel
std::set< Link * > driverCandidates
std::tuple< Link *, err_t > pickDriver()
pick the best available driver
int warning(const char *format,...)
log warning message
int error(const char *format,...)
log error message
err_t pause()
pause all communication.
pthread_cond_t out_buffer_not_empty
err_t get_channels(char *buffer)
get all names of the channels currently registered
size_t receive(uint8_t *buffer, size_t buffer_size)
Read data from the receive buffer.
err_t unregister_channel(channel_t &channel)
unregister an endpoint
static const comm::port_t port
const port_t port
port nr.
static bool binary_to_transmitpackage(uint8_t *bin, transmitpackage_t &transp)
get tansmitpackage of binary array.
int dump(level_t level, uint8_t *bin, size_t size)
dump array of bin data