Example usage for com.google.gson JsonObject getAsJsonPrimitive

List of usage examples for com.google.gson JsonObject getAsJsonPrimitive

Introduction

In this page you can find the example usage for com.google.gson JsonObject getAsJsonPrimitive.

Prototype

public JsonPrimitive getAsJsonPrimitive(String memberName) 

Source Link

Document

Convenience method to get the specified member as a JsonPrimitive element.

Usage

From source file:es.urjc.etsii.code.BenchmarkHandler.java

License:Apache License

private synchronized void presenter(WebSocketSession wsSession, String sessionNumber, JsonObject jsonMessage) {
    if (presenters.containsKey(sessionNumber)) {
        JsonObject response = new JsonObject();
        response.addProperty("id", "presenterResponse");
        response.addProperty("response", "rejected");
        response.addProperty("message", "Another user is currently acting as sender for session "
                + sessionNumber + ". Chose another session number ot try again later ...");
        sendMessage(wsSession, sessionNumber, new TextMessage(response.toString()));

    } else {//from   w ww .  ja  v a 2s .  c om
        int bandwidth = jsonMessage.getAsJsonPrimitive("bandwidth").getAsInt();
        UserSession presenterSession = new UserSession(wsSession, sessionNumber, this, bandwidth);
        presenters.put(sessionNumber, presenterSession);

        String sdpOffer = jsonMessage.getAsJsonPrimitive("sdpOffer").getAsString();
        presenterSession.initPresenter(sdpOffer);
    }
}

From source file:es.urjc.etsii.code.BenchmarkHandler.java

License:Apache License

private synchronized void viewer(WebSocketSession wsSession, String sessionNumber, JsonObject jsonMessage)
        throws InterruptedException {
    String wsSessionId = wsSession.getId();

    if (presenters.containsKey(sessionNumber)) {
        // Entry for viewers map
        Map<String, UserSession> viewersPerPresenter;
        if (viewers.containsKey(sessionNumber)) {
            viewersPerPresenter = viewers.get(sessionNumber);
        } else {/*  www  .  jav  a  2s.c  o  m*/
            viewersPerPresenter = new ConcurrentHashMap<>();
            viewers.put(sessionNumber, viewersPerPresenter);
        }

        if (viewersPerPresenter.containsKey(wsSessionId)) {
            JsonObject response = new JsonObject();
            response.addProperty("id", "viewerResponse");
            response.addProperty("response", "rejected");
            response.addProperty("message", "You are already viewing in session number " + viewersPerPresenter
                    + ". Use a different browser/tab to add additional viewers.");
            sendMessage(wsSession, sessionNumber, new TextMessage(response.toString()));
        } else {
            int bandwidth = jsonMessage.getAsJsonPrimitive("bandwidth").getAsInt();
            UserSession viewerSession = new UserSession(wsSession, sessionNumber, this, bandwidth);
            viewersPerPresenter.put(wsSessionId, viewerSession);

            viewerSession.initViewer(presenters.get(sessionNumber), jsonMessage);
        }

    } else {
        JsonObject response = new JsonObject();
        response.addProperty("id", "viewerResponse");
        response.addProperty("response", "rejected");
        response.addProperty("message", "No active presenter for sesssion number " + sessionNumber
                + " now. Become sender or try again later ...");
        sendMessage(wsSession, sessionNumber, new TextMessage(response.toString()));
    }
}

From source file:es.urjc.etsii.code.UserSession.java

License:Apache License

public void initViewer(UserSession presenterSession, JsonObject jsonMessage) throws InterruptedException {
    String processing = jsonMessage.get("processing").getAsString();
    String sdpOffer = jsonMessage.getAsJsonPrimitive("sdpOffer").getAsString();
    int fakeClients = jsonMessage.getAsJsonPrimitive("fakeClients").getAsInt();
    WebRtcEndpoint inputWebRtcEndpoint = singleKmsTopology(presenterSession, processing);

    String sdpAnswer = webRtcEndpoint.processOffer(sdpOffer);
    JsonObject response = new JsonObject();
    response.addProperty("id", "viewerResponse");
    response.addProperty("response", "accepted");
    response.addProperty("sdpAnswer", sdpAnswer);

    handler.sendMessage(wsSession, sessionNumber, new TextMessage(response.toString()));
    webRtcEndpoint.gatherCandidates();/*from   www .  j  av  a2  s .  c  om*/

    if (fakeClients > 0) {
        addFakeClients(presenterSession, jsonMessage, inputWebRtcEndpoint);
    }

}

From source file:es.urjc.etsii.code.UserSession.java

License:Apache License

private void addFakeClients(UserSession presenterSession, JsonObject jsonMessage,
        final WebRtcEndpoint inputWebRtcEndpoint) {

    final String sessionNumber = jsonMessage.get("sessionNumber").getAsString();
    final int fakeClients = jsonMessage.getAsJsonPrimitive("fakeClients").getAsInt();
    final int timeBetweenClients = jsonMessage.getAsJsonPrimitive("timeBetweenClients").getAsInt();
    final boolean removeFakeClients = jsonMessage.getAsJsonPrimitive("removeFakeClients").getAsBoolean();
    final int playTime = jsonMessage.getAsJsonPrimitive("playTime").getAsInt();
    final String processing = jsonMessage.get("processing").getAsString();
    final int fakeClientsPerInstance = jsonMessage.getAsJsonPrimitive("fakeClientsPerInstance").getAsInt();

    new Thread(new Runnable() {
        @Override//from w  w  w . j av a  2 s  .c  o  m
        public void run() {
            log.info("[Session number {} - WS session {}] Adding {} fake clients (rate {} ms) ", sessionNumber,
                    wsSession.getId(), fakeClients, timeBetweenClients);

            final CountDownLatch latch = new CountDownLatch(fakeClients);
            ExecutorService executor = Executors.newFixedThreadPool(fakeClients);
            for (int i = 0; i < fakeClients; i++) {
                waitMs(timeBetweenClients);
                final int j = i + 1;
                executor.execute(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            addFakeClient(j, processing, inputWebRtcEndpoint, fakeClientsPerInstance);
                        } finally {
                            latch.countDown();
                        }
                    }
                });
            }

            try {
                latch.await();
            } catch (InterruptedException e) {
                log.warn("Exception waiting thread pool to be finished", e);
            }
            executor.shutdown();

            if (removeFakeClients) {
                log.info(
                        "[Session number {} - WS session {}] Waiting {} seconds with all fake clients connected",
                        sessionNumber, wsSession.getId(), playTime);

                for (List<MediaElement> list : mediaElementsInFakeMediaPipelineMap.values()) {
                    waitMs(playTime * 1000);
                    for (int i = 0; i < list.size() / 3; i++) {
                        if (i != 0) {
                            waitMs(timeBetweenClients);
                        }
                        log.info("[Session number {} - WS session {}] Releasing fake viewer {}", sessionNumber,
                                wsSession.getId(), i);
                        for (int j = 0; j < 3; j++) {
                            MediaElement mediaElement = list.get(3 * i + j);
                            if (mediaElement != null) {
                                log.debug("[Session number {} - WS session {}] Releasing {}", sessionNumber,
                                        wsSession.getId(), mediaElement);
                                mediaElement.release();
                                mediaElement = null;
                            }
                        }
                    }
                }
                mediaElementsInFakeMediaPipelineMap.clear();
                releaseFakeMediaPipeline();
            }
        }
    }).start();
}

From source file:eu.seaclouds.platform.dashboard.resources.DeployerResource.java

License:Apache License

private JsonArray retrieveMetrics(String applicationId, String entityId)
        throws IOException, URISyntaxException {
    String monitorResponse = new HttpGetRequestBuilder().host(deployer.getEndpoint())
            .setCredentials(deployer.getUser(), deployer.getPassword())
            .path("/v1/applications/" + applicationId + "/entities/" + entityId + "/sensors").build();

    JsonArray metricList = new JsonParser().parse(monitorResponse).getAsJsonArray();

    Iterator<JsonElement> metricIterator = metricList.iterator();

    while (metricIterator.hasNext()) {
        JsonObject metric = metricIterator.next().getAsJsonObject();
        metric.remove("links");
        if (!isNumberType(metric.getAsJsonPrimitive("type").getAsString())) {
            metricIterator.remove();/* w  ww  .  j  ava 2  s.c  om*/
        }
    }

    return metricList.getAsJsonArray();

}

From source file:fi.vtt.nubomedia.armodule.Ar3DHandler.java

License:Open Source License

public void createPipeline(UserSession userSession, JsonObject jsonMessage) {
    try {//from  w w w .j  a va 2s  .c o  m
        System.err.println("ME CREATE PIPELINE");

        MediaPipeline mediaPipeline = userSession.getMediaPipeline();
        ArMarkerdetector arFilter = new ArMarkerdetector.Builder(mediaPipeline).build();

        if (jsonFile == null) {
            System.err.println("json from browser");
            arFilter.setArThing(createArThings(jsonMessage.getAsJsonPrimitive("augmentables").getAsString()));
        } else {
            System.err.println("json from file");
            arFilter.setArThing(createArThings(getFile(jsonFile)));
        }

        arFilter.enableTickEvents(false);
        arFilter.enableAugmentation(true);
        arFilter.setMarkerPoseFrequency(false, 1);
        arFilter.setMarkerPoseFrameFrequency(false, 10);
        arFilter.enableMarkerCountEvents(false);

        addModuleListeners(arFilter, userSession.getSessionId());

        WebRtcEndpoint webRtcEndpoint = userSession.getWebRtcEndpoint();
        webRtcEndpoint.connect(arFilter);
        arFilter.connect(webRtcEndpoint);

        userSession.setUserData(arFilter);
    } catch (Exception e) {
        System.err.println(e);
        throw new RuntimeException(e);
    }
}

From source file:fi.vtt.nubomedia.armodule.Ar3DHandler.java

License:Open Source License

private void pose(WebSocketSession session, JsonObject jsonMessage) {
    try {//from   w w  w. ja v a2 s .  com
        System.err.println("json POSE from browser");

        ArMarkerdetector arFilter = getArFilter(session);
        if (arFilter == null) {
            log.error("Start the filter first");
            return;
        }

        String json = jsonMessage.getAsJsonPrimitive("pose").getAsString();
        System.err.println("json:\n" + json);
        JsonObject jsonObjects = gson.fromJson(json, JsonObject.class);

        JsonArray jsonArray = jsonObjects.getAsJsonArray("pose");
        Iterator<JsonElement> itr = jsonArray.iterator();
        while (itr.hasNext()) {
            JsonElement jsonElm = itr.next();
            if (jsonElm.isJsonNull()) {
                System.err.println("Really Skip null");
                continue;
            }
            System.err.println("Got: " + jsonElm);

            JsonObject jsonObject = jsonElm.getAsJsonObject();
            int id = jsonObject.get("id").getAsInt();
            int type = jsonObject.get("type").getAsInt();
            //String id = jsonObject.get("id").getAsString();
            //String type = jsonObject.get("type").getAsString();
            float value = jsonObject.get("value").getAsFloat();
            System.err.println("" + id + "#" + type + "#" + value);

            arFilter.setPose(id, type, value);
        }
    } catch (Throwable t) {
        t.printStackTrace();
        error(session, t.getMessage());
    }
}

From source file:fi.vtt.nubomedia.armodule.Ar3DHandler.java

License:Open Source License

private void reload(WebSocketSession session, JsonObject jsonMessage) {
    try {/*from w  ww.jav  a2  s .  c  o  m*/
        System.err.println("json RELOAD from browser");
        ArMarkerdetector arFilter = getArFilter(session);
        if (arFilter == null) {
            log.error("Start the filter first");
            return;
        }
        arFilter.setArThing(createArThings(jsonMessage.getAsJsonPrimitive("augmentables").getAsString()));
    } catch (Throwable t) {
        t.printStackTrace();
        error(session, t.getMessage());
    }
}

From source file:fi.vtt.nubomedia.kurento.Ar3DHandler.java

License:Open Source License

private void pose(WebSocketSession session, JsonObject jsonMessage) {
    try {/*from w w w.j  ava  2  s.  c  om*/
        System.err.println("json POSE from browser");

        String json = jsonMessage.getAsJsonPrimitive("pose").getAsString();
        System.err.println("json:\n" + json);
        JsonObject jsonObjects = gson.fromJson(json, JsonObject.class);

        JsonArray jsonArray = jsonObjects.getAsJsonArray("pose");
        Iterator<JsonElement> itr = jsonArray.iterator();
        while (itr.hasNext()) {
            JsonElement jsonElm = itr.next();
            if (jsonElm.isJsonNull()) {
                System.err.println("Really Skip null");
                continue;
            }
            System.err.println("Got: " + jsonElm);

            JsonObject jsonObject = jsonElm.getAsJsonObject();
            int id = jsonObject.get("id").getAsInt();
            int type = jsonObject.get("type").getAsInt();
            //String id = jsonObject.get("id").getAsString();
            //String type = jsonObject.get("type").getAsString();
            float value = jsonObject.get("value").getAsFloat();
            System.err.println("" + id + "#" + type + "#" + value);
            if (arFilter != null) {
                arFilter.setPose(id, type, value);
            } else {
                System.err.println("Start the filter first");
            }
        }
    } catch (Throwable t) {
        t.printStackTrace();
        sendError(session, t.getMessage());
    }
}

From source file:fi.vtt.nubomedia.kurento.Ar3DHandler.java

License:Open Source License

private void reload(WebSocketSession session, JsonObject jsonMessage) {
    try {/*w  ww.jav a2  s.  c o  m*/
        System.err.println("json RELOAD from browser");
        if (arFilter != null) {
            arFilter.setArThing(createArThings(jsonMessage.getAsJsonPrimitive("augmentables").getAsString()));
        }
    } catch (Throwable t) {
        t.printStackTrace();
        sendError(session, t.getMessage());
    }
}