aboutsummaryrefslogtreecommitdiff
path: root/src/org/traccar/web/AsyncServlet.java
diff options
context:
space:
mode:
authorAnton Tananaev <anton.tananaev@gmail.com>2016-04-16 11:24:54 +1200
committerAnton Tananaev <anton.tananaev@gmail.com>2016-04-16 11:24:54 +1200
commit30f10f12d32915d74e36b4bf336e4e2d7ac3aada (patch)
tree995c926096db04c6b3e90b58bcb6a6f7db1bb322 /src/org/traccar/web/AsyncServlet.java
parentfdd2fdf77cc5152986e26abf79f348b23b24ca64 (diff)
downloadtrackermap-server-30f10f12d32915d74e36b4bf336e4e2d7ac3aada.tar.gz
trackermap-server-30f10f12d32915d74e36b4bf336e4e2d7ac3aada.tar.bz2
trackermap-server-30f10f12d32915d74e36b4bf336e4e2d7ac3aada.zip
Remove old web API service
Diffstat (limited to 'src/org/traccar/web/AsyncServlet.java')
-rw-r--r--src/org/traccar/web/AsyncServlet.java237
1 files changed, 0 insertions, 237 deletions
diff --git a/src/org/traccar/web/AsyncServlet.java b/src/org/traccar/web/AsyncServlet.java
deleted file mode 100644
index e4cb64c57..000000000
--- a/src/org/traccar/web/AsyncServlet.java
+++ /dev/null
@@ -1,237 +0,0 @@
-/*
- * Copyright 2015 Anton Tananaev (anton.tananaev@gmail.com)
- *
- * Licensed 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 org.traccar.web;
-
-import java.io.IOException;
-import java.util.Collection;
-import java.util.HashMap;
-import java.util.HashSet;
-import java.util.Iterator;
-import java.util.Map;
-import java.util.Map.Entry;
-import java.util.Set;
-import java.util.concurrent.TimeUnit;
-import javax.json.Json;
-import javax.json.JsonObjectBuilder;
-import javax.servlet.AsyncContext;
-import javax.servlet.ServletResponse;
-import javax.servlet.http.HttpServletRequest;
-import javax.servlet.http.HttpServletResponse;
-import org.jboss.netty.util.Timeout;
-import org.jboss.netty.util.TimerTask;
-import org.traccar.Context;
-import org.traccar.GlobalTimer;
-import org.traccar.database.ConnectionManager;
-import org.traccar.helper.Log;
-import org.traccar.model.Device;
-import org.traccar.model.Position;
-
-public class AsyncServlet extends BaseServlet {
-
- private static final long ASYNC_TIMEOUT = 120000;
-
- @Override
- protected boolean handle(String command, HttpServletRequest req, HttpServletResponse resp) throws Exception {
- async(req.startAsync(), getUserId(req));
- return true;
- }
-
- public static class AsyncSession {
-
- public static final boolean DEBUG_ASYNC = false;
-
- private static final long SESSION_TIMEOUT = 30;
- private static final long REQUEST_TIMEOUT = 20;
-
- private boolean destroyed;
- private final long userId;
- private final Set<Long> devices = new HashSet<>();
- private Timeout sessionTimeout;
- private Timeout requestTimeout;
- private final Set<Device> deviceUpdates = new HashSet<>();
- private final Set<Position> positionUpdates = new HashSet<>();
- private AsyncContext activeContext;
-
- private void logEvent(String message) {
- if (DEBUG_ASYNC) {
- Log.debug("AsyncSession: " + this.hashCode() + " destroyed: " + destroyed + " " + message);
- }
- }
-
- public AsyncSession(long userId, Collection<Long> devices) {
- logEvent("create userId: " + userId + " devices: " + devices.size());
- this.userId = userId;
- this.devices.addAll(devices);
-
- Collection<Position> initialPositions = Context.getConnectionManager().getInitialState(devices);
- for (Position position : initialPositions) {
- positionUpdates.add(position);
- }
-
- Context.getConnectionManager().addListener(devices, dataListener);
- }
-
- public boolean hasDevice(long deviceId) {
- return devices.contains(deviceId);
- }
-
- private final ConnectionManager.UpdateListener dataListener = new ConnectionManager.UpdateListener() {
- @Override
- public void onUpdateDevice(Device device) {
- synchronized (AsyncSession.this) {
- logEvent("onUpdateDevice deviceId: " + device.getId());
- if (!destroyed) {
- if (requestTimeout != null) {
- requestTimeout.cancel();
- requestTimeout = null;
- }
- deviceUpdates.add(device);
- if (activeContext != null) {
- response();
- }
- }
- }
- }
-
- @Override
- public void onUpdatePosition(Position position) {
- synchronized (AsyncSession.this) {
- logEvent("onUpdatePosition deviceId: " + position.getDeviceId());
- if (!destroyed) {
- if (requestTimeout != null) {
- requestTimeout.cancel();
- requestTimeout = null;
- }
- positionUpdates.add(position);
- if (activeContext != null) {
- response();
- }
- }
- }
- }
- };
-
- private final TimerTask sessionTimer = new TimerTask() {
- @Override
- public void run(Timeout tmt) throws Exception {
- synchronized (AsyncSession.this) {
- logEvent("sessionTimeout");
- destroyed = true;
- }
- Context.getConnectionManager().removeListener(devices, dataListener);
- synchronized (ASYNC_SESSIONS) {
- if (ASYNC_SESSIONS.get(userId) == AsyncSession.this) {
- ASYNC_SESSIONS.remove(userId);
- }
- }
- }
- };
-
- private final TimerTask requestTimer = new TimerTask() {
- @Override
- public void run(Timeout tmt) throws Exception {
- synchronized (AsyncSession.this) {
- logEvent("requestTimeout");
- if (!destroyed && activeContext != null) {
- response();
- }
- }
- }
- };
-
- public synchronized void request(AsyncContext context) {
- logEvent("request context: " + context.hashCode());
- if (!destroyed) {
- activeContext = context;
- if (sessionTimeout != null) {
- sessionTimeout.cancel();
- sessionTimeout = null;
- }
-
- if (!deviceUpdates.isEmpty() || !positionUpdates.isEmpty()) {
- response();
- } else {
- requestTimeout = GlobalTimer.getTimer().newTimeout(
- requestTimer, REQUEST_TIMEOUT, TimeUnit.SECONDS);
- }
- }
- }
-
- private synchronized void response() {
- logEvent("response context: " + activeContext.hashCode());
- if (!destroyed) {
- ServletResponse response = activeContext.getResponse();
-
- JsonObjectBuilder result = Json.createObjectBuilder();
- result.add("success", true);
-
- result.add("data", JsonConverter.arrayToJson(positionUpdates));
-
- deviceUpdates.clear();
- positionUpdates.clear();
-
- try {
- response.getWriter().println(result.build().toString());
- } catch (IOException error) {
- Log.warning(error);
- }
-
- activeContext.complete();
- activeContext = null;
-
- sessionTimeout = GlobalTimer.getTimer().newTimeout(
- sessionTimer, SESSION_TIMEOUT, TimeUnit.SECONDS);
- }
- }
-
- }
-
- private static final Map<Long, AsyncSession> ASYNC_SESSIONS = new HashMap<>();
-
- public static void sessionRefreshUser(long userId) {
- synchronized (ASYNC_SESSIONS) {
- ASYNC_SESSIONS.remove(userId);
- }
- }
-
- public static void sessionRefreshDevice(long deviceId) {
- synchronized (ASYNC_SESSIONS) {
- Iterator<Entry<Long, AsyncSession>> iterator = ASYNC_SESSIONS.entrySet().iterator();
- while (iterator.hasNext()) {
- if (iterator.next().getValue().hasDevice(deviceId)) {
- iterator.remove();
- }
- }
- }
- }
-
- private void async(final AsyncContext context, long userId) {
-
- context.setTimeout(ASYNC_TIMEOUT);
- HttpServletRequest req = (HttpServletRequest) context.getRequest();
-
- synchronized (ASYNC_SESSIONS) {
-
- if (Boolean.parseBoolean(req.getParameter("first")) || !ASYNC_SESSIONS.containsKey(userId)) {
- Collection<Long> devices = Context.getPermissionsManager().getDevicePermissions(userId);
- ASYNC_SESSIONS.put(userId, new AsyncSession(userId, devices));
- }
-
- ASYNC_SESSIONS.get(userId).request(context);
- }
- }
-
-}