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
229
230
231
232
233
234
235
236
|
package pl.edu.mimuw.cloudatlas.agent.modules;
import com.esotericsoftware.kryo.Kryo;
import com.esotericsoftware.kryo.Serializer;
import com.esotericsoftware.kryo.io.Input;
import com.esotericsoftware.kryo.io.Output;
import org.assertj.core.data.MapEntry;
import pl.edu.mimuw.cloudatlas.agent.messages.*;
import pl.edu.mimuw.cloudatlas.interpreter.query.Absyn.Program;
import pl.edu.mimuw.cloudatlas.model.*;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.rmi.Remote;
import java.text.DateFormat;
import java.util.*;
/**
* Serializes classes to and from byte arrays for UDP use
*/
public class UDUPSerializer {
private Kryo kryo;
UDUPSerializer() {
kryo = new Kryo();
kryo.setReferences(true);
kryo.setRegistrationRequired(true);
registerClasses();
}
private void registerClasses() {
kryo.register(Inet4Address.class, new Serializer() {
@Override
public void write(Kryo kryo, Output output, Object object) {
InetAddress ia = (InetAddress) object;
kryo.writeObject(output, ia.getAddress());
}
@Override
public Object read(Kryo kryo, Input input, Class type) {
try {
byte[] buf = kryo.readObject(input, byte[].class);
InetAddress addr = Inet4Address.getByAddress(buf);
return addr;
} catch (UnknownHostException e) {
System.out.println("Custom InetAddress read failed");
e.printStackTrace();
return null;
}
}
});
kryo.register(PathName.class, new Serializer() {
@Override
public void write(Kryo kryo, Output output, Object object) {
PathName pn = (PathName) object;
kryo.writeObject(output, pn.getName());
}
@Override
public Object read(Kryo kryo, Input input, Class type) {
String addr = input.readString();
return new PathName(addr);
}
});
kryo.register(ValueList.class, new Serializer() {
@Override
public void write(Kryo kryo, Output output, Object object) {
ValueList vl = (ValueList) object;
kryo.writeObject(output, ((TypeCollection) vl.getType()).getElementType());
ArrayList<Value> al = new ArrayList<>();
for (Value v : vl.getValue()) {
al.add(v);
}
kryo.writeObject(output, al);
}
@Override
public Object read(Kryo kryo, Input input, Class type) {
TypePrimitive t = kryo.readObject(input, TypePrimitive.class);
ArrayList list = kryo.readObject(input, ArrayList.class);
return new ValueList(list, t);
}
});
kryo.register(ValueSet.class, new Serializer() {
@Override
public void write(Kryo kryo, Output output, Object object) {
ValueSet vs = (ValueSet) object;
kryo.writeObject(output, ((TypeCollection) vs.getType()).getElementType());
HashSet<Value> hs = new HashSet();
for (Value v : vs.getValue()) {
hs.add(v);
}
kryo.writeObject(output, hs);
}
@Override
public Object read(Kryo kryo, Input input, Class type) {
TypePrimitive t = kryo.readObject(input, TypePrimitive.class);
HashSet set = kryo.readObject(input, HashSet.class);
return new ValueSet(set, t);
}
});
kryo.register(AttributesMap.class, new Serializer() {
@Override
public void write(Kryo kryo, Output output, Object object) {
AttributesMap attribMap = (AttributesMap) object;
HashMap<Attribute, Value> hashMap = new HashMap<>();
for (Map.Entry<Attribute, Value> e : attribMap) {
hashMap.put(e.getKey(), e.getValue());
}
kryo.writeObject(output, hashMap);
}
@Override
public Object read(Kryo kryo, Input input, Class type) {
HashMap<Attribute, Value> hashMap = kryo.readObject(input, HashMap.class);
AttributesMap attribMap = new AttributesMap();
for (Map.Entry<Attribute, Value> e : hashMap.entrySet()) {
attribMap.add(e.getKey(), e.getValue());
}
return attribMap;
}
});
kryo.register(ValueQuery.class, new Serializer() {
@Override
public void write(Kryo kryo, Output output, Object object) {
ValueQuery vq = (ValueQuery) object;
kryo.writeObject(output, vq.getCode());
}
@Override
public Object read(Kryo kryo, Input input, Class type) {
String code = kryo.readObject(input, String.class);
ValueQuery vq = null;
try {
vq = new ValueQuery(code);
} catch (Exception e) {
System.out.println("Value query deserialization failed");
e.printStackTrace();
}
return vq;
}
});
// model
kryo.register(Value.class);
kryo.register(ValueBoolean.class);
kryo.register(ValueContact.class);
kryo.register(ValueDouble.class);
kryo.register(ValueDuration.class);
kryo.register(ValueDouble.class);
kryo.register(ValueInt.class);
kryo.register(ValueNull.class);
kryo.register(ValueSet.class);
kryo.register(ValueString.class);
kryo.register(ValueTime.class);
kryo.register(ValueUtils.class);
kryo.register(ZMI.class);
kryo.register(Attribute.class);
kryo.register(AttributesMap.class);
kryo.register(AttributesUtil.class);
kryo.register(Type.class);
kryo.register(Type.PrimaryType.class);
kryo.register(TypeCollection.class);
kryo.register(TypePrimitive.class);
// messages in chronological order so it's easier to keep track
kryo.register(AgentMessage.class);
kryo.register(AttributesMessage.class);
kryo.register(GetStateMessage.class);
kryo.register(HejkaMessage.class);
kryo.register(NoCoTamMessage.class);
kryo.register(QueryMessage.class);
kryo.register(QurnikMessage.class);
kryo.register(RemikMessage.class);
kryo.register(RemoveZMIMessage.class);
kryo.register(RequestStateMessage.class);
kryo.register(ResponseMessage.class);
kryo.register(RunQueriesMessage.class);
kryo.register(SetAttributeMessage.class);
kryo.register(StanikMessage.Type.class);
kryo.register(StanikMessage.class);
kryo.register(TimerSchedulerMessage.class);
kryo.register(UDUPMessage.class);
kryo.register(UpdateAttributesMessage.class);
kryo.register(UpdateQueriesMessage.class);
kryo.register(GossipGirlMessage.class);
kryo.register(GossipGirlMessage.Type.class);
kryo.register(RemoteGossipGirlMessage.class);
// modules
kryo.register(TimerScheduledTask.class);
kryo.register(RecursiveScheduledTask.class);
// other
kryo.register(byte[].class);
kryo.register(LinkedHashMap.class);
kryo.register(HashMap.class);
kryo.register(HashSet.class);
kryo.register(ModuleType.class);
kryo.register(DateFormat.class);
kryo.register(ArrayList.class);
}
public UDUPMessage deserialize(byte[] packetData) {
ByteArrayInputStream in = new ByteArrayInputStream(packetData);
Input kryoInput = new Input(in);
UDUPMessage msg = kryo.readObject(kryoInput, UDUPMessage.class);
return msg;
}
public byte[] serialize(UDUPMessage msg) {
ByteArrayOutputStream out = new ByteArrayOutputStream();
Output kryoOut = new Output(out);
System.out.println("SERIALIZING " + msg.getContent());
kryo.writeObject(kryoOut, msg);
kryoOut.flush();
kryoOut.close();
return out.toByteArray();
}
}
|