-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathErrors.java
More file actions
79 lines (64 loc) · 1.85 KB
/
Errors.java
File metadata and controls
79 lines (64 loc) · 1.85 KB
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
/**
* Copyright (C) 2020, ControlThings Oy Ab
*
* 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
*
* @license Apache-2.0
*/
package mist.node;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
/**
* Created by jeppe on 3/2/17.
*/
public class Errors {
private static Errors instance;
static Errors getInstance() {
if (instance == null) {
instance = new Errors();
}
return instance;
}
private int id;
private HashMap<Integer, ListenCb> nodeCbHashMap;
private Errors() {
id = 1;
nodeCbHashMap = new HashMap<>();
MistNode.getInstance().registerRpcErrorHandler(new MistNode.Error() {
@Override
public void cb(int code, String msg) {
nodeError(code, msg);
}
});
}
private int registerCb(ListenCb cb) {
nodeCbHashMap.put(id, cb);
return id++;
}
public static int listen(ListenCb callback) {
return getInstance().registerCb(callback);
}
public interface ListenCb {
public void cb(int code, String msg);
}
public static void cancel(int id) {
if (getInstance().nodeCbHashMap.containsKey(id)) {
getInstance().nodeCbHashMap.remove(id);
return;
}
}
private static void nodeError(int code, String msg) {
Iterator iterator = getInstance().nodeCbHashMap.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry pair = (Map.Entry) iterator.next();
ListenCb mistCb = (ListenCb) pair.getValue();
mistCb.cb(code, msg);
iterator.remove();
}
}
}