Removed the restriction that transport updates have to be written in

delimited form.
This commit is contained in:
akwizgran
2011-08-14 11:41:56 +02:00
parent c2b0f0ab5a
commit 254da2da27
9 changed files with 119 additions and 46 deletions

View File

@@ -1,6 +1,7 @@
package net.sf.briar.protocol;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
@@ -13,9 +14,11 @@ import net.sf.briar.api.serial.Reader;
class TransportReader implements ObjectReader<TransportUpdate> {
private final TransportFactory transportFactory;
private final ObjectReader<TransportProperties> propertiesReader;
TransportReader(TransportFactory transportFactory) {
this.transportFactory = transportFactory;
propertiesReader = new TransportPropertiesReader();
}
public TransportUpdate readObject(Reader r) throws IOException {
@@ -23,27 +26,41 @@ class TransportReader implements ObjectReader<TransportUpdate> {
Consumer counting = new CountingConsumer(TransportUpdate.MAX_SIZE);
// Read the data
r.addConsumer(counting);
r.readUserDefinedTag(Tags.TRANSPORTS);
// Transport maps are always written in delimited form
r.readUserDefinedTag(Tags.TRANSPORT_UPDATE);
r.addObjectReader(Tags.TRANSPORT_PROPERTIES, propertiesReader);
r.setMaxStringLength(TransportUpdate.MAX_SIZE);
List<TransportProperties> l = r.readList(TransportProperties.class);
r.resetMaxStringLength();
r.removeObjectReader(Tags.TRANSPORT_PROPERTIES);
Map<String, Map<String, String>> transports =
new TreeMap<String, Map<String, String>>();
r.readMapStart();
while(!r.hasMapEnd()) {
String name = r.readString(TransportUpdate.MAX_SIZE);
Map<String, String> properties = new TreeMap<String, String>();
r.readMapStart();
while(!r.hasMapEnd()) {
String key = r.readString(TransportUpdate.MAX_SIZE);
String value = r.readString(TransportUpdate.MAX_SIZE);
properties.put(key, value);
}
r.readMapEnd();
transports.put(name, properties);
}
r.readMapEnd();
for(TransportProperties t : l) transports.put(t.name, t.properties);
long timestamp = r.readInt64();
r.removeConsumer(counting);
// Build and return the transport update
return transportFactory.createTransports(transports, timestamp);
}
private static class TransportProperties {
private final String name;
private final Map<String, String> properties;
TransportProperties(String name, Map<String, String> properties) {
this.name = name;
this.properties = properties;
}
}
private static class TransportPropertiesReader
implements ObjectReader<TransportProperties> {
public TransportProperties readObject(Reader r) throws IOException {
r.readUserDefinedTag(Tags.TRANSPORT_PROPERTIES);
String name = r.readString();
Map<String, String> properties =
r.readMap(String.class, String.class);
return new TransportProperties(name, properties);
}
}
}