Use a service name to check already connected service 37/257137/4
authorSeonah Moon <seonah1.moon@samsung.com>
Tue, 20 Apr 2021 02:05:54 +0000 (11:05 +0900)
committerSeonah Moon <seonah1.moon@samsung.com>
Tue, 20 Apr 2021 04:14:52 +0000 (13:14 +0900)
Change-Id: I22c6491a091ed6d750ec85f17b45053b681b8782

plugins/libwebsockets/libwebsockets-plugin.cpp
src/include/vine-data-path.h
src/include/vine-dp.h
src/vine-data-path.cpp
src/vine-dp.cpp

index 1e6ad622261e934219dc62b63ebecb8bcb5c0135..1631b16456b72f9e6bc59a5c7dd329c4b6408bae 100755 (executable)
@@ -388,7 +388,7 @@ static int _websocket_protocol_cb(struct lws *wsi,
                        return -1;
                }
 
-               if (lws_hdr_custom_copy(wsi, service_name, sizeof(service_name),
+               if (lws_hdr_custom_copy(wsi, service_name, client_ws->token_len + 1,
                                VINE_SERVICE_NAME_HEADER, strlen(VINE_SERVICE_NAME_HEADER)) < 0) {
                        VINE_LOGE("Failed to get custom header.");
                        free(service_name);
@@ -467,7 +467,7 @@ static int _websocket_protocol_cb(struct lws *wsi,
                if (!ws || !ws->token)
                        return 0;
 
-               VINE_LOGI("Add custom header.");
+               VINE_LOGI("Add custom header. [%s]", ws->token);
                unsigned char **p = (unsigned char **) in, *end = (*p) + len;
                unsigned char hdr[VINE_SERVICE_NAME_HEADER_LEN + 1] = {0, };
                memcpy(hdr, VINE_SERVICE_NAME_HEADER, VINE_SERVICE_NAME_HEADER_LEN);
@@ -879,8 +879,6 @@ static int websocket_create(vine_dp_plugin_h *handle, void *plugin_data, void *u
        ws->close_requested = false;
        ws->curr_conn = 0;
        ws->max_conn = 0;
-       ws->token = NULL;
-       ws->token_len = 0;
        ws->user = user;
        ws->recv_buffer = new VineQueue<websocket_data_s *>;
        ws->write_buffer = new VineQueue<websocket_data_s *>;
index e622aad68c3c8d1f034bf7c1e252059aa9763995..8b9b84b90555b6f6779ebd8f9ec33b4b736b48d5 100755 (executable)
@@ -58,8 +58,8 @@ int vine_data_path_open(vine_address_family_e addr_family, int port,
                vine_data_path_h *opened_datapath,
                vine_event_queue_h event_fd);
 int vine_data_path_connect(vine_address_family_e addr_family,
-               const char *ip, int port,
-               const char *iface_name, vine_security_h security,
+               const char *ip, int port, const char *iface_name,
+               vine_security_h security, const char *token,
                vine_data_path_connected_cb callback, void *user_data,
                vine_data_path_h *connected_datapath,
                vine_event_queue_h event_fd);
index f47b354a5ec77564c54ceb92890f1bbd6b1aad20..821ffff71a0c080f2cd644d91c8708372edce1e1 100644 (file)
@@ -137,8 +137,7 @@ private:
        int mServerPort;
 };
 
-typedef std::pair<std::string, int> DPKey;
-typedef std::map<DPKey, vine_data_path_h> DPMap;
+typedef std::map<std::string, vine_data_path_h> DPMap;
 
 enum {
        VINE_DP_PUBSUB_OPEN_STATE_NONE = 0,
@@ -181,9 +180,9 @@ public:
 
        int connect(const char *ip, int port);
        int close_server_dp();
-       bool is_joined_peer(const char *service_name, const char *ip, int port);
+       bool is_joined_peer(const char *service_name, const char *ip);
        int get_joined_peer();
-       void add_joined_peer(const char *ip, int port, vine_data_path_h datapath);
+       void add_joined_peer(const char *service_name, vine_data_path_h datapath);
        void del_joined_peer(vine_data_path_h datapath);
        void clear_joined_peer();
 
index 25a62ae93af14db95f9ccbf241a033f4161bf470..3a48609aaff3ad230d1edf394fc74cd0bb36d0df 100755 (executable)
@@ -680,7 +680,7 @@ int vine_data_path_open(vine_address_family_e addr_family, int port, const char
 
 int vine_data_path_connect(vine_address_family_e addr_family,
                const char *ip, int port, const char *iface_name,
-               vine_security_h security,
+               vine_security_h security, const char *token,
                vine_data_path_connected_cb callback, void *user_data,
                vine_data_path_h *connected_datapath, vine_event_queue_h event_fd)
 {
@@ -698,7 +698,17 @@ int vine_data_path_connect(vine_address_family_e addr_family,
        vine_dp_ssl ssl = {false, VINE_DP_TLS_VERSION_DEFAULT, 0, NULL, NULL, NULL};
        _extract_security_info(security, &ssl);
 
-       int ret = g_dp_plugin_fn.connect(dp->plugin_handle,
+       int ret;
+       if (token) {
+               ret = g_dp_plugin_fn.set_token(dp->plugin_handle, token);
+               if (ret != VINE_DATA_PATH_ERROR_NONE) {
+                       _vine_data_path_destroy(dp);
+                       _destroy_security_info(&ssl);
+                       return __convert_data_path_error_to_vine_error((vine_data_path_error)ret);
+               }
+       }
+
+       ret = g_dp_plugin_fn.connect(dp->plugin_handle,
                        addr_family == VINE_ADDRESS_FAMILY_IPV4 ? VINE_DP_IPV4 : VINE_DP_IPV6,
                        ip, port, iface_name, ssl);
        _destroy_security_info(&ssl);
index 8b5ed35d49e02be9d69150eae6a1a63054143ab1..5437d3e8f691ec3b2cd164fa616e6ad3971a84dc 100644 (file)
@@ -152,13 +152,6 @@ static void _pubsub_opened_cb(vine_data_path_h datapath, int result, int port, v
                static_cast<DataPath *>(userdata)->invoke_opened_cb(ret);
                return;
        }
-
-       ret = dp->subscribe_service();
-       if (ret != VINE_ERROR_NONE) {
-               dp->close();
-               static_cast<DataPath *>(userdata)->invoke_opened_cb(ret);
-               return;
-       }
 }
 
 static void _pubsub_accepted_cb(vine_data_path_h datapath, void *user_data)
@@ -174,9 +167,16 @@ static void _pubsub_accepted_cb(vine_data_path_h datapath, void *user_data)
                return;
        }
 
-       const char *ip = _vine_data_path_get_ip(datapath);
-       int port = _vine_data_path_get_port(datapath);
-       dp->add_joined_peer(ip, port, datapath);
+       char *token = NULL;
+       if (vine_data_path_get_token(datapath, &token) != VINE_ERROR_NONE) {
+               VINE_LOGE("Cannot find peer's service name. Ignore[%p]", datapath);
+               dp->decrease_init_disc_num();
+               _vine_data_path_close(datapath);
+               return;
+       }
+
+       dp->add_joined_peer(token, datapath);
+       free(token);
 
        _vine_data_path_set_received_cb(datapath, _pubsub_received_cb, user_data);
        vine_data_path_set_terminated_cb(datapath, _pubsub_terminated_cb, user_data);
@@ -201,10 +201,16 @@ static void _pubsub_connected_cb(vine_data_path_h datapath, int result, void *us
                return;
        }
 
-       const char *ip = _vine_data_path_get_ip(datapath);
-       int port = _vine_data_path_get_port(datapath);
+       char *token = NULL;
+       if (vine_data_path_get_token(datapath, &token) != VINE_ERROR_NONE) {
+               VINE_LOGE("Cannot find peer's service name. Ignore[%p]", datapath);
+               dp->decrease_init_disc_num();
+               _vine_data_path_close(datapath);
+               return;
+       }
 
-       dp->add_joined_peer(ip, port, datapath);
+       dp->add_joined_peer(token, datapath);
+       free(token);
 
        _vine_data_path_set_received_cb(datapath, _pubsub_received_cb, user_data);
        vine_data_path_set_terminated_cb(datapath, _pubsub_terminated_cb, user_data);
@@ -251,7 +257,7 @@ static void _ip_resolved_cb(vine_disc_h disc, vine_service_h service, bool add,
 
        DPPubSub *dp = static_cast<DPPubSub *>(user_data);
        int port = _vine_service_get_port(service);
-       if (dp->is_joined_peer(_vine_service_get_name(service), ip, port)) {
+       if (dp->is_joined_peer(_vine_service_get_name(service), ip)) {
                VINE_LOGD("%s:%d was already joined.", ip, port);
                return;
        }
@@ -354,6 +360,12 @@ static void _service_published_cb(vine_disc_h disc,
 
        DPPubSub *dp = static_cast<DPPubSub *>(user_data);
        dp->set_service_name(service_name);
+       int ret = dp->subscribe_service();
+       if (ret != VINE_ERROR_NONE) {
+               dp->close();
+               static_cast<DataPath *>(user_data)->invoke_opened_cb(ret);
+               return;
+       }
 }
 
 int DataPath::set_security(void *security)
@@ -618,7 +630,8 @@ int DPClient::open(vine_dp_opened_cb callback, void *user_data)
        mOpenedCbData = user_data;
 
        return vine_data_path_connect(mAddrFamily, mServerIp.c_str(), mServerPort,
-                       iface_name, mSecurity, _connected_cb, static_cast<void *>(this), &mDataPath, mEventFd);
+                       iface_name, mSecurity, NULL,
+                       _connected_cb, static_cast<void *>(this), &mDataPath, mEventFd);
 }
 
 void DPClient::close()
@@ -723,8 +736,10 @@ int DPPubSub::connect(const char *ip, int port)
 {
        vine_data_path_h datapath;
        const char *iface_name = mIfaceName.size() > 0 ? mIfaceName.c_str() : NULL;
-       int ret = vine_data_path_connect(mAddrFamily, ip, port, iface_name, mSecurity,
-                       _pubsub_connected_cb, static_cast<void *>(this), &datapath, mEventFd);
+       int ret = vine_data_path_connect(mAddrFamily, ip, port, iface_name,
+                       mSecurity, mServiceName.c_str(),
+                       _pubsub_connected_cb, static_cast<void *>(this),
+                       &datapath, mEventFd);
        return ret;
 }
 
@@ -896,7 +911,7 @@ int DPPubSub::recv(unsigned char *buf, size_t buf_len, size_t *read_len)
        return VINE_ERROR_NONE;
 }
 
-bool DPPubSub::is_joined_peer(const char *service_name, const char *ip, int port)
+bool DPPubSub::is_joined_peer(const char *service_name, const char *ip)
 {
        if (mServiceName.compare(service_name) == 0) {
                VINE_LOGD("It's me!");
@@ -904,11 +919,7 @@ bool DPPubSub::is_joined_peer(const char *service_name, const char *ip, int port
                return true;
        }
 
-       // TODO: compare the service name.
-       // service name might be unique.
-
-       DPKey key = std::make_pair(ip, port);
-       return (mDataPathList.count(key) > 0);
+       return (mDataPathList.count(string(service_name)) > 0);
 }
 
 int DPPubSub::get_joined_peer()
@@ -916,14 +927,13 @@ int DPPubSub::get_joined_peer()
        return mDataPathList.size();
 }
 
-void DPPubSub::add_joined_peer(const char *ip, int port, vine_data_path_h datapath)
+void DPPubSub::add_joined_peer(const char *service_name, vine_data_path_h datapath)
 {
-       if (!ip || !datapath)
+       if (!service_name || !datapath)
                return;
 
-       VINE_LOGD("%s:%d, %p is added.", ip, port, datapath);
-       DPKey key = std::make_pair(ip, port);
-       mDataPathList.insert(std::make_pair(key, datapath));
+       VINE_LOGD("%s, %p is added.", service_name, datapath);
+       mDataPathList.insert(std::make_pair(string(service_name), datapath));
 }
 
 void DPPubSub::del_joined_peer(vine_data_path_h datapath)
@@ -933,7 +943,7 @@ void DPPubSub::del_joined_peer(vine_data_path_h datapath)
 
        DPMap::iterator found = std::find_if(mDataPathList.begin(),
                        mDataPathList.end(),
-                       [datapath](std::pair<DPKey, vine_data_path_h> const& item)
+                       [datapath](std::pair<std::string, vine_data_path_h> const& item)
                        {
                                VINE_LOGD("item.second[%p]", item.second);
                                return (item.second == datapath);