void DefaultOperationTcpChannel::onPingTimeout(const boost::system::error_code& err)
{
    if (!err) {
        sendPingRequest();
    } else {
        KAA_MUTEX_LOCKING("channelGuard_");
        KAA_MUTEX_UNIQUE_DECLARE(channelLock, channelGuard_);
        KAA_MUTEX_LOCKED("channelGuard_");
        if (err != boost::asio::error::operation_aborted && isConnected_) {
            KAA_MUTEX_UNLOCKING("channelGuard_");
            KAA_UNLOCK(channelLock);
            KAA_MUTEX_UNLOCKED("channelGuard_")

            KAA_LOG_ERROR(boost::format("Channel [%1%] failed to process PING: %2%") % getId() % err.message());
            onServerFailed();
            return;
        } else {
            KAA_LOG_DEBUG(boost::format("Channel [%1%] PING timer aborted") % getId());
            return;
        }
    }

    KAA_MUTEX_LOCKING("channelGuard_");
    KAA_MUTEX_UNIQUE_DECLARE(channelLock, channelGuard_);
    KAA_MUTEX_LOCKED("channelGuard_");

    if (isConnected_) {
        setPingTimer();
    }
}
Esempio n. 2
0
void EventManager::setTransport(EventTransport *transport)
{
    eventTransport_ = transport;

    if (eventTransport_) {
        bool needSync = false;
        {
            KAA_MUTEX_LOCKING("pendingEventsGuard_");
            KAA_MUTEX_UNIQUE_DECLARE(eventsLock, pendingEventsGuard_);
            KAA_MUTEX_LOCKED("pendingEventsGuard_");
            needSync = !pendingEvents_.empty();
            KAA_MUTEX_UNLOCKED("pendingEventsGuard_");
        }

        if (!needSync) {
            KAA_MUTEX_LOCKING("eventListenersGuard_");
            KAA_MUTEX_UNIQUE_DECLARE(eventListenersLock, eventListenersGuard_);
            KAA_MUTEX_LOCKED("eventListenersGuard_");
            needSync = !eventListenersRequests_.empty();
            KAA_MUTEX_UNLOCKED("eventListenersGuard_");
        }

        if (needSync) {
            doSync();
        }
    }
}
Esempio n. 3
0
void AbstractHttpChannel::sync(TransportType type)
{
    const auto& supportedTypes = getSupportedTransportTypes();
    auto it = supportedTypes.find(type);
    if (it == supportedTypes.end() || it->second == ChannelDirection::DOWN) {
        KAA_LOG_ERROR(boost::format("Channel [%1%] unsupported transport type '%2%'")
                                                        % getId()
                                                        % LoggingUtils::toString(type));
        return;
    }

    KAA_MUTEX_LOCKING("channelGuard_");
    KAA_MUTEX_UNIQUE_DECLARE(lock, channelGuard_);
    KAA_MUTEX_LOCKED("channelGuard_");

    if (!currentServer_) {
        KAA_LOG_WARN(boost::format("Channel [%1%] can't sync: server is null") % getId());
        return;
    }

    processTypes(std::map<TransportType, ChannelDirection>({ { type, it->second } })
#ifdef KAA_THREADSAFE
               , lock
#endif
                );

}
void DefaultOperationTcpChannel::closeConnection()
{
    KAA_MUTEX_LOCKING("channelGuard_");
    KAA_MUTEX_UNIQUE_DECLARE(lock, channelGuard_);
    KAA_MUTEX_LOCKED("channelGuard_");

    bool wasConnected = isConnected_;

    KAA_LOG_TRACE(boost::format("Channel [%1%] closing connection: isConnected '%2%'")
                                                        % getId()
                                                        % boost::io::group(std::boolalpha, wasConnected));

    isFirstResponseReceived_ = false;
    isConnected_ = false;
    isPendingSyncRequest_ = false;

    KAA_MUTEX_UNLOCKING("channelGuard_");
    KAA_UNLOCK(lock);
    KAA_MUTEX_UNLOCKED("channelGuard_");

    if (wasConnected) {
        pingTimer_.cancel();
        connAckTimer_.cancel();
        sendDisconnect();
        boost::system::error_code errorCode;
        sock_->shutdown(boost::asio::ip::tcp::socket::shutdown_both, errorCode);
        sock_->close(errorCode);
        responseProcessor.flush();
    }
}
Esempio n. 5
0
void KaaChannelManager::onTransportConnectionInfoUpdated(ITransportConnectionInfoPtr connectionInfo) {
    if (isShutdown_) {
        KAA_LOG_WARN("Can't update server. Channel manager is down");
        return;
    }
    if (!connectionInfo) {
        KAA_LOG_WARN("Failed to update connection info: bad input data")
        throw KaaException("empty connection info pointer");
    }

    connectionInfo->resetFailedState();

    TransportProtocolId protocolId = connectionInfo->getTransportId();
    if (connectionInfo->getServerType() == ServerType::OPERATIONS) {
        KAA_MUTEX_LOCKING("lastOpsServersGuard_");
        KAA_MUTEX_UNIQUE_DECLARE(lastOpsServers_Lock, lastOpsServersGuard_);
        KAA_MUTEX_LOCKED("lastOpsServersGuard_");

        lastOpsServers_[protocolId] = connectionInfo;
    }

    KAA_MUTEX_LOCKING("channelGuard_");
    KAA_R_MUTEX_UNIQUE_DECLARE(channelLock, channelGuard_);
    KAA_MUTEX_LOCKED("channelGuard_");

    for (auto& channel : channels_) {
        if (channel->getServerType() == connectionInfo->getServerType() && channel->getTransportProtocolId() == protocolId) {
            KAA_LOG_DEBUG(boost::format("Setting a new connection data for channel \"%1%\" %2%")
                        % channel->getId() % LoggingUtils::TransportProtocolIdToString(protocolId));
            channel->setServer(connectionInfo);
        }
    }
}
Esempio n. 6
0
void AbstractHttpChannel::setDemultiplexer(IKaaDataDemultiplexer *demultiplexer)
{
    KAA_MUTEX_LOCKING("channelGuard_");
    KAA_MUTEX_UNIQUE_DECLARE(lock, channelGuard_);
    KAA_MUTEX_LOCKED("channelGuard_");
    demultiplexer_ = demultiplexer;
}
Esempio n. 7
0
bool LogCollector::isDeliveryTimeout()
{

    KAA_MUTEX_LOCKING("timeoutsGuard_");
    KAA_MUTEX_UNIQUE_DECLARE(timeoutsGuardLock, timeoutsGuard_);
    KAA_MUTEX_LOCKED("timeoutsGuard_");

    auto now = clock_t::now();

    IDataChannelPtr logChannel = channelManager_->getChannelByTransportType(TransportType::LOGGING);
    std::int32_t currentAccessPointId  = 0;
    if (logChannel && logChannel->getServer()) {
        currentAccessPointId = logChannel->getServer()->getAccessPointId();
    }

    bool isTimeout = false;
    timeoutAccessPointId_ = 0;

    for (const auto& request : timeouts_) {
        if (now >= request.second.getTimeoutTime()) {
            KAA_LOG_WARN(boost::format("Log delivery timeout detected, bucket id %li") % request.first);
            isTimeout = true;
            timeoutAccessPointId_ = request.second.getTransportAccessPointId();
            // Check if current access point already has timeout
            if (timeoutAccessPointId_ == currentAccessPointId) {
                break;
            }
        }
    }

    return isTimeout;
}
Esempio n. 8
0
bool EventManager::hasPendingEvents() const
{
    KAA_MUTEX_LOCKING("pendingEventsGuard_");
    KAA_MUTEX_UNIQUE_DECLARE(eventsLock, pendingEventsGuard_);
    KAA_MUTEX_LOCKED("pendingEventsGuard_");
    return !pendingEvents_.empty();
}
Esempio n. 9
0
std::int32_t EventManager::findEventListeners(const std::list<std::string>& eventFQNs, IFetchEventListenersPtr listener)
{
    if (eventFQNs.empty() || !listener) {
        KAA_LOG_WARN("Failed to add event listeners request: bad input data");
        throw KaaException("Bad event listeners data");
    }

    std::int32_t requestId = UuidGenerator::generateRandomInt();

    std::shared_ptr<EventListenersInfo> info(new EventListenersInfo);
    info->eventFQNs_ = eventFQNs;
    info->listener_ = listener;

    {
        KAA_MUTEX_LOCKING("eventListenersGuard_");
        KAA_MUTEX_UNIQUE_DECLARE(eventListenersLock, eventListenersGuard_);
        KAA_MUTEX_LOCKED("eventListenersGuard_");

        eventListenersRequests_.insert(std::make_pair(requestId, info));
        KAA_MUTEX_UNLOCKED("eventListenersGuard_");
    }

    KAA_LOG_TRACE("Added event listeners resolving request");

    doSync();

    return requestId;
}
void ConfigurationPersistenceManager::onConfigurationUpdated(const KaaRootConfiguration& configuration)
{
    if (ignoreConfigurationUpdate_) {
        ignoreConfigurationUpdate_ = false;
        return;
    }

    KAA_LOG_INFO("Configuration updated.");

    AvroByteArrayConverter<KaaRootConfiguration> converter;
    SharedDataBuffer buffer = converter.toByteArray(configuration);

    KAA_LOG_INFO(boost::format("Going to store configuration using configuration storage %1%") % storage_);

    KAA_MUTEX_LOCKING("confPersistenceGuard_");
    KAA_MUTEX_UNIQUE_DECLARE(confPersistenceGuardLock, confPersistenceGuard_);
    KAA_MUTEX_LOCKED("confPersistenceGuard_");

    if (storage_) {
        storage_->saveConfiguration(std::vector<std::uint8_t>(buffer.first.get(), buffer.first.get() + buffer.second));
    }

    KAA_MUTEX_UNLOCKING("confPersistenceGuard_");
    KAA_UNLOCK(confPersistenceGuardLock);
    KAA_MUTEX_UNLOCKED("confPersistenceGuard_");

    configurationHash_ = EndpointObjectHash(buffer);

    KAA_LOG_INFO(boost::format("Calculated configuration hash: %1%") % LoggingUtils::ByteArrayToString(configurationHash_.getHashDigest()));
}
Esempio n. 11
0
void DefaultOperationTcpChannel::doShutdown()
{
    KAA_MUTEX_LOCKING("channelGuard_");
    KAA_MUTEX_UNIQUE_DECLARE(lock, channelGuard_);
    KAA_MUTEX_LOCKED("channelGuard_");

    KAA_LOG_DEBUG(boost::format("Channel [%1%] is shutting down: isShutdown '%2%'")
                                                    % getId()
                                                    % boost::io::group(std::boolalpha, isShutdown_));

    if (!isShutdown_) {
        isShutdown_ = true;
        KAA_MUTEX_UNLOCKING("channelGuard_");
        KAA_UNLOCK(lock);
        KAA_MUTEX_UNLOCKED("channelGuard_");

        closeConnection();

        KAA_LOG_TRACE(boost::format("Channel [%1%] stopping IO service: isStopped '%2%'")
                                                    % getId()
                                                    % boost::io::group(std::boolalpha, io_.stopped()));

        if (!io_.stopped()) {
            io_.stop();
        }

        stopThreads();
    }
}
Esempio n. 12
0
std::shared_ptr<LogSyncRequest> LogCollector::getLogUploadRequest()
{
    ILogStorage::RecordPack recordPack;
    std::shared_ptr<LogSyncRequest> request;

    {
        KAA_MUTEX_LOCKING("storageGuard_");
        KAA_MUTEX_UNIQUE_DECLARE(storageGuardLock, storageGuard_);
        KAA_MUTEX_LOCKED("storageGuard_");

        recordPack = storage_->getRecordBlock(uploadStrategy_->getBatchSize(), uploadStrategy_->getRecordsBatchCount());
    }

    if (!recordPack.second.empty()) {
        request.reset(new LogSyncRequest);
        request->requestId = recordPack.first;

        std::vector<LogEntry> logs;
        logs.reserve(recordPack.second.size());
        for (const auto& log : recordPack.second) {
            logs.push_back(log->getLogEntry());
        }

        request->logEntries.set_array(std::move(logs));
        addDeliveryTimeout(request->requestId);
    }

    return request;
}
Esempio n. 13
0
void DefaultOperationTcpChannel::setDemultiplexer(IKaaDataDemultiplexer *demultiplexer)
{
    KAA_MUTEX_LOCKING("channelGuard_");
    KAA_MUTEX_UNIQUE_DECLARE(channelLock, channelGuard_);
    KAA_MUTEX_LOCKED("channelGuard_");
    demultiplexer_ = demultiplexer;
}
Esempio n. 14
0
void DefaultOperationTcpChannel::onReadEvent(const boost::system::error_code& err)
{
    if (!err) {
        std::ostringstream responseStream;
        responseStream << responseBuffer_.get();
        const auto& responseStr = responseStream.str();
        try {
            if (responseStr.empty()) {
                 KAA_LOG_ERROR(boost::format("Channel [%1%] no data read from socket") % getId());
            } else {
                responseProcessor.processResponseBuffer(responseStr.data(), responseStr.size());
            }
        } catch (const TransportRedirectException& exception) {
            KAA_LOG_INFO(boost::format("Channel [%1%] received REDIRECT response") % getId());
            return;
        } catch (const KaaException& exception) {
            KAA_LOG_ERROR(boost::format("Channel [%1%] failed to process data: %2%") % getId() % exception.what());
            onServerFailed();
        }
    } else {
        KAA_LOG_WARN(boost::format("Channel [%1%] socket error: %2%") % getId() % err.message());

        KAA_MUTEX_LOCKING("channelGuard_");
        KAA_MUTEX_UNIQUE_DECLARE(channelLock, channelGuard_);
        KAA_MUTEX_LOCKED("channelGuard_");

        if (err != boost::asio::error::operation_aborted && isConnected_) {
            KAA_MUTEX_UNLOCKING("channelGuard_");
            KAA_UNLOCK(channelLock);
            KAA_MUTEX_UNLOCKED("channelGuard_")

            onServerFailed();
            return;
        } else {
            KAA_LOG_DEBUG(boost::format("Channel [%1%] socket operations aborted") % getId());
            return;
        }
    }

    KAA_MUTEX_LOCKING("channelGuard_");
    KAA_MUTEX_UNIQUE_DECLARE(channelLock, channelGuard_);
    KAA_MUTEX_LOCKED("channelGuard_");

    if (isConnected_) {
        readFromSocket();
    }
}
Esempio n. 15
0
bool LogCollector::removeDeliveryTimeout(std::int32_t requestId)
{
    KAA_MUTEX_LOCKING("timeoutsGuard_");
    KAA_MUTEX_UNIQUE_DECLARE(timeoutsGuardLock, timeoutsGuard_);
    KAA_MUTEX_LOCKED("timeoutsGuard_");

    return timeouts_.erase(requestId);
}
Esempio n. 16
0
void ConfigurationManager::setConfigurationStorage(IConfigurationStoragePtr storage)
{
    KAA_MUTEX_LOCKING("configurationGuard_");
    KAA_MUTEX_UNIQUE_DECLARE(configurationGuardLock, configurationGuard_);
    KAA_MUTEX_LOCKED("configurationGuard_");

    storage_ = storage;
}
Esempio n. 17
0
bool EventManager::hasPendingListenerRequests() const
{
    KAA_MUTEX_LOCKING("eventListenersGuard_");
    KAA_MUTEX_UNIQUE_DECLARE(eventListenersLock, eventListenersGuard_);
    KAA_MUTEX_LOCKED("eventListenersGuard_");

    return !eventListenersRequests_.empty();
}
Esempio n. 18
0
void LogCollector::setTransport(LoggingTransport* transport)
{
    KAA_MUTEX_LOCKING("transportGuard_");
    KAA_MUTEX_UNIQUE_DECLARE(transportGuardLock, transportGuard_);
    KAA_MUTEX_LOCKED("transportGuard_");

    transport_ = transport;
}
Esempio n. 19
0
void ThreadPool::stop()
{
    {
        KAA_MUTEX_UNIQUE_DECLARE(tasksLock, threadPoolGuard_);
        state_ = State::PENDING_SHUTDOWN;
    }

    onThreadpoolEvent_.notify_all();
}
Esempio n. 20
0
boost::system::error_code DefaultOperationTcpChannel::sendKaaSync(const std::map<TransportType, ChannelDirection>& transportTypes)
{
    KAA_MUTEX_LOCKING("channelGuard_");
    KAA_MUTEX_UNIQUE_DECLARE(lock, channelGuard_);
    KAA_MUTEX_LOCKED("channelGuard_");
    KAA_LOG_DEBUG(boost::format("Channel [%1%] sending KAASYNC") % getId());
    const auto& requestBody = multiplexer_->compileRequest(transportTypes);
    const auto& requestEncoded = encDec_->encodeData(requestBody.data(), requestBody.size());
    return sendData(KaaSyncRequest(false, true, 0, requestEncoded, KaaSyncMessageType::SYNC));
}
Esempio n. 21
0
void DefaultOperationTcpChannel::sync(TransportType type)
{
    KAA_MUTEX_LOCKING("channelGuard_");
    KAA_MUTEX_UNIQUE_DECLARE(lock, channelGuard_);
    KAA_MUTEX_LOCKED("channelGuard_");

    if (isShutdown_) {
        KAA_LOG_WARN(boost::format("Channel [%1%] can't sync: channel is shut down") % getId());
        return;
    }

    if (isPaused_) {
        KAA_LOG_WARN(boost::format("Channel [%1%] can't sync: channel is paused") % getId());
        return;
    }

    const auto& suppportedTypes = getSupportedTransportTypes();
    auto it = suppportedTypes.find(type);
    if (it == suppportedTypes.end() || it->second == ChannelDirection::DOWN) {
        KAA_LOG_ERROR(boost::format("Channel [%1%] ignore sync: unsupported transport type %2%")
                                                                        % getId()
                                                                        % LoggingUtils::toString(type));
        return;
    }

    if (!currentServer_) {
        KAA_LOG_DEBUG(boost::format("Channel [%1%] can't sync: server is null") % getId());
        return;
    }

    if (isFirstResponseReceived_) {
        KAA_MUTEX_UNLOCKING("channelGuard_");
        KAA_UNLOCK(lock);
        KAA_MUTEX_UNLOCKED("channelGuard_");

        std::map<TransportType, ChannelDirection> syncTypes;
        syncTypes.insert(std::make_pair(type, it->second));
        for (const auto& typeIt : suppportedTypes) {
            if (typeIt.first != type) {
                syncTypes.insert(std::make_pair(typeIt.first, ChannelDirection::DOWN));
            }
        }

        boost::system::error_code errorCode = sendKaaSync(syncTypes);
        if (errorCode) {
            KAA_LOG_ERROR(boost::format("Channel [%1%] failed to sync: %2%")
                                                                % getId()
                                                                % errorCode.message());
            onServerFailed();
        }
    } else {
        KAA_LOG_DEBUG(boost::format("Channel [%1%] can't sync: waiting for CONNACK + KAASYNC") % getId());
        isPendingSyncRequest_ = true;
    }
}
Esempio n. 22
0
void DefaultOperationTcpChannel::syncAck(TransportType type)
{
    KAA_MUTEX_LOCKING("channelGuard_");
    KAA_MUTEX_UNIQUE_DECLARE(lock, channelGuard_);
    KAA_MUTEX_LOCKED("channelGuard_");

    KAA_LOG_DEBUG(boost::format("Channel [%1%] adding ACK for transport '%2%'")
                                                        % getId()
                                                        % LoggingUtils::toString(type));
    ackTypes_.push_back(type);
}
Esempio n. 23
0
std::map<std::int32_t, Event> EventManager::releasePendingEvents()
{
    KAA_MUTEX_LOCKING("pendingEventsGuard_");
    KAA_MUTEX_UNIQUE_DECLARE(eventsLock, pendingEventsGuard_);
    KAA_MUTEX_LOCKED("pendingEventsGuard_");

    std::map<std::int32_t, Event> result(std::move(pendingEvents_));
    pendingEvents_ = std::map<std::int32_t, Event>();
    currentEventIndex_ = 0;
    return result;
}
Esempio n. 24
0
void ThreadPool::forceStop()
{
    {
        KAA_MUTEX_UNIQUE_DECLARE(tasksLock, threadPoolGuard_);

        tasks_.clear();
        state_ = State::STOPPED;
    }

    onThreadpoolEvent_.notify_all();
}
void ConfigurationPersistenceManager::setConfigurationStorage(IConfigurationStoragePtr storage)
{
    if (storage) {
        KAA_MUTEX_LOCKING("confPersistenceGuard_");
        KAA_MUTEX_UNIQUE_DECLARE(lock, confPersistenceGuard_);
        KAA_MUTEX_LOCKED("confPersistenceGuard_");

        storage_ = storage;
        readStoredConfiguration();
    }
}
Esempio n. 26
0
const KaaRootConfiguration& ConfigurationManager::getConfiguration()
{
    KAA_MUTEX_LOCKING("configurationGuard_");
    KAA_MUTEX_UNIQUE_DECLARE(configurationGuardLock, configurationGuard_);
    KAA_MUTEX_LOCKED("configurationGuard_");

    if (!isConfigurationLoaded_) {
        loadConfiguration();
    }

    return configuration_;
}
Esempio n. 27
0
void ConfigurationManager::init()
{
    KAA_MUTEX_LOCKING("configurationGuard_");
    KAA_MUTEX_UNIQUE_DECLARE(configurationGuardLock, configurationGuard_);
    KAA_MUTEX_LOCKED("configurationGuard_");

    if (!isConfigurationLoaded_) {
        loadConfiguration();
    }

    notifySubscribers(configuration_);
}
Esempio n. 28
0
boost::system::error_code DefaultOperationTcpChannel::sendConnect()
{
    KAA_MUTEX_LOCKING("channelGuard_");
    KAA_MUTEX_UNIQUE_DECLARE(lock, channelGuard_);
    KAA_MUTEX_LOCKED("channelGuard_");
    KAA_LOG_DEBUG(boost::format("Channel [%1%] sending CONNECT") % getId());
    const auto& requestBody = multiplexer_->compileRequest(getSupportedTransportTypes());
    const auto& requestEncoded = encDec_->encodeData(requestBody.data(), requestBody.size());
    const auto& sessionKey = encDec_->getEncodedSessionKey();
    const auto& signature = encDec_->signData(sessionKey.data(), sessionKey.size());
    return sendData(ConnectMessage(CHANNEL_TIMEOUT, KAA_PLATFORM_PROTOCOL_AVRO_ID, signature, sessionKey, requestEncoded));
}
Esempio n. 29
0
std::map<std::int32_t, std::list<std::string> > EventManager::getPendingListenerRequests()
{
    KAA_MUTEX_LOCKING("eventListenersGuard_");
    KAA_MUTEX_UNIQUE_DECLARE(eventListenersLock, eventListenersGuard_);
    KAA_MUTEX_LOCKED("eventListenersGuard_");

    std::map<std::int32_t, std::list<std::string> > result;
    for (const auto& idToFqnList : eventListenersRequests_) {
        result.insert(std::make_pair(idToFqnList.first, idToFqnList.second->eventFQNs_));
    }
    return result;
}
Esempio n. 30
0
void LogCollector::doSync()
{
    KAA_MUTEX_LOCKING("transportGuard_");
    KAA_MUTEX_UNIQUE_DECLARE(transportGuardLock, transportGuard_);
    KAA_MUTEX_LOCKED("transportGuard_");

    if (transport_) {
        transport_->sync();
    } else {
        KAA_LOG_ERROR("Failed to upload logs: log transport isn't initialized");
        throw TransportNotFoundException("Log transport isn't set");
    }
}