diff --git a/src/rbus/rbus.c b/src/rbus/rbus.c index 938c577e..ce3e9d62 100644 --- a/src/rbus/rbus.c +++ b/src/rbus/rbus.c @@ -5869,70 +5869,83 @@ rbusError_t rbus_createSession(rbusHandle_t handle, uint32_t *pSessionId) { (void)handle; rbusError_t rc = RBUS_ERROR_SUCCESS; - rbusCoreError_t err = RBUSCORE_SUCCESS; - rbusMessage response = NULL; + rbusObject_t outParams = NULL; if (pSessionId && handle) { *pSessionId = 0; - if((err = rbus_invokeRemoteMethod(RBUS_SMGR_DESTINATION_NAME, RBUS_SMGR_METHOD_REQUEST_SESSION_ID, NULL, rbusConfig_ReadSetTimeout(), &response)) == RBUSCORE_SUCCESS) + if ((rc = rbusMethod_Invoke(handle, RBUS_SMGR_METHOD_REQUEST_SESSION_ID, NULL, &outParams)) == RBUS_ERROR_SUCCESS) { - rbusMessage_GetInt32(response, /*MESSAGE_FIELD_RESULT,*/ (int*) &err); - if(RBUSCORE_SUCCESS != err) + rbusProperty_t prop = NULL; + prop = rbusObject_GetProperties(outParams); + int result = rbusValue_GetInt32(rbusProperty_GetValue(prop)); + if (RBUS_ERROR_SUCCESS != result) { - RBUSLOG_ERROR("Session manager reports internal error %d for the object %s", err, RBUS_SMGR_DESTINATION_NAME); + RBUSLOG_ERROR("Session manager reports internal error %d from %s for the object %s", rc, handle->componentName, RBUS_SMGR_DESTINATION_NAME); rc = RBUS_ERROR_SESSION_ALREADY_EXIST; } else { - rbusMessage_GetInt32(response, /*MESSAGE_FIELD_PAYLOAD,*/ (int*) pSessionId); - RBUSLOG_INFO("Received new session id %u", *pSessionId); + prop = rbusProperty_GetNext(prop); + /* Get current session id*/ + if (prop) + { + *pSessionId = rbusValue_GetInt32(rbusProperty_GetValue(prop)); + RBUSLOG_INFO("Received new session id %u", *pSessionId); + } + else + RBUSLOG_ERROR("Malformed response from session manager.\n"); } } else { RBUSLOG_ERROR("Failed to communicated with session manager."); - rc = rbusCoreError_to_rbusError(err); } - rbusMessage_Release(response); } else { RBUSLOG_WARN("Invalid Input passed.."); rc = RBUS_ERROR_INVALID_INPUT; } - return rc; -} + return rc; + } rbusError_t rbus_getCurrentSession(rbusHandle_t handle, uint32_t *pSessionId) { (void)handle; rbusError_t rc = RBUS_ERROR_SUCCESS; - rbusCoreError_t err = RBUSCORE_SUCCESS; - rbusMessage response = NULL; + rbusObject_t outParams = NULL; if (pSessionId && handle) { *pSessionId = 0; - if((err = rbus_invokeRemoteMethod(RBUS_SMGR_DESTINATION_NAME, RBUS_SMGR_METHOD_GET_CURRENT_SESSION_ID, NULL, rbusConfig_ReadGetTimeout(), &response)) == RBUSCORE_SUCCESS) + if ((rc = rbusMethod_Invoke(handle, RBUS_SMGR_METHOD_GET_CURRENT_SESSION_ID, NULL, &outParams)) == RBUS_ERROR_SUCCESS) { - rbusMessage_GetInt32(response, /*MESSAGE_FIELD_RESULT,*/ (int*) &err); - if(RBUSCORE_SUCCESS != err) + rbusProperty_t prop = NULL; + prop = rbusObject_GetProperties(outParams); + int result = rbusValue_GetInt32(rbusProperty_GetValue(prop)); + if (RBUS_ERROR_SUCCESS != result) { - RBUSLOG_ERROR("Session manager reports internal error %d from %s for the object %s", err, handle->componentName, RBUS_SMGR_DESTINATION_NAME); + RBUSLOG_ERROR("Session manager reports internal error %d from %s for the object %s", rc, handle->componentName, RBUS_SMGR_DESTINATION_NAME); rc = RBUS_ERROR_SESSION_ALREADY_EXIST; } else { - rbusMessage_GetInt32(response, /*MESSAGE_FIELD_PAYLOAD,*/ (int*) pSessionId); - RBUSLOG_INFO("Received new session id %u", *pSessionId); + prop = rbusProperty_GetNext(prop); + /* Get current session id*/ + if (prop) + { + *pSessionId = rbusValue_GetInt32(rbusProperty_GetValue(prop)); + RBUSLOG_INFO("Received new session id %u", *pSessionId); + } + else + RBUSLOG_ERROR("Malformed response from session manager.\n"); } } else { RBUSLOG_ERROR("Failed to communicated with session manager."); - rc = rbusCoreError_to_rbusError(err); + return rc; } - rbusMessage_Release(response); } else { @@ -5946,44 +5959,49 @@ rbusError_t rbus_closeSession(rbusHandle_t handle, uint32_t sessionId) { (void)handle; rbusError_t rc = RBUS_ERROR_SUCCESS; - rbusCoreError_t err = RBUSCORE_SUCCESS; if (handle) { - rbusMessage inputSession; - rbusMessage response = NULL; - if (sessionId == 0) { RBUSLOG_WARN("Passing default session ID which is 0"); return RBUS_ERROR_SUCCESS; } - rbusMessage_Init(&inputSession); - rbusMessage_SetInt32(inputSession, /*MESSAGE_FIELD_PAYLOAD,*/ sessionId); - if((err = rbus_invokeRemoteMethod(RBUS_SMGR_DESTINATION_NAME, RBUS_SMGR_METHOD_END_SESSION, inputSession, rbusConfig_ReadSetTimeout(), &response)) == RBUSCORE_SUCCESS) + + rbusObject_t inParams = NULL, outParams = NULL; + rbusObject_Init(&inParams, NULL); + rbusValue_t inputSession; + rbusValue_Init(&inputSession); + rbusValue_SetInt32(inputSession, sessionId); + rbusObject_SetValue(inParams, "session", inputSession); + + if ((rc = rbusMethod_Invoke(handle, RBUS_SMGR_METHOD_END_SESSION, inParams, &outParams)) == RBUS_ERROR_SUCCESS) { - rbusMessage_GetInt32(response, /*MESSAGE_FIELD_RESULT,*/ (int*) &err); - if(RBUSCORE_SUCCESS != err) + VERIFY_NULL(outParams); + rbusProperty_t prop = NULL; + prop = rbusObject_GetProperties(outParams); + int result = rbusValue_GetInt32(rbusProperty_GetValue(prop)); + + if (RBUS_ERROR_SUCCESS == result) { - RBUSLOG_ERROR("Session manager reports internal error %d from %s for the object %s", err, handle->componentName, RBUS_SMGR_DESTINATION_NAME); - rc = RBUS_ERROR_SESSION_ALREADY_EXIST; + RBUSLOG_INFO("Successfully ended session %u.", sessionId); } else - RBUSLOG_INFO("Successfully ended session %u.", sessionId); + { + RBUSLOG_ERROR("Session manager reports internal error %d from %s for the object %s", result, handle->componentName, RBUS_SMGR_DESTINATION_NAME); + rc = RBUS_ERROR_SESSION_ALREADY_EXIST; + } } else { RBUSLOG_ERROR("Failed to communicated with session manager."); - rc = rbusCoreError_to_rbusError(err); } - rbusMessage_Release(response); } else { RBUSLOG_WARN("Invalid Input passed.."); rc = RBUS_ERROR_INVALID_INPUT; } - return rc; } diff --git a/src/session_manager/CMakeLists.txt b/src/session_manager/CMakeLists.txt index f44e5c04..7b13800d 100755 --- a/src/session_manager/CMakeLists.txt +++ b/src/session_manager/CMakeLists.txt @@ -17,20 +17,21 @@ # limitations under the License. ########################################################################## include_directories(. - ../core - ../rtmessage) + ../../include + ../core + ../rtmessage) add_executable(rbus_session_mgr session_manager.c) -add_dependencies(rbus_session_mgr rbuscore) -target_link_libraries(rbus_session_mgr rbuscore) +add_dependencies(rbus_session_mgr rbus rbuscore) +target_link_libraries(rbus_session_mgr rbus rbuscore) if (BUILD_SESSIONMGR_SAMPLE_APPS) add_executable(rbus_session_mgr_client test/sample_client.c) -add_dependencies(rbus_session_mgr_client rbuscore) -target_link_libraries(rbus_session_mgr_client rbuscore) +add_dependencies(rbus_session_mgr_client rbus rbuscore) +target_link_libraries(rbus_session_mgr_client rbus rbuscore) endif (BUILD_SESSIONMGR_SAMPLE_APPS) -install (TARGETS rbus_session_mgr +install (TARGETS rbus_session_mgr RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}) install(FILES rbus_session_mgr.h DESTINATION "include/rbus") diff --git a/src/session_manager/session_manager.c b/src/session_manager/session_manager.c index 85803352..936d3d92 100644 --- a/src/session_manager/session_manager.c +++ b/src/session_manager/session_manager.c @@ -21,113 +21,158 @@ #include #include #include +#include "rbus.h" #include "rbuscore.h" #include "rtLog.h" #include "rbus_session_mgr.h" - #define CCSP_CURRENT_SESSION_ID_SIGNAL "currentSessionIDSignal" static int g_counter; static int g_current_session_id; +rbusHandle_t g_busHandle = 0; -static int request_session_id(const char * destination, const char * method, rbusMessage request, void * user_data, rbusMessage *response, const rtMessageHeader* hdr) +/* MethodHandler */ +static rbusError_t sessionManager_methodHandler(rbusHandle_t handle, + char const* methodName, rbusObject_t inParams, + rbusObject_t outParams, rbusMethodAsyncHandle_t asyncHandle) { - (void) request; - (void) user_data; - (void) destination; - (void) method; - (void) hdr; - rbusMessage msg; - rbusCoreError_t err = RBUSCORE_SUCCESS; + (void)handle; + (void)asyncHandle; + int rc = RBUS_ERROR_SUCCESS; - rbusMessage_Init(response); - if(0 == g_current_session_id) - { - g_current_session_id = 1; - ++g_counter; - printf("Creating new session %d\n", g_current_session_id); - // For debugging purpose - //printf("Total number of times sessions created = %d\n", g_counter); - rbusMessage_SetInt32(*response, RBUSCORE_SUCCESS); - rbusMessage_SetInt32(*response, g_current_session_id); - } - else + /* Get Current Session id*/ + if (strcmp(methodName, RBUS_SMGR_METHOD_GET_CURRENT_SESSION_ID) == 0) { - printf("Cannot create new session when session %d is active.\n", g_current_session_id); - rbusMessage_SetInt32(*response, RBUSCORE_ERROR_INVALID_STATE); + printf("Current session id is %d\n", g_current_session_id); + rbusValue_t return_value, sessionid_value; + rbusValue_Init(&return_value); + rbusValue_Init(&sessionid_value); + rbusValue_SetInt32(return_value, RBUSCORE_SUCCESS); + rbusValue_SetInt32(sessionid_value, g_current_session_id); + rbusObject_SetValue(outParams, "return_value", return_value); + rbusObject_SetValue(outParams, "sessionid", sessionid_value); + rbusValue_Release(return_value); + rbusValue_Release(sessionid_value); + return RBUS_ERROR_SUCCESS; } - rbusMessage_Init(&msg); - rbusMessage_SetInt32(msg, RBUSCORE_SUCCESS); - rbusMessage_SetInt32(msg, (int32_t)g_current_session_id); - err = rbus_publishEvent(RBUS_SMGR_DESTINATION_NAME, CCSP_CURRENT_SESSION_ID_SIGNAL, msg); - if(err != RBUSCORE_SUCCESS) + /* Request for session id*/ + else if (strcmp(methodName, RBUS_SMGR_METHOD_REQUEST_SESSION_ID) == 0) { - printf("rbus_publishEvent failed with ret = %d\n", err); - } - rbusMessage_Release(msg); + if (0 == g_current_session_id) + { + g_current_session_id = 1; + ++g_counter; + printf("Creating new session %d\n", g_current_session_id); + rbusValue_t return_value, sessionid_value; + rbusValue_Init(&return_value); + rbusValue_Init(&sessionid_value); + rbusValue_SetInt32(return_value, RBUSCORE_SUCCESS); + rbusValue_SetInt32(sessionid_value, g_current_session_id); + rbusObject_SetValue(outParams, "return_value", return_value); + rbusObject_SetValue(outParams, "sessionid", sessionid_value); + rbusValue_Release(return_value); + rbusValue_Release(sessionid_value); + } + else + { + printf("Cannot create new session when session %d is active.\n", g_current_session_id); + rbusValue_t return_value; + rbusValue_Init(&return_value); + rbusValue_SetInt32(return_value, RBUSCORE_ERROR_INVALID_STATE); + rbusObject_SetValue(outParams, "return_value", return_value); + rbusValue_Release(return_value); + } + rbusEvent_t event = {0}; + rbusObject_t data; + rbusValue_t return_value, sessionid_value; + rbusValue_Init(&return_value); + rbusValue_Init(&sessionid_value); + rbusObject_Init(&data, NULL); + rbusValue_SetInt32(return_value, RBUSCORE_SUCCESS); + rbusValue_SetInt32(sessionid_value, g_current_session_id); + rbusObject_SetValue(data, "return_value", return_value); + rbusObject_SetValue(data, "sessionid", sessionid_value); - return 0; -} + event.name = CCSP_CURRENT_SESSION_ID_SIGNAL; + event.data = data; + event.type = RBUS_EVENT_GENERAL; -static int get_session_id(const char * destination, const char * method, rbusMessage request, void * user_data, rbusMessage *response, const rtMessageHeader* hdr) -{ - (void) request; - (void) user_data; - (void) destination; - (void) method; - (void) hdr; - rbusMessage_Init(response); - printf("Current session id is %d\n", g_current_session_id); - rbusMessage_SetInt32(*response, RBUSCORE_SUCCESS); - rbusMessage_SetInt32(*response, g_current_session_id); - return 0; -} + rc = rbusEvent_Publish(handle, &event); -static int end_session(const char * destination, const char * method, rbusMessage request, void * user_data, rbusMessage *response, const rtMessageHeader* hdr) -{ - (void) user_data; - (void) destination; - (void) method; - (void) hdr; - rbusMessage msg; + if (rc != RBUS_ERROR_SUCCESS) + printf("provider: rbusEvent_Publish Event failed: %d\n", rc); - rbusMessage_Init(response); - int sessionid = 0; - rbusCoreError_t result = RBUSCORE_SUCCESS; - - if(RT_OK == rbusMessage_GetInt32(request, &sessionid)) + rbusValue_Release(return_value); + rbusValue_Release(sessionid_value); + rbusObject_Release(data); + } + /* End of the session */ + else if (strcmp(methodName, RBUS_SMGR_METHOD_END_SESSION) == 0) { - if(sessionid == g_current_session_id) + rbusProperty_t prop = NULL; + int sessionid = 0; + int result = 0; + prop = rbusObject_GetProperties(inParams); + if ((prop) && (sessionid = rbusValue_GetInt32(rbusProperty_GetValue(prop)))) { - printf("End of session %d\n", g_current_session_id); - g_current_session_id = 0; - //Cue event announcing end of session. + if(sessionid == g_current_session_id) + { + printf("End of session %d\n", g_current_session_id); + g_current_session_id = 0; + result = RBUS_ERROR_SUCCESS; + } + else + { + printf("Cannot end session %d. It doesn't match active session, which is %d.\n", sessionid, g_current_session_id); + result = RBUS_ERROR_SESSION_ALREADY_EXIST; + } } else { - printf("Cannot end session %d. It doesn't match active session, which is %d.\n", sessionid, g_current_session_id); - result = RBUSCORE_ERROR_INVALID_STATE; + printf("Session id not found. Cannot process end of session.\n"); + result = RBUS_ERROR_INVALID_INPUT; } + + rbusValue_t resultValue; + rbusValue_Init(&resultValue); + rbusValue_SetInt32(resultValue, result); + rbusObject_SetValue(outParams, "result", resultValue); + rbusValue_Release(resultValue); + + rbusEvent_t event = {0}; + rbusObject_t data; + rbusValue_t return_value, sessionid_value; + + rbusValue_Init(&return_value); + rbusValue_Init(&sessionid_value); + rbusObject_Init(&data, NULL); + + rbusValue_SetInt32(return_value, RBUS_ERROR_SUCCESS); + rbusValue_SetInt32(sessionid_value, g_current_session_id); + rbusObject_SetValue(data, "return_value", return_value); + rbusObject_SetValue(data, "sessionid", sessionid_value); + + event.name = CCSP_CURRENT_SESSION_ID_SIGNAL; + event.data = data; + event.type = RBUS_EVENT_GENERAL; + + rc = rbusEvent_Publish(handle, &event); + + rbusValue_Release(return_value); + rbusValue_Release(sessionid_value); + rbusObject_Release(data); + + if (rc != RBUS_ERROR_SUCCESS) + printf("provider: rbusEvent_Publish Event failed: %d\n", rc); } else { - printf("Session id not found. Cannot process end of session.\n"); - result = RBUSCORE_ERROR_INVALID_PARAM; + return RBUS_ERROR_BUS_ERROR; } - rbusMessage_SetInt32(*response, result); - rbusMessage_Init(&msg); - rbusMessage_SetInt32(msg, RBUSCORE_SUCCESS); - rbusMessage_SetInt32(msg, (int32_t)g_current_session_id); - result = rbus_publishEvent(RBUS_SMGR_DESTINATION_NAME, CCSP_CURRENT_SESSION_ID_SIGNAL, msg); - if(result != RBUSCORE_SUCCESS) - { - printf("rbus_publishEvent failed with ret = %d\n", result); - } - rbusMessage_Release(msg); - return 0; + return RBUS_ERROR_SUCCESS; } - +#if 0 static int callback(const char * destination, const char * method, rbusMessage message, void * user_data, rbusMessage *response, const rtMessageHeader* hdr) { (void) user_data; @@ -145,11 +190,16 @@ static int callback(const char * destination, const char * method, rbusMessage m return 0; } +#endif /*Signal handler for closing broker connection*/ static void handle_signal(int sig) { (void) sig; - rbus_closeBrokerConnection(); + if (g_busHandle) + { + rbus_close(g_busHandle); + g_busHandle = 0; + } printf("rbus session manager exiting.\n"); exit(0); } @@ -158,10 +208,10 @@ int main(int argc, char *argv[]) { (void) argc; (void) argv; - rbusCoreError_t err = RBUSCORE_SUCCESS; printf("rbus session manager launching.\n"); rtLog_SetLevel(RT_LOG_INFO); - + int rc = RBUS_ERROR_SUCCESS; + char componentName[] = RBUS_SMGR_DESTINATION_NAME; if (argc == 2) { if (-1 == daemon(0 /*chdir to "/"*/, 1 /*redirect stdout/stderr to /dev/null*/ )) @@ -172,9 +222,16 @@ int main(int argc, char *argv[]) signal(SIGTERM, handle_signal); } + rbusDataElement_t dataElements[4] = { + {CCSP_CURRENT_SESSION_ID_SIGNAL, RBUS_ELEMENT_TYPE_EVENT, {NULL, NULL, NULL, NULL, NULL, NULL}}, + {RBUS_SMGR_METHOD_GET_CURRENT_SESSION_ID, RBUS_ELEMENT_TYPE_METHOD, {NULL, NULL, NULL, NULL, NULL, sessionManager_methodHandler}}, + {RBUS_SMGR_METHOD_REQUEST_SESSION_ID, RBUS_ELEMENT_TYPE_METHOD, {NULL, NULL, NULL, NULL, NULL, sessionManager_methodHandler}}, + {RBUS_SMGR_METHOD_END_SESSION, RBUS_ELEMENT_TYPE_METHOD, {NULL, NULL, NULL, NULL, NULL, sessionManager_methodHandler}} + }; + while(1) { - if((err = rbus_openBrokerConnection(RBUS_SMGR_DESTINATION_NAME)) == RBUSCORE_SUCCESS) + if ((g_busHandle == NULL) && ((rc = rbus_open(&g_busHandle, componentName) == RBUS_ERROR_SUCCESS))) { printf("Successfully connected to bus.\n"); break; @@ -186,27 +243,25 @@ int main(int argc, char *argv[]) } } - if((err = rbus_registerObj(RBUS_SMGR_DESTINATION_NAME, callback, NULL)) == RBUSCORE_SUCCESS) +#if 0 + if ((rc = rbus_unregisterObj(RBUS_SMGR_DESTINATION_NAME)) == RBUSCORE_SUCCESS) { - printf("Successfully registered object.\n"); + printf("Successfully Unregistered object.\n"); } - - if((err = rbus_registerEvent(RBUS_SMGR_DESTINATION_NAME, CCSP_CURRENT_SESSION_ID_SIGNAL, NULL, NULL)) == RBUSCORE_SUCCESS) + if ((rc = rbus_registerObj(RBUS_SMGR_DESTINATION_NAME, callback, NULL)) == RBUSCORE_SUCCESS) { - printf("Successfully registered Event.\n"); + printf("Successfully registered object.\n"); } +#endif + if ((rc = rbus_regDataElements(g_busHandle, 4, dataElements)) == RBUS_ERROR_SUCCESS) + printf("Successfully registered Event.\n"); - rbus_method_table_entry_t table[3] = { - {RBUS_SMGR_METHOD_GET_CURRENT_SESSION_ID, NULL, get_session_id}, - {RBUS_SMGR_METHOD_REQUEST_SESSION_ID, NULL, request_session_id}, - {RBUS_SMGR_METHOD_END_SESSION, NULL, end_session}}; - rbus_registerMethodTable(RBUS_SMGR_DESTINATION_NAME, table, 3); pause(); - if((err = rbus_closeBrokerConnection()) == RBUSCORE_SUCCESS) - { - printf("Successfully disconnected from bus.\n"); - } + rbus_unregDataElements(g_busHandle, 4, dataElements); + rbus_close(g_busHandle); + g_busHandle = NULL; + printf("rbus session manager exiting.\n"); return 0; -} +} \ No newline at end of file diff --git a/src/session_manager/test/sample_client.c b/src/session_manager/test/sample_client.c index ef443659..0fefaa23 100644 --- a/src/session_manager/test/sample_client.c +++ b/src/session_manager/test/sample_client.c @@ -19,30 +19,29 @@ #include #include #include -#include "rbuscore.h" - -#include "rbus_session_mgr.h" +#include +#include "rbus.h" #include "rtLog.h" - -static int g_current_session_id = 0; - +static int g_current_session_id = 0; +rbusHandle_t g_handle; void create_session() { - rbusMessage response; - if(RBUSCORE_SUCCESS == rbus_invokeRemoteMethod(RBUS_SMGR_DESTINATION_NAME, RBUS_SMGR_METHOD_REQUEST_SESSION_ID, NULL, 1000, &response)) + rbusObject_t outParams = NULL; + if (RBUS_ERROR_SUCCESS == rbusMethod_Invoke(g_handle, RBUS_SMGR_METHOD_REQUEST_SESSION_ID, NULL, &outParams)) { - int result; - if(RT_OK == rbusMessage_GetInt32(response, &result)) + rbusProperty_t prop = NULL; + prop = rbusObject_GetProperties(outParams); + int result = rbusValue_GetInt32(rbusProperty_GetValue(prop)); + if(RBUS_ERROR_SUCCESS != result) { - if(RBUSCORE_SUCCESS != result) - { - printf("Session manager reports internal error %d.\n", result); - return; - } + printf("Session manager reports internal error %d.\n", result); + return; } - if(RT_OK == rbusMessage_GetInt32(response, &g_current_session_id)) + prop = rbusProperty_GetNext(prop); + if (prop) { + g_current_session_id = rbusValue_GetInt32(rbusProperty_GetValue(prop)); printf("Got new session id %d\n", g_current_session_id); } else @@ -54,20 +53,22 @@ void create_session() void print_current_session_id() { - rbusMessage response; - if(RBUSCORE_SUCCESS == rbus_invokeRemoteMethod(RBUS_SMGR_DESTINATION_NAME, RBUS_SMGR_METHOD_GET_CURRENT_SESSION_ID, NULL, 1000, &response)) + rbusObject_t outParams = NULL; + if (RBUS_ERROR_SUCCESS == rbusMethod_Invoke(g_handle, RBUS_SMGR_METHOD_GET_CURRENT_SESSION_ID, NULL, &outParams)) { - int result; - if(RT_OK == rbusMessage_GetInt32(response, &result)) + rbusProperty_t prop = NULL; + prop = rbusObject_GetProperties(outParams); + int result = rbusValue_GetInt32(rbusProperty_GetValue(prop)); + if(RBUS_ERROR_SUCCESS != result) { - if(RBUSCORE_SUCCESS != result) - { - printf("Session manager reports internal error %d.\n", result); - return; - } + printf("Session manager reports internal error %d.\n", result); + return; } - if(RT_OK == rbusMessage_GetInt32(response, &g_current_session_id)) + prop = rbusProperty_GetNext(prop); + /* Get current session id*/ + if (prop) { + g_current_session_id = rbusValue_GetInt32(rbusProperty_GetValue(prop)); printf("Current session id %d\n", g_current_session_id); } else @@ -79,24 +80,24 @@ void print_current_session_id() void end_session(int session) { - rbusMessage out; - rbusMessage response; - - rbusMessage_Init(&out); - rbusMessage_SetInt32(out, session); - if(RBUSCORE_SUCCESS == rbus_invokeRemoteMethod(RBUS_SMGR_DESTINATION_NAME, RBUS_SMGR_METHOD_END_SESSION, out, 1000, &response)) + rbusObject_t inParams = NULL, outParams = NULL; + rbusObject_Init(&inParams, NULL); + rbusValue_t sessionValue; + rbusValue_Init(&sessionValue); + rbusValue_SetInt32(sessionValue, session); + rbusObject_SetValue(inParams, "session", sessionValue); + if (RBUS_ERROR_SUCCESS == rbusMethod_Invoke(g_handle, RBUS_SMGR_METHOD_END_SESSION, inParams, &outParams)) { - int result; - if(RT_OK == rbusMessage_GetInt32(response, &result)) + rbusProperty_t prop = NULL; + prop = rbusObject_GetProperties(outParams); + int result = rbusValue_GetInt32(rbusProperty_GetValue(prop)); + if(RBUS_ERROR_SUCCESS != result) { - if(RBUSCORE_SUCCESS != result) - { - printf("Session manager reports internal error %d.\n", result); - return; - } - else - printf("Successfully ended session %d.\n", session); + printf("Session manager reports internal error %d.\n", result); + return; } + else + printf("Successfully ended session %d.\n", session); } else printf("RPC with session manager failed.\n"); @@ -106,14 +107,14 @@ int main(int argc, char *argv[]) { (void) argc; (void) argv; - rbusCoreError_t err = RBUSCORE_SUCCESS; rtLog_SetLevel(RT_LOG_INFO); + int err = RBUS_ERROR_SUCCESS; - if((err = rbus_openBrokerConnection("rbus_smgr_client")) == RBUSCORE_SUCCESS) + if ((err = rbus_open(&g_handle, "rbus_smgr_client")) == RBUS_ERROR_SUCCESS) { printf("Successfully connected to bus.\n"); } - + create_session(); print_current_session_id(); create_session();//Negative test case. @@ -122,11 +123,7 @@ int main(int argc, char *argv[]) create_session(); print_current_session_id(); end_session(g_current_session_id); - - if((err = rbus_closeBrokerConnection()) == RBUSCORE_SUCCESS) - { - printf("Successfully disconnected from bus.\n"); - } + rbus_close(g_handle); return 0; -} +} \ No newline at end of file diff --git a/unittests/rbusSessionTest.cpp b/unittests/rbusSessionTest.cpp index 9f12e296..87010bf4 100644 --- a/unittests/rbusSessionTest.cpp +++ b/unittests/rbusSessionTest.cpp @@ -48,3 +48,106 @@ TEST(rbusSessionTest, test1) } free(componentName); } + +TEST(rbusSessionTest, test2) +{ + int rc = RBUS_ERROR_BUS_ERROR; + rbusHandle_t handle = NULL; + unsigned int sessionId = 0 , newSessionId = 0; + char *componentName = NULL; + + componentName = strdup("sessiontest"); + + rc = rbus_open(&handle, componentName); + EXPECT_EQ(rc,RBUS_ERROR_SUCCESS); + + if(RBUS_ERROR_SUCCESS == rc) + { + rc = rbus_createSession(handle , &sessionId); + EXPECT_EQ(rc, RBUS_ERROR_SUCCESS); + + rc = rbus_createSession(handle , &newSessionId); + EXPECT_EQ(rc, RBUS_ERROR_SESSION_ALREADY_EXIST); + + rc = rbus_getCurrentSession(handle , &sessionId); + EXPECT_EQ(rc, RBUS_ERROR_SUCCESS); + + rc = rbus_closeSession(handle, sessionId); + EXPECT_EQ(rc, RBUS_ERROR_SUCCESS); + rbus_close(handle); + } + free(componentName); +} + +TEST(rbusSessionTest, test3) +{ + int rc = RBUS_ERROR_BUS_ERROR; + rbusHandle_t handle = NULL; + unsigned int sessionId = 0 , newSessionId = 0; + char *componentName = NULL; + + componentName = strdup("sessiontest"); + + rc = rbus_open(&handle, componentName); + EXPECT_EQ(rc,RBUS_ERROR_SUCCESS); + + if(RBUS_ERROR_SUCCESS == rc) + { + rc = rbus_createSession(handle , &sessionId); + EXPECT_EQ(rc, RBUS_ERROR_SUCCESS); + + rc = rbus_getCurrentSession(handle , &newSessionId); + EXPECT_EQ(rc, RBUS_ERROR_SUCCESS); + EXPECT_EQ(sessionId, newSessionId); + + rc = rbus_closeSession(handle, sessionId); + EXPECT_EQ(rc, RBUS_ERROR_SUCCESS); + + rc = rbus_closeSession(handle, sessionId); + EXPECT_EQ(rc, RBUS_ERROR_SESSION_ALREADY_EXIST); + + rbus_close(handle); + } + free(componentName); +} + +TEST(rbusSessionTest, test4) +{ + int rc = RBUS_ERROR_BUS_ERROR; + rbusHandle_t handle = NULL; + unsigned int sessionId_1 = 0 , sessionId_2 = 0, sessionId_3 = 0, newSessionId = 0; + char *componentName = NULL; + + componentName = strdup("sessiontest"); + + rc = rbus_open(&handle, componentName); + EXPECT_EQ(rc,RBUS_ERROR_SUCCESS); + + if(RBUS_ERROR_SUCCESS == rc) + { + rc = rbus_createSession(handle , &sessionId_1); + EXPECT_EQ(rc, RBUS_ERROR_SUCCESS); + + rc = rbus_createSession(handle , &sessionId_2); + EXPECT_EQ(rc, RBUS_ERROR_SESSION_ALREADY_EXIST); + + rc = rbus_createSession(handle , &sessionId_3); + EXPECT_EQ(rc, RBUS_ERROR_SESSION_ALREADY_EXIST); + + rc = rbus_getCurrentSession(handle , &newSessionId); + EXPECT_EQ(rc, RBUS_ERROR_SUCCESS); + EXPECT_EQ(sessionId_1, newSessionId); + + rc = rbus_closeSession(handle, sessionId_1); + EXPECT_EQ(rc, RBUS_ERROR_SUCCESS); + + rc = rbus_closeSession(handle, sessionId_1); + EXPECT_EQ(rc, RBUS_ERROR_SESSION_ALREADY_EXIST); + + rc = rbus_closeSession(handle, sessionId_1); + EXPECT_EQ(rc, RBUS_ERROR_SESSION_ALREADY_EXIST); + + rbus_close(handle); + } + free(componentName); +} \ No newline at end of file diff --git a/unittests/rbus_unit_test_client.cpp b/unittests/rbus_unit_test_client.cpp index 22cfaa52..5f72ab09 100644 --- a/unittests/rbus_unit_test_client.cpp +++ b/unittests/rbus_unit_test_client.cpp @@ -183,123 +183,6 @@ static bool CALL_RBUS_PULL_OBJECT_DETAILED(char* server_obj, test_struct_t expec return result; } -rbusCoreError_t CREATE_SESSION() -{ - rbusMessage response = NULL; - rbusCoreError_t ret = RBUSCORE_SUCCESS; - - ret = rbus_invokeRemoteMethod(RBUS_SMGR_DESTINATION_NAME, RBUS_SMGR_METHOD_REQUEST_SESSION_ID, NULL, 1000, &response); - if(RBUSCORE_SUCCESS == ret) - { - int result; - if(RT_OK == rbusMessage_GetInt32(response, &result)) - { - if(RBUSCORE_SUCCESS != result) - { - printf("ERROR Cannot create a new session, Session already exist \n"); - ret = RBUSCORE_ERROR_INVALID_STATE; - rbusMessage_Release(response); - return ret; - } - } - if(RT_OK == rbusMessage_GetInt32(response, &g_current_session_id)) - { - printf("Got new session id %d\n", g_current_session_id); - ret = RBUSCORE_SUCCESS; - rbusMessage_Release(response); - return ret; - } - else{ - printf("Malformed response from session manager.\n"); - } - } - else{ - printf("RPC with session manager failed.\n"); - ret = RBUSCORE_ERROR_ENTRY_NOT_FOUND; - rbusMessage_Release(response); - return ret; - } - return ret; -} - -rbusCoreError_t PRINT_CURRENT_SESSION_ID() -{ - rbusMessage response = NULL; - rbusCoreError_t ret = RBUSCORE_SUCCESS; - - ret = rbus_invokeRemoteMethod(RBUS_SMGR_DESTINATION_NAME, RBUS_SMGR_METHOD_GET_CURRENT_SESSION_ID, NULL, 1000, &response); - - if(RBUSCORE_SUCCESS == ret) - { - int result; - if(RT_OK == rbusMessage_GetInt32(response, &result)) - { - if(RBUSCORE_SUCCESS != result) - { - printf("Session manager reports internal error %d.\n", result); - ret = RBUSCORE_ERROR_INVALID_STATE; - rbusMessage_Release(response); - return ret; - } - } - if(RT_OK == rbusMessage_GetInt32(response, &g_current_session_id)) - { - printf("Current session id %d\n", g_current_session_id); - ret = RBUSCORE_SUCCESS; - rbusMessage_Release(response); - return ret; - } - else{ - printf("Malformed response from session manager.\n"); - } - } - else{ - printf("RPC with session manager failed.\n"); - ret = RBUSCORE_ERROR_ENTRY_NOT_FOUND; - rbusMessage_Release(response); - return ret; - } - return ret; -} - -rbusCoreError_t END_SESSION(int session) -{ - rbusMessage out; - rbusMessage response = NULL; - - rbusMessage_Init(&out); - rbusMessage_SetInt32(out, session); - rbusCoreError_t ret = RBUSCORE_SUCCESS; - - if(RBUSCORE_SUCCESS == rbus_invokeRemoteMethod(RBUS_SMGR_DESTINATION_NAME, RBUS_SMGR_METHOD_END_SESSION, out, 1000, &response)) - { - int result; - if(RT_OK == rbusMessage_GetInt32(response, &result)) - { - if(RBUSCORE_SUCCESS != result) - { - printf("ERROR Cannot end session, It doesn't match active session\n"); - ret = RBUSCORE_ERROR_INVALID_STATE; - rbusMessage_Release(response); - return ret; - } - else{ - printf("Successfully ended session %d.\n", session); - ret = RBUSCORE_SUCCESS; - rbusMessage_Release(response); - return ret; - } - } - } - else{ - printf("RPC with session manager failed.\n"); - ret = RBUSCORE_ERROR_ENTRY_NOT_FOUND; - rbusMessage_Release(response); - return ret; - } - return ret; -} - TEST_F(TestClient, sample_test) { EXPECT_EQ(1, 1); @@ -479,125 +362,4 @@ TEST_F(TestClient, rbus_test_obj_coexistance_test1) if(conn_status) CALL_RBUS_CLOSE_BROKER_CONNECTION(); -} - -TEST_F(TestClient, rbus_session_manager_test1) -{ - char client_name[] = "TEST_CLIENT_1"; - bool conn_status = false; - rbusCoreError_t err = RBUSCORE_SUCCESS; - - printf("********************* CREATING CLIENT : %s \n", client_name); - conn_status = CALL_RBUS_OPEN_BROKER_CONNECTION(client_name); - if(conn_status) - printf("Successfully connected to bus.\n"); - err = CREATE_SESSION(); - EXPECT_EQ(err, RBUSCORE_SUCCESS) << "CREATE_SESSION failed"; - err = PRINT_CURRENT_SESSION_ID(); - EXPECT_EQ(err, RBUSCORE_SUCCESS) << "PRINT_CURRENT_SESSION_ID failed"; - err = END_SESSION(g_current_session_id); - EXPECT_EQ(err, RBUSCORE_SUCCESS) << "END_SESSION failed"; - conn_status = CALL_RBUS_CLOSE_BROKER_CONNECTION(); - if(conn_status) - printf("Successfully disconnected from bus.\n"); -} - -TEST_F(TestClient, rbus_session_manager_test2) -{ - char client_name[] = "TEST_CLIENT_1"; - bool conn_status = false; - rbusCoreError_t err = RBUSCORE_SUCCESS; - - printf("********************* CREATING CLIENT : %s \n", client_name); - conn_status = CALL_RBUS_OPEN_BROKER_CONNECTION(client_name); - if(conn_status) - printf("Successfully connected to bus.\n"); - err = CREATE_SESSION(); - EXPECT_EQ(err, RBUSCORE_SUCCESS) << "CREATE_SESSION failed"; - //Neg Test Calling CREATE_SESSION again - err = CREATE_SESSION(); - EXPECT_EQ(err, RBUSCORE_ERROR_INVALID_STATE) << "CREATE_SESSION failed"; - err = PRINT_CURRENT_SESSION_ID(); - EXPECT_EQ(err, RBUSCORE_SUCCESS) << "PRINT_CURRENT_SESSION_ID failed"; - err = END_SESSION(g_current_session_id); - EXPECT_EQ(err, RBUSCORE_SUCCESS) << "END_SESSION failed"; - conn_status = CALL_RBUS_CLOSE_BROKER_CONNECTION(); - if(conn_status) - printf("Successfully disconnected from bus.\n"); -} - -TEST_F(TestClient, rbus_session_manager_test3) -{ - char client_name[] = "TEST_CLIENT_1"; - bool conn_status = false; - rbusCoreError_t err = RBUSCORE_SUCCESS; - - printf("********************* CREATING CLIENT : %s \n", client_name); - conn_status = CALL_RBUS_OPEN_BROKER_CONNECTION(client_name); - if(conn_status) - printf("Successfully connected to bus.\n"); - err = CREATE_SESSION(); - EXPECT_EQ(err, RBUSCORE_SUCCESS) << "CREATE_SESSION failed"; - err = PRINT_CURRENT_SESSION_ID(); - EXPECT_EQ(err, RBUSCORE_SUCCESS) << "PRINT_CURRENT_SESSION_ID failed"; - err = END_SESSION(g_current_session_id); - EXPECT_EQ(err, RBUSCORE_SUCCESS) << "END_SESSION failed"; - //Neg Test Calling END_SESSION again - err = END_SESSION(g_current_session_id); - EXPECT_EQ(err, RBUSCORE_ERROR_INVALID_STATE) << "END_SESSION failed"; - conn_status = CALL_RBUS_CLOSE_BROKER_CONNECTION(); - if(conn_status) - printf("Successfully disconnected from bus.\n"); -} - -TEST_F(TestClient, rbus_session_manager_test4) -{ - char client_name[] = "TEST_CLIENT_1"; - bool conn_status = false; - rbusCoreError_t err = RBUSCORE_SUCCESS; - - printf("********************* CREATING CLIENT : %s \n", client_name); - conn_status = CALL_RBUS_OPEN_BROKER_CONNECTION(client_name); - if(conn_status) - printf("Successfully connected to bus.\n"); - err = CREATE_SESSION(); - EXPECT_EQ(err, RBUSCORE_SUCCESS) << "CREATE_SESSION failed"; - //Neg Test Calling CREATE_SESSION multiple times - err = CREATE_SESSION(); - EXPECT_EQ(err, RBUSCORE_ERROR_INVALID_STATE) << "CREATE_SESSION failed"; - err = CREATE_SESSION(); - EXPECT_EQ(err, RBUSCORE_ERROR_INVALID_STATE) << "CREATE_SESSION failed"; - err = PRINT_CURRENT_SESSION_ID(); - EXPECT_EQ(err, RBUSCORE_SUCCESS) << "PRINT_CURRENT_SESSION_ID failed"; - err = END_SESSION(g_current_session_id); - EXPECT_EQ(err, RBUSCORE_SUCCESS) << "END_SESSION failed"; - conn_status = CALL_RBUS_CLOSE_BROKER_CONNECTION(); - if(conn_status) - printf("Successfully disconnected from bus.\n"); -} - -TEST_F(TestClient, rbus_session_manager_test5) -{ - char client_name[] = "TEST_CLIENT_1"; - bool conn_status = false; - rbusCoreError_t err = RBUSCORE_SUCCESS; - - printf("********************* CREATING CLIENT : %s \n", client_name); - conn_status = CALL_RBUS_OPEN_BROKER_CONNECTION(client_name); - if(conn_status) - printf("Successfully connected to bus.\n"); - err = CREATE_SESSION(); - EXPECT_EQ(err, RBUSCORE_SUCCESS) << "CREATE_SESSION failed"; - err = PRINT_CURRENT_SESSION_ID(); - EXPECT_EQ(err, RBUSCORE_SUCCESS) << "PRINT_CURRENT_SESSION_ID failed"; - err = END_SESSION(g_current_session_id); - EXPECT_EQ(err, RBUSCORE_SUCCESS) << "END_SESSION failed"; - //Neg Test Calling END_SESSION multiple times - err = END_SESSION(g_current_session_id); - EXPECT_EQ(err, RBUSCORE_ERROR_INVALID_STATE) << "END_SESSION failed"; - err = END_SESSION(g_current_session_id); - EXPECT_EQ(err, RBUSCORE_ERROR_INVALID_STATE) << "END_SESSION failed"; - conn_status = CALL_RBUS_CLOSE_BROKER_CONNECTION(); - if(conn_status) - printf("Successfully disconnected from bus.\n"); -} +} \ No newline at end of file