Last active
May 16, 2017 09:23
-
-
Save rybalkinsd/dd64a84a8e9c09d7aa70661f12f86776 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
commit 090be79cc57bfa07b532febd15f706e8a23e1459 | |
Author: sergey.r <sergey.r@alibaba-inc.com> | |
Date: Fri May 12 17:43:33 2017 +0300 | |
Correct handling of enhanced ws connection parameter | |
diff --git a/game/src/main/java/ru/atom/game/game/GameHandler.java b/game/src/main/java/ru/atom/game/game/GameHandler.java | |
index e1133f2..faf581f 100644 | |
--- a/game/src/main/java/ru/atom/game/game/GameHandler.java | |
+++ b/game/src/main/java/ru/atom/game/game/GameHandler.java | |
@@ -4,28 +4,30 @@ package ru.atom.game.game; | |
import org.apache.logging.log4j.LogManager; | |
import org.apache.logging.log4j.Logger; | |
import org.eclipse.jetty.websocket.api.Session; | |
+import org.eclipse.jetty.websocket.api.StatusCode; | |
import org.eclipse.jetty.websocket.api.WebSocketAdapter; | |
-import ru.atom.game.dao.Database; | |
-import ru.atom.game.dao.TokenDao; | |
+import org.jetbrains.annotations.NotNull; | |
import ru.atom.game.message.Topic; | |
import ru.atom.game.model.GameSession; | |
-import ru.atom.game.model.Token; | |
-import ru.atom.game.model.User; | |
import ru.atom.game.network.Broker; | |
import ru.atom.game.network.ConnectionPool; | |
+import java.util.Collections; | |
import java.util.List; | |
public class GameHandler extends WebSocketAdapter { | |
private static final Logger log = LogManager.getLogger(GameHandler.class); | |
@Override | |
- public void onWebSocketConnect(Session sess) { | |
+ public void onWebSocketConnect(@NotNull Session sess) { | |
super.onWebSocketConnect(sess); | |
- List<String> params = sess.getUpgradeRequest().getParameterMap().get("id"); | |
- if (params.isEmpty()) { | |
+ List<String> gameIds = sess.getUpgradeRequest().getParameterMap() | |
+ .getOrDefault("id", Collections.emptyList()); | |
+ | |
+ if (gameIds.isEmpty()) { | |
log.info("Params empty"); | |
- sess.close(); | |
+ // choose correct status code and message | |
+ sess.close(StatusCode.POLICY_VIOLATION,"Params are empty"); | |
} else { | |
// TODO check id and get GameSession from Hash | |
GameSession gameSession = GameServer.getGameSession(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
commit 54a07092c231040b720f6039b55010f4d02a3bba | |
Author: sergey.r <sergey.r@alibaba-inc.com> | |
Date: Tue May 16 12:20:23 2017 +0300 | |
Cross browser filter, fixCheckStyle | |
diff --git a/game/src/main/java/ru/atom/game/controller/Ticker.java b/game/src/main/java/ru/atom/game/controller/Ticker.java | |
index a07be06..2c947e9 100644 | |
--- a/game/src/main/java/ru/atom/game/controller/Ticker.java | |
+++ b/game/src/main/java/ru/atom/game/controller/Ticker.java | |
@@ -2,14 +2,10 @@ package ru.atom.game.controller; | |
import org.apache.logging.log4j.LogManager; | |
import org.apache.logging.log4j.Logger; | |
-import org.eclipse.jetty.websocket.api.Session; | |
import ru.atom.game.message.Topic; | |
import ru.atom.game.model.GameSession; | |
import ru.atom.game.network.Broker; | |
-import ru.atom.game.network.ConnectionPool; | |
-import java.util.ArrayList; | |
-import java.util.Map; | |
import java.util.concurrent.TimeUnit; | |
import java.util.concurrent.locks.LockSupport; | |
@@ -31,12 +27,12 @@ public class Ticker { | |
act(FRAME_TIME); | |
long elapsed = System.currentTimeMillis() - started; | |
if (elapsed < FRAME_TIME) { | |
-// log.info("All tick finish at {} ms", elapsed); | |
+ // log.info("All tick finish at {} ms", elapsed); | |
LockSupport.parkNanos(TimeUnit.MILLISECONDS.toNanos(FRAME_TIME - elapsed)); | |
} else { | |
log.warn("tick lag {} ms", elapsed - FRAME_TIME); | |
} | |
-// log.info("{}: tick ", tickNumber); | |
+ // log.info("{}: tick ", tickNumber); | |
tickNumber++; | |
} | |
} | |
diff --git a/game/src/main/java/ru/atom/game/game/GameServer.java b/game/src/main/java/ru/atom/game/game/GameServer.java | |
index cb49f1e..0fdb334 100644 | |
--- a/game/src/main/java/ru/atom/game/game/GameServer.java | |
+++ b/game/src/main/java/ru/atom/game/game/GameServer.java | |
@@ -14,7 +14,8 @@ import ru.atom.game.model.GameSession; | |
public class GameServer { | |
private static GameSession gameSession; | |
- public static GameSession getGameSession(){ | |
+ | |
+ public static GameSession getGameSession() { | |
return gameSession; | |
} | |
@@ -32,10 +33,7 @@ public class GameServer { | |
context.setContextPath("/"); | |
ContextHandlerCollection contexts = new ContextHandlerCollection(); | |
- contexts.setHandlers(new Handler[] { | |
- context, | |
- createResourceContext() | |
- }); | |
+ contexts.setHandlers(new Handler[] { context, createResourceContext() }); | |
server.setHandler(contexts); | |
// Add a websocket to a specific path spec | |
@@ -60,6 +58,8 @@ public class GameServer { | |
ResourceHandler handler = new ResourceHandler(); | |
handler.setWelcomeFiles(new String[]{"index.html"}); | |
+ // Хорошо бы сделать так | |
+ // String serverRoot = GameServer.class.getResource("/front").toString(); | |
String serverRoot = "/home/zarina/technoatom/java/atom/bomberman/frontend/src/main/webapp"; | |
handler.setResourceBase(serverRoot); | |
context.setHandler(handler); | |
diff --git a/game/src/main/java/ru/atom/game/matchmaker/CrossBrowserFilter.java b/game/src/main/java/ru/atom/game/matchmaker/CrossBrowserFilter.java | |
new file mode 100644 | |
index 0000000..61671c6 | |
--- /dev/null | |
+++ b/game/src/main/java/ru/atom/game/matchmaker/CrossBrowserFilter.java | |
@@ -0,0 +1,22 @@ | |
+package ru.atom.game.matchmaker; | |
+ | |
+import javax.ws.rs.container.ContainerRequestContext; | |
+import javax.ws.rs.container.ContainerResponseContext; | |
+import javax.ws.rs.container.ContainerResponseFilter; | |
+import javax.ws.rs.ext.Provider; | |
+import java.io.IOException; | |
+ | |
+@Provider | |
+public class CrossBrowserFilter implements ContainerResponseFilter { | |
+ | |
+ @Override | |
+ public void filter(ContainerRequestContext request, | |
+ ContainerResponseContext response) throws IOException { | |
+ response.getHeaders().add("Access-Control-Allow-Origin", "*"); | |
+ response.getHeaders().add("Access-Control-Allow-Headers", | |
+ "origin, content-type, accept, authorization"); | |
+ response.getHeaders().add("Access-Control-Allow-Credentials", "true"); | |
+ response.getHeaders().add("Access-Control-Allow-Methods", | |
+ "GET, POST, PUT, DELETE, OPTIONS, HEAD"); | |
+ } | |
+} | |
diff --git a/game/src/main/java/ru/atom/game/matchmaker/MatchMaker.java b/game/src/main/java/ru/atom/game/matchmaker/MatchMaker.java | |
index 48d2f53..c53eaff 100644 | |
--- a/game/src/main/java/ru/atom/game/matchmaker/MatchMaker.java | |
+++ b/game/src/main/java/ru/atom/game/matchmaker/MatchMaker.java | |
@@ -2,16 +2,12 @@ package ru.atom.game.matchmaker; | |
import org.apache.logging.log4j.LogManager; | |
import org.apache.logging.log4j.Logger; | |
-import org.hibernate.Session; | |
-import org.hibernate.Transaction; | |
-import ru.atom.game.dao.Database; | |
-import ru.atom.game.dao.GameDao; | |
-import ru.atom.game.model.Game; | |
import ru.atom.game.model.User; | |
import ru.atom.game.util.ThreadSafeQueue; | |
import java.util.ArrayList; | |
import java.util.List; | |
+import java.util.Optional; | |
import java.util.UUID; | |
import java.util.concurrent.ConcurrentHashMap; | |
import java.util.concurrent.TimeUnit; | |
@@ -58,14 +54,22 @@ public class MatchMaker implements Runnable { | |
} | |
public static String popLink(User user) { | |
- String link = memory.get(user); | |
- memory.remove(user); | |
- return link; | |
+ return memory.remove(user); | |
} | |
- | |
- | |
private String stringGenerate() { | |
return UUID.randomUUID().toString(); | |
} | |
+ | |
+ public static Optional<String> tryPopLink(User user, int attemps) { | |
+ String link = null; | |
+ try { | |
+ while (null == (link = popLink(user)) && attemps-- > 0) { | |
+ Thread.sleep(100); | |
+ } | |
+ } catch (InterruptedException ignored) { | |
+ } | |
+ | |
+ return Optional.ofNullable(link); | |
+ } | |
} | |
diff --git a/game/src/main/java/ru/atom/game/matchmaker/MatchMakerResource.java b/game/src/main/java/ru/atom/game/matchmaker/MatchMakerResource.java | |
index ffac58e..580464e 100644 | |
--- a/game/src/main/java/ru/atom/game/matchmaker/MatchMakerResource.java | |
+++ b/game/src/main/java/ru/atom/game/matchmaker/MatchMakerResource.java | |
@@ -1,24 +1,28 @@ | |
package ru.atom.game.matchmaker; | |
+import com.google.gson.Gson; | |
+import com.google.gson.JsonElement; | |
+import com.google.gson.JsonObject; | |
import org.apache.logging.log4j.LogManager; | |
import org.apache.logging.log4j.Logger; | |
import org.hibernate.Session; | |
import org.hibernate.Transaction; | |
import ru.atom.game.auth.Authorized; | |
import ru.atom.game.dao.*; | |
-import com.google.gson.Gson; | |
-import com.google.gson.JsonElement; | |
-import com.google.gson.JsonObject; | |
import ru.atom.game.model.Game; | |
import ru.atom.game.model.Score; | |
import ru.atom.game.model.Token; | |
import ru.atom.game.model.User; | |
import ru.atom.game.util.ThreadSafeQueue; | |
-import javax.ws.rs.*; | |
+import javax.ws.rs.Consumes; | |
+import javax.ws.rs.HeaderParam; | |
+import javax.ws.rs.POST; | |
+import javax.ws.rs.Path; | |
import javax.ws.rs.core.HttpHeaders; | |
import javax.ws.rs.core.Response; | |
import java.util.Map; | |
+import java.util.Optional; | |
/** | |
* Created by zarina on 01.05.17. | |
@@ -33,19 +37,23 @@ public class MatchMakerResource { | |
@Path("/join") | |
public Response join(@HeaderParam(HttpHeaders.AUTHORIZATION) String tokenParam) { | |
Response response; | |
- String s_token = tokenParam.substring("Bearer".length()).trim(); | |
- log.info("Join with token " + s_token); | |
+ String receivedToken = tokenParam.substring("Bearer".length()).trim(); | |
+ log.info("Join with token " + receivedToken); | |
Transaction txn = null; | |
try (Session session = Database.session()) { | |
txn = session.beginTransaction(); | |
- Token token = TokenDao.getInstance().getByToken(session, s_token); | |
+ Token token = TokenDao.getInstance().getByToken(session, receivedToken); | |
if (token == null) { | |
log.info("Token not found"); | |
response = Response.status(Response.Status.BAD_REQUEST).build(); | |
} else { | |
ThreadSafeQueue.getInstance().offer(token.getUser()); | |
- while (MatchMaker.getLink(token.getUser()) == null); | |
- response = Response.ok("localhost:8090/game/?id=" + MatchMaker.popLink(token.getUser())).build(); | |
+ | |
+ Optional<String> link = MatchMaker.tryPopLink(token.getUser(), 100); | |
+ | |
+ // придумать нормальный ответ | |
+ response = link.map(s -> Response.ok("localhost:8090/game/?id=" + s).build()) | |
+ .orElseGet(() -> Response.status(Response.Status.BAD_REQUEST).build()); | |
} | |
txn.commit(); | |
@@ -65,8 +73,8 @@ public class MatchMakerResource { | |
public Response finish(String json) { | |
JsonObject jobj = new Gson().fromJson(json, JsonObject.class); | |
- if (jobj.get("id").isJsonNull() || jobj.get("id").toString().isEmpty() || jobj.get("result").isJsonNull() || | |
- jobj.get("result").toString().isEmpty()) { | |
+ if (jobj.get("id").isJsonNull() || jobj.get("id").toString().isEmpty() || jobj.get("result").isJsonNull() | |
+ || jobj.get("result").toString().isEmpty()) { | |
log.info("Params empty"); | |
return Response.status(Response.Status.BAD_REQUEST).build(); | |
} | |
@@ -78,13 +86,13 @@ public class MatchMakerResource { | |
txn = session.beginTransaction(); | |
Game game = GameDao.getInstance().getById(session, jobj.get("id").getAsInt()); | |
- if (game == null){ | |
+ if (game == null) { | |
log.info("Game " + jobj.get("id") + " not found"); | |
response = Response.status(Response.Status.BAD_REQUEST).build(); | |
} else { | |
for (Map.Entry<String, JsonElement> result : jobj.get("result").getAsJsonObject().entrySet()) { | |
User user = UserDao.getInstance().getByName(session, result.getKey()); | |
- if (user == null){ | |
+ if (user == null) { | |
log.info("User " + result.getKey() + " not found"); | |
txn.rollback(); | |
return Response.status(Response.Status.BAD_REQUEST).build(); | |
diff --git a/game/src/main/java/ru/atom/game/model/GameSession.java b/game/src/main/java/ru/atom/game/model/GameSession.java | |
index f6ebf2c..3b978e6 100644 | |
--- a/game/src/main/java/ru/atom/game/model/GameSession.java | |
+++ b/game/src/main/java/ru/atom/game/model/GameSession.java | |
@@ -19,8 +19,9 @@ public class GameSession implements Tickable { | |
private List<Player> players = new ArrayList<>(); | |
private Set<Pair<Action, Integer>> actions = new LinkedHashSet<>(); | |
- public enum Action{ | |
- PLANT_BOMB, MOVE_UP, MOVE_DOWN, MOVE_RIGHT, MOVE_LEFT, MOVE_IDLE | |
+ | |
+ public enum Action { | |
+ PLANT_BOMB, MOVE_UP, MOVE_DOWN, MOVE_RIGHT, MOVE_LEFT, MOVE_IDLE, | |
} | |
public static int createId() { | |
@@ -45,8 +46,8 @@ public class GameSession implements Tickable { | |
if (x % 2 == 0 && y % 2 == 0 || y == 0 || y == MAX_Y - 1 || x == 0 || x == MAX_X - 1) { | |
// Create Wall | |
addGameObject(new Wall(new Point(x, y))); | |
- } else if (!((x == 1 || x == MAX_X - 2) && (y < 3 || y > MAX_Y - 4) || (x == 2 || x== MAX_X - 3) && | |
- (y == 1 || y == MAX_Y - 2))) { | |
+ } else if (!((x == 1 || x == MAX_X - 2) && (y < 3 || y > MAX_Y - 4) || (x == 2 || x == MAX_X - 3) | |
+ && (y == 1 || y == MAX_Y - 2))) { | |
// Create Wood | |
addGameObject(new Wood(new Point(x, y))); | |
} | |
@@ -71,7 +72,7 @@ public class GameSession implements Tickable { | |
@Override | |
public void tick(long elapsed) { | |
- log.info("tick"); | |
+ // log.info("tick"); | |
ArrayList<Temporary> dead = new ArrayList<>(); | |
for (GameObject gameObject : gameObjects) { | |
if (gameObject instanceof Tickable) { | |
@@ -81,19 +82,25 @@ public class GameSession implements Tickable { | |
dead.add((Temporary)gameObject); | |
} | |
-// log.info(actions); | |
+ // log.info(actions); | |
// TODO reaction on action | |
for (Pair pair: actions) { | |
if (gameObject instanceof Movable && gameObject.getId() == (Integer) pair.getValue()) { | |
switch ((Action) pair.getKey()) { | |
case MOVE_UP: | |
((Movable) gameObject).move(Movable.Direction.UP); | |
+ break; | |
case MOVE_DOWN: | |
((Movable) gameObject).move(Movable.Direction.DOWN); | |
+ break; | |
case MOVE_RIGHT: | |
((Movable) gameObject).move(Movable.Direction.RIGHT); | |
+ break; | |
case MOVE_LEFT: | |
((Movable) gameObject).move(Movable.Direction.LEFT); | |
+ break; | |
+ default: | |
+ log.warn("Illegal action {}", pair.getKey().toString()); | |
} | |
} | |
} | |
diff --git a/game/src/main/java/ru/atom/game/model/User.java b/game/src/main/java/ru/atom/game/model/User.java | |
index 4c11f05..e1dee28 100644 | |
--- a/game/src/main/java/ru/atom/game/model/User.java | |
+++ b/game/src/main/java/ru/atom/game/model/User.java | |
@@ -2,7 +2,12 @@ package ru.atom.game.model; | |
import org.hibernate.annotations.CreationTimestamp; | |
-import javax.persistence.*; | |
+import javax.persistence.Column; | |
+import javax.persistence.Entity; | |
+import javax.persistence.GeneratedValue; | |
+import javax.persistence.GenerationType; | |
+import javax.persistence.Id; | |
+import javax.persistence.Table; | |
import java.io.UnsupportedEncodingException; | |
import java.math.BigInteger; | |
diff --git a/game/src/main/java/ru/atom/game/network/Broker.java b/game/src/main/java/ru/atom/game/network/Broker.java | |
index 0e8a4f6..b9edbe8 100644 | |
--- a/game/src/main/java/ru/atom/game/network/Broker.java | |
+++ b/game/src/main/java/ru/atom/game/network/Broker.java | |
@@ -34,7 +34,7 @@ public class Broker { | |
if (message.getTopic() == Topic.PLANT_BOMB) { | |
gameSession.addAction(GameSession.Action.PLANT_BOMB, playerId); | |
log.info("receive [" + Topic.PLANT_BOMB + "] message: " + message.getData()); | |
- } else if(message.getTopic() == Topic.MOVE) { | |
+ } else if (message.getTopic() == Topic.MOVE) { | |
HashMap<String, String> data = JsonHelper.fromJson(message.getData(), HashMap.class); | |
if (data.get("direction").equals(Movable.Direction.UP.name())) { | |
gameSession.addAction(GameSession.Action.MOVE_UP, playerId); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment