use of org.bimserver.utils.GrowingByteBuffer in project BIMserver by opensourceBIM.
the class Streamer method onText.
public void onText(Reader reader) {
JsonReader jsonreader = new JsonReader(reader);
JsonParser parser = new JsonParser();
JsonObject request = (JsonObject) parser.parse(jsonreader);
if (request.has("hb")) {
// Heartbeat, ignore
} else if (request.has("action")) {
if (request.get("action").getAsString().equals("download")) {
final long topicId = request.get("topicId").getAsLong();
Thread thread = new Thread() {
@Override
public void run() {
try {
LongAction<?> longAction = bimServer.getLongActionManager().getLongAction(topicId);
Writer writer = null;
if (longAction instanceof LongStreamingDownloadAction) {
LongStreamingDownloadAction longStreamingDownloadAction = (LongStreamingDownloadAction) longAction;
writer = longStreamingDownloadAction.getMessagingStreamingSerializer();
} else {
LongDownloadOrCheckoutAction longDownloadAction = (LongDownloadOrCheckoutAction) longAction;
writer = longDownloadAction.getMessagingSerializer();
}
boolean writeMessage = true;
int counter = 0;
long bytes = 0;
long start = System.nanoTime();
// TODO whenever a large object has been sent, the large buffer stays in memory until websocket closes...
ReusableLittleEndianDataOutputStream byteArrayOutputStream = new ReusableLittleEndianDataOutputStream();
GrowingByteBuffer growingByteBuffer = byteArrayOutputStream.getGrowingByteBuffer();
ProgressReporter progressReporter = new ProgressReporter() {
@Override
public void update(long progress, long max) {
longAction.updateProgress("test", (int) ((progress * 100) / max));
}
@Override
public void setTitle(String title) {
}
};
do {
byteArrayOutputStream.reset();
byteArrayOutputStream.writeLong(topicId);
writeMessage = writer.writeMessage(byteArrayOutputStream, progressReporter);
bytes += growingByteBuffer.usedSize();
streamingSocketInterface.sendBlocking(growingByteBuffer.array(), 0, growingByteBuffer.usedSize());
counter++;
} while (writeMessage);
long end = System.nanoTime();
LOGGER.info(counter + " messages written " + Formatters.bytesToString(bytes) + " in " + ((end - start) / 1000000) + " ms");
} catch (IOException e) {
// Probably closed/F5-ed browser
} catch (SerializerException e) {
LOGGER.error("", e);
}
}
};
thread.setName("Streamer " + topicId);
thread.start();
}
} else if (request.has("token")) {
String token = request.get("token").getAsString();
try {
ServiceMap serviceMap = bimServer.getServiceFactory().get(token, AccessMethod.JSON);
uoid = serviceMap.getBimServerAuthInterface().getLoggedInUser().getOid();
this.endpointid = bimServer.getEndPointManager().register(this);
JsonObject enpointMessage = new JsonObject();
enpointMessage.add("endpointid", new JsonPrimitive(endpointid));
streamingSocketInterface.send(enpointMessage);
} catch (UserException e) {
LOGGER.error("", e);
} catch (ServerException e) {
LOGGER.error("", e);
}
} else {
bimServer.getJsonHandler().execute(request, null, new NullWriter());
}
}
Aggregations