liukangdong
2024-05-21 4658e9353f7aa76a833d36ed988d77bf362ad0ea
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
package cn.emay.sdk.util;
 
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
 
import cn.emay.sdk.common.CommonConstants;
import cn.emay.sdk.core.dto.cache.UrlDTO;
import cn.emay.sdk.core.dto.sms.common.ResultModel;
import cn.emay.sdk.core.dto.sms.request.BalanceRequest;
import cn.emay.sdk.core.dto.sms.response.BalanceResponse;
import cn.emay.sdk.util.http.client.EmayHttpClient;
import cn.emay.sdk.util.http.request.impl.EmayHttpRequestString;
import cn.emay.sdk.util.http.response.impl.string.EmayHttpResponseString;
import cn.emay.sdk.util.http.response.impl.string.EmayHttpResponseStringPraser;
import cn.emay.sdk.util.json.JsonHelper;
import cn.emay.sdk.util.json.gson.JsonArray;
import cn.emay.sdk.util.json.gson.JsonElement;
import cn.emay.sdk.util.json.gson.JsonParser;
import cn.emay.sdk.util.json.gson.reflect.TypeToken;
 
public class HostUtil {
 
    public static String getUrl(String appId, String secretkey) {
        String host = "";
        List<String> list = CommonConstants.interList;
        try {
            if (list != null) {
                long minUseTime = -1l;
                for (String url : list) {
                    long useTime = getBalance(appId, secretkey, url);
                    if (useTime == -1) {
                        continue;
                    } else if (minUseTime == -1 || minUseTime > useTime) {
                        minUseTime = useTime;
                        host = url;
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return host;
    }
 
    public static void getSDKInter() {
 
        EmayHttpClient client = new EmayHttpClient();
        EmayHttpRequestString request = null;
        try {
            request = new EmayHttpRequestString(CommonConstants.getSdkUrl, "UTF-8", "POST", null, null, null);
            EmayHttpResponseString res = client.service(request, new EmayHttpResponseStringPraser());
            if (res != null && res.getResultCode().getCode().equals("SUCCESS")) {
                String json = res.getResultString();
                if (json != null && !json.equals("")) {
                    List<String> list = new ArrayList<String>();
                    JsonArray array = new JsonParser().parse(json).getAsJsonObject().getAsJsonArray("result");
                    Iterator<JsonElement> iterator = array.iterator();
                    while (iterator.hasNext()) {
                        JsonElement element = iterator.next();
                        String ip = element.getAsJsonObject().get("ip").getAsString();
                        Integer port = element.getAsJsonObject().get("port").getAsInt();
                        list.add("http://" + ip + ":" + port);
                    }
                    CommonConstants.interList = list;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
 
    }
 
    public static long getBalance(String appId, String secretKey, String host) {
        long useTime = 0l;
        try {
            long time = System.currentTimeMillis();
            BalanceRequest request = new BalanceRequest();
            ResultModel<BalanceResponse> result = HttpUtil.request(appId, secretKey, host + "/inter/getBalance", request, BalanceResponse.class);
            if (result != null && result.getCode().equals("SUCCESS")) {
                useTime = System.currentTimeMillis() - time;
            } else {
                useTime = -1l;
            }
        } catch (Exception e) {
            e.printStackTrace();
            useTime = -1l;
        }
        return useTime;
 
    }
 
    public static List<Entry<String, Integer>> sortMap(Map<String, Integer> map) {
        List<Entry<String, Integer>> list = new ArrayList<Entry<String, Integer>>(map.entrySet());
        Collections.sort(list, new Comparator<Map.Entry<String, Integer>>() {
            public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) {
                return (o2.getValue() - o1.getValue());
            }
        });
        return list;
    }
 
    public static File creatCacheFile(String filePath) {
        File file = null;
        try {
            if (StringUtil.isEmpty(filePath)) {
                file = new File(CommonConstants.fileName);
                if (!file.exists()) {
                    file.createNewFile();
                }
                return file;
            }
            file = new File(filePath);
            if (!file.exists()) {
                File fileParent = file.getParentFile();
                if (!fileParent.exists()) {
                    fileParent.mkdirs();
                }
                file.createNewFile();
            }
            if (!file.canWrite() || !file.canRead()) {
                return null;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        return file;
    }
 
    public static boolean writeFile(File file, String content) {
        BufferedOutputStream bos = null;
        try {
            bos = new BufferedOutputStream(new FileOutputStream(file));
            bos.write(content.getBytes("UTF-8"));
            bos.flush();
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        } finally {
            try {
                bos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return true;
    }
 
    public static String readFile(File file) {
        long fileSize = file.length();
        String json = "";
        if (fileSize > Integer.MAX_VALUE) {
            return null;
        }
        byte[] by = new byte[(int) fileSize];
        try {
            FileInputStream in = new FileInputStream(file);
            int offset = 0;
            int numRead = 0;
            while (offset < by.length && (numRead = in.read(by, offset, by.length - offset)) >= 0) {
                offset += numRead;
            }
            json = new String(by, "UTF-8");
            in.close();
        } catch (Exception e) {
            return null;
        }
        return json;
    }
 
    public static void contrastWrite(File file, String appId, String host) {
        List<UrlDTO> list = new ArrayList<UrlDTO>();
        String json = readFile(file);
        if (StringUtil.isEmpty(json)) {
            UrlDTO dto = new UrlDTO(appId, host);
            list.add(dto);
            json = JsonHelper.toJsonString(list);
            writeFile(file, json);
        } else {
            list = JsonHelper.fromJson(new TypeToken<List<UrlDTO>>() {
            }, json);
            Map<String, String> map = new HashMap<String, String>();
            for (UrlDTO urlDTO : list) {
                map.put(urlDTO.getAppId(), urlDTO.getUrl());
                if (urlDTO.getAppId().equals(appId) && !urlDTO.getUrl().equals(host)) {
                    urlDTO.setUrl(host);
                    json = JsonHelper.toJsonString(list);
                    writeFile(file, json);
                    break;
                }
 
            }
            if (!map.containsKey(appId)) {
                list.add(new UrlDTO(appId, host));
                writeFile(file, JsonHelper.toJsonString(list));
            }
        }
    }
 
    public static String getFileUrl(File file, String appId) {
        String url = "";
        String json = readFile(file);
        if (StringUtil.isEmpty(json)) {
            return url;
        } else {
            List<UrlDTO> list = JsonHelper.fromJson(new TypeToken<List<UrlDTO>>() {
            }, json);
            for (UrlDTO urlDTO : list) {
                if (urlDTO.getAppId().equals(appId)) {
                    url = urlDTO.getUrl();
                    break;
                }
            }
        }
        return url;
    }
}