diff --git a/CHAOSFramework.xcodeproj/project.pbxproj b/CHAOSFramework.xcodeproj/project.pbxproj index 39a74f1535d0c43ca572f4e8f695729c1f7c544b..2e6322f6a6c114d8da82ee06cf820960075a5b81 100644 --- a/CHAOSFramework.xcodeproj/project.pbxproj +++ b/CHAOSFramework.xcodeproj/project.pbxproj @@ -350,10 +350,10 @@ 325D2FE21AFB9C2100B2A706 /* ProcessBye.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 325D2FE01AFB9C2100B2A706 /* ProcessBye.cpp */; }; 325DE9F115EC031E00D6F34E /* EventChannel.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 325DE9EF15EC031E00D6F34E /* EventChannel.cpp */; }; 325DE9F215EC031E00D6F34E /* EventChannel.h in Headers */ = {isa = PBXBuildFile; fileRef = 325DE9F015EC031E00D6F34E /* EventChannel.h */; }; - 325F517A1F3C3DD2000B842F /* RawDriverUnitProxy.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 325F51781F3C3DD2000B842F /* RawDriverUnitProxy.cpp */; }; - 325F517B1F3C3DD2000B842F /* RawDriverUnitProxy.h in Headers */ = {isa = PBXBuildFile; fileRef = 325F51791F3C3DD2000B842F /* RawDriverUnitProxy.h */; }; - 325F517E1F3C3E11000B842F /* RawDriverHandlerWrapper.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 325F517C1F3C3E11000B842F /* RawDriverHandlerWrapper.cpp */; }; - 325F517F1F3C3E11000B842F /* RawDriverHandlerWrapper.h in Headers */ = {isa = PBXBuildFile; fileRef = 325F517D1F3C3E11000B842F /* RawDriverHandlerWrapper.h */; }; + 325F517A1F3C3DD2000B842F /* ExternalDriverUnitProxy.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 325F51781F3C3DD2000B842F /* ExternalDriverUnitProxy.cpp */; }; + 325F517B1F3C3DD2000B842F /* ExternalDriverUnitProxy.h in Headers */ = {isa = PBXBuildFile; fileRef = 325F51791F3C3DD2000B842F /* ExternalDriverUnitProxy.h */; }; + 325F517E1F3C3E11000B842F /* ExternalDriverHandlerWrapper.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 325F517C1F3C3E11000B842F /* ExternalDriverHandlerWrapper.cpp */; }; + 325F517F1F3C3E11000B842F /* ExternalDriverHandlerWrapper.h in Headers */ = {isa = PBXBuildFile; fileRef = 325F517D1F3C3E11000B842F /* ExternalDriverHandlerWrapper.h */; }; 325F51821F3C3E4D000B842F /* UnitProxyHandlerWrapper.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 325F51801F3C3E4D000B842F /* UnitProxyHandlerWrapper.cpp */; }; 325F51831F3C3E4D000B842F /* UnitProxyHandlerWrapper.h in Headers */ = {isa = PBXBuildFile; fileRef = 325F51811F3C3E4D000B842F /* UnitProxyHandlerWrapper.h */; }; 325F86EC14D9E59D00CDF35F /* InetUtility.h in Headers */ = {isa = PBXBuildFile; fileRef = 325F86EB14D9E59D00CDF35F /* InetUtility.h */; }; @@ -1705,10 +1705,10 @@ 325D2FE11AFB9C2100B2A706 /* ProcessBye.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ProcessBye.h; sourceTree = "<group>"; }; 325DE9EF15EC031E00D6F34E /* EventChannel.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = EventChannel.cpp; sourceTree = "<group>"; }; 325DE9F015EC031E00D6F34E /* EventChannel.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = EventChannel.h; sourceTree = "<group>"; }; - 325F51781F3C3DD2000B842F /* RawDriverUnitProxy.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = RawDriverUnitProxy.cpp; sourceTree = "<group>"; }; - 325F51791F3C3DD2000B842F /* RawDriverUnitProxy.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RawDriverUnitProxy.h; sourceTree = "<group>"; }; - 325F517C1F3C3E11000B842F /* RawDriverHandlerWrapper.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = RawDriverHandlerWrapper.cpp; sourceTree = "<group>"; }; - 325F517D1F3C3E11000B842F /* RawDriverHandlerWrapper.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RawDriverHandlerWrapper.h; sourceTree = "<group>"; }; + 325F51781F3C3DD2000B842F /* ExternalDriverUnitProxy.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = ExternalDriverUnitProxy.cpp; sourceTree = "<group>"; }; + 325F51791F3C3DD2000B842F /* ExternalDriverUnitProxy.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ExternalDriverUnitProxy.h; sourceTree = "<group>"; }; + 325F517C1F3C3E11000B842F /* ExternalDriverHandlerWrapper.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = ExternalDriverHandlerWrapper.cpp; sourceTree = "<group>"; }; + 325F517D1F3C3E11000B842F /* ExternalDriverHandlerWrapper.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ExternalDriverHandlerWrapper.h; sourceTree = "<group>"; }; 325F51801F3C3E4D000B842F /* UnitProxyHandlerWrapper.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = UnitProxyHandlerWrapper.cpp; sourceTree = "<group>"; }; 325F51811F3C3E4D000B842F /* UnitProxyHandlerWrapper.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = UnitProxyHandlerWrapper.h; sourceTree = "<group>"; }; 325F86EB14D9E59D00CDF35F /* InetUtility.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = InetUtility.h; sourceTree = "<group>"; }; @@ -3685,10 +3685,10 @@ 325F51771F3C3D89000B842F /* raw_driver */ = { isa = PBXGroup; children = ( - 325F51781F3C3DD2000B842F /* RawDriverUnitProxy.cpp */, - 325F51791F3C3DD2000B842F /* RawDriverUnitProxy.h */, - 325F517C1F3C3E11000B842F /* RawDriverHandlerWrapper.cpp */, - 325F517D1F3C3E11000B842F /* RawDriverHandlerWrapper.h */, + 325F51781F3C3DD2000B842F /* ExternalDriverUnitProxy.cpp */, + 325F51791F3C3DD2000B842F /* ExternalDriverUnitProxy.h */, + 325F517C1F3C3E11000B842F /* ExternalDriverHandlerWrapper.cpp */, + 325F517D1F3C3E11000B842F /* ExternalDriverHandlerWrapper.h */, ); path = raw_driver; sourceTree = "<group>"; @@ -5833,13 +5833,13 @@ files = ( 329065E41F34763F00356572 /* connection.h in Headers */, 324D08AC1F31D5EA00836E29 /* ConnectionManager.h in Headers */, - 325F517B1F3C3DD2000B842F /* RawDriverUnitProxy.h in Headers */, + 325F517B1F3C3DD2000B842F /* ExternalDriverUnitProxy.h in Headers */, 324D08B61F31D7F300836E29 /* AbstractUnitProxy.h in Headers */, 3229005F1F3477D800717A38 /* micro_unit_toolkit.h in Headers */, 324D08A21F30CE2B00836E29 /* ChaosMicroUnitToolkit.h in Headers */, 324D08B11F31D70600836E29 /* AbstractConnectionAdapter.h in Headers */, 324D08D51F31F5A000836E29 /* mongoose.h in Headers */, - 325F517F1F3C3E11000B842F /* RawDriverHandlerWrapper.h in Headers */, + 325F517F1F3C3E11000B842F /* ExternalDriverHandlerWrapper.h in Headers */, 325F51831F3C3E4D000B842F /* UnitProxyHandlerWrapper.h in Headers */, 324D08C61F31EC5000836E29 /* HTTPConnectionAdapter.h in Headers */, 324BE84C1F3CA31A00FFDA02 /* base64.h in Headers */, @@ -6837,7 +6837,7 @@ 324D08B51F31D7F300836E29 /* AbstractUnitProxy.cpp in Sources */, 3265F9571FB89F6B00FEA246 /* bson-value.c in Sources */, 324D08C51F31EC5000836E29 /* HTTPConnectionAdapter.cpp in Sources */, - 325F517E1F3C3E11000B842F /* RawDriverHandlerWrapper.cpp in Sources */, + 325F517E1F3C3E11000B842F /* ExternalDriverHandlerWrapper.cpp in Sources */, 3265F9531FB89F6B00FEA246 /* bson-iso8601.c in Sources */, 3265F95C1FB89F6B00FEA246 /* bson-timegm.c in Sources */, 3265F94E1FB89F6B00FEA246 /* bson-error.c in Sources */, @@ -6856,7 +6856,7 @@ 324D08B01F31D70600836E29 /* AbstractConnectionAdapter.cpp in Sources */, 3265F9561FB89F6B00FEA246 /* bson-writer.c in Sources */, 3265F9541FB89F6B00FEA246 /* bson-md5.c in Sources */, - 325F517A1F3C3DD2000B842F /* RawDriverUnitProxy.cpp in Sources */, + 325F517A1F3C3DD2000B842F /* ExternalDriverUnitProxy.cpp in Sources */, 3265F9551FB89F6B00FEA246 /* bson-memory.c in Sources */, 3265F94D1FB89F6B00FEA246 /* bson-oid.c in Sources */, 3265F9581FB89F6B00FEA246 /* bson-json.c in Sources */, diff --git a/chaos/cu_toolkit/driver_manager/driver/AbstractRemoteIODriver.h b/chaos/cu_toolkit/driver_manager/driver/AbstractRemoteIODriver.h index 74971a295e25d736f41361eb502ff0f3712a4e24..2302ea90018932e5eabf85a4f767fba0e7f12f46 100644 --- a/chaos/cu_toolkit/driver_manager/driver/AbstractRemoteIODriver.h +++ b/chaos/cu_toolkit/driver_manager/driver/AbstractRemoteIODriver.h @@ -101,16 +101,14 @@ namespace chaos { chaos::common::data::CDWUniquePtr driver_init_pack; //!initialization and deinitialization driver methods void driverInit(const char *initParameter) throw (chaos::CException) { - AbstractRemoteIODriver_DBG <<" Initialization from string..."<<initParameter; - - + //AbstractRemoteIODriver_DBG <<" Initialization from string..."<<initParameter; } void driverInit(const chaos::common::data::CDataWrapper& init_parameter) throw(chaos::CException) { CHECK_ASSERTION_THROW_AND_LOG((init_parameter.isEmpty() == false), AbstractRemoteIODriver_ERR, -1, "Init parameter need to be formated in a json document"); //CHECK_ASSERTION_THROW_AND_LOG(init_parameter.hasKey(AUTHORIZATION_KEY), AbstractRemoteIODriver_ERR, -3, "The authorization key is mandatory") //get the authorization key - AbstractRemoteIODriver_DBG <<" Initialization params:"<<init_parameter.getCompliantJSONString(); - + //AbstractRemoteIODriver_DBG <<" Initialization params:"<<init_parameter.getCompliantJSONString(); + if(init_parameter.hasKey(AUTHORIZATION_KEY)) { authorization_key = init_parameter.getStringValue(AUTHORIZATION_KEY); } else { @@ -158,7 +156,7 @@ namespace chaos { message_response->isInt32Value("err")) { int err; if((err=message_response->getInt32Value("err")) != 0) { - AbstractRemoteIODriver_ERR <<" Returned not zero err="<<err; + //AbstractRemoteIODriver_ERR <<" Returned not zero err="<<err; return false; } } @@ -187,7 +185,7 @@ namespace chaos { //!set a new uri for driver void setDriverUri(std::string _new_remote_uri) { - remote_uri = _new_remote_uri; + remote_uri = _new_remote_uri; } //!Send opcode request to the remote driver @@ -300,24 +298,24 @@ namespace chaos { int handleReceivedeMessage(const std::string& connection_identifier, ChaosUniquePtr<chaos::common::data::CDataWrapper> message) { if(message.get() == NULL) return 0; - AbstractRemoteIODriver_DBG << CHAOS_FORMAT("Received message: %1%", %message->getCompliantJSONString()); + //AbstractRemoteIODriver_DBG << CHAOS_FORMAT("Received message: %1%", %message->getCompliantJSONString()); if(message->hasKey(MESSAGE) == false) { //error msg key is mandatory //EndpointType::sendError(connection_identifier, // -1, "message field is mandatory", __PRETTY_FUNCTION__); - AbstractRemoteIODriver_ERR<< CHAOS_FORMAT("[%1%]Message field is mandatory on connection", %connection_identifier); + //AbstractRemoteIODriver_ERR<< CHAOS_FORMAT("[%1%]Message field is mandatory on connection", %connection_identifier); } else if(message->isCDataWrapperValue(MESSAGE) == false) { //error message need to be an object //EndpointType::sendError(connection_identifier, // -2, "message field need to be an object type", __PRETTY_FUNCTION__); - AbstractRemoteIODriver_ERR<< CHAOS_FORMAT("[%1%]Message field need to be an object type", %connection_identifier); + //AbstractRemoteIODriver_ERR<< CHAOS_FORMAT("[%1%]Message field need to be an object type", %connection_identifier); } else if(message->hasKey(REQUEST_IDENTIFICATION)) { //we have a request if(message->isInt32Value(REQUEST_IDENTIFICATION) == false) { //error request id need to be a int32 value //EndpointType::sendError(connection_identifier, // -3, "request_id field need to be a int32 type", __PRETTY_FUNCTION__); - AbstractRemoteIODriver_ERR<< CHAOS_FORMAT("[%1%]request_id field need to be a int32 type", %connection_identifier); + //AbstractRemoteIODriver_ERR<< CHAOS_FORMAT("[%1%]request_id field need to be a int32 type", %connection_identifier); } else { //we can forward const uint32_t req_index = message->getUInt32Value(REQUEST_IDENTIFICATION); @@ -330,7 +328,7 @@ namespace chaos { //error request id need to be a int32 value //EndpointType::sendError(connection_identifier, // -4, "msg_type field need to be a int32 type", __PRETTY_FUNCTION__); - AbstractRemoteIODriver_ERR<< CHAOS_FORMAT("[%1%]msg_type field need to be a int32 type", %connection_identifier); + //AbstractRemoteIODriver_ERR<< CHAOS_FORMAT("[%1%]msg_type field need to be a int32 type", %connection_identifier); } asyncMessageReceived(ChaosMoveOperator(message)); } else { @@ -338,7 +336,7 @@ namespace chaos { //error request id need to be a int32 value //EndpointType::sendError(connection_identifier, // -5, "remote layer side message lack of msg_type key", __PRETTY_FUNCTION__); - AbstractRemoteIODriver_ERR<< CHAOS_FORMAT("[%1%]remote layer side message lack of msg_type key", %connection_identifier); + //AbstractRemoteIODriver_ERR<< CHAOS_FORMAT("[%1%]remote layer side message lack of msg_type key", %connection_identifier); } return 0; } @@ -354,13 +352,11 @@ namespace chaos { int err = 0; switch(conn_phase) { case RDConnectionPhaseDisconnected: - AbstractRemoteIODriver_DBG<<" RDConnectionPhaseDisconnected"; - + //AbstractRemoteIODriver_DBG<<" RDConnectionPhaseDisconnected"; return AR_ERROR_NO_CONNECTION; break; case RDConnectionPhaseConnected: { - AbstractRemoteIODriver_DBG<<" Connected..."; - + //AbstractRemoteIODriver_DBG<<" Connected..."; CHAOS_ASSERT(current_connection_identifier().size()); if(authorization_key.size() != 0) { err = AR_ERROR_NOT_AUTORIZED; @@ -371,32 +367,33 @@ namespace chaos { } case RDConnectionPhaseManageAutorization: { - chaos::common::data::CDWShrdPtr message_response; - chaos::common::data::CDWUniquePtr auth_ack_data(new chaos::common::data::CDataWrapper()); - AbstractRemoteIODriver_DBG<<" Connection OK, authorizing..."; - - auth_ack_data->addStringValue(AUTHORIZATION_KEY, authorization_key); - if((err = _sendRawOpcodeRequest(remote_uri, - "auth", - ChaosMoveOperator(auth_ack_data), - message_response)) ==0 ){ - if(checkAuthenticationState(message_response)) { - conn_phase = RDConnectionPhaseAutorized; - } else { - AbstractRemoteIODriver_ERR<<" Authorization Fails, cannot configure"; - - err = AR_ERROR_NOT_AUTORIZED; - break; + if(authorization_key.size() != 0) { + chaos::common::data::CDWShrdPtr message_response; + chaos::common::data::CDWUniquePtr auth_ack_data(new chaos::common::data::CDataWrapper()); + //AbstractRemoteIODriver_DBG<<" Connection OK, authorizing..."; + + auth_ack_data->addStringValue(AUTHORIZATION_KEY, authorization_key); + if((err = _sendRawOpcodeRequest(remote_uri, + "auth", + ChaosMoveOperator(auth_ack_data), + message_response)) ==0 ){ + if(checkAuthenticationState(message_response)) { + conn_phase = RDConnectionPhaseAutorized; + } else { + //AbstractRemoteIODriver_ERR<<" Authorization Fails, cannot configure"; + err = AR_ERROR_NOT_AUTORIZED; + break; + } } + } else { + conn_phase = RDConnectionPhaseAutorized; } - } case RDConnectionPhaseAutorized: { chaos::common::data::CDWUniquePtr conf_msg(driver_init_pack->clone()); chaos::common::data::CDWShrdPtr message_response; - AbstractRemoteIODriver_DBG<<" Authorization OK, configuring..."; - + //AbstractRemoteIODriver_DBG<<" Authorization OK, configuring..."; if((err = _sendRawOpcodeRequest((remote_uri_instance.size()?remote_uri_instance:remote_uri), "init", ChaosMoveOperator(conf_msg), @@ -404,8 +401,7 @@ namespace chaos { if(checkConfigurationState(message_response)) { conn_phase = RDConnectionPhaseConfigured; } else { - AbstractRemoteIODriver_ERR<<" Init Fails, Not Configured"; - + //AbstractRemoteIODriver_ERR<<" Init Fails, Not Configured"; err = AR_ERROR_NOT_CONFIGURED; break; } @@ -414,8 +410,7 @@ namespace chaos { case RDConnectionPhaseConfigured: //we can proceeed - AbstractRemoteIODriver_DBG<<" Configuration OK!, start working"; - + //AbstractRemoteIODriver_DBG<<" Configuration OK!, start working"; break; } return err; @@ -431,7 +426,7 @@ namespace chaos { opcpde_msg->addStringValue(MESSAGE_URI, uri); opcpde_msg->addStringValue(MESSAGE_OPCODE, opcode); opcpde_msg->addCSDataValue(MESSAGE_OPCODE_PARAMETER, *message_data); - AbstractRemoteIODriver_DBG<<"sending "+uri+" opcode:"<<opcode<<" msg:"<<message_data->getCompliantJSONString(); + //AbstractRemoteIODriver_DBG<<"sending "+uri+" opcode:"<<opcode<<" msg:"<<message_data->getCompliantJSONString(); return _sendRawRequest(ChaosMoveOperator(opcpde_msg), message_response, timeout); @@ -465,7 +460,7 @@ namespace chaos { message_response = request_future.get(); return AR_ERROR_OK; } else { - AbstractRemoteIODriver_ERR<<" Timeout Arised!"; + //AbstractRemoteIODriver_ERR<<" Timeout Arised!"; return AR_ERROR_TIMEOUT; } } diff --git a/chaos_micro_unit_toolkit/CMakeLists.txt b/chaos_micro_unit_toolkit/CMakeLists.txt index bd5f611f2aabd08135e4d1c283b202a2fbd112df..37f34fb46bfe554a37ddc86b0b14d9c9d5127659 100644 --- a/chaos_micro_unit_toolkit/CMakeLists.txt +++ b/chaos_micro_unit_toolkit/CMakeLists.txt @@ -11,8 +11,8 @@ SET(source ${source} connection/connection_adapter/AbstractConnectionAdapter.cpp connection/connection_adapter/http/HTTPConnectionAdapter.cpp) SET(source ${source} connection/unit_proxy/AbstractUnitProxy.cpp connection/unit_proxy/UnitProxyHandlerWrapper.cpp) -SET(source ${source} connection/unit_proxy/raw_driver/RawDriverUnitProxy.cpp - connection/unit_proxy/raw_driver/RawDriverHandlerWrapper.cpp) +SET(source ${source} connection/unit_proxy/raw_driver/ExternalDriverUnitProxy.cpp + connection/unit_proxy/raw_driver/ExternalDriverHandlerWrapper.cpp) SET(source ${source} external_lib/bson/bson/bson.c external_lib/bson/bson/bson-atomic.c diff --git a/chaos_micro_unit_toolkit/ChaosMicroUnitToolkit.cpp b/chaos_micro_unit_toolkit/ChaosMicroUnitToolkit.cpp index 7ee5a7fcd2380b8063a1a2d6797c6fac7ce5d925..c188adde43ae53e768b6ef35b3cb85f9aba816a9 100644 --- a/chaos_micro_unit_toolkit/ChaosMicroUnitToolkit.cpp +++ b/chaos_micro_unit_toolkit/ChaosMicroUnitToolkit.cpp @@ -36,7 +36,7 @@ ChaosUniquePtr<connection::connection_adapter::AbstractConnectionAdapter> ChaosM protocol_option); } -ChaosUniquePtr<raw_driver::RawDriverHandlerWrapper> ChaosMicroUnitToolkit::createNewRawDriverHandlerWrapper(connection::ConnectionType protocol_type, +ChaosUniquePtr<raw_driver::ExternalDriverHandlerWrapper> ChaosMicroUnitToolkit::createNewExternalDriverHandlerWrapper(connection::ConnectionType protocol_type, const std::string& protocol_endpoint, const std::string& protocol_option, connection::unit_proxy::UnitProxyHandler handler, @@ -45,11 +45,11 @@ ChaosUniquePtr<raw_driver::RawDriverHandlerWrapper> ChaosMicroUnitToolkit::creat ChaosUniquePtr<connection_adapter::AbstractConnectionAdapter> protocol_adater = cman.getProtocolAdapter(protocol_type, protocol_endpoint, protocol_option); - ChaosUniquePtr<raw_driver::RawDriverUnitProxy> unit_proxy = ChaosUniquePtr<raw_driver::RawDriverUnitProxy>(static_cast< raw_driver::RawDriverUnitProxy* >(cman.getUnitProxy(raw_driver::RawDriverUnitProxy::proxy_type, + ChaosUniquePtr<raw_driver::ExternalDriverUnitProxy> unit_proxy = ChaosUniquePtr<raw_driver::ExternalDriverUnitProxy>(static_cast< raw_driver::ExternalDriverUnitProxy* >(cman.getUnitProxy(raw_driver::ExternalDriverUnitProxy::proxy_type, + authorization_key, protocol_adater).release())); - return ChaosUniquePtr<raw_driver::RawDriverHandlerWrapper>(new raw_driver::RawDriverHandlerWrapper(handler, + return ChaosUniquePtr<raw_driver::ExternalDriverHandlerWrapper>(new raw_driver::ExternalDriverHandlerWrapper(handler, user_data, - authorization_key, unit_proxy)); } diff --git a/chaos_micro_unit_toolkit/ChaosMicroUnitToolkit.h b/chaos_micro_unit_toolkit/ChaosMicroUnitToolkit.h index f85183e2b52db216691ec2da138a56d817cb530d..1bcd274853dd60092d33db2111ad973dda526497 100644 --- a/chaos_micro_unit_toolkit/ChaosMicroUnitToolkit.h +++ b/chaos_micro_unit_toolkit/ChaosMicroUnitToolkit.h @@ -39,7 +39,7 @@ namespace chaos { const std::string& protocol_endpoint, const std::string& protocol_option); - ChaosUniquePtr<connection::unit_proxy::raw_driver::RawDriverHandlerWrapper> createNewRawDriverHandlerWrapper(connection::ConnectionType protocol_type, + ChaosUniquePtr<connection::unit_proxy::raw_driver::ExternalDriverHandlerWrapper> createNewExternalDriverHandlerWrapper(connection::ConnectionType protocol_type, const std::string& protocol_endpoint, const std::string& protocol_option, connection::unit_proxy::UnitProxyHandler handler, diff --git a/chaos_micro_unit_toolkit/connection/ConnectionManager.cpp b/chaos_micro_unit_toolkit/connection/ConnectionManager.cpp index 44345fb2cb49cdeada7daa1f128083e33b3e3e56..6325180335c36c123c2dcbbc111c972390edac12 100644 --- a/chaos_micro_unit_toolkit/connection/ConnectionManager.cpp +++ b/chaos_micro_unit_toolkit/connection/ConnectionManager.cpp @@ -25,7 +25,7 @@ #include <chaos_micro_unit_toolkit/connection/connection_adapter/http/HTTPConnectionAdapter.h> //unit -#include <chaos_micro_unit_toolkit/connection/unit_proxy/raw_driver/RawDriverUnitProxy.h> +#include <chaos_micro_unit_toolkit/connection/unit_proxy/raw_driver/ExternalDriverUnitProxy.h> using namespace chaos::micro_unit_toolkit; using namespace chaos::micro_unit_toolkit::connection; @@ -33,7 +33,7 @@ using namespace chaos::micro_unit_toolkit::connection::unit_proxy; using namespace chaos::micro_unit_toolkit::connection::connection_adapter; ConnectionManager::ConnectionManager() { - registerUnitProxy<raw_driver::RawDriverUnitProxy>(); + registerUnitProxy<raw_driver::ExternalDriverUnitProxy>(); registerProtocolAdapter<http::HTTPConnectionAdapter>(); } @@ -49,8 +49,9 @@ ChaosUniquePtr<AbstractConnectionAdapter> ConnectionManager::getProtocolAdapter( } ChaosUniquePtr<AbstractUnitProxy> ConnectionManager::getUnitProxy(ProxyType type, + const std::string& authorization_key, ChaosUniquePtr<connection_adapter::AbstractConnectionAdapter>& connection_adapter){ if(map_proxy.count(type) == 0){ return ChaosUniquePtr<unit_proxy::AbstractUnitProxy>();} UnitProxyInstancer is = map_proxy[type]; - return ChaosUniquePtr<unit_proxy::AbstractUnitProxy>(is->getInstance(connection_adapter)); + return ChaosUniquePtr<unit_proxy::AbstractUnitProxy>(is->getInstance(authorization_key, connection_adapter)); } diff --git a/chaos_micro_unit_toolkit/connection/ConnectionManager.h b/chaos_micro_unit_toolkit/connection/ConnectionManager.h index d141ee24fb309471ee88f8c2847e89795127fc44..1a111b39beb84a90942ab0232c6bf863b3b10fc1 100644 --- a/chaos_micro_unit_toolkit/connection/ConnectionManager.h +++ b/chaos_micro_unit_toolkit/connection/ConnectionManager.h @@ -40,7 +40,7 @@ namespace chaos { class ConnectionManager { friend class chaos::micro_unit_toolkit::ChaosMicroUnitToolkit; - typedef ChaosSharedPtr< ObjectInstancerP1<unit_proxy::AbstractUnitProxy, ChaosUniquePtr<connection_adapter::AbstractConnectionAdapter>& > > UnitProxyInstancer; + typedef ChaosSharedPtr< ObjectInstancerP2<unit_proxy::AbstractUnitProxy, const std::string&, ChaosUniquePtr<connection_adapter::AbstractConnectionAdapter>& > > UnitProxyInstancer; typedef std::map<ProxyType, UnitProxyInstancer > MapProxy; typedef ChaosSharedPtr< ObjectInstancerP2<connection_adapter::AbstractConnectionAdapter, std::string, std::string> > ProtocolAdapterInstancer; @@ -62,8 +62,9 @@ namespace chaos { template<typename T> void registerUnitProxy() { - map_proxy.insert(std::make_pair(T::proxy_type, UnitProxyInstancer(new TypedObjectInstancerP1<T, + map_proxy.insert(std::make_pair(T::proxy_type, UnitProxyInstancer(new TypedObjectInstancerP2<T, unit_proxy::AbstractUnitProxy, + const std::string&, ChaosUniquePtr<connection_adapter::AbstractConnectionAdapter>&>()))); } @@ -73,6 +74,7 @@ namespace chaos { const std::string& protocol_option); ChaosUniquePtr<unit_proxy::AbstractUnitProxy> getUnitProxy(ProxyType type, + const std::string& authorization_key, ChaosUniquePtr<connection_adapter::AbstractConnectionAdapter> &connection_adapter); }; } diff --git a/chaos_micro_unit_toolkit/connection/connection.h b/chaos_micro_unit_toolkit/connection/connection.h index bb78f0fc92b47c0ece7be63a26e4b4beb6d3328b..8abf57cef4acb5bdfa9d9bb73472ddb1c5d8b43f 100644 --- a/chaos_micro_unit_toolkit/connection/connection.h +++ b/chaos_micro_unit_toolkit/connection/connection.h @@ -29,6 +29,6 @@ #include <chaos_micro_unit_toolkit/connection/connection_adapter/http/HTTPConnectionAdapter.h> #include <chaos_micro_unit_toolkit/connection/unit_proxy/AbstractUnitProxy.h> -#include <chaos_micro_unit_toolkit/connection/unit_proxy/raw_driver/RawDriverUnitProxy.h> -#include <chaos_micro_unit_toolkit/connection/unit_proxy/raw_driver/RawDriverHandlerWrapper.h> +#include <chaos_micro_unit_toolkit/connection/unit_proxy/raw_driver/ExternalDriverUnitProxy.h> +#include <chaos_micro_unit_toolkit/connection/unit_proxy/raw_driver/ExternalDriverHandlerWrapper.h> #endif /* __CHAOSFramework__45EFE2C_AE25_43D8_B40A_C3070A610391_connection_h */ diff --git a/chaos_micro_unit_toolkit/connection/connection_adapter/AbstractConnectionAdapter.h b/chaos_micro_unit_toolkit/connection/connection_adapter/AbstractConnectionAdapter.h index 2767dffa20fefbeda47f7e1af3b0a955712e4582..8831cf8c617d1b657d0d86e720ed7ba451ec5d44 100644 --- a/chaos_micro_unit_toolkit/connection/connection_adapter/AbstractConnectionAdapter.h +++ b/chaos_micro_unit_toolkit/connection/connection_adapter/AbstractConnectionAdapter.h @@ -46,8 +46,6 @@ namespace chaos { ConnectionStateAccepted, //!connection has not been accepted by remote endpoint ConnectionStateNotAccepted, - //!the remote endpoint has been autorized - ConnectionStateAuthorized, //!the is an error on connection ConnectionStateConnectionError } ConnectionState; diff --git a/chaos_micro_unit_toolkit/connection/unit_proxy/AbstractUnitProxy.cpp b/chaos_micro_unit_toolkit/connection/unit_proxy/AbstractUnitProxy.cpp index d5ff8847da309e1c934c143a1b81a1de725e01d5..442a7613ccd335122b7706888fb578c7a1698883 100644 --- a/chaos_micro_unit_toolkit/connection/unit_proxy/AbstractUnitProxy.cpp +++ b/chaos_micro_unit_toolkit/connection/unit_proxy/AbstractUnitProxy.cpp @@ -26,36 +26,37 @@ using namespace chaos::micro_unit_toolkit::data; using namespace chaos::micro_unit_toolkit::connection::unit_proxy; using namespace chaos::micro_unit_toolkit::connection::connection_adapter; -RemoteMessage::RemoteMessage(const CDWShrdPtr& _message): -message(_message), -is_request((message->hasKey("req_id") && message->isInt32Value("req_id"))), -message_id(is_request?message->getInt32Value("req_id"):0){ - if(is_request && - message->hasKey("msg") && - message->isCDWValue("msg")) { - request_message.reset(message->getCDWValue("msg").release()); +RemoteMessage::RemoteMessage(const CDWShrdPtr& _remote_message): +remote_message(_remote_message), +is_request((remote_message->hasKey(REQUEST_KEY) && remote_message->isInt32Value(REQUEST_KEY))), +message_id(is_request?remote_message->getInt32Value(REQUEST_KEY):0){ + if(remote_message->hasKey(MSG_KEY) && + remote_message->isCDWValue(MSG_KEY)) { + message.reset(remote_message->getCDWValue(MSG_KEY).release()); } } bool RemoteMessage::isError() const { - return message.get() && message->hasKey("error_code"); + return message.get() && message->hasKey(ERR_CODE); } int32_t RemoteMessage::getErrorCode() const { - return message.get()?message->getInt32Value("error_code"):0; + return message.get()?message->getInt32Value(ERR_CODE):0; } std::string RemoteMessage::getErrorMessage() const { - return message.get()?message->getStringValue("error_message"):""; + return message.get()?message->getStringValue(ERR_DOM):""; } std::string RemoteMessage::getErrorDomain() const { return message.get()?message->getStringValue("error_domain"):""; } -AbstractUnitProxy::AbstractUnitProxy(ChaosUniquePtr<connection_adapter::AbstractConnectionAdapter>& _protocol_adapter): +AbstractUnitProxy::AbstractUnitProxy(const std::string& _authorization_key, + ChaosUniquePtr<connection_adapter::AbstractConnectionAdapter>& _protocol_adapter): connection_adapter(ChaosMoveOperator(_protocol_adapter)), -unit_state(UnitStateUnknown){assert(connection_adapter.get());} +unit_state(UnitStateUnknown), +authorization_key(_authorization_key){assert(connection_adapter.get());} AbstractUnitProxy::~AbstractUnitProxy() { std::cout <<"exit"; @@ -71,7 +72,8 @@ bool AbstractUnitProxy::hasMoreMessage() { RemoteMessageUniquePtr AbstractUnitProxy::getNextMessage() { if(connection_adapter->hasMoreMessage() == false) return RemoteMessageUniquePtr(); - RemoteMessageUniquePtr next_message(new RemoteMessage(connection_adapter->getNextMessage())); + data::CDWShrdPtr next_msg = connection_adapter->getNextMessage(); + RemoteMessageUniquePtr next_message(new RemoteMessage(next_msg)); return next_message; } @@ -98,3 +100,23 @@ const ConnectionState& AbstractUnitProxy::getConnectionState() const { void AbstractUnitProxy::resetAuthorization() { unit_state = UnitStateUnknown; } +void AbstractUnitProxy::manageAuthenticationRequest() { + bool result = false; + if(authorization_key.size()) { + if((result = hasMoreMessage())) { + //!check authentication state + RemoteMessageUniquePtr result = getNextMessage(); + if(result->message->hasKey(AUTHORIZATION_KEY) || + result->message->isStringValue(AUTHORIZATION_KEY)) { + const std::string remote_authentication_key = result->message->getStringValue(AUTHORIZATION_KEY); + if(remote_authentication_key.compare(authorization_key) == 0) { + unit_state = UnitStateAuthenticated; + } else { + unit_state = UnitStateNotAuthenticated; + } + } + } + } else { + unit_state = UnitStateAuthenticated; + } +} diff --git a/chaos_micro_unit_toolkit/connection/unit_proxy/AbstractUnitProxy.h b/chaos_micro_unit_toolkit/connection/unit_proxy/AbstractUnitProxy.h index 7509f5f281d6369fe7e5893051175c8755ac26f6..71617ea2584ed173d4834bba39f13a4e2601d862 100644 --- a/chaos_micro_unit_toolkit/connection/unit_proxy/AbstractUnitProxy.h +++ b/chaos_micro_unit_toolkit/connection/unit_proxy/AbstractUnitProxy.h @@ -25,6 +25,15 @@ #include <chaos_micro_unit_toolkit/data/DataPack.h> #include <chaos_micro_unit_toolkit/micro_unit_toolkit_types.h> #include <chaos_micro_unit_toolkit/connection/connection_adapter/AbstractConnectionAdapter.h> + +#define AUTHORIZATION_KEY "authorization_key" +#define AUTHORIZATION_STATE "authorization_state" +#define REQUEST_KEY "req_id" +#define MSG_KEY "msg" +#define ERR_CODE "err" +#define ERR_MSG "err_msg" +#define ERR_DOM "err_dmn" + namespace chaos { namespace micro_unit_toolkit { namespace connection { @@ -36,11 +45,11 @@ namespace chaos { class RemoteMessage { bool is_error; public: - data::CDWShrdPtr message; + data::CDWShrdPtr remote_message; const bool is_request; const uint32_t message_id; - data::CDWShrdPtr request_message; - RemoteMessage(const data::CDWShrdPtr& _message); + data::CDWShrdPtr message; + RemoteMessage(const data::CDWShrdPtr& _remote_message); bool isError() const; int32_t getErrorCode() const; @@ -51,8 +60,7 @@ namespace chaos { typedef enum { UnitStateUnknown, UnitStateNotAuthenticated, - UnitStateAuthenticated, - UnitStateConfigured + UnitStateAuthenticated } UnitState; typedef ChaosUniquePtr<RemoteMessage> RemoteMessageUniquePtr; @@ -63,6 +71,7 @@ namespace chaos { ChaosUniquePtr<connection_adapter::AbstractConnectionAdapter> connection_adapter; protected: UnitState unit_state; + const std::string authorization_key; virtual int sendMessage(data::CDWUniquePtr& message_data); bool hasMoreMessage(); @@ -74,17 +83,14 @@ namespace chaos { void poll(int32_t milliseconds_wait = 100); int close(); + + void manageAuthenticationRequest(); public: - AbstractUnitProxy(ChaosUniquePtr<connection_adapter::AbstractConnectionAdapter>& _protocol_adapter); + AbstractUnitProxy(const std::string& _authorization_key, + ChaosUniquePtr<connection_adapter::AbstractConnectionAdapter>& _protocol_adapter); virtual ~AbstractUnitProxy(); - //! need to be called once per connection pool for manage the autorization untile it return true - /*! - when this function is completed(return true) connection state need to be tested - */ - virtual void manageAuthenticationRequest() = 0; - const UnitState& getUnitState() const; const connection_adapter::ConnectionState& getConnectionState() const; diff --git a/chaos_micro_unit_toolkit/connection/unit_proxy/UnitProxyHandlerWrapper.h b/chaos_micro_unit_toolkit/connection/unit_proxy/UnitProxyHandlerWrapper.h index 56dbe7d9a42428aef5e8396e35f008afa10a74b0..21ca150fb644b121446129df1415c309e0ae4e1c 100644 --- a/chaos_micro_unit_toolkit/connection/unit_proxy/UnitProxyHandlerWrapper.h +++ b/chaos_micro_unit_toolkit/connection/unit_proxy/UnitProxyHandlerWrapper.h @@ -27,20 +27,26 @@ #include <chaos_micro_unit_toolkit/connection/unit_proxy/AbstractUnitProxy.h> #include <chaos_micro_unit_toolkit/connection/connection_adapter/AbstractConnectionAdapter.h> +#define RETRY_TIME 5 + +#define UP_EV_USR_ACTION 0 /* user can take here his action event_data is the pointer to the handler warapepr instance that manage the handler function */ +#define UP_EV_CONN_CONNECTED 1 /* connection to remote host has been done */ +#define UP_EV_CONN_ERROR 3 /* there was and error on connection */ +#define UP_EV_CONN_DISCONNECTED 4 /* connection to remote host has been closed */ +#define UP_EV_CONN_ACCEPTED 5 /* connection has been accepted */ +#define UP_EV_CONN_REJECTED 6 /* connection has been rejexted */ +#define UP_EV_CONN_RETRY 7 /* retry to reconnect event_data point to and unsigned integer that is the current waiting time for retry */ +#define UP_EV_AUTH_ACCEPTED 8 /* unit has been accepted autorization key*/ +#define UP_EV_AUTH_REJECTED 9 /* unit has rejected the autorization key */ +#define UP_EV_MSG_RECEIVED 10 /* new message has been received, event_data point to UPMessage*, memory is valido only during call*/ +#define UP_EV_REQ_RECEIVED 11 /* new request has been received, event_data point to UPRequest*, memory is valido only during call*/ +#define UP_EV_ERR_RECEIVED 12 /* error message has been received, event_data point to UPError*, memory is valido only during call*/ + namespace chaos { namespace micro_unit_toolkit { namespace connection { namespace unit_proxy { - struct UPMessage { - data::CDWShrdPtr message; - }; - - struct UPRequest { - data::CDWShrdPtr message; - data::CDWUniquePtr response; - }; - struct UPError { const int error; const std::string message; @@ -53,20 +59,6 @@ namespace chaos { */ typedef int (*UnitProxyHandler)(void *usr_data, unsigned int event, void *event_data); -#define RETRY_TIME 5 - -#define UP_EV_USR_ACTION 0 /* user can take here his action event_data is the pointer to the handler warapepr instance that manage the handler function */ -#define UP_EV_CONN_CONNECTED 1 /* connection to remote host has been done */ -#define UP_EV_CONN_ERROR 3 /* there was and error on connection */ -#define UP_EV_CONN_DISCONNECTED 4 /* connection to remote host has been closed */ -#define UP_EV_CONN_ACCEPTED 5 /* connection has been accepted */ -#define UP_EV_CONN_REJECTED 6 /* connection has been rejexted */ -#define UP_EV_CONN_RETRY 7 /* retry to reconnect event_data point to and unsigned integer that is the current waiting time for retry */ -#define UP_EV_AUTH_ACCEPTED 8 /* unit has been accepted autorization key*/ -#define UP_EV_AUTH_REJECTED 9 /* unit has rejected the autorization key */ -#define UP_EV_MSG_RECEIVED 10 /* new message has been received, event_data point to UPMessage*, memory is valido only during call*/ -#define UP_EV_REQ_RECEIVED 11 /* new request has been received, event_data point to UPRequest*, memory is valido only during call*/ -#define UP_EV_ERR_RECEIVED 12 /* error message has been received, event_data point to UPError*, memory is valido only during call*/ //Base class use a function as handler for all event /*! this is a base class that handle all necessary operation to keep connection and guide user @@ -87,8 +79,8 @@ namespace chaos { virtual int unitEventLoop() = 0; public: UnitProxyHandlerWrapper(UnitProxyHandler _handler, - void *_user_data, - ChaosUniquePtr<AbstractUnitProxy> _base_unit); + void *_user_data, + ChaosUniquePtr<AbstractUnitProxy> _base_unit); virtual ~UnitProxyHandlerWrapper(); diff --git a/chaos_micro_unit_toolkit/connection/unit_proxy/raw_driver/ExternalDriverHandlerWrapper.cpp b/chaos_micro_unit_toolkit/connection/unit_proxy/raw_driver/ExternalDriverHandlerWrapper.cpp new file mode 100644 index 0000000000000000000000000000000000000000..062fc333c0d077385a9993c5b97918eedb7865fc --- /dev/null +++ b/chaos_micro_unit_toolkit/connection/unit_proxy/raw_driver/ExternalDriverHandlerWrapper.cpp @@ -0,0 +1,167 @@ +/* + * Copyright 2012, 2017 INFN + * + * Licensed under the EUPL, Version 1.2 or – as soon they + * will be approved by the European Commission - subsequent + * versions of the EUPL (the "Licence"); + * You may not use this work except in compliance with the + * Licence. + * You may obtain a copy of the Licence at: + * + * https://joinup.ec.europa.eu/software/page/eupl + * + * Unless required by applicable law or agreed to in + * writing, software distributed under the Licence is + * distributed on an "AS IS" basis, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either + * express or implied. + * See the Licence for the specific language governing + * permissions and limitations under the Licence. + */ + +#include <chaos_micro_unit_toolkit/connection/unit_proxy/raw_driver/ExternalDriverHandlerWrapper.h> +#include <iostream> +#include <cassert> +using namespace chaos::micro_unit_toolkit::data; +using namespace chaos::micro_unit_toolkit::connection; +using namespace chaos::micro_unit_toolkit::connection::unit_proxy::raw_driver; + + +#define MSG_KEY "msg" +#define OPCODE_KEY "opc" +#define OPCODE_PARAMETER_KEY "par" +#define INITIALIZATION_URI "uri" +#define INIT_OPCODE "init" +#define DEINIT_OPCODE "deinit" + +ExternalDriverHandlerWrapper::ExternalDriverHandlerWrapper(UnitProxyHandler handler, + void *user_data, + ChaosUniquePtr<ExternalDriverUnitProxy>& _u_proxy): +UnitProxyHandlerWrapper(handler, + user_data, +#if __cplusplus >= 201103L + ChaosMoveOperator(_u_proxy) +#else + ChaosMoveOperator(ChaosUniquePtr<AbstractUnitProxy>(_u_proxy)) +#endif + ), +unit_state(base_unit->getUnitState()), +authorized(false){} + +ExternalDriverHandlerWrapper::~ExternalDriverHandlerWrapper(){} + +int ExternalDriverHandlerWrapper::sendMessage(data::CDWUniquePtr& message_data) { + ExternalDriverUnitProxy * const rd = static_cast<ExternalDriverUnitProxy*>(base_unit.get()); + return rd->sendMessage(message_data); +} + +int ExternalDriverHandlerWrapper::unitEventLoop() { + int err = 0; + switch (unit_state) { + case UnitStateUnknown: + authorized = false; + static_cast<ExternalDriverUnitProxy*>(base_unit.get())->manageAuthenticationRequest(); + break; + case UnitStateNotAuthenticated: + err = callHandler(UP_EV_AUTH_REJECTED, NULL); + break; + case UnitStateAuthenticated: + if(authorized == false){ + err = callHandler(UP_EV_AUTH_ACCEPTED, NULL); + authorized = true; + } + //manage request by remote user + err = manageRemoteMessage(); + //perform idle operation + if(!err) { + //call handler for user action passing the instace as public interface + err = callHandler(UP_EV_USR_ACTION, this); + } + break; + } + return err; +} + +int ExternalDriverHandlerWrapper::manageRemoteMessage() { + int err = 0; + bool init_opcode = false; + bool deinit_opcode = false; + ExternalDriverUnitProxy * const rd = static_cast<ExternalDriverUnitProxy*>(base_unit.get()); + RemoteMessageUniquePtr remote_message; + while(rd->hasMoreMessage()) { + remote_message = rd->getNextMessage(); + if(remote_message->message.get() == NULL) return -1; + + std::string uri; + std::string opcode; + data::CDWShrdPtr r_msg; + if(remote_message->message->hasKey(OPCODE_KEY) && + remote_message->message->isStringValue(OPCODE_KEY)){ + opcode = remote_message->message->getStringValue(OPCODE_KEY); + } else { + return -2; + } + + if(remote_message->message->hasKey(INITIALIZATION_URI) && + remote_message->message->isStringValue(INITIALIZATION_URI)){ + uri = remote_message->message->getStringValue(INITIALIZATION_URI); + } else { + return -3; + } + + if(remote_message->message->hasKey(OPCODE_PARAMETER_KEY) && + remote_message->message->isCDWValue(OPCODE_PARAMETER_KEY)){ + r_msg = remote_message->message->getCDWValue(OPCODE_PARAMETER_KEY); + } + //compose message and requests + EDMessage req_message = {uri, opcode, r_msg}; + //check if we have and init msg + if((init_opcode = opcode.compare(INIT_OPCODE) == 0)) { + EDInitRequest init_request = {req_message,{}}; + if((err = callHandler(UP_EV_INIT_RECEIVED, + &init_request)) == 0) { + CDWUniquePtr response_msg(new DataPack()); + // response_msg->addInt32Value(ERR_CODE, req.response.error_code); + // if(req.response.error_message.size()) { + // response_msg->addStringValue(ERR_MSG, req.response.error_message); + // } + // if(req.response.error_domain.size()) { + // response_msg->addStringValue(ERR_MSG, req.response.error_message); + // } + rd->sendAnswer(remote_message, response_msg); + } + } else if((deinit_opcode = opcode.compare(DEINIT_OPCODE) == 0)) { + EDDeinitRequest deinit_request = {req_message,{}}; + if((err = callHandler(UP_EV_DEINIT_RECEIVED, + &deinit_request)) == 0) { + CDWUniquePtr response_msg(new DataPack()); + // response_msg->addInt32Value(ERR_CODE, req.response.error_code); + // if(req.response.error_message.size()) { + // response_msg->addStringValue(ERR_MSG, req.response.error_message); + // } + // if(req.response.error_domain.size()) { + // response_msg->addStringValue(ERR_MSG, req.response.error_message); + // } + rd->sendAnswer(remote_message, response_msg); + } + } else if(remote_message->is_request) { + EDNormalRequest normal_request = {req_message,{}}; + if((err = callHandler(UP_EV_REQ_RECEIVED, + &normal_request)) == 0) { + CDWUniquePtr response_msg(new DataPack()); + // response_msg->addInt32Value(ERR_CODE, req.response.error_code); + // if(req.response.error_message.size()) { + // response_msg->addStringValue(ERR_MSG, req.response.error_message); + // } + // if(req.response.error_domain.size()) { + // response_msg->addStringValue(ERR_MSG, req.response.error_message); + // } + rd->sendAnswer(remote_message, response_msg); + } + } else { + err = callHandler(UP_EV_MSG_RECEIVED, &req_message); + } + + } + return err; +} diff --git a/chaos_micro_unit_toolkit/connection/unit_proxy/raw_driver/ExternalDriverHandlerWrapper.h b/chaos_micro_unit_toolkit/connection/unit_proxy/raw_driver/ExternalDriverHandlerWrapper.h new file mode 100644 index 0000000000000000000000000000000000000000..4f0d5e7b17943fc5ed537dfaf55dda98cf369f16 --- /dev/null +++ b/chaos_micro_unit_toolkit/connection/unit_proxy/raw_driver/ExternalDriverHandlerWrapper.h @@ -0,0 +1,124 @@ +/* + * Copyright 2012, 2017 INFN + * + * Licensed under the EUPL, Version 1.2 or – as soon they + * will be approved by the European Commission - subsequent + * versions of the EUPL (the "Licence"); + * You may not use this work except in compliance with the + * Licence. + * You may obtain a copy of the Licence at: + * + * https://joinup.ec.europa.eu/software/page/eupl + * + * Unless required by applicable law or agreed to in + * writing, software distributed under the Licence is + * distributed on an "AS IS" basis, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either + * express or implied. + * See the Licence for the specific language governing + * permissions and limitations under the Licence. + */ + +#ifndef __CHAOSFramework_B96F52E0_9934_48BA_BC70_301A02449FF7_ExternalDriverHandlerWrapper_h +#define __CHAOSFramework_B96F52E0_9934_48BA_BC70_301A02449FF7_ExternalDriverHandlerWrapper_h + +#include <chaos_micro_unit_toolkit/connection/unit_proxy/UnitProxyHandlerWrapper.h> +#include <chaos_micro_unit_toolkit/connection/unit_proxy/raw_driver/ExternalDriverUnitProxy.h> + +#define UP_EV_INIT_RECEIVED 13 /* */ +#define UP_EV_DEINIT_RECEIVED 14 /* */ + +namespace chaos { + namespace micro_unit_toolkit { + namespace connection { + namespace unit_proxy { + namespace raw_driver { + + //!difine an external driver message + struct EDMessage { + //!driver instance identification + const std::string uri; + //!driver opcode + const std::string opcode; + //!opcode paramter + data::CDWShrdPtr opcode_par; + }; + + //!define an external driver response + struct EDResponse { + int error_code; + std::string error_message; + std::string error_domain; + + EDResponse(): + error_code(0), + error_message(), + error_domain(){} + }; + + + //!Define the aswer for init opcode + struct EDInitResponse: + public EDResponse { + //!is the result of the configuration opration(0-not configured, 1-configured) + int32_t configuration_state; + //!is the unique id asscoiated to the configuration instances + /*! + This field must to be used to identified the isntance od the driver., can be composed also suing this + alghoritm (uri/#instance) + */ + std::string new_uri_id; + + EDInitResponse(): + configuration_state(0), + new_uri_id(){} + }; + + struct EDReqResponse: + public EDResponse { + data::CDWShrdPtr message; + EDReqResponse(): + EDResponse(), + message(new data::DataPack()){} + }; + + //!identify an external driver request + struct EDInitRequest { + EDMessage message; + EDInitResponse response; + }; + + struct EDNormalRequest { + EDMessage message; + EDReqResponse response; + }; + + struct EDDeinitRequest { + EDMessage message; + EDResponse response; + }; + + class ExternalDriverHandlerWrapper: + public UnitProxyHandlerWrapper { + const UnitState& unit_state; + bool authorized = false; + protected: + int unitEventLoop(); + int manageRemoteMessage(); + public: + ExternalDriverHandlerWrapper(UnitProxyHandler handler, + void *user_data, + ChaosUniquePtr<ExternalDriverUnitProxy>& _u_proxy); + ~ExternalDriverHandlerWrapper(); + + //! send spontaneus message to the remote raw driver layer + int sendMessage(data::CDWUniquePtr& message_data); + }; + + } + } + } + } +} + +#endif /* __CHAOSFramework_B96F52E0_9934_48BA_BC70_301A02449FF7_ExternalDriverHandlerWrapper_h */ diff --git a/chaos_micro_unit_toolkit/connection/unit_proxy/raw_driver/RawDriverUnitProxy.cpp b/chaos_micro_unit_toolkit/connection/unit_proxy/raw_driver/ExternalDriverUnitProxy.cpp similarity index 51% rename from chaos_micro_unit_toolkit/connection/unit_proxy/raw_driver/RawDriverUnitProxy.cpp rename to chaos_micro_unit_toolkit/connection/unit_proxy/raw_driver/ExternalDriverUnitProxy.cpp index d7a4a22898465100f6b28563aea6dd3ae96e49eb..5e137ed369da6e0e54f897c5298de217d7ec08fe 100644 --- a/chaos_micro_unit_toolkit/connection/unit_proxy/raw_driver/RawDriverUnitProxy.cpp +++ b/chaos_micro_unit_toolkit/connection/unit_proxy/raw_driver/ExternalDriverUnitProxy.cpp @@ -19,56 +19,34 @@ * permissions and limitations under the Licence. */ -#include <chaos_micro_unit_toolkit/connection/unit_proxy/raw_driver/RawDriverUnitProxy.h> +#include <chaos_micro_unit_toolkit/connection/unit_proxy/raw_driver/ExternalDriverUnitProxy.h> using namespace chaos::micro_unit_toolkit::data; using namespace chaos::micro_unit_toolkit::connection; using namespace chaos::micro_unit_toolkit::connection::unit_proxy; using namespace chaos::micro_unit_toolkit::connection::unit_proxy::raw_driver; -#define AUTHORIZATION_KEY "authorization_key" -#define AUTHORIZATION_STATE "authorization_state" #define MESSAGE "msg" #define REQUEST_IDENTIFICATION "req_id" -const ProxyType RawDriverUnitProxy::proxy_type = ProxyTypeRawDriver; +const ProxyType ExternalDriverUnitProxy::proxy_type = ProxyTypeRawDriver; -RawDriverUnitProxy::RawDriverUnitProxy(ChaosUniquePtr<connection_adapter::AbstractConnectionAdapter>& connection_adapter): -AbstractUnitProxy(connection_adapter){} +ExternalDriverUnitProxy::ExternalDriverUnitProxy(const std::string& _authorization_key, + ChaosUniquePtr<connection_adapter::AbstractConnectionAdapter>& connection_adapter): +AbstractUnitProxy(_authorization_key, +connection_adapter){} -RawDriverUnitProxy::~RawDriverUnitProxy() {} +ExternalDriverUnitProxy::~ExternalDriverUnitProxy() {} -void RawDriverUnitProxy::manageAuthenticationRequest() { - bool result = false; - if((result = hasMoreMessage())) { - //!check authentication state - RemoteMessageUniquePtr result = getNextMessage(); - if(result->message->hasKey(AUTHORIZATION_STATE) || - result->message->isInt32Value(AUTHORIZATION_STATE)) { - switch (result->message->getInt32Value(AUTHORIZATION_STATE)) { - case 0: - unit_state = UnitStateNotAuthenticated; - break; - case 1: - unit_state = UnitStateAuthenticated; - break; - default: - unit_state = UnitStateNotAuthenticated; - break; - } - - } - } -} - -int RawDriverUnitProxy::sendMessage(CDWUniquePtr& message_data) { +int ExternalDriverUnitProxy::sendMessage(CDWUniquePtr& message_data) { CDWUniquePtr message(new DataPack()); message->addCDWValue(MESSAGE, *message_data); return AbstractUnitProxy::sendMessage(message); } -int RawDriverUnitProxy::sendAnswer(RemoteMessageUniquePtr& message, - CDWUniquePtr& message_data) { +int ExternalDriverUnitProxy::sendAnswer(RemoteMessageUniquePtr& message, + CDWUniquePtr& message_data) { + if(message->is_request == false) return - 1; CDWUniquePtr answer(new DataPack()); answer->addInt32Value(REQUEST_IDENTIFICATION, message->message_id); diff --git a/chaos_micro_unit_toolkit/connection/unit_proxy/raw_driver/RawDriverUnitProxy.h b/chaos_micro_unit_toolkit/connection/unit_proxy/raw_driver/ExternalDriverUnitProxy.h similarity index 71% rename from chaos_micro_unit_toolkit/connection/unit_proxy/raw_driver/RawDriverUnitProxy.h rename to chaos_micro_unit_toolkit/connection/unit_proxy/raw_driver/ExternalDriverUnitProxy.h index c069d0df967554b5719253a207f2a42db33bd9d3..5893ddf601b569bc50911e45edc3c98f8df9ed5f 100644 --- a/chaos_micro_unit_toolkit/connection/unit_proxy/raw_driver/RawDriverUnitProxy.h +++ b/chaos_micro_unit_toolkit/connection/unit_proxy/raw_driver/ExternalDriverUnitProxy.h @@ -19,23 +19,28 @@ * permissions and limitations under the Licence. */ -#ifndef __CHAOSFramework__E639CBF_4E39_4FA4_B612_E6ED8C8410DE_RawDriverUnitProxy_h -#define __CHAOSFramework__E639CBF_4E39_4FA4_B612_E6ED8C8410DE_RawDriverUnitProxy_h +#ifndef __CHAOSFramework__E639CBF_4E39_4FA4_B612_E6ED8C8410DE_ExternalDriverUnitProxy_h +#define __CHAOSFramework__E639CBF_4E39_4FA4_B612_E6ED8C8410DE_ExternalDriverUnitProxy_h #include <chaos_micro_unit_toolkit/connection/unit_proxy/AbstractUnitProxy.h> namespace chaos { namespace micro_unit_toolkit { namespace connection { namespace unit_proxy { namespace raw_driver { + + class ExternalDriverHandlerWrapper; //! Abstract base class for all unit proxy - class RawDriverUnitProxy: + class ExternalDriverUnitProxy: public AbstractUnitProxy { + friend class ExternalDriverHandlerWrapper; + protected: + using AbstractUnitProxy::manageAuthenticationRequest; public: static const ProxyType proxy_type; - RawDriverUnitProxy(ChaosUniquePtr<connection_adapter::AbstractConnectionAdapter>& connection_adapter); - virtual ~RawDriverUnitProxy(); + ExternalDriverUnitProxy(const std::string& _authorization_key, + ChaosUniquePtr<connection_adapter::AbstractConnectionAdapter>& connection_adapter); + virtual ~ExternalDriverUnitProxy(); - void manageAuthenticationRequest(); int sendAnswer(RemoteMessageUniquePtr& message, data::CDWUniquePtr& message_data); int sendMessage(data::CDWUniquePtr& message_data); @@ -49,4 +54,4 @@ namespace chaos { } } -#endif /* __CHAOSFramework__E639CBF_4E39_4FA4_B612_E6ED8C8410DE_RawDriverUnitProxy_h */ +#endif /* __CHAOSFramework__E639CBF_4E39_4FA4_B612_E6ED8C8410DE_ExternalDriverUnitProxy_h */ diff --git a/chaos_micro_unit_toolkit/connection/unit_proxy/raw_driver/RawDriverHandlerWrapper.cpp b/chaos_micro_unit_toolkit/connection/unit_proxy/raw_driver/RawDriverHandlerWrapper.cpp deleted file mode 100644 index 3bf778cc12fcfc4b5d7fff90fc6ace900a0049c9..0000000000000000000000000000000000000000 --- a/chaos_micro_unit_toolkit/connection/unit_proxy/raw_driver/RawDriverHandlerWrapper.cpp +++ /dev/null @@ -1,111 +0,0 @@ -/* - * Copyright 2012, 2017 INFN - * - * Licensed under the EUPL, Version 1.2 or – as soon they - * will be approved by the European Commission - subsequent - * versions of the EUPL (the "Licence"); - * You may not use this work except in compliance with the - * Licence. - * You may obtain a copy of the Licence at: - * - * https://joinup.ec.europa.eu/software/page/eupl - * - * Unless required by applicable law or agreed to in - * writing, software distributed under the Licence is - * distributed on an "AS IS" basis, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either - * express or implied. - * See the Licence for the specific language governing - * permissions and limitations under the Licence. - */ - -#include <chaos_micro_unit_toolkit/connection/unit_proxy/raw_driver/RawDriverHandlerWrapper.h> -#include <iostream> -#include <cassert> -using namespace chaos::micro_unit_toolkit::data; -using namespace chaos::micro_unit_toolkit::connection; -using namespace chaos::micro_unit_toolkit::connection::unit_proxy::raw_driver; - -RawDriverHandlerWrapper::RawDriverHandlerWrapper(UnitProxyHandler handler, - void *user_data, - const std::string& _authorization_key, - ChaosUniquePtr<RawDriverUnitProxy>& _u_proxy): -UnitProxyHandlerWrapper(handler, - user_data, - #if __cplusplus >= 201103L - ChaosMoveOperator(_u_proxy) - #else - ChaosMoveOperator(ChaosUniquePtr<AbstractUnitProxy>(_u_proxy)) - #endif - ), -authorization_key(_authorization_key), -unit_state(base_unit->getUnitState()), -authorized(false){} - -RawDriverHandlerWrapper::~RawDriverHandlerWrapper(){} - -int RawDriverHandlerWrapper::sendMessage(data::CDWUniquePtr& message_data) { - RawDriverUnitProxy * const rd = static_cast<RawDriverUnitProxy*>(base_unit.get()); - return rd->sendMessage(message_data); -} - -int RawDriverHandlerWrapper::unitEventLoop() { - int err = 0; - switch (unit_state) { - case UnitStateUnknown: - authorized = false; - base_unit->manageAuthenticationRequest(); - break; - case UnitStateNotAuthenticated: - err = callHandler(UP_EV_AUTH_REJECTED, NULL); - break; - case UnitStateAuthenticated: - if(authorized == false){ - err = callHandler(UP_EV_AUTH_ACCEPTED, NULL); - authorized = true; - } - //manage configuration - break; - case UnitStateConfigured: - //manage request by remote user - err = manageRemoteMessage(); - //perform idle operation - if(!err) { - //call handler for user action passing the instace as public interface - err = callHandler(UP_EV_USR_ACTION, this); - } - break; - } - return err; -} - -int RawDriverHandlerWrapper::manageRemoteMessage() { - int err = 0; - RawDriverUnitProxy * const rd = static_cast<RawDriverUnitProxy*>(base_unit.get()); - RemoteMessageUniquePtr remote_message; - while(rd->hasMoreMessage()) { - remote_message = rd->getNextMessage(); - if(remote_message->is_request && - remote_message->request_message.get()) { - if(remote_message->request_message->hasKey("opcode") && - remote_message->request_message->isInt32Value("opcode")){ - UPRequest req = {remote_message->request_message, ChaosUniquePtr<DataPack>(new DataPack())}; - if((err = callHandler(UP_EV_REQ_RECEIVED, - &req)) == 0) { - rd->sendAnswer(remote_message, req.response); - } - } else { - //message lack of opcode key or it is not int32 - } - } else if(remote_message->isError() == false){ - UPMessage msg = {remote_message->request_message}; - err = callHandler(UP_EV_MSG_RECEIVED, &msg); - } else { - UPError err_msg = {remote_message->getErrorCode(), - remote_message->getErrorMessage(), - remote_message->getErrorDomain()}; - err = callHandler(UP_EV_ERR_RECEIVED, &err_msg); - } - } - return err; -} diff --git a/chaos_micro_unit_toolkit/connection/unit_proxy/raw_driver/RawDriverHandlerWrapper.h b/chaos_micro_unit_toolkit/connection/unit_proxy/raw_driver/RawDriverHandlerWrapper.h deleted file mode 100644 index ce6af9d4df4a24ceb2942020d86b61bb18d9abfe..0000000000000000000000000000000000000000 --- a/chaos_micro_unit_toolkit/connection/unit_proxy/raw_driver/RawDriverHandlerWrapper.h +++ /dev/null @@ -1,59 +0,0 @@ -/* - * Copyright 2012, 2017 INFN - * - * Licensed under the EUPL, Version 1.2 or – as soon they - * will be approved by the European Commission - subsequent - * versions of the EUPL (the "Licence"); - * You may not use this work except in compliance with the - * Licence. - * You may obtain a copy of the Licence at: - * - * https://joinup.ec.europa.eu/software/page/eupl - * - * Unless required by applicable law or agreed to in - * writing, software distributed under the Licence is - * distributed on an "AS IS" basis, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either - * express or implied. - * See the Licence for the specific language governing - * permissions and limitations under the Licence. - */ - -#ifndef __CHAOSFramework_B96F52E0_9934_48BA_BC70_301A02449FF7_RawDriverHandlerWrapper_h -#define __CHAOSFramework_B96F52E0_9934_48BA_BC70_301A02449FF7_RawDriverHandlerWrapper_h - -#include <chaos_micro_unit_toolkit/connection/unit_proxy/UnitProxyHandlerWrapper.h> -#include <chaos_micro_unit_toolkit/connection/unit_proxy/raw_driver/RawDriverUnitProxy.h> - -namespace chaos { - namespace micro_unit_toolkit { - namespace connection { - namespace unit_proxy { - namespace raw_driver { - - class RawDriverHandlerWrapper: - public UnitProxyHandlerWrapper { - std::string authorization_key; - const UnitState& unit_state; - bool authorized = false; - protected: - int unitEventLoop(); - int manageRemoteMessage(); - public: - RawDriverHandlerWrapper(UnitProxyHandler handler, - void *user_data, - const std::string& _authorization_key, - ChaosUniquePtr<RawDriverUnitProxy>& _u_proxy); - ~RawDriverHandlerWrapper(); - - //! send spontaneus message to the remote raw driver layer - int sendMessage(data::CDWUniquePtr& message_data); - }; - - } - } - } - } -} - -#endif /* __CHAOSFramework_B96F52E0_9934_48BA_BC70_301A02449FF7_RawDriverHandlerWrapper_h */