collapse SimpleChannelSearchManagerImpl into ChannelSearchManager

Virtual base with only one sub-class.  So avoid some unnecessary
indirection.
This commit is contained in:
Michael Davidsaver
2018-03-28 13:37:42 -07:00
parent 1a100a0955
commit 735dc22e2d
5 changed files with 166 additions and 256 deletions

View File

@@ -9,7 +9,7 @@ pvAccess_SRCS += blockingUDPTransport.cpp
pvAccess_SRCS += blockingUDPConnector.cpp
pvAccess_SRCS += beaconHandler.cpp
pvAccess_SRCS += blockingTCPConnector.cpp
pvAccess_SRCS += simpleChannelSearchManagerImpl.cpp
pvAccess_SRCS += channelSearchManager.cpp
pvAccess_SRCS += abstractResponseHandler.cpp
pvAccess_SRCS += blockingTCPAcceptor.cpp
pvAccess_SRCS += transportRegistry.cpp

View File

@@ -11,7 +11,7 @@
#include <pv/timeStamp.h>
#define epicsExportSharedSymbols
#include <pv/simpleChannelSearchManagerImpl.h>
#include <pv/channelSearchManager.h>
#include <pv/pvaConstants.h>
#include <pv/blockingUDP.h>
#include <pv/serializeHelper.h>
@@ -23,25 +23,25 @@ using namespace epics::pvData;
namespace epics {
namespace pvAccess {
const int SimpleChannelSearchManagerImpl::DATA_COUNT_POSITION = PVA_MESSAGE_HEADER_SIZE + 4+1+3+16+2+1+4;
const int SimpleChannelSearchManagerImpl::CAST_POSITION = PVA_MESSAGE_HEADER_SIZE + 4;
const int SimpleChannelSearchManagerImpl::PAYLOAD_POSITION = 4;
const int ChannelSearchManager::DATA_COUNT_POSITION = PVA_MESSAGE_HEADER_SIZE + 4+1+3+16+2+1+4;
const int ChannelSearchManager::CAST_POSITION = PVA_MESSAGE_HEADER_SIZE + 4;
const int ChannelSearchManager::PAYLOAD_POSITION = 4;
// 225ms +/- 25ms random
const double SimpleChannelSearchManagerImpl::ATOMIC_PERIOD = 0.225;
const int SimpleChannelSearchManagerImpl::PERIOD_JITTER_MS = 25;
const double ChannelSearchManager::ATOMIC_PERIOD = 0.225;
const int ChannelSearchManager::PERIOD_JITTER_MS = 25;
const int SimpleChannelSearchManagerImpl::DEFAULT_USER_VALUE = 1;
const int SimpleChannelSearchManagerImpl::BOOST_VALUE = 1;
const int ChannelSearchManager::DEFAULT_USER_VALUE = 1;
const int ChannelSearchManager::BOOST_VALUE = 1;
// must be power of two (so that search is done)
const int SimpleChannelSearchManagerImpl::MAX_COUNT_VALUE = 1 << 8;
const int SimpleChannelSearchManagerImpl::MAX_FALLBACK_COUNT_VALUE = (1 << 7) + 1;
const int ChannelSearchManager::MAX_COUNT_VALUE = 1 << 8;
const int ChannelSearchManager::MAX_FALLBACK_COUNT_VALUE = (1 << 7) + 1;
const int SimpleChannelSearchManagerImpl::MAX_FRAMES_AT_ONCE = 10;
const int SimpleChannelSearchManagerImpl::DELAY_BETWEEN_FRAMES_MS = 50;
const int ChannelSearchManager::MAX_FRAMES_AT_ONCE = 10;
const int ChannelSearchManager::DELAY_BETWEEN_FRAMES_MS = 50;
SimpleChannelSearchManagerImpl::SimpleChannelSearchManagerImpl(Context::shared_pointer const & context) :
ChannelSearchManager::ChannelSearchManager(Context::shared_pointer const & context) :
m_context(context),
m_responseAddress(), // initialized in activate()
m_canceled(),
@@ -58,7 +58,7 @@ SimpleChannelSearchManagerImpl::SimpleChannelSearchManagerImpl(Context::shared_p
srand ( time(NULL) );
}
void SimpleChannelSearchManagerImpl::activate()
void ChannelSearchManager::activate()
{
m_responseAddress = Context::shared_pointer(m_context)->getSearchTransport()->getRemoteAddress();
@@ -73,15 +73,15 @@ void SimpleChannelSearchManagerImpl::activate()
context->getTimer()->schedulePeriodic(shared_from_this(), period, period);
}
SimpleChannelSearchManagerImpl::~SimpleChannelSearchManagerImpl()
ChannelSearchManager::~ChannelSearchManager()
{
Lock guard(m_mutex);
if (!m_canceled.get()) {
LOG(logLevelWarn, "Logic error: SimpleChannelSearchManagerImpl destroyed w/o cancel()");
LOG(logLevelWarn, "Logic error: ChannelSearchManager destroyed w/o cancel()");
}
}
void SimpleChannelSearchManagerImpl::cancel()
void ChannelSearchManager::cancel()
{
Lock guard(m_mutex);
@@ -94,13 +94,13 @@ void SimpleChannelSearchManagerImpl::cancel()
context->getTimer()->cancel(shared_from_this());
}
int32_t SimpleChannelSearchManagerImpl::registeredCount()
int32_t ChannelSearchManager::registeredCount()
{
Lock guard(m_channelMutex);
return static_cast<int32_t>(m_channels.size());
}
void SimpleChannelSearchManagerImpl::registerSearchInstance(SearchInstance::shared_pointer const & channel, bool penalize)
void ChannelSearchManager::registerSearchInstance(SearchInstance::shared_pointer const & channel, bool penalize)
{
if (m_canceled.get())
return;
@@ -122,7 +122,7 @@ void SimpleChannelSearchManagerImpl::registerSearchInstance(SearchInstance::shar
callback();
}
void SimpleChannelSearchManagerImpl::unregisterSearchInstance(SearchInstance::shared_pointer const & channel)
void ChannelSearchManager::unregisterSearchInstance(SearchInstance::shared_pointer const & channel)
{
Lock guard(m_channelMutex);
pvAccessID id = channel->getSearchInstanceID();
@@ -131,7 +131,7 @@ void SimpleChannelSearchManagerImpl::unregisterSearchInstance(SearchInstance::sh
m_channels.erase(id);
}
void SimpleChannelSearchManagerImpl::searchResponse(const ServerGUID & guid, pvAccessID cid, int32_t /*seqNo*/, int8_t minorRevision, osiSockAddr* serverAddress)
void ChannelSearchManager::searchResponse(const ServerGUID & guid, pvAccessID cid, int32_t /*seqNo*/, int8_t minorRevision, osiSockAddr* serverAddress)
{
Lock guard(m_channelMutex);
m_channels_t::iterator channelsIter = m_channels.find(cid);
@@ -162,13 +162,13 @@ void SimpleChannelSearchManagerImpl::searchResponse(const ServerGUID & guid, pvA
}
}
void SimpleChannelSearchManagerImpl::newServerDetected()
void ChannelSearchManager::newServerDetected()
{
boost();
callback();
}
void SimpleChannelSearchManagerImpl::initializeSendBuffer()
void ChannelSearchManager::initializeSendBuffer()
{
// for now OK, since it is only set here
m_sequenceNumber++;
@@ -202,7 +202,7 @@ void SimpleChannelSearchManagerImpl::initializeSendBuffer()
m_sendBuffer.putShort((int16_t)0); // count
}
void SimpleChannelSearchManagerImpl::flushSendBuffer()
void ChannelSearchManager::flushSendBuffer()
{
Lock guard(m_mutex);
@@ -219,7 +219,7 @@ void SimpleChannelSearchManagerImpl::flushSendBuffer()
}
bool SimpleChannelSearchManagerImpl::generateSearchRequestMessage(SearchInstance::shared_pointer const & channel,
bool ChannelSearchManager::generateSearchRequestMessage(SearchInstance::shared_pointer const & channel,
ByteBuffer* requestMessage, TransportSendControl* control)
{
epics::pvData::int16 dataCount = requestMessage->getShort(DATA_COUNT_POSITION);
@@ -245,7 +245,7 @@ bool SimpleChannelSearchManagerImpl::generateSearchRequestMessage(SearchInstance
return true;
}
bool SimpleChannelSearchManagerImpl::generateSearchRequestMessage(SearchInstance::shared_pointer const & channel,
bool ChannelSearchManager::generateSearchRequestMessage(SearchInstance::shared_pointer const & channel,
bool allowNewFrame, bool flush)
{
Lock guard(m_mutex);
@@ -267,7 +267,7 @@ bool SimpleChannelSearchManagerImpl::generateSearchRequestMessage(SearchInstance
return flush;
}
void SimpleChannelSearchManagerImpl::boost()
void ChannelSearchManager::boost()
{
Lock guard(m_channelMutex);
Lock guard2(m_userValueMutex);
@@ -281,7 +281,7 @@ void SimpleChannelSearchManagerImpl::boost()
}
}
void SimpleChannelSearchManagerImpl::callback()
void ChannelSearchManager::callback()
{
// high-frequency beacon anomaly trigger guard
{
@@ -346,12 +346,12 @@ void SimpleChannelSearchManagerImpl::callback()
flushSendBuffer();
}
bool SimpleChannelSearchManagerImpl::isPowerOfTwo(int32_t x)
bool ChannelSearchManager::isPowerOfTwo(int32_t x)
{
return ((x > 0) && (x & (x - 1)) == 0);
}
void SimpleChannelSearchManagerImpl::timerStopped()
void ChannelSearchManager::timerStopped()
{
}

View File

@@ -20,6 +20,7 @@
#endif
#include <pv/pvaDefs.h>
#include <pv/remote.h>
namespace epics {
namespace pvAccess {
@@ -49,34 +50,56 @@ public:
virtual void searchResponse(const ServerGUID & guid, int8_t minorRevision, osiSockAddr* serverAddress) = 0;
};
class ChannelSearchManager {
class MockTransportSendControl: public TransportSendControl
{
public:
void endMessage() {}
void flush(bool /*lastMessageCompleted*/) {}
void setRecipient(const osiSockAddr& /*sendTo*/) {}
void startMessage(epics::pvData::int8 /*command*/, std::size_t /*ensureCapacity*/, epics::pvData::int32 /*payloadSize*/) {}
void ensureBuffer(std::size_t /*size*/) {}
void alignBuffer(std::size_t /*alignment*/) {}
void flushSerializeBuffer() {}
void cachedSerialize(const std::tr1::shared_ptr<const epics::pvData::Field>& field, epics::pvData::ByteBuffer* buffer)
{
// no cache
field->serialize(buffer, this);
}
virtual bool directSerialize(epics::pvData::ByteBuffer* /*existingBuffer*/, const char* /*toSerialize*/,
std::size_t /*elementCount*/, std::size_t /*elementSize*/)
{
return false;
}
};
class ChannelSearchManager :
public epics::pvData::TimerCallback,
public std::tr1::enable_shared_from_this<ChannelSearchManager>
{
public:
POINTER_DEFINITIONS(ChannelSearchManager);
virtual ~ChannelSearchManager();
/**
* Destructor
* Cancel.
*/
virtual ~ChannelSearchManager() {};
void cancel();
/**
* Get number of registered channels.
* @return number of registered channels.
*/
virtual int32_t registeredCount() = 0;
int32_t registeredCount();
/**
* Register channel.
* @param channel
* @param channel to register.
*/
virtual void registerSearchInstance(SearchInstance::shared_pointer const & channel, bool penalize = false) = 0;
void registerSearchInstance(SearchInstance::shared_pointer const & channel, bool penalize = false);
/**
* Unregister channel.
* @param channel
* @param channel to unregister.
*/
virtual void unregisterSearchInstance(SearchInstance::shared_pointer const & channel) = 0;
void unregisterSearchInstance(SearchInstance::shared_pointer const & channel);
/**
* Search response from server (channel found).
* @param guid server GUID.
@@ -85,19 +108,110 @@ public:
* @param minorRevision server minor PVA revision.
* @param serverAddress server address.
*/
virtual void searchResponse(const ServerGUID & guid, pvAccessID cid, int32_t seqNo, int8_t minorRevision, osiSockAddr* serverAddress) = 0;
void searchResponse(const ServerGUID & guid, pvAccessID cid, int32_t seqNo, int8_t minorRevision, osiSockAddr* serverAddress);
/**
* New server detected.
* Boost searching of all channels.
*/
virtual void newServerDetected() = 0;
void newServerDetected();
/// Timer callback.
virtual void callback() OVERRIDE FINAL;
/// Timer stooped callback.
virtual void timerStopped() OVERRIDE FINAL;
/**
* Cancel.
* Private constructor.
* @param context
*/
virtual void cancel() = 0;
ChannelSearchManager(Context::shared_pointer const & context);
void activate();
private:
bool generateSearchRequestMessage(SearchInstance::shared_pointer const & channel, bool allowNewFrame, bool flush);
static bool generateSearchRequestMessage(SearchInstance::shared_pointer const & channel,
epics::pvData::ByteBuffer* byteBuffer, TransportSendControl* control);
void boost();
void initializeSendBuffer();
void flushSendBuffer();
static bool isPowerOfTwo(int32_t x);
/**
* Context.
*/
Context::weak_pointer m_context;
/**
* Response address.
*/
osiSockAddr m_responseAddress;
/**
* Canceled flag.
*/
AtomicBoolean m_canceled;
/**
* Search (datagram) sequence number.
*/
int32_t m_sequenceNumber;
/**
* Send byte buffer (frame)
*/
epics::pvData::ByteBuffer m_sendBuffer;
/**
* Set of registered channels.
*/
typedef std::map<pvAccessID,SearchInstance::weak_pointer> m_channels_t;
m_channels_t m_channels;
/**
* Time of last frame send.
*/
int64_t m_lastTimeSent;
/**
* Mock transport send control
*/
MockTransportSendControl m_mockTransportSendControl;
/**
* This instance mutex.
*/
epics::pvData::Mutex m_channelMutex;
/**
* User value lock.
*/
epics::pvData::Mutex m_userValueMutex;
/**
* m_channels mutex.
*/
epics::pvData::Mutex m_mutex;
static const int DATA_COUNT_POSITION;
static const int CAST_POSITION;
static const int PAYLOAD_POSITION;
static const double ATOMIC_PERIOD;
static const int PERIOD_JITTER_MS;
static const int DEFAULT_USER_VALUE;
static const int BOOST_VALUE;
static const int MAX_COUNT_VALUE;
static const int MAX_FALLBACK_COUNT_VALUE;
static const int MAX_FRAMES_AT_ONCE;
static const int DELAY_BETWEEN_FRAMES_MS;
};
}

View File

@@ -1,204 +0,0 @@
/**
* Copyright - See the COPYRIGHT that is included with this distribution.
* pvAccessCPP is distributed subject to a Software License Agreement found
* in file LICENSE that is included with this distribution.
*/
#ifndef SIMPLECHANNELSEARCHMANAGERIMPL_H
#define SIMPLECHANNELSEARCHMANAGERIMPL_H
#ifdef epicsExportSharedSymbols
# define simpleChannelSearchManagerEpicsExportSharedSymbols
# undef epicsExportSharedSymbols
#endif
#include <pv/lock.h>
#include <pv/byteBuffer.h>
#include <pv/timer.h>
#ifdef simpleChannelSearchManagerEpicsExportSharedSymbols
# define epicsExportSharedSymbols
# undef simpleChannelSearchManagerEpicsExportSharedSymbols
#endif
#include <pv/channelSearchManager.h>
#include <pv/remote.h>
namespace epics {
namespace pvAccess {
class MockTransportSendControl: public TransportSendControl
{
public:
void endMessage() {}
void flush(bool /*lastMessageCompleted*/) {}
void setRecipient(const osiSockAddr& /*sendTo*/) {}
void startMessage(epics::pvData::int8 /*command*/, std::size_t /*ensureCapacity*/, epics::pvData::int32 /*payloadSize*/) {}
void ensureBuffer(std::size_t /*size*/) {}
void alignBuffer(std::size_t /*alignment*/) {}
void flushSerializeBuffer() {}
void cachedSerialize(const std::tr1::shared_ptr<const epics::pvData::Field>& field, epics::pvData::ByteBuffer* buffer)
{
// no cache
field->serialize(buffer, this);
}
virtual bool directSerialize(epics::pvData::ByteBuffer* /*existingBuffer*/, const char* /*toSerialize*/,
std::size_t /*elementCount*/, std::size_t /*elementSize*/)
{
return false;
}
};
class SimpleChannelSearchManagerImpl :
public ChannelSearchManager,
public epics::pvData::TimerCallback,
public std::tr1::enable_shared_from_this<SimpleChannelSearchManagerImpl>
{
public:
POINTER_DEFINITIONS(SimpleChannelSearchManagerImpl);
/**
* Constructor.
* @param context
*/
virtual ~SimpleChannelSearchManagerImpl();
/**
* Cancel.
*/
void cancel();
/**
* Get number of registered channels.
* @return number of registered channels.
*/
int32_t registeredCount();
/**
* Register channel.
* @param channel to register.
*/
void registerSearchInstance(SearchInstance::shared_pointer const & channel, bool penalize = false);
/**
* Unregister channel.
* @param channel to unregister.
*/
void unregisterSearchInstance(SearchInstance::shared_pointer const & channel);
/**
* Search response from server (channel found).
* @param guid server GUID.
* @param cid client channel ID.
* @param seqNo search sequence number.
* @param minorRevision server minor PVA revision.
* @param serverAddress server address.
*/
void searchResponse(const ServerGUID & guid, pvAccessID cid, int32_t seqNo, int8_t minorRevision, osiSockAddr* serverAddress);
/**
* New server detected.
* Boost searching of all channels.
*/
void newServerDetected();
/// Timer callback.
void callback();
/// Timer stooped callback.
void timerStopped();
/**
* Private constructor.
* @param context
*/
SimpleChannelSearchManagerImpl(Context::shared_pointer const & context);
void activate();
private:
bool generateSearchRequestMessage(SearchInstance::shared_pointer const & channel, bool allowNewFrame, bool flush);
static bool generateSearchRequestMessage(SearchInstance::shared_pointer const & channel,
epics::pvData::ByteBuffer* byteBuffer, TransportSendControl* control);
void boost();
void initializeSendBuffer();
void flushSendBuffer();
static bool isPowerOfTwo(int32_t x);
/**
* Context.
*/
Context::weak_pointer m_context;
/**
* Response address.
*/
osiSockAddr m_responseAddress;
/**
* Canceled flag.
*/
AtomicBoolean m_canceled;
/**
* Search (datagram) sequence number.
*/
int32_t m_sequenceNumber;
/**
* Send byte buffer (frame)
*/
epics::pvData::ByteBuffer m_sendBuffer;
/**
* Set of registered channels.
*/
typedef std::map<pvAccessID,SearchInstance::weak_pointer> m_channels_t;
m_channels_t m_channels;
/**
* Time of last frame send.
*/
int64_t m_lastTimeSent;
/**
* Mock transport send control
*/
MockTransportSendControl m_mockTransportSendControl;
/**
* This instance mutex.
*/
epics::pvData::Mutex m_channelMutex;
/**
* User value lock.
*/
epics::pvData::Mutex m_userValueMutex;
/**
* m_channels mutex.
*/
epics::pvData::Mutex m_mutex;
static const int DATA_COUNT_POSITION;
static const int CAST_POSITION;
static const int PAYLOAD_POSITION;
static const double ATOMIC_PERIOD;
static const int PERIOD_JITTER_MS;
static const int DEFAULT_USER_VALUE;
static const int BOOST_VALUE;
static const int MAX_COUNT_VALUE;
static const int MAX_FALLBACK_COUNT_VALUE;
static const int MAX_FRAMES_AT_ONCE;
static const int DELAY_BETWEEN_FRAMES_MS;
};
}
}
#endif /* SIMPLECHANNELSEARCHMANAGERIMPL_H */

View File

@@ -28,7 +28,7 @@
#include <pv/codec.h>
#include <pv/channelSearchManager.h>
#include <pv/serializationHelper.h>
#include <pv/simpleChannelSearchManagerImpl.h>
#include <pv/channelSearchManager.h>
#include <pv/clientContextImpl.h>
#include <pv/configuration.h>
#include <pv/beaconHandler.h>
@@ -4152,7 +4152,7 @@ private:
// stores many weak_ptr
m_responseHandler.reset(new ClientResponseHandler(thisPointer));
m_channelSearchManager.reset(new SimpleChannelSearchManagerImpl(thisPointer));
m_channelSearchManager.reset(new ChannelSearchManager(thisPointer));
// preinitialize security plugins
SecurityPluginRegistry::instance();
@@ -4586,7 +4586,7 @@ private:
* Channel search manager.
* Manages UDP search requests.
*/
SimpleChannelSearchManagerImpl::shared_pointer m_channelSearchManager;
ChannelSearchManager::shared_pointer m_channelSearchManager;
/**
* Beacon handler map.