blob: c2529b8cfa0455dfce113de243d6e6c4799294a8 [file] [log] [blame]
/*
Copyright (c) 2018-2019 Nokia.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
/*
* This source code is part of the near-RT RIC (RAN Intelligent Controller)
* platform project (RICP).
*/
#include "private/redis/hiredisepolladapter.hpp"
#include <sys/epoll.h>
#include "private/engine.hpp"
#include "private/redis/hiredissystem.hpp"
using namespace shareddatalayer;
using namespace shareddatalayer::redis;
namespace
{
void addReadWrap(void* data)
{
auto instance(static_cast<HiredisEpollAdapter*>(data));
instance->addRead();
}
void delReadWrap(void* data)
{
auto instance(static_cast<HiredisEpollAdapter*>(data));
instance->delRead();
}
void addWriteWrap(void* data)
{
auto instance(static_cast<HiredisEpollAdapter*>(data));
instance->addWrite();
}
void delWriteWrap(void* data)
{
auto instance(static_cast<HiredisEpollAdapter*>(data));
instance->delWrite();
}
void cleanUpWrap(void* data)
{
auto instance(static_cast<HiredisEpollAdapter*>(data));
instance->cleanUp();
}
}
HiredisEpollAdapter::HiredisEpollAdapter(Engine& engine):
HiredisEpollAdapter(engine, HiredisSystem::getHiredisSystem())
{
}
HiredisEpollAdapter::HiredisEpollAdapter(Engine& engine, HiredisSystem& hiredisSystem):
engine(engine),
hiredisSystem(hiredisSystem),
ac(nullptr),
eventState(0),
reading(false),
writing(false),
isMonitoring(false)
{
}
HiredisEpollAdapter::~HiredisEpollAdapter()
{
if (ac && isMonitoring)
cleanUp();
}
void HiredisEpollAdapter::attach(redisAsyncContext* ac)
{
eventState = 0;
reading = false;
writing = false;
this->ac = ac;
this->ac->ev.data = this;
this->ac->ev.addRead = addReadWrap;
this->ac->ev.delRead = delReadWrap;
this->ac->ev.addWrite = addWriteWrap;
this->ac->ev.delWrite = delWriteWrap;
this->ac->ev.cleanup = cleanUpWrap;
engine.addMonitoredFD(ac->c.fd,
eventState,
std::bind(&HiredisEpollAdapter::eventHandler,
this,
std::placeholders::_1));
isMonitoring = true;
}
void HiredisEpollAdapter::eventHandler(unsigned int events)
{
if (events & Engine::EVENT_IN)
if (reading)
hiredisSystem.redisAsyncHandleRead(ac);
if (events & Engine::EVENT_OUT)
if (writing)
hiredisSystem.redisAsyncHandleWrite(ac);
}
void HiredisEpollAdapter::addRead()
{
if (reading)
return;
reading = true;
eventState |= Engine::EVENT_IN;
engine.modifyMonitoredFD(ac->c.fd, eventState);
}
void HiredisEpollAdapter::delRead()
{
reading = false;
eventState &= ~Engine::EVENT_IN;
engine.modifyMonitoredFD(ac->c.fd, eventState);
}
void HiredisEpollAdapter::addWrite()
{
if (writing)
return;
writing = true;
eventState |= Engine::EVENT_OUT;
engine.modifyMonitoredFD(ac->c.fd, eventState);
}
void HiredisEpollAdapter::delWrite()
{
writing = false;
eventState &= ~Engine::EVENT_OUT;
engine.modifyMonitoredFD(ac->c.fd, eventState);
}
void HiredisEpollAdapter::cleanUp()
{
reading = false;
writing = false;
eventState = 0;
engine.deleteMonitoredFD(ac->c.fd);
isMonitoring = false;
}