diff --git a/common/src/main/java/ctbrec/sites/mfc/MyFreeCamsClient.java b/common/src/main/java/ctbrec/sites/mfc/MyFreeCamsClient.java index 9330a271..1b91293d 100644 --- a/common/src/main/java/ctbrec/sites/mfc/MyFreeCamsClient.java +++ b/common/src/main/java/ctbrec/sites/mfc/MyFreeCamsClient.java @@ -407,51 +407,6 @@ public class MyFreeCamsClient { } } - private void updateSessionState(SessionState newState) { - if (newState.getUid() <= 0) { - return; - } - SessionState storedState = sessionStates.getIfPresent(newState.getUid()); - if (storedState != null) { - storedState.merge(newState); - updateModel(storedState); - } else { - lock.lock(); - try { - sessionStates.put(newState.getUid(), newState); - updateModel(newState); - } finally { - lock.unlock(); - } - } - } - - private void updateModel(SessionState state) { - // essential data not yet available - if (state.getNm() == null || state.getM() == null || state.getU() == null || state.getU().getCamserv() == null - || state.getU().getCamserv() == 0) { - return; - } - - // tokens not yet available - if (ctxenc == null) { - return; - } - - // uid not set, we can't identify this model - if (state.getUid() == null || state.getUid() <= 0) { - return; - } - - MyFreeCamsModel model = models.getIfPresent(state.getUid()); - if (model == null) { - model = mfc.createModel(state.getNm()); - model.setUid(state.getUid()); - models.put(state.getUid(), model); - } - model.update(state, getStreamUrl(state)); - } - @Override public void onMessage(WebSocket webSocket, ByteString bytes) { super.onMessage(webSocket, bytes); @@ -461,6 +416,51 @@ public class MyFreeCamsClient { return websocket; } + private void updateSessionState(SessionState newState) { + if (newState.getUid() <= 0) { + return; + } + SessionState storedState = sessionStates.getIfPresent(newState.getUid()); + if (storedState != null) { + storedState.merge(newState); + updateModel(storedState); + } else { + lock.lock(); + try { + sessionStates.put(newState.getUid(), newState); + updateModel(newState); + } finally { + lock.unlock(); + } + } + } + + private void updateModel(SessionState state) { + // essential data not yet available + if (state.getNm() == null || state.getM() == null || state.getU() == null || state.getU().getCamserv() == null + || state.getU().getCamserv() == 0) { + return; + } + + // tokens not yet available + if (ctxenc == null) { + return; + } + + // uid not set, we can't identify this model + if (state.getUid() == null || state.getUid() <= 0) { + return; + } + + MyFreeCamsModel model = models.getIfPresent(state.getUid()); + if (model == null) { + model = mfc.createModel(state.getNm()); + model.setUid(state.getUid()); + models.put(state.getUid(), model); + } + model.update(state, getStreamUrl(state)); + } + private Message parseMessage(StringBuilder msgBuffer) { int packetLengthBytes = 6; if (msgBuffer.length() < packetLengthBytes) { @@ -676,6 +676,15 @@ public class MyFreeCamsClient { LOG.trace("Search result: {}", msg); if (StringUtil.isNotBlank(msg.getMessage()) && !Objects.equals(msg.getMessage(), q)) { JSONObject json = new JSONObject(msg.getMessage()); + + JsonAdapter<SessionState> adapter = moshi.adapter(SessionState.class); + try { + SessionState sessionState = Objects.requireNonNull(adapter.fromJson(msg.getMessage())); + updateSessionState(sessionState); + } catch (Exception e) { + LOG.error("Couldn't parse session state message {}", msg, e); + } + String name = json.getString("nm"); MyFreeCamsModel model = mfc.createModel(name); model.setUid(json.getInt("uid"));