From: Lukasz Wojciechowski Date: Thu, 26 Jun 2014 10:12:13 +0000 (+0200) Subject: Use shared and weak pointers for ReponseTaker and RequestContext X-Git-Tag: v0.1.0~82 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=bac671b3d90f28fd309529275bc976842e6f03a9;p=platform%2Fcore%2Fsecurity%2Fcynara.git Use shared and weak pointers for ReponseTaker and RequestContext Change-Id: I711a2c97819d7a363fa1b2d1401e7156554a2e09 --- diff --git a/src/service/logic/Logic.cpp b/src/service/logic/Logic.cpp index 9aa2e309..3944a8f2 100644 --- a/src/service/logic/Logic.cpp +++ b/src/service/logic/Logic.cpp @@ -26,6 +26,7 @@ #include
#include +#include #include #include @@ -38,14 +39,14 @@ Logic::Logic() { Logic::~Logic() { } -void Logic::execute(const RequestContext &context, CheckRequestPtr request) { +void Logic::execute(RequestContextPtr context, CheckRequestPtr request) { PolicyResult result(PredefinedPolicyType::DENY); if (check(context, request->key(), result)) { - context.returnResponse(CheckResponse(result)); + context->returnResponse(context, CheckResponse(result)); } } -bool Logic::check(const RequestContext &context UNUSED, const PolicyKey &key, +bool Logic::check(RequestContextPtr context UNUSED, const PolicyKey &key, PolicyResult& result) { result = Cynara::getStorage()->checkPolicy(key); diff --git a/src/service/logic/Logic.h b/src/service/logic/Logic.h index 362b47c4..9ef96f4e 100644 --- a/src/service/logic/Logic.h +++ b/src/service/logic/Logic.h @@ -27,7 +27,6 @@ #include #include -#include #include namespace Cynara { @@ -37,10 +36,10 @@ public: Logic(); virtual ~Logic(); - virtual void execute(const RequestContext &context, CheckRequestPtr request); + virtual void execute(RequestContextPtr context, CheckRequestPtr request); private: - bool check(const RequestContext &context, const PolicyKey &key, PolicyResult& result); + bool check(RequestContextPtr context, const PolicyKey &key, PolicyResult& result); }; diff --git a/src/service/request/CheckRequest.cpp b/src/service/request/CheckRequest.cpp index 8bcbf65f..5473a795 100644 --- a/src/service/request/CheckRequest.cpp +++ b/src/service/request/CheckRequest.cpp @@ -27,7 +27,7 @@ namespace Cynara { void CheckRequest::execute(RequestPtr self, RequestTaker &taker, - const RequestContext &context) const { + RequestContextPtr context) const { taker.execute(context, std::dynamic_pointer_cast(self)); } diff --git a/src/service/request/CheckRequest.h b/src/service/request/CheckRequest.h index 54a8acf0..83f0203f 100644 --- a/src/service/request/CheckRequest.h +++ b/src/service/request/CheckRequest.h @@ -27,7 +27,6 @@ #include #include -#include #include namespace Cynara { @@ -46,7 +45,7 @@ public: return m_key; } - virtual void execute(RequestPtr self, RequestTaker &taker, const RequestContext &context) const; + virtual void execute(RequestPtr self, RequestTaker &taker, RequestContextPtr context) const; }; } // namespace Cynara diff --git a/src/service/request/Request.h b/src/service/request/Request.h index 8df08b94..fa76ffaa 100644 --- a/src/service/request/Request.h +++ b/src/service/request/Request.h @@ -24,7 +24,7 @@ #define SRC_SERVICE_REQUEST_REQUEST_H_ #include -#include +#include #include namespace Cynara { @@ -34,8 +34,7 @@ public: Request() = default; virtual ~Request() = default; - virtual void execute(RequestPtr self, RequestTaker &taker, - const RequestContext &context) const = 0; + virtual void execute(RequestPtr self, RequestTaker &taker, RequestContextPtr context) const = 0; }; } // namespace Cynara diff --git a/src/service/request/RequestContext.h b/src/service/request/RequestContext.h index 55ff9fea..d8e95783 100644 --- a/src/service/request/RequestContext.h +++ b/src/service/request/RequestContext.h @@ -23,8 +23,12 @@ #ifndef SRC_SERVICE_REQUEST_REQUESTCONTEXT_H_ #define SRC_SERVICE_REQUEST_REQUESTCONTEXT_H_ +#include + #include +#include +#include #include namespace Cynara { @@ -32,17 +36,23 @@ namespace Cynara { class RequestContext { private: int m_desc; - ResponseTaker &m_responseTaker; + ResponseTakerWeakPtr m_responseTaker; BinaryQueue &m_responseQueue; public: - RequestContext(int desc, ResponseTaker &responseTaker, BinaryQueue &responseQueue) + RequestContext(int desc, ResponseTakerPtr responseTaker, BinaryQueue &responseQueue) : m_desc(desc), m_responseTaker(responseTaker), m_responseQueue(responseQueue) { } template - void returnResponse(T &&response) const { - m_responseTaker.appendResponseToBuffer(m_responseQueue, std::move(response)); + void returnResponse(RequestContextPtr self, T &&response) const { + ResponseTakerPtr taker = m_responseTaker.lock(); + if (taker) + taker->execute(self, std::move(response)); + } + + BinaryQueue &responseQueue(void) const { + return m_responseQueue; } }; diff --git a/src/service/request/RequestTaker.cpp b/src/service/request/RequestTaker.cpp index e56f4aaf..f634ea82 100644 --- a/src/service/request/RequestTaker.cpp +++ b/src/service/request/RequestTaker.cpp @@ -20,13 +20,15 @@ * @brief This file implements RequestTaker class */ +#include + #include #include "RequestTaker.h" namespace Cynara { -void RequestTaker::execute(const RequestContext &context UNUSED, CheckRequestPtr request UNUSED) { +void RequestTaker::execute(RequestContextPtr context UNUSED, CheckRequestPtr request UNUSED) { throw NotImplementedException(); } diff --git a/src/service/request/RequestTaker.h b/src/service/request/RequestTaker.h index 5564ece7..9ebbe329 100644 --- a/src/service/request/RequestTaker.h +++ b/src/service/request/RequestTaker.h @@ -24,7 +24,6 @@ #define SRC_SERVICE_REQUEST_REQUESTTAKER_H_ #include -#include namespace Cynara { @@ -33,7 +32,7 @@ public: RequestTaker() = default; virtual ~RequestTaker() = default; - virtual void execute(const RequestContext &context, CheckRequestPtr request); + virtual void execute(RequestContextPtr context, CheckRequestPtr request); }; } // namespace Cynara diff --git a/src/service/request/pointers.h b/src/service/request/pointers.h index 79f15399..7bfc66eb 100644 --- a/src/service/request/pointers.h +++ b/src/service/request/pointers.h @@ -27,11 +27,14 @@ namespace Cynara { +class CheckRequest; +typedef std::shared_ptr CheckRequestPtr; + class Request; typedef std::shared_ptr RequestPtr; -class CheckRequest; -typedef std::shared_ptr CheckRequestPtr; +class RequestContext; +typedef std::shared_ptr RequestContextPtr; } // namespace Cynara diff --git a/src/service/response/ResponseTaker.cpp b/src/service/response/ResponseTaker.cpp index 9ce248a1..fff97743 100644 --- a/src/service/response/ResponseTaker.cpp +++ b/src/service/response/ResponseTaker.cpp @@ -28,8 +28,7 @@ namespace Cynara { -void ResponseTaker::appendResponseToBuffer(BinaryQueue &queue UNUSED, - CheckResponse &&response UNUSED) { +void ResponseTaker::execute(RequestContextPtr context UNUSED, CheckResponse &&response UNUSED) { throw NotImplementedException(); } diff --git a/src/service/response/ResponseTaker.h b/src/service/response/ResponseTaker.h index 771adbc8..0c880b69 100644 --- a/src/service/response/ResponseTaker.h +++ b/src/service/response/ResponseTaker.h @@ -25,6 +25,7 @@ #include +#include #include namespace Cynara { @@ -34,7 +35,7 @@ public: ResponseTaker() = default; virtual ~ResponseTaker() = default; - virtual void appendResponseToBuffer(BinaryQueue &queue, CheckResponse &&response); + virtual void execute(RequestContextPtr context, CheckResponse &&response); }; } // namespace Cynara diff --git a/src/service/response/pointers.h b/src/service/response/pointers.h new file mode 100644 index 00000000..6d12a8bf --- /dev/null +++ b/src/service/response/pointers.h @@ -0,0 +1,36 @@ +/* + * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved + * + * 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. + */ +/* + * @file pointers.h + * @author Lukasz Wojciechowski + * @version 1.0 + * @brief This file defines response classes pointers + */ + +#ifndef SRC_SERVICE_RESPONSE_POINTERS_H_ +#define SRC_SERVICE_RESPONSE_POINTERS_H_ + +#include + +namespace Cynara { + +class ResponseTaker; +typedef std::shared_ptr ResponseTakerPtr; +typedef std::weak_ptr ResponseTakerWeakPtr; + +} // namespace Cynara + +#endif /* SRC_SERVICE_RESPONSE_POINTERS_H_ */ diff --git a/src/service/sockets/Descriptor.cpp b/src/service/sockets/Descriptor.cpp index 661833d9..f58d28ea 100644 --- a/src/service/sockets/Descriptor.cpp +++ b/src/service/sockets/Descriptor.cpp @@ -31,6 +31,10 @@ bool Descriptor::hasDataToWrite(void) const { return !(m_writeQueue.empty() && m_writeBuffer.empty()); } +ResponseTakerPtr Descriptor::responseTaker(void) const { + return std::static_pointer_cast(m_protocol); +} + void Descriptor::pushReadBuffer(const RawBuffer &readbuffer) { m_readQueue.appendCopy(readbuffer.data(), readbuffer.size()); } diff --git a/src/service/sockets/Descriptor.h b/src/service/sockets/Descriptor.h index 769abdd9..86ee4dbb 100644 --- a/src/service/sockets/Descriptor.h +++ b/src/service/sockets/Descriptor.h @@ -29,6 +29,7 @@ #include #include +#include namespace Cynara { @@ -60,9 +61,7 @@ public: return m_protocol; } - ResponseTaker &responseTaker(void) { - return *m_protocol; - } + ResponseTakerPtr responseTaker(void) const; BinaryQueue &writeQueue(void) { return m_writeQueue; diff --git a/src/service/sockets/SocketManager.cpp b/src/service/sockets/SocketManager.cpp index aa7cb513..fae4984c 100644 --- a/src/service/sockets/SocketManager.cpp +++ b/src/service/sockets/SocketManager.cpp @@ -227,7 +227,8 @@ bool SocketManager::handleRead(int fd, const RawBuffer &readbuffer) { LOGD("request extracted"); //build context - RequestContext context(fd, desc.responseTaker(), desc.writeQueue()); + auto context = std::make_shared(fd, desc.responseTaker(), + desc.writeQueue()); //pass request to request taker req->execute(req, requestTaker(), context); }