Add to search download id from history db
[profile/ivi/download-provider.git] / src / download-provider-receiver.c
index 3a06667..a2146b5 100644 (file)
@@ -392,12 +392,24 @@ int _handle_new_connection(download_clientinfo_slot *clientinfo_list, download_c
                                                request_clientinfo->requestinfo->requestid);
                if (searchindex < 0) {
                        TRACE_DEBUG_INFO_MSG("Not Found Same Request ID");
-                       // Invalid id
-                       request_clientinfo->state = DOWNLOAD_STATE_FAILED;
-                       request_clientinfo->err = DOWNLOAD_ERROR_INVALID_PARAMETER;
-                       ipc_send_request_stateinfo(request_clientinfo);
-                       clear_clientinfo(request_clientinfo);
-                       return 0;
+                       /* Try to search history db */
+                       download_dbinfo *dbinfo = download_provider_db_history_get_info(
+                                       request_clientinfo->requestinfo->requestid);
+                       if (dbinfo == NULL) {
+                               /* Try to serach downloading db. The crashed job can not be uploaded to memory */
+                               dbinfo = download_provider_db_get_info(
+                                               request_clientinfo->requestinfo->requestid);
+                               if (dbinfo == NULL) {
+                                       /* Invalid id */
+                                       request_clientinfo->state = DOWNLOAD_STATE_FAILED;
+                                       request_clientinfo->err = DOWNLOAD_ERROR_INVALID_PARAMETER;
+                                       ipc_send_request_stateinfo(request_clientinfo);
+                                       clear_clientinfo(request_clientinfo);
+                                       return 0;
+                               }
+                       }
+                       download_provider_db_info_free(dbinfo);
+                       free(dbinfo);
                } else {        // found request id. // how to deal etag ?
                        // connect to slot.
                        TRACE_DEBUG_INFO_MSG("Found Same Request ID slot[%d]", searchindex);
@@ -408,7 +420,6 @@ int _handle_new_connection(download_clientinfo_slot *clientinfo_list, download_c
                        // change to new socket.
                        clientinfo_list[searchindex].clientinfo->clientfd =
                                request_clientinfo->clientfd;
-                       ipc_send_request_stateinfo(clientinfo_list[searchindex].clientinfo);
                        // update some info.
                        clientinfo_list[searchindex].clientinfo->requestinfo->callbackinfo =
                                request_clientinfo->requestinfo->callbackinfo;
@@ -417,6 +428,21 @@ int _handle_new_connection(download_clientinfo_slot *clientinfo_list, download_c
                        request_clientinfo->clientfd = 0;       // prevent to not be disconnected.
                        CLIENT_MUTEX_UNLOCK(&(request_clientinfo->client_mutex));
                        clear_clientinfo(request_clientinfo);
+
+                       if (clientinfo_list[searchindex].clientinfo->state
+                               == DOWNLOAD_STATE_READY
+                               || clientinfo_list[searchindex].clientinfo->state
+                               >= DOWNLOAD_STATE_FINISHED) {
+                               active_count = get_downloading_count(clientinfo_list);
+                               if (active_count >= DA_MAX_DOWNLOAD_REQ_AT_ONCE) {
+                                       // deal as pended job.
+                                       _change_pended_download(clientinfo_list[searchindex].clientinfo);
+                                       TRACE_DEBUG_INFO_MSG ("Pended Request is saved to [%d/%d]",
+                                       searchslot, MAX_CLIENT);
+                               } else
+                                       _create_download_thread(&clientinfo_list[searchindex]);
+                       } else
+                               ipc_send_request_stateinfo(clientinfo_list[searchindex].clientinfo);
                        return 0;
                }
        }