示例#1
0
  /**
   * Called to disconnect a user (eventually remove the reserved {@link Room}) when one of the
   * conference legs are cut, i.e. a WebSocket connection is closed
   */
  public static void disconnect(String token) {
    if (token != null) {
      String[] values = token.split("/");
      String room_key = values[0];
      String user = values[1];
      logger.info("Removing user " + user + " from room " + room_key);

      Room room = Room.get_by_key_name(room_key);
      if (room != null && room.has_user(user)) {
        String other_user = room.get_other_user(user);
        room.remove_user(user);
        logger.info("Room " + room_key + " has state " + room.toString());
        if (other_user != null) {
          SignalingWebSocket.send(Helper.make_token(room, other_user), "{'type':'bye'}");
          logger.info("Sent BYE to " + other_user);
        } else {
          room.delete();
        }
      } else logger.warning("Unknown room " + room_key);
    }
  }
  /**
   * Renders the main page. When this page is shown, we create a new channel to push asynchronous
   * updates to the client.
   */
  public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
    // String PATH = req.getContextPath().replace("/", "");
    String query = req.getQueryString();
    System.out.println("Consulta " + query);
    if (query == null) {
      String redirect = "/" + PATH + "/?r=" + Helper.generate_random(16);
      logger.info(
          "Sin consulta (query Room) -> Redirigiendo al visitante de la base URL a " + redirect);
      resp.sendRedirect(redirect);
      return;
    }
    Map<String, String> params = Helper.get_query_map(query);
    String room_key = Helper.sanitize(params.get("r"));
    // String debug       = params.get("debug");
    if (room_key == null || room_key.equals("")) {
      room_key = Helper.generate_random(16);
      String redirect = "/" + PATH + "/?r=" + room_key;
      logger.info("Sin habitacion (room key) -> Redirigiendo al usuario de base URL a " + redirect);
      resp.sendRedirect(redirect);
      return;
    } else {
      String user = null;
      int initiator = 0;
      Room room = Room.get_by_key_name(room_key);
      if (room == null) {
        user = Helper.generate_random(16);
        room = new Room(room_key);
        room.add_user(user);
        initiator = 0;
        logger.info("Nueva habitacion " + room_key);
      } else if (room != null && room.get_occupancy() == 1) {
        user = Helper.generate_random(16);
        room.add_user(user);
        initiator = 1;
        logger.info("Habitacion " + room_key + " con 1 usuario, anadiendo a otro usuario: " + user);
      } else {
        Map<String, String> template_values = new HashMap<String, String>();
        template_values.put("room_key", room_key);
        resp.setContentType("text/html");
        String filepath = getServletContext().getRealPath("webrtc/full.jtpl");
        File file = new File(filepath);
        resp.getWriter().print(Helper.generatePage(file, template_values));
        logger.info("Habitacion " + room_key + " con 2 usuarios (completo).");
        return;
      }

      String server_name = req.getServerName();
      int server_port = req.getServerPort();
      // int  server_port   = 8080;
      String room_link =
          "http://" + server_name + ":" + server_port + "/" + PATH + "/?r=" + room_key;
      // String stun_server = "";
      String username = "******";
      setToken(Helper.make_token(room_key, user));
      setRoomKey(room_key);
      String pc_config = Helper.make_pc_config("");
      Map<String, String> template_values = new HashMap<String, String>();
      template_values.put("server_name", server_name);
      template_values.put("server_port", server_port + "");
      template_values.put("PATH", PATH);
      template_values.put("token", token);
      template_values.put("me", user);
      template_values.put("username", username);
      template_values.put("room_key", room_key);
      template_values.put("room_link", room_link);
      template_values.put("initiator", "" + initiator);
      template_values.put("pc_config", pc_config);
      resp.setContentType("text/html");
      File file = new File(getServletContext().getRealPath("webrtc/index.jtpl"));
      resp.getWriter().println(Helper.generatePage(file, template_values));
      logger.info("Usuario " + user + " anadido a la habitacion " + room_key);
      logger.info("La habitacion " + room_key + " tiene el estado " + room);
      resp.setStatus(HttpServletResponse.SC_OK);
    }
  }