// Path: src/krasa/frameswitcher/FrameSwitcherApplicationService.java
// @State(name = "FrameSwitcherSettings", storages = {@Storage("FrameSwitcherSettings.xml")})
// public class FrameSwitcherApplicationService implements PersistentStateComponent<FrameSwitcherSettings>, Disposable {
// 
// 	private static final Logger LOG = Logger.getInstance(FrameSwitcherApplicationService.class);
// 
// 	public static final String IDE_MAX_RECENT_PROJECTS = "ide.max.recent.projects";
// 
// 
// 	private FrameSwitcherSettings settings=new FrameSwitcherSettings();
// 	private RemoteInstancesState remoteInstancesState = new RemoteInstancesState();
// 	private ProjectFocusMonitor projectFocusMonitor=new ProjectFocusMonitor();
// 	private RemoteSender remoteSender;
// 	boolean initialized;
// 
// 	public static FrameSwitcherApplicationService getInstance() {
// 		FrameSwitcherApplicationService service = ServiceManager.getService(FrameSwitcherApplicationService.class);
// 		if (!service.initialized) {
// 			service.initComponent();
// 		}
// 		return service;
// 	}
// 
// 	public FrameSwitcherApplicationService() {
// 	}
// 
// 	public void initComponent() {
// 		long start = System.currentTimeMillis();
// 		initialized = true;
// 		initRemoting();
// 		LOG.debug("initComponent done in ", System.currentTimeMillis() - start, "ms");
// 	}
// 
// 	private synchronized void initRemoting() {
// 		if (this.settings.isRemoting()) {
// 			if (!(remoteSender instanceof RemoteSenderImpl)) {
// 				try {
// 					UUID uuid = getState().getOrInitializeUuid();
// 					remoteSender = new RemoteSenderImpl(this, uuid, new Receiver(uuid, this), getState().getPort());
// 				} catch (Throwable e) {
// 					remoteInstancesState = new RemoteInstancesState();
// 					remoteSender = new DummyRemoteSender();
// 					LOG.error(e);
// 				}
// 			}
// 		} else {
// 			if (remoteSender instanceof RemoteSenderImpl) {
// 				remoteSender.dispose();
// 			}
// 
// 			if (!(remoteSender instanceof DummyRemoteSender)) {
// 				remoteInstancesState = new RemoteInstancesState();
// 				remoteSender = new DummyRemoteSender();
// 			}
// 		}
// 
// 	}
// 
// 	public ProjectFocusMonitor getProjectFocusMonitor() {
// 		return projectFocusMonitor;
// 	}
// 
// 
// 	@NotNull
// 	@Override
// 	public FrameSwitcherSettings getState() {
// 		return settings;
// 	}
// 
// 	@Override
// 	public void loadState(FrameSwitcherSettings frameSwitcherSettings) {
// 		this.settings = frameSwitcherSettings;
// 		frameSwitcherSettings.applyMaxRecentProjectsToRegistry();
// 	}
// 
// 	public void updateSettings(FrameSwitcherSettings settings) {
// 		this.settings = settings;
// 		this.settings.applyOrResetMaxRecentProjectsToRegistry();
// 		initRemoting();
// 	}
// 
// 	public RemoteInstancesState getRemoteInstancesState() {
// 		return remoteInstancesState;
// 	}
// 
// 	public RemoteSender getRemoteSender() {
// 		return remoteSender;
// 	}
// 
// 	@Override
// 	public void dispose() {
// 		long start = System.currentTimeMillis();
// 		if (getRemoteSender() != null) {
// 			getRemoteSender().dispose();
// 		}
// 		LOG.debug("disposeComponent done in ", System.currentTimeMillis() - start, "ms");
// 	}
// }


import com.intellij.openapi.diagnostic.Logger;
import krasa.frameswitcher.FrameSwitcherApplicationService;
import krasa.frameswitcher.networking.dto.*;
import java.util.*;
package krasa.frameswitcher.networking;



public class RemoteInstancesState {
	private static final Logger LOG = Logger.getInstance(RemoteInstancesState.class);

	public static final int TIMEOUT_millis = 10 * 1000;
	                                           
	volatile long  lastPingSend = 0;
	public Map<UUID, RemoteIdeInstance> remoteIdeInstances = new HashMap<>();

	public synchronized List<RemoteIdeInstance> getRemoteIdeInstances() {
		List<RemoteIdeInstance> list = new ArrayList<>(this.remoteIdeInstances.values());
		list.sort(Comparator.comparing(o -> o.ideName));
		return list;
	}

	public synchronized void sweepRemoteInstance() {
		for (Iterator<Map.Entry<UUID, RemoteIdeInstance>> iterator = remoteIdeInstances.entrySet().iterator(); iterator.hasNext(); ) {
			Map.Entry<UUID, RemoteIdeInstance> entry = iterator.next();
			RemoteIdeInstance value = entry.getValue();
			if (lastPingSend - value.lastResponse > TIMEOUT_millis) {
				LOG.debug("Removing " + value);
				iterator.remove();
			}
		}
	}

	public synchronized void processPingResponse(PingResponse object) {
		RemoteIdeInstance remoteIdeInstance = remoteIdeInstances.get(object.getUuid());
		if (remoteIdeInstance != null) {
			remoteIdeInstance.lastResponse = System.currentTimeMillis();
		} else {
			LOG.warn("DESYNC " + object);
// Path: src/krasa/frameswitcher/FrameSwitcherApplicationService.java
// @State(name = "FrameSwitcherSettings", storages = {@Storage("FrameSwitcherSettings.xml")})
// public class FrameSwitcherApplicationService implements PersistentStateComponent<FrameSwitcherSettings>, Disposable {
// 
// 	private static final Logger LOG = Logger.getInstance(FrameSwitcherApplicationService.class);
// 
// 	public static final String IDE_MAX_RECENT_PROJECTS = "ide.max.recent.projects";
// 
// 
// 	private FrameSwitcherSettings settings=new FrameSwitcherSettings();
// 	private RemoteInstancesState remoteInstancesState = new RemoteInstancesState();
// 	private ProjectFocusMonitor projectFocusMonitor=new ProjectFocusMonitor();
// 	private RemoteSender remoteSender;
// 	boolean initialized;
// 
// 	public static FrameSwitcherApplicationService getInstance() {
// 		FrameSwitcherApplicationService service = ServiceManager.getService(FrameSwitcherApplicationService.class);
// 		if (!service.initialized) {
// 			service.initComponent();
// 		}
// 		return service;
// 	}
// 
// 	public FrameSwitcherApplicationService() {
// 	}
// 
// 	public void initComponent() {
// 		long start = System.currentTimeMillis();
// 		initialized = true;
// 		initRemoting();
// 		LOG.debug("initComponent done in ", System.currentTimeMillis() - start, "ms");
// 	}
// 
// 	private synchronized void initRemoting() {
// 		if (this.settings.isRemoting()) {
// 			if (!(remoteSender instanceof RemoteSenderImpl)) {
// 				try {
// 					UUID uuid = getState().getOrInitializeUuid();
// 					remoteSender = new RemoteSenderImpl(this, uuid, new Receiver(uuid, this), getState().getPort());
// 				} catch (Throwable e) {
// 					remoteInstancesState = new RemoteInstancesState();
// 					remoteSender = new DummyRemoteSender();
// 					LOG.error(e);
// 				}
// 			}
// 		} else {
// 			if (remoteSender instanceof RemoteSenderImpl) {
// 				remoteSender.dispose();
// 			}
// 
// 			if (!(remoteSender instanceof DummyRemoteSender)) {
// 				remoteInstancesState = new RemoteInstancesState();
// 				remoteSender = new DummyRemoteSender();
// 			}
// 		}
// 
// 	}
// 
// 	public ProjectFocusMonitor getProjectFocusMonitor() {
// 		return projectFocusMonitor;
// 	}
// 
// 
// 	@NotNull
// 	@Override
// 	public FrameSwitcherSettings getState() {
// 		return settings;
// 	}
// 
// 	@Override
// 	public void loadState(FrameSwitcherSettings frameSwitcherSettings) {
// 		this.settings = frameSwitcherSettings;
// 		frameSwitcherSettings.applyMaxRecentProjectsToRegistry();
// 	}
// 
// 	public void updateSettings(FrameSwitcherSettings settings) {
// 		this.settings = settings;
// 		this.settings.applyOrResetMaxRecentProjectsToRegistry();
// 		initRemoting();
// 	}
// 
// 	public RemoteInstancesState getRemoteInstancesState() {
// 		return remoteInstancesState;
// 	}
// 
// 	public RemoteSender getRemoteSender() {
// 		return remoteSender;
// 	}
// 
// 	@Override
// 	public void dispose() {
// 		long start = System.currentTimeMillis();
// 		if (getRemoteSender() != null) {
// 			getRemoteSender().dispose();
// 		}
// 		LOG.debug("disposeComponent done in ", System.currentTimeMillis() - start, "ms");
// 	}
// }


// Path: src/krasa/frameswitcher/networking/RemoteInstancesState.java
import com.intellij.openapi.diagnostic.Logger;
import krasa.frameswitcher.FrameSwitcherApplicationService;
import krasa.frameswitcher.networking.dto.*;
import java.util.*;

package krasa.frameswitcher.networking;



public class RemoteInstancesState {
	private static final Logger LOG = Logger.getInstance(RemoteInstancesState.class);

	public static final int TIMEOUT_millis = 10 * 1000;
	                                           
	volatile long  lastPingSend = 0;
	public Map<UUID, RemoteIdeInstance> remoteIdeInstances = new HashMap<>();

	public synchronized List<RemoteIdeInstance> getRemoteIdeInstances() {
		List<RemoteIdeInstance> list = new ArrayList<>(this.remoteIdeInstances.values());
		list.sort(Comparator.comparing(o -> o.ideName));
		return list;
	}

	public synchronized void sweepRemoteInstance() {
		for (Iterator<Map.Entry<UUID, RemoteIdeInstance>> iterator = remoteIdeInstances.entrySet().iterator(); iterator.hasNext(); ) {
			Map.Entry<UUID, RemoteIdeInstance> entry = iterator.next();
			RemoteIdeInstance value = entry.getValue();
			if (lastPingSend - value.lastResponse > TIMEOUT_millis) {
				LOG.debug("Removing " + value);
				iterator.remove();
			}
		}
	}

	public synchronized void processPingResponse(PingResponse object) {
		RemoteIdeInstance remoteIdeInstance = remoteIdeInstances.get(object.getUuid());
		if (remoteIdeInstance != null) {
			remoteIdeInstance.lastResponse = System.currentTimeMillis();
		} else {
			LOG.warn("DESYNC " + object);
			FrameSwitcherApplicationService.getInstance().getRemoteSender().asyncSendRefresh();