// Path: src/main/java/khs/trouble/model/Service.java
// public class Service {
// 
// 	private String name;
// 	private List<ServiceInstance> instances;
// 
// 	public String getName() {
// 		return name;
// 	}
// 
// 	public void setName(String name) {
// 		this.name = name;
// 	}
// 
// 	public List<ServiceInstance> getInstances() {
// 		return instances;
// 	}
// 
// 	public void setInstances(List<ServiceInstance> instances) {
// 		this.instances = instances;
// 	}
// }
// 
// Path: src/main/java/khs/trouble/model/ServiceContainer.java
// public class ServiceContainer {
// 
// 	private List<Service> services;
// 
// 	public List<Service> getServices() {
// 		return services;
// 	}
// 
// 	public void setServices(List<Service> services) {
// 		this.services = services;
// 	}
// }


import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.socket.CloseStatus;
import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketSession;
import org.springframework.web.socket.handler.TextWebSocketHandler;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import khs.trouble.model.Service;
import khs.trouble.model.ServiceContainer;
package khs.trouble.controller;





public class ServicesHandler extends TextWebSocketHandler {

	public List<WebSocketSession> sessions = new ArrayList<WebSocketSession>();

	@Autowired
	private DiscoveryClient discoveryClient;

	@Autowired
	private ObjectMapper objectMapper;

	private ServiceContainer serviceContainer;

	public ServicesHandler() {
		super();
	}

	@Override
	public void afterConnectionEstablished(WebSocketSession session) throws Exception {
		sessions.add(session);
		sendServices(session);
	}

	private void sendServices(WebSocketSession session) throws JsonProcessingException, IOException {
		session.sendMessage(new TextMessage(objectMapper.writeValueAsString(this.serviceContainer)));
	}

	@Override
	public void afterConnectionClosed(WebSocketSession session, CloseStatus status) throws Exception {
		sessions.remove(session);
	}

// Path: src/main/java/khs/trouble/model/Service.java
// public class Service {
// 
// 	private String name;
// 	private List<ServiceInstance> instances;
// 
// 	public String getName() {
// 		return name;
// 	}
// 
// 	public void setName(String name) {
// 		this.name = name;
// 	}
// 
// 	public List<ServiceInstance> getInstances() {
// 		return instances;
// 	}
// 
// 	public void setInstances(List<ServiceInstance> instances) {
// 		this.instances = instances;
// 	}
// }
// 
// Path: src/main/java/khs/trouble/model/ServiceContainer.java
// public class ServiceContainer {
// 
// 	private List<Service> services;
// 
// 	public List<Service> getServices() {
// 		return services;
// 	}
// 
// 	public void setServices(List<Service> services) {
// 		this.services = services;
// 	}
// }


// Path: src/main/java/khs/trouble/controller/ServicesHandler.java
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.socket.CloseStatus;
import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketSession;
import org.springframework.web.socket.handler.TextWebSocketHandler;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import khs.trouble.model.Service;
import khs.trouble.model.ServiceContainer;

package khs.trouble.controller;





public class ServicesHandler extends TextWebSocketHandler {

	public List<WebSocketSession> sessions = new ArrayList<WebSocketSession>();

	@Autowired
	private DiscoveryClient discoveryClient;

	@Autowired
	private ObjectMapper objectMapper;

	private ServiceContainer serviceContainer;

	public ServicesHandler() {
		super();
	}

	@Override
	public void afterConnectionEstablished(WebSocketSession session) throws Exception {
		sessions.add(session);
		sendServices(session);
	}

	private void sendServices(WebSocketSession session) throws JsonProcessingException, IOException {
		session.sendMessage(new TextMessage(objectMapper.writeValueAsString(this.serviceContainer)));
	}

	@Override
	public void afterConnectionClosed(WebSocketSession session, CloseStatus status) throws Exception {
		sessions.remove(session);
	}

	@Scheduled(fixedDelayString = "${trouble.refreshInterval:30000}")