ChannelAccessFactory implemented and tested

This commit is contained in:
Matej Sekoranja
2010-11-03 16:07:20 +01:00
parent 9edc459254
commit 620f0f0681
6 changed files with 566 additions and 398 deletions

View File

@@ -1,2 +1,3 @@
pvAccessApp/client/pvAccess.h
pvAccessApp/client/ChannelAccessFactory.cpp
pvAccessApp/testClient/testChannelAccessFactory.cpp

View File

@@ -1,6 +1,6 @@
TOP = ..
include $(TOP)/configure/CONFIG
DIRS += client #server
DIRS += client testClient #server
#DIRS += localImpl remoteClientImpl remoteServerImpl
#DIRS += localImplTest remoteImplTest
include $(TOP)/configure/RULES_DIRS

View File

@@ -1,24 +1,61 @@
/*ChannelAccessFactory.cpp*/
#include <lock.h>
#include <noDefaultMethods.h>
#include "pvAccess.h"
#include "pvData.h"
#include "factory.h"
#include <map>
#include <vector>
namespace epics { namespace pvAccess {
static ChannelAccess* channelAccess = 0;
static Mutex channelProviderMutex = Mutex();
ChannelAccess * getChannelAccess() {
static Mutex mutex = Mutex();
Lock guard(&mutex);
typedef std::map<String, ChannelProvider*> ChannelProviderMap;
static ChannelProviderMap channelProviders;
if(channelAccess==0){
//channelAccess = new ChannelAccessImpl();
}
return channelAccess;
}
class ChannelAccessImpl : public ChannelAccess, public NoDefaultMethods {
public:
ChannelProvider* getProvider(String providerName) {
Lock guard(&channelProviderMutex);
return channelProviders[providerName];
}
std::vector<String>* getProviderNames() {
Lock guard(&channelProviderMutex);
std::vector<String>* providers = new std::vector<String>();
for (ChannelProviderMap::const_iterator i = channelProviders.begin();
i != channelProviders.end(); i++)
providers->push_back(i->first);
return providers;
}
};
ChannelAccess * getChannelAccess() {
static Mutex mutex = Mutex();
Lock guard(&mutex);
if(channelAccess==0){
channelAccess = new ChannelAccessImpl();
}
return channelAccess;
}
void registerChannelProvider(ChannelProvider *channelProvider) {
Lock guard(&channelProviderMutex);
channelProviders[channelProvider->getProviderName()] = channelProvider;
}
void unregisterChannelProvider(ChannelProvider *channelProvider) {
Lock guard(&channelProviderMutex);
channelProviders.erase(channelProvider->getProviderName());
}
}}

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,12 @@
TOP=../..
include $(TOP)/configure/CONFIG
PROD_HOST += testChannelAccessFactory
testChannelAccessFactory_SRCS += testChannelAccessFactory.cpp
testChannelAccessFactory_LIBS += pvAccessClient Com
include $(TOP)/configure/RULES
#----------------------------------------
# ADD RULES AFTER THIS LINE

View File

@@ -0,0 +1,87 @@
/* testChannelAccessFactory.cpp */
/* Author: Matej Sekoranja Date: 2010.11.03 */
#include <stddef.h>
#include <stdlib.h>
#include <stddef.h>
#include <string.h>
#include <stdio.h>
#include "pvAccess.h"
#include <epicsAssert.h>
using namespace epics::pvAccess;
class DummyChannelProvider : public ChannelProvider {
private:
String m_name;
public:
DummyChannelProvider(String name) : m_name(name) {};
void destroy() {};
String getProviderName() { return m_name; };
ChannelFind* channelFind(String channelName,ChannelFindRequester *channelFindRequester)
{ return 0; }
Channel* createChannel(String channelName,ChannelRequester *channelRequester,short priority)
{ return 0; }
Channel* createChannel(String channelName,ChannelRequester *channelRequester,short priority,String address)
{ return 0; }
};
void testChannelAccessFactory() {
printf("testChannelAccessFactory... ");
ChannelAccess* ca = getChannelAccess();
assert(ca);
// empty
std::vector<String>* providers = ca->getProviderNames();
assert(providers);
assert(providers->size() == 0);
delete providers;
// register 2
ChannelProvider* cp1 = new DummyChannelProvider("dummy1");
registerChannelProvider(cp1);
ChannelProvider* cp2 = new DummyChannelProvider("dummy2");
registerChannelProvider(cp2);
providers = ca->getProviderNames();
assert(providers);
assert(providers->size() == 2);
assert(providers->at(0) == "dummy1");
assert(providers->at(1) == "dummy2");
assert(ca->getProvider("dummy1") == cp1);
assert(ca->getProvider("dummy2") == cp2);
delete providers;
// unregister first
unregisterChannelProvider(cp1);
providers = ca->getProviderNames();
assert(providers);
assert(providers->size() == 1);
assert(providers->at(0) == "dummy2");
assert(ca->getProvider("dummy2") == cp2);
delete providers;
printf("PASSED\n");
}
int main(int argc,char *argv[])
{
testChannelAccessFactory();
return(0);
}