diff options
author | Christian Mollekopf <chrigi_1@fastmail.fm> | 2016-02-12 18:11:48 +0100 |
---|---|---|
committer | Christian Mollekopf <chrigi_1@fastmail.fm> | 2016-02-12 18:11:48 +0100 |
commit | a9a0f53af2138cf259daa2dfee62cb3d785d00aa (patch) | |
tree | da7123bab1c73222e25b40586783ecd0a5714cf8 /common/facade.cpp | |
parent | 1240a482ab13d285d2624c33015f0f61f16d91a9 (diff) | |
download | sink-a9a0f53af2138cf259daa2dfee62cb3d785d00aa.tar.gz sink-a9a0f53af2138cf259daa2dfee62cb3d785d00aa.zip |
moved ResourceAccessFactory
Diffstat (limited to 'common/facade.cpp')
-rw-r--r-- | common/facade.cpp | 57 |
1 files changed, 0 insertions, 57 deletions
diff --git a/common/facade.cpp b/common/facade.cpp index 1b91ce4..1219887 100644 --- a/common/facade.cpp +++ b/common/facade.cpp | |||
@@ -32,63 +32,6 @@ using namespace Sink; | |||
32 | #undef DEBUG_AREA | 32 | #undef DEBUG_AREA |
33 | #define DEBUG_AREA "client.facade" | 33 | #define DEBUG_AREA "client.facade" |
34 | 34 | ||
35 | /** | ||
36 | * A factory for resource access instances that caches the instance for some time. | ||
37 | * | ||
38 | * This avoids constantly recreating connections, and should allow a single process to have one connection per resource. | ||
39 | */ | ||
40 | class ResourceAccessFactory { | ||
41 | public: | ||
42 | static ResourceAccessFactory &instance() | ||
43 | { | ||
44 | static ResourceAccessFactory *instance = 0; | ||
45 | if (!instance) { | ||
46 | instance = new ResourceAccessFactory; | ||
47 | } | ||
48 | return *instance; | ||
49 | } | ||
50 | |||
51 | Sink::ResourceAccess::Ptr getAccess(const QByteArray &instanceIdentifier) | ||
52 | { | ||
53 | if (!mCache.contains(instanceIdentifier)) { | ||
54 | //Reuse the pointer if something else kept the resourceaccess alive | ||
55 | if (mWeakCache.contains(instanceIdentifier)) { | ||
56 | auto sharedPointer = mWeakCache.value(instanceIdentifier).toStrongRef(); | ||
57 | if (sharedPointer) { | ||
58 | mCache.insert(instanceIdentifier, sharedPointer); | ||
59 | } | ||
60 | } | ||
61 | if (!mCache.contains(instanceIdentifier)) { | ||
62 | //Create a new instance if necessary | ||
63 | auto sharedPointer = Sink::ResourceAccess::Ptr::create(instanceIdentifier); | ||
64 | QObject::connect(sharedPointer.data(), &Sink::ResourceAccess::ready, sharedPointer.data(), [this, instanceIdentifier](bool ready) { | ||
65 | if (!ready) { | ||
66 | mCache.remove(instanceIdentifier); | ||
67 | } | ||
68 | }); | ||
69 | mCache.insert(instanceIdentifier, sharedPointer); | ||
70 | mWeakCache.insert(instanceIdentifier, sharedPointer); | ||
71 | } | ||
72 | } | ||
73 | if (!mTimer.contains(instanceIdentifier)) { | ||
74 | auto timer = new QTimer; | ||
75 | //Drop connection after 3 seconds (which is a random value) | ||
76 | QObject::connect(timer, &QTimer::timeout, timer, [this, instanceIdentifier]() { | ||
77 | mCache.remove(instanceIdentifier); | ||
78 | }); | ||
79 | timer->setInterval(3000); | ||
80 | mTimer.insert(instanceIdentifier, timer); | ||
81 | } | ||
82 | auto timer = mTimer.value(instanceIdentifier); | ||
83 | timer->start(); | ||
84 | return mCache.value(instanceIdentifier); | ||
85 | } | ||
86 | |||
87 | QHash<QByteArray, QWeakPointer<Sink::ResourceAccess> > mWeakCache; | ||
88 | QHash<QByteArray, Sink::ResourceAccess::Ptr> mCache; | ||
89 | QHash<QByteArray, QTimer*> mTimer; | ||
90 | }; | ||
91 | |||
92 | template<class DomainType> | 35 | template<class DomainType> |
93 | GenericFacade<DomainType>::GenericFacade(const QByteArray &resourceIdentifier, const DomainTypeAdaptorFactoryInterface::Ptr &adaptorFactory , const QSharedPointer<Sink::ResourceAccessInterface> resourceAccess) | 36 | GenericFacade<DomainType>::GenericFacade(const QByteArray &resourceIdentifier, const DomainTypeAdaptorFactoryInterface::Ptr &adaptorFactory , const QSharedPointer<Sink::ResourceAccessInterface> resourceAccess) |
94 | : Sink::StoreFacade<DomainType>(), | 37 | : Sink::StoreFacade<DomainType>(), |