Add RequestTaker - interface for objects executing requests.
Derive Logic from RequestTaker.
Implement CheckRequest execution in Logic.
Swap const references to Request derivatives with shared pointers.
Add pointers.h file to gather all request related shared pointers.
Remove Request.cpp file (all methods inlined).
Fix SocketManager request execution.
Remove not any more needed NoResponseGeneratedException class.
Add temporary impl. of method requestTaker() to SocketManager.
Change-Id: I67526fb1f68cc90c5340572445ce63068bed03ea
${CYNARA_SERVICE_PATH}/protocol/ProtocolSignal.cpp
${CYNARA_SERVICE_PATH}/protocol/ResponseTaker.cpp
${CYNARA_SERVICE_PATH}/request/CheckRequest.cpp
- ${CYNARA_SERVICE_PATH}/request/Request.cpp
+ ${CYNARA_SERVICE_PATH}/request/RequestTaker.cpp
${CYNARA_SERVICE_PATH}/sockets/Descriptor.cpp
${CYNARA_SERVICE_PATH}/sockets/SocketManager.cpp
${CYNARA_SERVICE_PATH}/storage/InMemoryStorageBackend.cpp
#include <exceptions/PluginNotFoundException.h>
#include <main/Cynara.h>
+#include <request/CheckRequest.h>
+#include <response/CheckResponse.h>
#include <storage/Storage.h>
#include "Logic.h"
Logic::~Logic() {
}
-PolicyResult Logic::check(const RequestContext &context UNUSED, const PolicyKey &key) {
- PolicyResult result = Cynara::getStorage()->checkPolicy(key);
+void Logic::execute(const RequestContext &context, CheckRequestPtr request) {
+ PolicyResult result(PredefinedPolicyType::DENY);
+ if (check(context, request->key(), result)) {
+ context.returnResponse(CheckResponse(result));
+ }
+}
+
+bool Logic::check(const RequestContext &context UNUSED, const PolicyKey &key,
+ PolicyResult& result) {
+ result = Cynara::getStorage()->checkPolicy(key);
switch (result.policyType()) {
case PredefinedPolicyType::ALLOW :
LOGD("check of policy key <%s> returned ALLOW", key.toString().c_str());
- return result;
+ return true;
case PredefinedPolicyType::DENY :
LOGD("check of policy key <%s> returned DENY", key.toString().c_str());
- return result;
+ return true;
}
//todo pass question to proper plugin that:
- // 1) might throw NoResponseGeneratedException when answer has to be waited for (UI)
- // 2) might return PolicyResult
+ // 1) returns false when answer has to be waited for (UI)
+ // 2) returns true if Response is to be generated
// In case 1) context should be saved in plugin in order to return answer when ready.
//in case no proper plugin is found
throw PluginNotFoundException(result);
+ return false;
}
} // namespace Cynara
#include <types/PolicyKey.h>
#include <types/PolicyResult.h>
+#include <request/pointers.h>
#include <request/RequestContext.h>
+#include <request/RequestTaker.h>
namespace Cynara {
-class Logic {
+class Logic : public RequestTaker {
public:
Logic();
- ~Logic();
+ virtual ~Logic();
+
+ virtual void execute(const RequestContext &context, CheckRequestPtr request);
+
+private:
+ bool check(const RequestContext &context, const PolicyKey &key, PolicyResult& result);
- PolicyResult check(const RequestContext &context, const PolicyKey &key);
};
} // namespace Cynara
#include <common.h>
#include <protocol/ResponseTaker.h>
-#include <request/Request.h>
-#include <response/CheckResponse.h>
+#include <request/pointers.h>
namespace Cynara {
* @brief This file implements check request class
*/
-#include <common.h>
-#include <types/PolicyResult.h>
-
-#include <logic/Logic.h>
-#include <main/Cynara.h>
-#include <request/RequestContext.h>
+#include <memory>
#include "CheckRequest.h"
namespace Cynara {
-CheckRequest::CheckRequest(const PolicyKey &key) : m_key(key) {
-}
-
-void CheckRequest::execute(const RequestContext &context) {
- PolicyResult result = Cynara::getLogic()->check(context, m_key);
- context.returnResponse(CheckResponse(result));
+void CheckRequest::execute(RequestPtr self, RequestTaker &taker,
+ const RequestContext &context) const {
+ taker.execute(context, std::dynamic_pointer_cast<CheckRequest>(self));
}
} // namespace Cynara
#include <types/PolicyKey.h>
+#include <request/pointers.h>
#include <request/Request.h>
#include <request/RequestContext.h>
+#include <request/RequestTaker.h>
namespace Cynara {
PolicyKey m_key;
public:
- CheckRequest(const PolicyKey &key);
+ CheckRequest(const PolicyKey &key) : m_key(key) {
+ }
+
virtual ~CheckRequest() = default;
- virtual void execute(const RequestContext &context);
+ const PolicyKey &key(void) const {
+ return m_key;
+ }
+
+ virtual void execute(RequestPtr self, RequestTaker &taker, const RequestContext &context) const;
};
} // namespace Cynara
#ifndef SRC_SERVICE_REQUEST_REQUEST_H_
#define SRC_SERVICE_REQUEST_REQUEST_H_
-#include <memory>
-
-#include "RequestContext.h"
+#include <request/pointers.h>
+#include <request/RequestContext.h>
+#include <request/RequestTaker.h>
namespace Cynara {
class Request {
public:
- Request();
- virtual ~Request();
+ Request() = default;
+ virtual ~Request() = default;
- virtual void execute(const RequestContext &context) = 0;
+ virtual void execute(RequestPtr self, RequestTaker &taker,
+ const RequestContext &context) const = 0;
};
-typedef std::shared_ptr<Request> RequestPtr;
-
} // namespace Cynara
#endif /* SRC_SERVICE_REQUEST_REQUEST_H_ */
* limitations under the License.
*/
/*
- * @file Request.cpp
+ * @file RequestTaker.cpp
* @author Lukasz Wojciechowski <l.wojciechow@partner.samsung.com>
* @version 1.0
- * @brief This file implements request base class
+ * @brief This file implements RequestTaker class
*/
-#include "Request.h"
+#include <exceptions/NotImplementedException.h>
-namespace Cynara {
+#include "RequestTaker.h"
-Request::Request() {
-}
+namespace Cynara {
-Request::~Request() {
+void RequestTaker::execute(const RequestContext &context UNUSED, CheckRequestPtr request UNUSED) {
+ throw NotImplementedException();
}
} // namespace Cynara
* limitations under the License.
*/
/*
- * @file NoResponseGeneratedException.h
+ * @file RequestTaker.h
* @author Lukasz Wojciechowski <l.wojciechow@partner.samsung.com>
* @version 1.0
- * @brief Implementation of NoResponseGeneratedException
+ * @brief This file defines RequestTaker class
*/
-#ifndef SRC_COMMON_EXCEPTIONS_NORESPONSEGENERATEDEXCEPTION_H_
-#define SRC_COMMON_EXCEPTIONS_NORESPONSEGENERATEDEXCEPTION_H_
+#ifndef SRC_SERVICE_REQUEST_REQUESTTAKER_H_
+#define SRC_SERVICE_REQUEST_REQUESTTAKER_H_
-#include "Exception.h"
-
-#include <exception>
+#include <request/pointers.h>
+#include <request/RequestContext.h>
namespace Cynara {
-class NoResponseGeneratedException : public Exception {
+class RequestTaker {
public:
- NoResponseGeneratedException() = default;
- virtual ~NoResponseGeneratedException() = default;
+ RequestTaker() = default;
+ virtual ~RequestTaker() = default;
+
+ virtual void execute(const RequestContext &context, CheckRequestPtr request);
};
-} /* namespace Cynara */
+} // namespace Cynara
-#endif /* SRC_COMMON_EXCEPTIONS_NORESPONSEGENERATEDEXCEPTION_H_ */
+#endif /* SRC_SERVICE_REQUEST_REQUESTTAKER_H_ */
--- /dev/null
+/*
+ * 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 <l.wojciechow@partner.samsung.com>
+ * @version 1.0
+ * @brief This file defines request base class
+ */
+
+#ifndef SRC_SERVICE_REQUEST_POINTERS_H_
+#define SRC_SERVICE_REQUEST_POINTERS_H_
+
+#include <memory>
+
+namespace Cynara {
+
+class Request;
+typedef std::shared_ptr<Request> RequestPtr;
+
+class CheckRequest;
+typedef std::shared_ptr<CheckRequest> CheckRequestPtr;
+
+} // namespace Cynara
+
+#endif /* SRC_SERVICE_REQUEST_POINTERS_H_ */
#include <common.h>
#include <exceptions/DescriptorNotExistsException.h>
#include <exceptions/InitException.h>
-#include <exceptions/NoResponseGeneratedException.h>
#include <exceptions/UnexpectedErrorException.h>
#include <logic/Logic.h>
#include <protocol/ProtocolAdmin.h>
#include <protocol/ProtocolClient.h>
#include <protocol/ProtocolSignal.h>
+#include <request/pointers.h>
+#include <request/RequestContext.h>
#include <stdexcept>
#include "SocketManager.h"
try {
while(true) {
+ //try extract request from binary data received on socket
auto req = desc.extractRequest();
- if (!req)
+ if (!req) // not enough data to build request yet
break;
-
LOGD("request extracted");
- try {
- req->execute(RequestContext(fd, desc.responseTaker(), desc.writeQueue()));
- } catch (const NoResponseGeneratedException &ex) {
- LOGD("no response generated");
- }
+
+ //build context
+ RequestContext context(fd, desc.responseTaker(), desc.writeQueue());
+ //pass request to request taker
+ req->execute(req, requestTaker(), context);
}
} catch (const Exception &ex) {
LOGE("Error handling request <%s>. Closing socket", ex.what());
FD_CLR(fd, &m_writeSet);
}
+RequestTaker &SocketManager::requestTaker(void) {
+ //todo change
+ return *Cynara::getLogic();
+}
+
} // namespace Cynara
#include <common.h>
#include <protocol/Protocol.h>
-#include <request/RequestContext.h>
+#include <request/RequestTaker.h>
#include "Descriptor.h"
namespace Cynara {
void removeReadSocket(int fd);
void addWriteSocket(int fd);
void removeWriteSocket(int fd);
+
+ RequestTaker &requestTaker(void);
};
} // namespace Cynara