Package model

Source Code of model.ATMSModel$MyTimerTask

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package model;

import enums.ServiceTags;
import framework.IATMSModel;
import framework.IPlaneUpdateObject;
import framework.IUserInfo;
import java.io.IOException;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;
import java.util.logging.Level;
import java.util.logging.Logger;
import logic.ATMServer;
import messages.ServiceObject;

/**
*
* @author swift
*/
public class ATMSModel implements IATMSModel {

    private ATMServer server;
    private List<IPlaneUpdateObject> updateList = Collections.synchronizedList(new LinkedList<IPlaneUpdateObject>());
    private List<String> funcList = Collections.synchronizedList(new LinkedList<String>());
    private Map<String, IUserInfo> userList = Collections.synchronizedMap(new HashMap<String, IUserInfo>());
    private Map<String, Long> activityList = Collections.synchronizedMap(new HashMap<String, Long>());
    // Enthaelt den TimerTask
    private MyTimerTask timerTask;
    // Der Timer der Laeuft
    private Timer timer;

    public ATMSModel(ATMServer server) {
        this.server = server;
        funcList.add("-no function-");
        funcList.add("Pilot");
        funcList.add("SwissRadar");
        funcList.add("TowerBern");
        funcList.add("TowerZurich");
        funcList.add("TowerGenf");


        timer = new Timer();
        timerTask = new MyTimerTask();
        timer.schedule(timerTask, 5000L, 5000L);
    }

    public Object[] getUserNames() {
        return userList.keySet().toArray();
    }

    public synchronized void sendUpdateList(IUserInfo ui) {
        Iterator it = updateList.iterator();
        while (it.hasNext()) {
            try {
                IPlaneUpdateObject puo = (IPlaneUpdateObject) it.next();
                ui.getOOS().writeObject(puo);
            } catch (IOException ex) {
                Logger.getLogger(ATMSModel.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    public List<IUserInfo> getUserList() {
        return new LinkedList(userList.values());
    }

    public void addUserInfo(IUserInfo ui) {
        userList.put(ui.getNickName(), ui);
        server.broadcasUserList(ui);
    }

    public synchronized void setActivity(String iP) {
        activityList.put(iP, System.currentTimeMillis());
    }

    public void updateUserInfo(IUserInfo ui) {
        userList.remove(ui.getNickName());
        userList.put(ui.getNickName(), ui);
        server.sendUserList(ui);
    }

    public void removeUserInfo(IUserInfo ui) {
        userList.remove(ui.getNickName());
        server.broadcasUserList(ui);
    }

    public boolean isNameUnique(String name) {
        return !userList.containsKey(name);
    }

    public synchronized void addPlaneUpdate(IPlaneUpdateObject puo, IUserInfo senderUi) {
        System.out.println("update received");
        Iterator<IPlaneUpdateObject> it = updateList.iterator();
        while (it.hasNext()) {
            IPlaneUpdateObject current = it.next();
            if (current.getIp().equals(puo.getIp())) {
                it.remove();
            }
        }
        updateList.add(puo);
        server.broadcast(puo, senderUi);
    }

    public synchronized void resetPlanes(String func) {
        if (func != null && !(func.equals("Pilot") || func.equals("-no function-"))) {
            System.out.println("resetting planes from " + func);
            Iterator<IPlaneUpdateObject> it = updateList.iterator();
            while (it.hasNext()) {
                IPlaneUpdateObject current = it.next();
//            System.out.println(current.getActiveController()+" - "+func);
                if (current.getActiveController().equals(func)) {
                    current.setController("???");
                    System.out.println(func + " deleted, new " + current.getActiveController());
                    server.broadcast(current);
                }
            }
        }
    }

    public synchronized String[] getFuncs() {
        Object[] funcs = funcList.toArray();
        String[] tmp = new String[funcs.length];

        for (int i = 0; i < funcs.length; i++) {
            tmp[i] = (String) funcs[i];
        }

        return tmp;
    }

    public void addFunction(String func) {
        if (func != null && !(func.equals("Pilot") || func.equals("-no function-"))) {
            resetPlanes(func);
            funcList.add(func);
            ServiceObject sout = new ServiceObject("Server", ServiceTags.FunctionList);
            sout.setFunctions(getFuncs());
            server.broadcast(sout);
        }
    }

    public void removeFunction(String func) {
        if (!(func.equals("Pilot") || func.equals("-no function-"))) {
            funcList.remove(func);
        }
        ServiceObject sout = new ServiceObject("Server", ServiceTags.FunctionList);
        sout.setFunctions(getFuncs());
        server.broadcast(sout);
    }

    public ATMServer getServer() {
        return server;
    }

    private class MyTimerTask extends TimerTask {

        private long start;

        public MyTimerTask() {
            start = System.currentTimeMillis();
        }

        public synchronized void run() {
            if (System.currentTimeMillis() - start > 5000L) {

                LinkedList act = new LinkedList(activityList.keySet());
                Iterator<String> actIt = act.iterator();

                while (actIt.hasNext()) {
                    String ip = actIt.next();
                    Long time = activityList.get(ip);
                    if (time != null) {
                        if (System.currentTimeMillis() - time > 5000) {
                            System.out.println("client without signal since 5 seconds");
                            activityList.remove(ip);
                            Iterator it = updateList.iterator();
                            while (it.hasNext()) {
                                IPlaneUpdateObject puo = (IPlaneUpdateObject) it.next();
                                if (ip.equals(puo.getIp())) {
                                    System.out.println("client removed");
                                    it.remove();
                                }
                            }
                        }
                    }
                }
            }
        }

        public void setActive() {
            start = System.currentTimeMillis();
        }
    }
}
TOP

Related Classes of model.ATMSModel$MyTimerTask

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.