/*
 * Copyright (c) 2015 LingoChamp Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.liulishuo.filedownloader.message;

import com.liulishuo.filedownloader.model.FileDownloadStatus;

/**
 * Created by Jacksgong on 4/30/16.
 * <p/>
 * The internal message snapshot station.
 * Making message snapshots keep flowing in order.
 */
public class MessageSnapshotFlow {

    private MessageSnapshotThreadPool flowThreadPool;
    private MessageReceiver receiver;

    public final static class HolderClass {
        private final static MessageSnapshotFlow INSTANCE = new MessageSnapshotFlow();
    }

    public static MessageSnapshotFlow getImpl() {
        return HolderClass.INSTANCE;
    }

    public void setReceiver(MessageReceiver receiver) {
        this.receiver = receiver;
        flowThreadPool = new MessageSnapshotThreadPool(5, receiver);
    }

    public void inflow(final MessageSnapshot snapshot) {
        switch (snapshot.getStatus()) {
            case FileDownloadStatus.warn:
                receiver.receive(snapshot);
                return;
            case FileDownloadStatus.completed:
                if (snapshot.isReusedDownloadedFile()) {
                    receiver.receive(snapshot);
                    return;
                }
        }

        flowThreadPool.execute(snapshot);
    }


    public interface MessageReceiver {
        void receive(MessageSnapshot snapshot);
    }
}

@@ -895,24 +895,33 @@
     private 
+volatile 
 MessageSnaps
@@ -962,16 +962,25 @@
 private 
+volatile 
 MessageR
@@ -1320,32 +1320,130 @@
 ceiver;%0A        
+if (receiver == null) %7B%0A            this.flowThreadPool = null;%0A        %7D else %7B%0A            this.
 flowThreadPool =
@@ -1487,16 +1487,26 @@
 eiver);%0A
+        %7D%0A
     %7D%0A%0A 
@@ -1557,24 +1557,71 @@
 snapshot) %7B%0A
+        boolean isReceivedImmediately = false;%0A
         swit
@@ -1710,34 +1710,36 @@
     
-r
+isR
 eceive
-r.receive(snapshot)
+dImmediately = true
 ;%0A  
@@ -1744,38 +1744,37 @@
                 
+b
 re
-turn
+ak
 ;%0A            ca
@@ -1859,32 +1859,207 @@
 edFile()) %7B%0A    
+                isReceivedImmediately = true;%0A                    break;%0A                %7D%0A        %7D%0A%0A        if (isReceivedImmediately) %7B%0A            if (receiver != null) %7B%0A
                 
@@ -2098,24 +2098,26 @@
         
+%7D%0A
         
 return;%0A
@@ -2112,15 +2112,16 @@
     
-return;
+%7D else %7B
 %0A   
@@ -2121,37 +2121,61 @@
 e %7B%0A            
-    %7D
+if (flowThreadPool != null) %7B
 %0A        %7D%0A%0A    
@@ -2159,35 +2159,32 @@
 null) %7B%0A        
-%7D%0A%0A
         flowThre
@@ -2205,24 +2205,49 @@
 (snapshot);%0A
+            %7D%0A        %7D%0A%0A
     %7D%0A%0A%0A    

fix occur the NPE-crash when execute receiving snapshot-message after FileDownloader#service already onDestory Closes #213