/* * CommunicationLink.cpp * * Created on: 31 марта 2017 г. * Author: titov */ #include "../service/CommunicationLink.hh" namespace communication { namespace service { CommunicationLink::CommunicationLink( driver::ISlipCommunication & isc, Subscrib * subs, std::size_t max_subs ) : communication(isc), answer_created(false), message_handled(false), count_subscriber(0), subscribers(subs), max_subscribers(max_subs), packet_id(0u), free_counter(0) {} driver::IRxLink & CommunicationLink::getData() { return communication; } driver::ITxLink & CommunicationLink::createMessage( std::size_t size ) { answer_created = communication.sendHead( packet_id | 0x8000u, size * 2 ); return communication; } bool CommunicationLink::registerMessageHandler( IMessageHandler * handler, size_t id ) { if( count_subscriber < max_subscribers ) { Subscrib & subscrib( subscribers[count_subscriber++] ); subscrib.handler = handler; subscrib.id = static_cast(id); return true; } return false; } void CommunicationLink::send_answer() { message_handled = ( answer_created = not communication.sendEnd() ); } void CommunicationLink::process() { std::pair packet_info = communication.isPacketAvailable(); if( packet_info.first ) { free_counter++; packet_id = packet_info.second; for( unsigned int i = 0; i < count_subscriber; i++ ) if( subscribers[i].id == packet_id && subscribers[i].handler ) { subscribers[i].handler->handler(this); message_handled = true; } communication.recieveReset(); } if( message_handled and answer_created ) send_answer(); if( message_handled and not answer_created ) send_default(); } } } void communication::service::CommunicationLink::send_default() { message_handled = not( communication.sendHead( packet_id | 0x8000u, 0 ) and communication.sendEnd() ); } float communication::service::CommunicationLink::getPacketCounter() const { return free_counter; }