Search in sources :

Example 1 with GrowingByteBuffer

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());
    }
}
Also used : LongAction(org.bimserver.longaction.LongAction) ServerException(org.bimserver.shared.exceptions.ServerException) ServiceMap(org.bimserver.webservices.ServiceMap) JsonPrimitive(com.google.gson.JsonPrimitive) JsonObject(com.google.gson.JsonObject) LongStreamingDownloadAction(org.bimserver.longaction.LongStreamingDownloadAction) ProgressReporter(org.bimserver.plugins.serializers.ProgressReporter) IOException(java.io.IOException) SerializerException(org.bimserver.plugins.serializers.SerializerException) NullWriter(org.apache.commons.io.output.NullWriter) LongDownloadOrCheckoutAction(org.bimserver.longaction.LongDownloadOrCheckoutAction) JsonReader(com.google.gson.stream.JsonReader) UserException(org.bimserver.shared.exceptions.UserException) NullWriter(org.apache.commons.io.output.NullWriter) Writer(org.bimserver.plugins.serializers.Writer) GrowingByteBuffer(org.bimserver.utils.GrowingByteBuffer) JsonParser(com.google.gson.JsonParser)

Aggregations

JsonObject (com.google.gson.JsonObject)1 JsonParser (com.google.gson.JsonParser)1 JsonPrimitive (com.google.gson.JsonPrimitive)1 JsonReader (com.google.gson.stream.JsonReader)1 IOException (java.io.IOException)1 NullWriter (org.apache.commons.io.output.NullWriter)1 LongAction (org.bimserver.longaction.LongAction)1 LongDownloadOrCheckoutAction (org.bimserver.longaction.LongDownloadOrCheckoutAction)1 LongStreamingDownloadAction (org.bimserver.longaction.LongStreamingDownloadAction)1 ProgressReporter (org.bimserver.plugins.serializers.ProgressReporter)1 SerializerException (org.bimserver.plugins.serializers.SerializerException)1 Writer (org.bimserver.plugins.serializers.Writer)1 ServerException (org.bimserver.shared.exceptions.ServerException)1 UserException (org.bimserver.shared.exceptions.UserException)1 GrowingByteBuffer (org.bimserver.utils.GrowingByteBuffer)1 ServiceMap (org.bimserver.webservices.ServiceMap)1