package com.yihu.wlyy.util.fastdfs;


import org.csource.common.MyException;
import org.csource.fastdfs.*;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

/**
 * Created by szx on 2015/9/19.
 */
public class FastDFSClientPool {
    private static volatile FastDFSClientPool pool;

    private Map<StorageClient, Boolean> map;

    private int maxPoolSize = 20;
    private int waitTime = 500;
    private int initPoolSize = 5;

    static {
        try {
            
            String basePath = FastDFSClientPool.class.getResource("/").getPath();
           
            ClientGlobal.init(FastDFSUtil.class.getResource("/config/fdfs_client.conf").getPath());


        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private FastDFSClientPool() {

        init();
    }

    public static FastDFSClientPool getInstance() {
        if (pool == null) {
            synchronized (FastDFSClientPool.class) {
                if(pool == null) {
                    pool = new FastDFSClientPool();
                }
            }
        }
        System.out.println("FastDFSClientPool:"+pool.toString());
        return pool;
    }

    private void init() {
        try {

            map = new HashMap<StorageClient, Boolean>();

            //不做初始缓存
//            for (int i = 0; i < initPoolSize; i++) {
//                map.put(getNewStorageClient(), true);
//            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    public TrackerServer getTrackerServer(){
        try {
            TrackerClient tracker = new TrackerClient();
            return tracker.getConnection();
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;

    }

    private StorageClient getNewStorageClient() {
        try {
            TrackerClient tracker = new TrackerClient();
            TrackerServer trackerServer = tracker.getConnection();

            StorageClient client = new StorageClient(trackerServer, null);
            return client;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public synchronized StorageClient getStorageClient() {
        StorageClient client = null;
        try {
            for (Entry<StorageClient, Boolean> entry : map.entrySet()) {
                if (entry.getValue()) {
                    client = entry.getKey();
                    System.out.println("从缓存中获得fastdfs client");
                    map.put(client, false);
                    break;
                }
            }
            if (client == null) {
                if(map.size()<maxPoolSize) {
                    System.out.println("创建新fastdfs client:");
                    client = getNewStorageClient();
                    map.put(client, false);
                }

            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return client;
    }

    public void releaseStorageClient(StorageClient client) {
        if (client == null) {
            return;
        }
        try {
            if(map.containsKey(client)) {
                    map.put(client, true);

            } else {
                client =null;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
