DeteMin
2020-03-31 77c62e023d2dc31200fc696158df84b3aee90ee7
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
package com.basic.security.manager.impl.erlang;
 
import android.text.TextUtils;
 
import com.basic.security.utils.Constants;
import com.basic.security.utils.ErlangLocalNodeName;
import com.ericsson.otp.erlang.OtpConnection;
import com.ericsson.otp.erlang.OtpPeer;
import com.ericsson.otp.erlang.OtpSelf;
 
import java.util.HashMap;
import java.util.Map;
 
public class ErlangConnection extends ErBaseManger {
 
    static OtpConnection localConnection = null;
    static Map<String, OtpConnection> remoteConnectionMap = new HashMap<>();
 
    public static OtpConnection getLocalConnection() {
        if (localConnection == null || !localConnection.isConnected()) {
            try {
                OtpSelf node = new OtpSelf(ErlangLocalNodeName.getNodeName());
                node.setCookie("123");
                OtpPeer otpPeer = new OtpPeer(Constants.erlangLocalNode);
                localConnection = node.connect(otpPeer);
            } catch (Exception e) {
//                System.out.println("getLocalConnection " + e.getMessage());
            }
        }
        return localConnection;
    }
 
    public static OtpConnection getRemoteConnection(String nodeName) {
        OtpConnection remoteConnection = remoteConnectionMap.get(nodeName);
        if (remoteConnection == null || !remoteConnection.isConnected()) {
            try {
                String remoteNodeName = NodeNameManager.getNodeNameByNodeName(nodeName);
                if (!TextUtils.isEmpty(remoteNodeName)) {
                    OtpSelf node = new OtpSelf(ErlangLocalNodeName.getNodeName());
                    node.setCookie("123");
                    OtpPeer otpPeer = new OtpPeer(remoteNodeName);
                    remoteConnection = node.connect(otpPeer);
                    remoteConnectionMap.put(nodeName, remoteConnection);
                } else {
//                    System.out.println(nodeName+" 不在线");
                }
            } catch (Exception e) {
                System.out.println("ErlangConnection getRemoteConnection " + e.getMessage() + " " + nodeName);
            }
        }
        return remoteConnection;
    }
 
 
}