-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPeer.java
More file actions
164 lines (133 loc) · 4.41 KB
/
Peer.java
File metadata and controls
164 lines (133 loc) · 4.41 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
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
package wish;
import org.bson.BSONException;
import org.bson.BsonBinary;
import org.bson.BsonBinaryWriter;
import org.bson.BsonDocument;
import org.bson.BsonWriter;
import org.bson.RawBsonDocument;
import org.bson.io.BasicOutputBuffer;
import java.io.Serializable;
import java.util.Arrays;
/**
* Created by jeppe on 11/16/16.
*/
public class Peer implements Serializable {
private byte[] luid;
private byte[] ruid;
private byte[] rhid;
private byte[] rsid;
private String protocol;
private boolean online;
private static int WISH_UID_LEN = 32;
public static Peer fromBson(byte[] data) {
try {
return fromBson(new RawBsonDocument(data));
} catch (BSONException e) {
return null;
}
}
public static Peer fromBson(BsonDocument bsonDocument) {
Peer peer = new Peer();
try {
if (bsonDocument.containsKey("luid")
&& bsonDocument.containsKey("ruid")
&& bsonDocument.containsKey("rhid")
&& bsonDocument.containsKey("protocol")
&& bsonDocument.containsKey("online")) {
peer.luid = bsonDocument.get("luid").asBinary().getData();
peer.ruid = bsonDocument.get("ruid").asBinary().getData();
peer.rhid = bsonDocument.get("rhid").asBinary().getData();
peer.rsid = bsonDocument.get("rsid").asBinary().getData();
peer.protocol = bsonDocument.get("protocol").asString().getValue();
peer.online = bsonDocument.get("online").asBoolean().getValue();
if (peer.luid.length != WISH_UID_LEN) {
return null;
}
if (peer.ruid.length != WISH_UID_LEN) {
return null;
}
if (peer.rhid.length != WISH_UID_LEN) {
return null;
}
if (peer.rsid.length != WISH_UID_LEN) {
return null;
}
} else {
return null;
}
} catch (BSONException e) {
return null;
}
return peer;
}
public byte[] toBson() {
BasicOutputBuffer buffer = new BasicOutputBuffer();
BsonWriter writer = new BsonBinaryWriter(buffer);
writer.writeStartDocument();
writer.writeBinaryData("luid", new BsonBinary(getLuid()));
writer.writeBinaryData("ruid", new BsonBinary(getRuid()));
writer.writeBinaryData("rhid", new BsonBinary(getRhid()));
writer.writeBinaryData("rsid", new BsonBinary(getRsid()));
writer.writeString("protocol", getProtocol());
writer.writeBoolean("online", isOnline());
writer.writeEndDocument();
writer.flush();
return buffer.toByteArray();
}
public byte[] getLuid() {
return luid;
}
public byte[] getRuid() {
return ruid;
}
public byte[] getRhid() {
return rhid;
}
public byte[] getRsid() {
return rsid;
}
public String getProtocol() {
return protocol;
}
public boolean isOnline() {
return online;
}
private String byteArrayAsString(byte[] array) {
String ret = new String();
if (array == null) {
ret = "null";
} else {
for (int i = 0; i < array.length; i++) {
ret += "0x" + Integer.toHexString(array[i]) + ", ";
}
}
return ret;
}
public String toString() {
String s = new String();
s = "luid: " + byteArrayAsString(luid);
s += " ruid: " + byteArrayAsString(ruid);
s += " rhid: " + byteArrayAsString(rhid);
s += " rsid: " + byteArrayAsString(rsid);
s += " protocol: " + protocol;
s += online ? " online" : " offline";
return s;
}
public boolean equals(Object object) {
if (this == object) {
return true;
}
if (object == null) {
return false;
}
if (getClass() != object.getClass()) {
return false;
}
Peer peer = (Peer) object;
return Arrays.equals(luid, peer.getLuid())
&& Arrays.equals(ruid, peer.getRuid())
&& Arrays.equals(rhid, peer.getRhid())
&& Arrays.equals(rsid, peer.getRsid())
&& protocol.equals(peer.getProtocol());
}
}