ttomcat-1778514358873.zip-extract/apache-tomcat-11.0.18-src/webapps/examples/WEB-INF/classes/websocket/snake/SnakeTimer.java

Path
ttomcat-1778514358873.zip-extract/apache-tomcat-11.0.18-src/webapps/examples/WEB-INF/classes/websocket/snake/SnakeTimer.java
Status
scanned
Type
file
Name
SnakeTimer.java
Extension
.java
Programming language
Java
Mime type
text/plain
File type
ASCII text, with CRLF line terminators
Tag

      
    
Rootfs path

      
    
Size
3833 (3.7 KB)
MD5
ce25cc49c11056ddf7cebc12fa949446
SHA1
cd21c7800e4a536257d1406e9197efad9099f442
SHA256
c376cde72662df05f0c5a770155587259db86400272764d1aff1541046681776
SHA512

      
    
SHA1_git
c7a059710596cb25a5248ab78bcf933ef35e2d01
Is binary

      
    
Is text
True
Is archive

      
    
Is media

      
    
Is legal

      
    
Is manifest

      
    
Is readme

      
    
Is top level

      
    
Is key file

      
    
SnakeTimer.java | 3.7 KB |

/* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package websocket.snake; import java.util.Collection; import java.util.Collections; import java.util.Iterator; import java.util.Timer; import java.util.TimerTask; import java.util.concurrent.ConcurrentHashMap; import org.apache.juli.logging.Log; import org.apache.juli.logging.LogFactory; /** * Sets up the timer for the multi-player snake game WebSocket example. */ public class SnakeTimer { private static final Log log = LogFactory.getLog(SnakeTimer.class); private static Timer gameTimer = null; static final long TICK_DELAY = 100; private static final ConcurrentHashMap<Integer, Snake> snakes = new ConcurrentHashMap<>(); protected static synchronized void addSnake(Snake snake) { if (snakes.size() == 0) { startTimer(); } snakes.put(Integer.valueOf(snake.getId()), snake); } protected static Collection<Snake> getSnakes() { return Collections.unmodifiableCollection(snakes.values()); } protected static synchronized void removeSnake(Snake snake) { snakes.remove(Integer.valueOf(snake.getId())); if (snakes.size() == 0) { stopTimer(); } } protected static void tick() { StringBuilder sb = new StringBuilder(); for (Iterator<Snake> iterator = getSnakes().iterator(); iterator.hasNext();) { Snake snake = iterator.next(); snake.update(getSnakes()); sb.append(snake.getLocationsJson()); if (iterator.hasNext()) { sb.append(','); } } broadcast(String.format("{\"type\": \"update\", \"data\" : [%s]}", sb.toString())); } protected static void broadcast(String message) { for (Snake snake : getSnakes()) { try { snake.sendMessage(message); } catch (IllegalStateException ise) { // An ISE can occur if an attempt is made to write to a // WebSocket connection after it has been closed. The // alternative to catching this exception is to synchronise // the writes to the clients along with the addSnake() and // removeSnake() methods that are already synchronised. } } } public static void startTimer() { gameTimer = new Timer(SnakeTimer.class.getSimpleName() + " Timer"); gameTimer.scheduleAtFixedRate(new TimerTask() { @Override public void run() { try { tick(); } catch (RuntimeException e) { log.error("Caught to prevent timer from shutting down", e); } } }, TICK_DELAY, TICK_DELAY); } public static void stopTimer() { if (gameTimer != null) { gameTimer.cancel(); } } }
Detected license expression
apache-2.0
Detected license expression (SPDX)
Apache-2.0
Percentage of license text
30.05
Copyrights

      
    
Holders

      
    
Authors

      
    
License detections License expression License expression SPDX
apache_2_0-4bde3f57-78aa-4201-96bf-531cba09e7de apache-2.0 Apache-2.0
URL Start line End line
http://www.apache.org/licenses/LICENSE-2.0 9 9