/*
 * 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.http;

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.ServletException;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServlet;
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.Position;
import org.traccar.model.User;

public class AsyncServlet extends HttpServlet {

    private static final long ASYNC_TIMEOUT = 120000;
    
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        async(req.startAsync());
    }
    
    public class AsyncSession {
        
        private 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<Long>();
        private Timeout sessionTimeout;
        private Timeout requestTimeout;
        private final Map<Long, Position> positions = new HashMap<Long, Position>();
        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) {
                positions.put(position.getDeviceId(), position);
            }
            
            Context.getConnectionManager().addListener(devices, dataListener);
        }
        
        public boolean hasDevice(long deviceId) {
            return devices.contains(deviceId);
        }
        
        private final ConnectionManager.DataCacheListener dataListener = new ConnectionManager.DataCacheListener() {
            @Override
            public void onUpdate(Position position) {
                synchronized (AsyncSession.this) {
                    logEvent("onUpdate deviceId: " + position.getDeviceId());
                    if (!destroyed) {
                        if (requestTimeout != null) {
                            requestTimeout.cancel();
                            requestTimeout = null;
                        }
                        positions.put(position.getDeviceId(), position);
                        if (activeContext != null) {
                            response();
                        }
                    }
                }
            }
        };
        
        private final TimerTask sessionTimer = new TimerTask() {
            @Override
            public void run(Timeout tmt) throws Exception {
                synchronized (AsyncSession.this) {
                    logEvent("sessionTimeout");
                    Context.getConnectionManager().removeListener(devices, dataListener);
                    synchronized (asyncSessions) {
                        asyncSessions.remove(userId);
                    }
                    destroyed = true;
                }
            }
        };
                
        private final TimerTask requestTimer = new TimerTask() {
            @Override
            public void run(Timeout tmt) throws Exception {
                synchronized (AsyncSession.this) {
                    logEvent("requestTimeout");
                    if (!destroyed) {
                        if (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 (!positions.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(positions.values()));
                positions.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> asyncSessions = new HashMap<Long, AsyncSession>();
    
    public static void sessionRefreshUser(long userId) {
        asyncSessions.remove(userId);
    }
    
    public static void sessionRefreshDevice(long deviceId) {
        Iterator<Entry<Long, AsyncSession>> iterator = asyncSessions.entrySet().iterator();
        while (iterator.hasNext()) {
            if (iterator.next().getValue().hasDevice(deviceId)) {
                iterator.remove();
            }
        }
    }
    
    private void async(final AsyncContext context) {
        
        context.setTimeout(ASYNC_TIMEOUT);
        HttpServletRequest req = (HttpServletRequest) context.getRequest();
        User user = (User) req.getSession().getAttribute(MainServlet.USER_KEY);
        
        synchronized (asyncSessions) {
            
            if (Boolean.valueOf(req.getParameter("first")) || !asyncSessions.containsKey(user.getId())) {
                Collection<Long> devices = Context.getPermissionsManager().allowedDevices(user.getId());
                asyncSessions.put(user.getId(), new AsyncSession(user.getId(), devices));
            }
            
            asyncSessions.get(user.getId()).request(context);
        }
    }

}

@@ -1511,20 +1511,20 @@
 extends 
-Http
+Base
 Servlet 
@@ -1617,21 +1617,39 @@
 ted 
-void service(
+boolean handle(String command, 
 Http
@@ -1705,28 +1705,8 @@
 ows 
-ServletException, IO
 Exce
@@ -1743,17 +1743,54 @@
 tAsync()
-)
+, getUserId(req));%0A        return true
 ;%0A    %7D%0A
@@ -7302,32 +7302,45 @@
 cContext context
+, long userId
 ) %7B%0A        %0A   
@@ -7459,96 +7459,8 @@
 ();%0A
-        User user = (User) req.getSession().getAttribute(MainServlet.USER_KEY);%0A        
 %0A   
@@ -7601,24 +7601,18 @@
 Key(user
-.getId()
+Id
 )) %7B%0A   
@@ -7702,24 +7702,18 @@
 ces(user
-.getId()
+Id
 );%0A     
@@ -7745,24 +7745,18 @@
 put(user
-.getId()
+Id
 , new As
@@ -7770,24 +7770,18 @@
 ion(user
-.getId()
+Id
 , device
@@ -7850,16 +7850,10 @@
 user
-.getId()
+Id
 ).re

Handle async servlet exceptions