public void stopPoll(Map<String, Object> message) {
    String meetingID = Red5.getConnectionLocal().getScope().getName();
    String userId = getBbbSession().getInternalUserID();
    String pollId = (String) message.get("pollId");

    red5GW.stopPoll(meetingID, userId, pollId);
  }
  public void validateToken(Map<String, String> msg) {
    String token = (String) msg.get("authToken");

    BigBlueButtonSession bbbSession =
        (BigBlueButtonSession) Red5.getConnectionLocal().getAttribute(Constants.SESSION);
    assert bbbSession != null;
    String userId = bbbSession.getInternalUserID();
    String meetingId = Red5.getConnectionLocal().getScope().getName();
    String connId = Red5.getConnectionLocal().getSessionId();
    String sessionId = CONN + connId + "-" + userId;

    Map<String, Object> logData = new HashMap<String, Object>();
    logData.put("meetingId", meetingId);
    logData.put("connId", connId);
    logData.put("sessionId", sessionId);
    logData.put("userId", userId);
    logData.put("token", token);
    logData.put("event", "user_validate_token_bbb_apps");
    logData.put("description", "User validate token BBB Apps.");

    Gson gson = new Gson();
    String logStr = gson.toJson(logData);

    log.info("User validate token bbb-apps: data={}", logStr);
    red5InGW.validateAuthToken(meetingId, userId, token, meetingId + "/" + userId, sessionId);
  }
  public void sendPrivateMessage(Map<String, Object> msg) {
    String chatType = msg.get(ChatKeyUtil.CHAT_TYPE).toString();
    String fromUserID = msg.get(ChatKeyUtil.FROM_USERID).toString();
    String fromUsername = msg.get(ChatKeyUtil.FROM_USERNAME).toString();
    String fromColor = msg.get(ChatKeyUtil.FROM_COLOR).toString();
    String fromTime = msg.get(ChatKeyUtil.FROM_TIME).toString();
    String fromTimezoneOffset = msg.get(ChatKeyUtil.FROM_TZ_OFFSET).toString();
    String toUserID = msg.get(ChatKeyUtil.TO_USERID).toString();
    String toUsername = msg.get(ChatKeyUtil.TO_USERNAME).toString();
    String chatText = msg.get(ChatKeyUtil.MESSAGE).toString();

    Map<String, String> message = new HashMap<String, String>();
    message.put(ChatKeyUtil.CHAT_TYPE, chatType);
    message.put(ChatKeyUtil.FROM_USERID, fromUserID);
    message.put(ChatKeyUtil.FROM_USERNAME, fromUsername);
    message.put(ChatKeyUtil.FROM_COLOR, fromColor);
    message.put(ChatKeyUtil.FROM_TIME, fromTime);
    message.put(ChatKeyUtil.FROM_TZ_OFFSET, fromTimezoneOffset);
    message.put(ChatKeyUtil.TO_USERID, toUserID);
    message.put(ChatKeyUtil.TO_USERNAME, toUsername);
    message.put(ChatKeyUtil.MESSAGE, chatText);

    String meetingID = Red5.getConnectionLocal().getScope().getName();
    String requesterID = getBbbSession().getInternalUserID();

    red5BBBInGw.sendPrivateMessage(meetingID, requesterID, message);
  }
  public void showPollResult(Map<String, Object> message) {
    String meetingID = Red5.getConnectionLocal().getScope().getName();
    String userId = getBbbSession().getInternalUserID();
    String pollId = (String) message.get("pollId");
    Boolean show = (Boolean) message.get("show");

    red5GW.showPollResult(meetingID, userId, pollId, show);
  }
  public void sendPublicChatHistory() {
    String meetingID = Red5.getConnectionLocal().getScope().getName();
    String requesterID = getBbbSession().getInternalUserID();
    // Just hardcode as we don't really need it for flash client. (ralam may 7, 2014)
    String replyTo = meetingID + "/" + requesterID;

    red5BBBInGw.getChatHistory(meetingID, requesterID, replyTo);
  }
  public void votePoll(Map<String, Object> message) {
    String meetingID = Red5.getConnectionLocal().getScope().getName();
    String userId = getBbbSession().getInternalUserID();
    String pollId = (String) message.get("pollId");
    Integer questionId = (Integer) message.get("answerId");
    Integer answerId = (Integer) message.get("answerId");

    red5GW.votePoll(meetingID, userId, pollId, questionId, answerId);
  }
  @Override
  public void roomDisconnect(IConnection conn) {

    String remoteHost = Red5.getConnectionLocal().getRemoteAddress();
    int remotePort = Red5.getConnectionLocal().getRemotePort();
    String clientId = Red5.getConnectionLocal().getClient().getId();

    BigBlueButtonSession bbbSession =
        (BigBlueButtonSession) Red5.getConnectionLocal().getAttribute(Constants.SESSION);

    String meetingId = bbbSession.getRoom();
    String userId = bbbSession.getInternalUserID();
    String connType = getConnectionType(Red5.getConnectionLocal().getType());
    String userFullname = bbbSession.getUsername();
    String connId = Red5.getConnectionLocal().getSessionId();

    String sessionId = CONN + userId;

    Map<String, Object> logData = new HashMap<String, Object>();
    logData.put("meetingId", meetingId);
    logData.put("connType", connType);
    logData.put("connId", connId);
    logData.put("conn", remoteHost + ":" + remotePort);
    logData.put("sessionId", sessionId);
    logData.put("userId", userId);
    logData.put("username", userFullname);
    logData.put("event", "user_leaving_bbb_apps");
    logData.put("description", "User leaving BBB Apps.");

    Gson gson = new Gson();
    String logStr = gson.toJson(logData);

    boolean removeUser = userConnections.userDisconnected(userId, connId);

    if (removeUser) {
      log.info("User leaving bbb-apps: data={}", logStr);
      red5InGW.userLeft(bbbSession.getRoom(), getBbbSession().getInternalUserID(), sessionId);
    } else {
      log.info("User not leaving bbb-apps but just disconnected: data={}", logStr);
    }

    super.roomDisconnect(conn);
  }
  @Override
  public boolean roomConnect(IConnection connection, Object[] params) {
    String username = ((String) params[0]).toString();
    String role = ((String) params[1]).toString();
    String room = ((String) params[2]).toString();

    String voiceBridge = ((String) params[3]).toString();

    boolean record = (Boolean) params[4];

    String externalUserID = ((String) params[5]).toString();
    String internalUserID = ((String) params[6]).toString();

    Boolean muted = false;
    if (params.length >= 7 && ((Boolean) params[7])) {
      muted = true;
    }

    Map<String, Boolean> lsMap = null;
    if (params.length >= 8) {
      try {
        lsMap = (Map<String, Boolean>) params[8];
      } catch (Exception e) {
        lsMap = new HashMap<String, Boolean>();
      }
    }

    String userId = internalUserID;
    String sessionId = CONN + userId;
    BigBlueButtonSession bbbSession =
        new BigBlueButtonSession(
            room,
            internalUserID,
            username,
            role,
            voiceBridge,
            record,
            externalUserID,
            muted,
            sessionId);
    connection.setAttribute(Constants.SESSION, bbbSession);
    connection.setAttribute("INTERNAL_USER_ID", internalUserID);
    connection.setAttribute("USER_SESSION_ID", sessionId);
    connection.setAttribute("TIMESTAMP", System.currentTimeMillis());

    red5InGW.initLockSettings(room, lsMap);

    red5InGW.initAudioSettings(room, internalUserID, muted);

    String meetingId = bbbSession.getRoom();

    String connType = getConnectionType(Red5.getConnectionLocal().getType());
    String userFullname = bbbSession.getUsername();
    String connId = Red5.getConnectionLocal().getSessionId();

    String remoteHost = Red5.getConnectionLocal().getRemoteAddress();
    int remotePort = Red5.getConnectionLocal().getRemotePort();

    Map<String, Object> logData = new HashMap<String, Object>();
    logData.put("meetingId", meetingId);
    logData.put("connType", connType);
    logData.put("connId", connId);
    logData.put("conn", remoteHost + ":" + remotePort);
    logData.put("userId", userId);
    logData.put("externalUserId", externalUserID);
    logData.put("sessionId", sessionId);
    logData.put("username", userFullname);
    logData.put("event", "user_joining_bbb_apps");
    logData.put("description", "User joining BBB Apps.");

    Gson gson = new Gson();
    String logStr = gson.toJson(logData);

    log.info("User joining bbb-apps: data={}", logStr);

    userConnections.addUserConnection(userId, connId);

    return super.roomConnect(connection, params);
  }
  public void sendPollingMessage(String json) {
    String meetingID = Red5.getConnectionLocal().getScope().getName();
    String userId = getBbbSession().getInternalUserID();

    red5GW.sendPollingMessage(json);
  }