Revisão | ffe2678929201cd2bf4ab3f068a489bc6ee18e78 (tree) |
---|---|
Hora | 2019-01-17 05:33:19 |
Autor | sebastian_bugiu <sebastian_ <bugiu@head...> |
Commiter | sebastian_bugiu <sebastian_ |
Adding lobby support.
@@ -157,4 +157,10 @@ | ||
157 | 157 | |
158 | 158 | public static final int CREATE_LOBBY_TOO_MANY_INVITATIONS = 51; |
159 | 159 | public static final String CREATE_LOBBY_TOO_MANY_INVITATIONS_STR = "Too many invitations"; |
160 | + | |
161 | + public static final int CREATE_SERVER_ALREADY_EXISTS = 52; | |
162 | + public static final String CREATE_SERVER_ALREADY_EXISTS_STR = "Server has been already registered"; | |
163 | + | |
164 | + public static final int CREATE_SERVER_CANNOT_FIND_SERVER_ID = 53; | |
165 | + public static final String CREATE_SERVER_CANNOT_FIND_SERVER_ID_STR = "Cannot find server id"; | |
160 | 166 | } |
@@ -707,19 +707,45 @@ | ||
707 | 707 | @Produces({"application/json", "text/plain"}) |
708 | 708 | public Response createServer(@Context HttpServletRequest requestContext, Server server) { |
709 | 709 | String clientIP = requestContext.getRemoteAddr(); |
710 | - List<Server> serverList = em.createNamedQuery(Server.GET_SERVER_BY_NAME_AND_IP) | |
711 | - .setParameter("serverName", server.getServerName()) | |
712 | - .setParameter("serverIp", server.getIp()) | |
713 | - .getResultList(); | |
714 | -// if (!serverList.isEmpty()) { | |
715 | -// server.setError("Server name already exists"); | |
716 | -// return createResponse(server); | |
710 | + // We get the actual ip not the one that might be from the local DHCP of the server. | |
711 | + server.setIp(clientIP); | |
712 | + if (server.getId() == 0) { | |
713 | + // This is a new server. Still check if it was just a reinstallation. | |
714 | + List<Server> serverList = em.createNamedQuery(Server.GET_SERVER_BY_NAME_AND_IP) | |
715 | + .setParameter("serverName", server.getServerName()) | |
716 | + .setParameter("serverIp", server.getIp()) | |
717 | + .getResultList(); | |
718 | + if (serverList.size() > 1) { | |
719 | + server.setErrorCode(ErrorCodes.CREATE_SERVER_ALREADY_EXISTS); | |
720 | + server.setError(ErrorCodes.CREATE_SERVER_ALREADY_EXISTS_STR); | |
721 | + return createResponse(server); | |
722 | + } else if (serverList.size() == 1) { | |
723 | + // Renew the id. | |
724 | + server.setId(serverList.get(0).getId()); | |
725 | + return createResponse(server); | |
726 | + } else { | |
727 | + em.persist(server); | |
728 | + em.flush(); | |
729 | + return createResponse(server); | |
730 | + } | |
731 | + } else { | |
732 | + final List<Server> serverList = em.createNamedQuery(Server.GET_SERVER_BY_ID) | |
733 | + .setParameter("serverId", server.getId()).getResultList(); | |
734 | + if (serverList.isEmpty()) { | |
735 | + server.setErrorCode(ErrorCodes.CREATE_SERVER_CANNOT_FIND_SERVER_ID); | |
736 | + server.setError(ErrorCodes.CREATE_SERVER_CANNOT_FIND_SERVER_ID_STR); | |
737 | + return createResponse(server); | |
738 | + } | |
739 | + final Server selectedServer = serverList.get(0); | |
740 | + // Check that the server is the same with the one from the database? | |
741 | + return createResponse(server); | |
742 | + } | |
743 | + | |
744 | + | |
745 | +// if (serverList.isEmpty()) { | |
746 | +// em.persist(server); | |
717 | 747 | // } |
718 | - server.setIp(clientIP); | |
719 | - if (serverList.isEmpty()) { | |
720 | - em.persist(server); | |
721 | - } | |
722 | - return createResponse(server); | |
748 | +// return createResponse(server); | |
723 | 749 | } |
724 | 750 | |
725 | 751 | @POST |
@@ -322,7 +322,7 @@ | ||
322 | 322 | public void testCreateServer() throws Exception { |
323 | 323 | Server server = new Server(); |
324 | 324 | server.setServerName("mainServer"); |
325 | -// server.setIp("127.0.0.1"); | |
325 | + server.setIp("127.0.0.1"); | |
326 | 326 | server.setServerLoad(0); |
327 | 327 | server.setSessionNum(0); |
328 | 328 | server.setUserNum(0); |