Integrated new user name message 33/190933/5
authorKrzysztof Wieclaw <k.wieclaw@samsung.com>
Tue, 9 Oct 2018 11:52:11 +0000 (13:52 +0200)
committerKrzysztof Wieclaw <k.wieclaw@samsung.com>
Fri, 26 Oct 2018 16:06:30 +0000 (18:06 +0200)
Change-Id: Ief594278304acbb7a6a57f5a246beb17af232df1
Signed-off-by: Krzysztof Wieclaw <k.wieclaw@samsung.com>
inc/car_connection_manager.h
inc/model/model_car_connection.h
inc/model/model_cloud_connection.h
src/car_connection_manager.c
src/controller/controller_name_input.c
src/messages/message_factory.c
src/model/model_car_connection.c
src/model/model_cloud_connection.c

index 03ee807..de82f83 100644 (file)
@@ -78,11 +78,17 @@ void car_connection_manager_handle_message(message_t *message);
 
 /**
  * @brief Sends data about driving.
- * @param[in] command command to be
+ * @param[in] command Command to be sent.
  */
 int car_connection_manager_send_command(command_s command);
 
 /**
+ * @brief Sends user name.
+ * @param[in] name User name to be sent.
+ */
+int car_connection_manager_send_user_name(const char *name);
+
+/**
  * @brief Destroys connection manager.
  */
 void car_connection_manager_fini();
index 26ccc22..bdcb3ec 100644 (file)
@@ -41,5 +41,6 @@ void model_car_connection_set_stop(bool stop);
 void model_car_connection_send_cam_elevation(float cam_elevation);
 void model_car_connection_send_cam_azimuth(float cam_azimuth);
 bool model_car_connection_is_connected();
+bool model_car_connection_player_name_set(const char *player_name);
 
 #endif /* MODEL_MODEL_CAR_CONNECTION_H_ */
index b79aae7..cfb9a4e 100644 (file)
@@ -35,7 +35,6 @@ void model_cloud_connection_init(void);
 void model_cloud_connection_subscribe_event(t_model_cloud_connection_update_cb model_update_cb);
 void model_cloud_connection_unsubscirbe_event(void);
 void model_cloud_connection_subscribe_reconnect(t_model_cloud_connection_update_cb reconnect_cb);
-bool model_cloud_connection_player_name_set(const char *player_name);
 void model_cloud_connection_try_reconnect(void);
 void model_cloud_connection_model_state_change(void);
 void model_cloud_connection_start_get_names(t_model_cloud_connection_names_cb callback);
index 0d6ff09..f8efffb 100644 (file)
 #include "messages/message_manager.h"
 #include "messages/message_factory.h"
 #include "messages/message_ack.h"
+#include "messages/message_config_user_name.h"
 
 #define KEEP_ALIVE_ATTEMPTS 5
 #define HELLO_START_ATTEMPTS 5
 #define KEEP_ALIVE_INTERVAL 1000 //In ms
-#define HELLO_INTERVAL 1000 //In ms
-
-#define SAFE_SOURCE_REMOVE(source) do{if(source) { _W("Removing source no %d", source) ;g_source_remove(source); } source = 0;}while(0)
+#define HELLO_INTERVAL 5000 //In ms
+#define USER_NAME_INTERVAL 3000 //In ms
+
+#define SAFE_SOURCE_REMOVE(source) \
+do { \
+       if(source) \
+       { \
+               g_source_remove(source); \
+       } \
+       source = 0; \
+}while(0)
 
 typedef struct _car_connection_manager_info {
        car_connection_state_e state;
@@ -41,7 +50,9 @@ typedef struct _car_connection_manager_info {
        unsigned int hello_attempts_left;
        guint keep_alive_timer;
        guint hello_timer;
+       guint user_name_timer;
        message_factory_t *message_factory;
+       unsigned long long int user_name_serial;
        unsigned long long int last_acked;
 } _car_connection_manager_s;
 
@@ -62,6 +73,8 @@ static void _receive_cb(message_t *message, void *data);
 static gboolean _send_hello();
 static void _reset_counters();
 static gboolean _send_keep_alive();
+static gboolean _send_user_name();
+static int _start_user_name_timer();
 static gboolean _try_connect_cb(gpointer data);
 static gboolean _keep_alive_cb(gpointer data);
 static int _addr_cmp(const char *addr1, int port1, const char *addr2, int port2);
@@ -139,6 +152,7 @@ void car_connection_manager_disconnect()
        s_info.car_port = -1;
        SAFE_SOURCE_REMOVE(s_info.hello_timer);
        SAFE_SOURCE_REMOVE(s_info.keep_alive_timer);
+       SAFE_SOURCE_REMOVE(s_info.user_name_timer);
        _set_state(CAR_CONNECTION_STATE_DISCONNECTED);
 }
 
@@ -169,6 +183,10 @@ void car_connection_manager_handle_message(message_t *message)
        case MESSAGE_ACK:
                if(s_info.state == CAR_CONNECTION_STATE_CONNECTED) {
                        unsigned long long int serial = message_ack_get_ack_serial((message_ack_t*)message);
+                       if(serial == s_info.user_name_serial) {
+                               SAFE_SOURCE_REMOVE(s_info.user_name_timer);
+                               break;
+                       }
                        if(serial > s_info.last_acked) {
                                _D("Received KEEP_ALIVE_ACK");
                                s_info.keep_alive_attempts_left = KEEP_ALIVE_ATTEMPTS;
@@ -220,6 +238,17 @@ int car_connection_manager_send_command(command_s command)
        return 0;
 }
 
+int car_connection_manager_send_user_name(const char *name)
+{
+       if(s_info.state != CAR_CONNECTION_STATE_CONNECTED) {
+               _E("Connection not established, user name not sent");
+               return -1;
+
+       }
+       _start_user_name_timer(name);
+       return 0;
+}
+
 void car_connection_manager_fini()
 {
        if(s_info.state == CAR_CONNECTION_STATE_CONNECTED || s_info.state == CAR_CONNECTION_STATE_CONNECTING)
@@ -280,7 +309,6 @@ static gboolean _send_keep_alive()
 {
        if(s_info.state == CAR_CONNECTION_STATE_DISCONNECTED) {
                s_info.keep_alive_attempts_left = 0;
-               s_info.keep_alive_timer = 0;
                return FALSE;
        }
 
@@ -309,6 +337,30 @@ static gboolean _send_keep_alive()
        return TRUE;
 }
 
+static gboolean _send_user_name(gpointer data)
+{
+       if(s_info.state != CAR_CONNECTION_STATE_CONNECTED) {
+               return FALSE;
+       }
+
+       message_t *message = message_factory_create_message(s_info.message_factory, MESSAGE_CONFIG_USER_NAME);
+       if(!message) {
+               _E("Failed to create CONFIG_USER_NAME message");
+               return TRUE;
+       }
+       message_set_receiver(message, s_info.car_address, s_info.car_port);
+       message_config_user_name_set_name((message_config_user_name_t*)message, (const char*)data);
+       int res = message_manager_send_message(message);
+       if(res) {
+               _E("Failed to send user name");
+               message_destroy(message);
+               return TRUE;
+       }
+       s_info.user_name_serial = message_get_serial(message);
+       message_destroy(message);
+       return TRUE;
+}
+
 static gboolean _keep_alive_cb(gpointer data)
 {
        return _send_keep_alive();
@@ -337,6 +389,13 @@ static int _start_keep_alive_timer()
        return -1;
 }
 
+static int _start_user_name_timer(const char *name)
+{
+       _send_user_name((gpointer)name);
+       s_info.user_name_timer = g_timeout_add(USER_NAME_INTERVAL, _send_user_name, (gpointer)name);
+       return 0;
+}
+
 static void _reset_counters()
 {
        s_info.keep_alive_attempts_left = HELLO_START_ATTEMPTS;
index da3e3b6..8567da5 100644 (file)
@@ -16,7 +16,6 @@
 
 
 #include "gear-racing-controller.h"
-#include "model/model_cloud_connection.h"
 #include "model/model_car_connection.h"
 #include "controller/controller_name_input.h"
 #include "view_manager/view_manager.h"
@@ -25,14 +24,12 @@ static s_controller s_info = {  0, };
 
 void controller_name_input_destroy(void)
 {
-       model_cloud_connection_shutdown();
        s_info.view_update_cb = NULL;
 }
 
 void controller_name_input_init(t_view_update_cb view_update_cb)
 {
        s_info.view_update_cb = view_update_cb;
-       model_cloud_connection_init();
 }
 
 void controller_name_input_back(void)
@@ -51,6 +48,6 @@ void controller_name_input_next()
 
 void controller_player_name_set(const char *player_name)
 {
-       model_cloud_connection_player_name_set(player_name);
+       model_car_connection_player_name_set(player_name);
 }
 
index 03ec278..0ca2638 100644 (file)
@@ -25,6 +25,7 @@
 #include "messages/message_connect_accepted.h"
 #include "messages/message_connect_refused.h"
 #include "messages/message_keep_alive.h"
+#include "messages/message_config_user_name.h"
 
 struct _message_factory {
        union {
@@ -35,6 +36,7 @@ struct _message_factory {
                message_connect_accepted_t connect_accepted;
                message_connect_refused_t connect_refused;
                message_keep_alive_t keep_alive;
+               message_config_user_name_t config_user_name;
        } messages;
 };
 
@@ -62,6 +64,9 @@ message_t *message_factory_create_message(message_factory_t *factory, message_ty
        case MESSAGE_BYE:
                message_bye_init(&factory->messages.bye);
                return &factory->messages.bye.base;
+       case MESSAGE_CONFIG_USER_NAME:
+               message_config_user_name_init(&factory->messages.config_user_name);
+               return &factory->messages.config_user_name.base;
        default:
                return NULL;
        }
index 5c7378e..1174a9a 100644 (file)
@@ -29,7 +29,7 @@
 #include "gear-racing-controller.h"
 #include "log.h"
 
-#define SEND_TIMEOUT 20
+#define SEND_TIMEOUT 30
 #define DIRECTION_BASE_VALUE 10000
 #define THROTTLE_BASE_VALUE 10000
 #define HELLO_TIMER_WAIT 3.0
@@ -37,7 +37,7 @@
 typedef struct _s_car_model_connection {
        t_model_car_connection_update_cb controller_update_cb;
        bool ready_to_drive;
-
+       char player_name[PLAYER_NAME_MAX_LEN + 1];
        float direction;
        float throttle;
        float cam_elevation;
@@ -162,10 +162,24 @@ void model_car_connection_set_stop(bool stop)
        }
 }
 
-bool model_car_connection_is_connected() {
+bool model_car_connection_is_connected()
+{
        return CAR_CONNECTION_STATE_CONNECTED == car_connection_manager_get_state();
 }
 
+bool model_car_connection_player_name_set(const char *player_name)
+{
+       if(!model_car_connection_is_connected()) {
+               _E("Connection hasn't been established");
+               return false;
+       }
+
+       strncpy(s_info.player_name, player_name, PLAYER_NAME_MAX_LEN);
+
+       car_connection_manager_send_user_name(s_info.player_name);
+       return true;
+}
+
 void _connection_state_cb(car_connection_state_e previous, car_connection_state_e current) {
        static s_model_car_connection_cb_data model_data;
        if(current == CAR_CONNECTION_STATE_CONNECTING) {
index 4493633..16fcfbf 100644 (file)
@@ -24,7 +24,6 @@
 typedef struct _s_cloud_model_connection {
        t_model_cloud_connection_update_cb controller_update_cb;
        t_model_cloud_connection_update_cb reconnect_cb;
-       char player_name[PLAYER_NAME_MAX_LEN + 1];
        car_info_t **cars;
        int cars_size;
        gboolean is_initialized;
@@ -78,18 +77,6 @@ void model_cloud_connection_unsubscirbe_event(void)
        model_cloud_connection.reconnect_cb = NULL;
 }
 
-bool model_cloud_connection_player_name_set(const char *player_name)
-{
-       if(!model_cloud_connection.is_initialized) {
-               _E("Model cloud connection hasn't been initialized");
-               return false;
-       }
-
-       strncpy(model_cloud_connection.player_name, player_name, PLAYER_NAME_MAX_LEN);
-       //In case of failure, return 0
-       return true;
-}
-
 void model_cloud_connection_subscribe_reconnect(t_model_cloud_connection_update_cb model_update_cb)
 {
        if(!model_cloud_connection.is_initialized) {