a
554325746@qq.com
2019-12-24 570a73851c26d810c2597596a8acc8a8d4cde211
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
package com.basic.security.manager;
 
import android.os.SystemClock;
import android.text.TextUtils;
 
import com.basic.security.activity.MainActivity;
import com.basic.security.base.BaseApplication;
import com.basic.security.model.ClusterSetting;
import com.basic.security.model.ModelAdapter;
import com.basic.security.model.Node;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
 
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.PriorityQueue;
import java.util.Queue;
 
import androidSync.AndroidSync;
 
public class ClusterSerfSyncManager {
    public static final Object serfQueueLock = new Object();
    public static final Object serfReceiveQueueLock = new Object();
    public static final Object serfSendQueueLock = new Object();
    public static Queue<String> serfQueue = new PriorityQueue<>();
    public static List<String> serfReceiveQueue = new ArrayList<>();
    public static List<String> serfReceiveQueueCopy = new ArrayList<>();
    public static List<String> serfSendQueueCopy = new ArrayList<>();
    public static List<String> serfSendQueue = new ArrayList<>();
    public static Comparator<Map<String, String>> mapComparator = (m1, m2) -> {
        try {
            return m1.get(Node.nodeID).compareTo(m2.get(Node.nodeID));
        } catch (Exception e) {
            return 0;
        }
    };
    static int count = 0;
    static boolean started = false;
    static boolean prevStarted = false;
    static Map<String, Map<String, String>> prevNodeIdNodeMap = new HashMap<>();
    static String serfQueueStr = "";
 
    public static void start() {
        started = false;
        prevStarted = false;
        BaseApplication.getApplication().executorService.execute(() -> {
            boolean fromTimeOut = false;
            MainActivity mainActivity = BaseApplication.getApplication().activity;
            while (true) {
                try {
//                    System.out.println("ClusterSerfSyncManager.start 2 ");
                    ModelAdapter cluster = ClusterSettingManager.cluster;
                    if (cluster.getBool(ClusterSetting.exit)) {
                        if (mainActivity.currentFragment == mainActivity.fragment_cluster) {
                            BaseApplication.getApplication().activity.fragment_cluster.updateDeviceList(new ArrayList<>());
                        }
                        prevNodeIdNodeMap.clear();
                        started = false;
                        AndroidSync.leave();
                    }
                    prevStarted = started;
                    if (!cluster.getBool(ClusterSetting.exit) && !fromTimeOut) {
                        String localNodeId = ClusterSettingManager.getLocalNodeId();
                        String getClusterId = ClusterSettingManager.getClusterId();
                        String password = ClusterSettingManager.getPassword();
                        if (
                                !TextUtils.isEmpty(localNodeId) &&
                                        !TextUtils.isEmpty(getClusterId) &&
                                        !TextUtils.isEmpty(password)
                                ) {
                            AndroidSync.leave();
                            AndroidSync.registerReceiveSqlInterfaceFromJava(ClusterSerfSyncManager::receiveData);
                            AndroidSync.initCluster();
                            AndroidSync.initAgent(localNodeId);
                            AndroidSync.syncInit(getClusterId, password, localNodeId, "");
//                            System.out.println("ClusterSerfSyncManager.start 0 " + getClusterId + " " + password + " " + localNodeId + " ");
//                            System.out.println("ClusterSerfSyncManager.start 3 " + cluster.getBool(ClusterSetting.exit));
                            started = true;
                        }
                    }
                    if (started) {
                        String otherNodeIp = ClusterSettingManager.getOtherNodeIp();
                        if (!TextUtils.isEmpty(otherNodeIp)) {
//                            AndroidSync.joinByNodeAddrs(otherNodeIp + ":30190");
                        }
                        String nodesStr = new String(AndroidSync.getNodes());
                        List<Map<String, String>> availableNodes = new ArrayList<>();
                        try {
                            Gson gson = new Gson();
                            Type typeOfListOfTypeX = new TypeToken<List<Map<String, String>>>() {
                            }.getType();
                            List<Map<String, String>> nodes = gson.fromJson(nodesStr, typeOfListOfTypeX);
                            for (Map<String, String> node : nodes) {
                                if ("1".equals(node.get("isAlive"))) {
                                    availableNodes.add(node);
                                }
                            }
                            if (mainActivity.currentFragment == mainActivity.fragment_cluster) {
                                Collections.sort(availableNodes, mapComparator);
                                BaseApplication.getApplication().activity.fragment_cluster.updateDeviceList(availableNodes);
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        if (availableNodes.size() <= 1) {
                            otherNodeIp = ClusterSettingManager.getOtherNodeIp();
                            if (!TextUtils.isEmpty(otherNodeIp)) {
                                AndroidSync.joinByNodeAddrs(otherNodeIp + ":30190");
//                                System.out.println("ClusterSerfSyncManager.start 1 " + otherNodeIp + ":30190");
                            }
                        }
                        try {
                            synchronized (serfSendQueueLock) {
                                if (serfSendQueue.size() == 0) {
                                    serfSendQueueLock.wait(3 * 1000);
                                }
                                if (serfSendQueue.size() > 0) {
                                    serfSendQueueCopy.addAll(serfSendQueue);
                                    serfSendQueue.clear();
                                }
                            }
                            for (String serfSendCopy : serfSendQueueCopy) {
//                                System.out.println("ClusterSerfSyncManager.start serfSendCopy=" + serfSendCopy);
                            }
                            serfSendQueueCopy.clear();
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        if (prevStarted != started) {
                            SystemClock.sleep(5 * 1000);
                        }
                        count++;
                    }
                    serfQueueStr = "";
                    synchronized (serfQueueLock) {
                        if (serfQueue.peek() == null) {
                            serfQueueLock.wait(3 * 1000);
                        }
                        if (serfQueue.size() > 0) {
                            serfQueueStr = serfQueue.remove();
                            fromTimeOut = false;
                        } else {
                            fromTimeOut = true;
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
        BaseApplication.getApplication().executorService.execute(() -> {
            while (true) {
                sendData("select * from table1 count=" + count + ", node=node3");
                SystemClock.sleep(4 * 1000);
            }
        });
        BaseApplication.getApplication().executorService.execute(() -> {
            while (true) {
                try {
                    synchronized (serfReceiveQueueLock) {
                        if (serfReceiveQueue.size() == 0) {
                            serfReceiveQueueLock.wait(3 * 1000);
                        }
                        if (serfReceiveQueue.size() > 0) {
                            serfReceiveQueueCopy.addAll(serfReceiveQueue);
                            serfReceiveQueue.clear();
                        }
                    }
                    for (String serfReceiveCopy : serfReceiveQueueCopy) {
                        System.out.println("ClusterSerfSyncManager.start serfReceiveCopy=" + serfReceiveCopy);
                        try {
                            DatabaseManager.execSQL(serfReceiveCopy);
                            PersonAManager.afterExecutedSql(serfReceiveCopy);
                            PersonATypeManager.afterExecutedSql(serfReceiveCopy);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                    serfReceiveQueueCopy.clear();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }
 
    private static void receiveData(String content) {
        try {
            synchronized (serfReceiveQueueLock) {
                serfReceiveQueue.add(content);
                serfReceiveQueueLock.notify();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
 
    public static void sendData(String content) {
        BaseApplication.getApplication().executorService.execute(new Runnable() {
            @Override
            public void run() {
                try {
                    synchronized (serfSendQueueLock) {
                        serfSendQueue.add(content);
                        serfSendQueueLock.notify();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }
}