// Path: src/main/java/khs/trouble/controller/ServicesHandler.java
// 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}")
// 	private void fetchCurrentServices() throws JsonProcessingException, IOException {
// 		this.serviceContainer = new ServiceContainer();
// 		List<Service> services = new ArrayList<Service>();
// 		serviceContainer.setServices(services);
// 
// 		List<String> list = discoveryClient.getServices();
// 		for (Iterator<String> iterator = list.iterator(); iterator.hasNext();) {
// 			String serviceId = (String) iterator.next();
// 			Service application = new Service();
// 			application.setName(serviceId);
// 			application.setInstances(discoveryClient.getInstances(serviceId));
// 			services.add(application);
// 		}
// 		for (Iterator<WebSocketSession> iterator = sessions.iterator(); iterator.hasNext();) {
// 			WebSocketSession session = iterator.next();
// 			sendServices(session);
// 		}
// 	}
// }
// 
// Path: src/main/java/khs/trouble/controller/EventsHandler.java
// @Component
// public class EventsHandler extends TextWebSocketHandler {
// 
// 	public List<WebSocketSession> sessions = new ArrayList<WebSocketSession>();
// 
// 	@Autowired
// 	private EventService eventService;
// 
// 	@Autowired
// 	private ObjectMapper objectMapper;
// 
// 	private EventContainer eventContainer;
// 
// 	public EventsHandler() {
// 	}
// 
// 	@Override
// 	public void afterConnectionEstablished(WebSocketSession session) throws Exception {
// 		sessions.add(session);
// 		sendEvents(session);
// 	}
// 
// 	private void sendEvents(WebSocketSession session) throws JsonProcessingException, IOException {
// 		session.sendMessage(new TextMessage(objectMapper.writeValueAsString(this.eventContainer)));
// 	}
// 
// 	private void sendEvent(WebSocketSession session, Event event) throws JsonProcessingException, IOException {
// 		session.sendMessage(new TextMessage(objectMapper.writeValueAsString(event)));
// 	}
// 
// 	public void sendSingleEvent(Event event) throws JsonProcessingException, IOException {
// 		for (Iterator<WebSocketSession> iterator = sessions.iterator(); iterator.hasNext();) {
// 			WebSocketSession session = iterator.next();
// 			sendEvent(session, event);
// 		}
// 	}
// 
// 	@Override
// 	public void afterConnectionClosed(WebSocketSession session, CloseStatus status) throws Exception {
// 		sessions.remove(session);
// 	}
// 
// 	public void fetchCurrentEvents() throws JsonProcessingException, IOException {
// 		this.eventContainer = new EventContainer();
// 		Iterable<Event> list = eventService.events();
// 		eventContainer.setEvents(list);
// 
// 		for (Iterator<WebSocketSession> iterator = sessions.iterator(); iterator.hasNext();) {
// 			WebSocketSession session = iterator.next();
// 			sendEvents(session);
// 		}
// 	}
// }


import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.WebSocketHandler;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;
import khs.trouble.controller.ServicesHandler;
import khs.trouble.controller.EventsHandler;
package khs.trouble;


//import org.springframework.beans.factory.annotation.Autowired;


@EnableWebSocket
@Configuration
public class WebSocketConfiguration implements WebSocketConfigurer {
	
//	@Autowired
//	EventsHandler eventsHandler;

	@Override
	public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
		registry.addHandler(servicesHandler(), "/ws/services").setAllowedOrigins("*");
		registry.addHandler(eventsHandler(), "/ws/events").setAllowedOrigins("*");
	}

	@Bean
	public WebSocketHandler servicesHandler() {
// Path: src/main/java/khs/trouble/controller/ServicesHandler.java
// 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}")
// 	private void fetchCurrentServices() throws JsonProcessingException, IOException {
// 		this.serviceContainer = new ServiceContainer();
// 		List<Service> services = new ArrayList<Service>();
// 		serviceContainer.setServices(services);
// 
// 		List<String> list = discoveryClient.getServices();
// 		for (Iterator<String> iterator = list.iterator(); iterator.hasNext();) {
// 			String serviceId = (String) iterator.next();
// 			Service application = new Service();
// 			application.setName(serviceId);
// 			application.setInstances(discoveryClient.getInstances(serviceId));
// 			services.add(application);
// 		}
// 		for (Iterator<WebSocketSession> iterator = sessions.iterator(); iterator.hasNext();) {
// 			WebSocketSession session = iterator.next();
// 			sendServices(session);
// 		}
// 	}
// }
// 
// Path: src/main/java/khs/trouble/controller/EventsHandler.java
// @Component
// public class EventsHandler extends TextWebSocketHandler {
// 
// 	public List<WebSocketSession> sessions = new ArrayList<WebSocketSession>();
// 
// 	@Autowired
// 	private EventService eventService;
// 
// 	@Autowired
// 	private ObjectMapper objectMapper;
// 
// 	private EventContainer eventContainer;
// 
// 	public EventsHandler() {
// 	}
// 
// 	@Override
// 	public void afterConnectionEstablished(WebSocketSession session) throws Exception {
// 		sessions.add(session);
// 		sendEvents(session);
// 	}
// 
// 	private void sendEvents(WebSocketSession session) throws JsonProcessingException, IOException {
// 		session.sendMessage(new TextMessage(objectMapper.writeValueAsString(this.eventContainer)));
// 	}
// 
// 	private void sendEvent(WebSocketSession session, Event event) throws JsonProcessingException, IOException {
// 		session.sendMessage(new TextMessage(objectMapper.writeValueAsString(event)));
// 	}
// 
// 	public void sendSingleEvent(Event event) throws JsonProcessingException, IOException {
// 		for (Iterator<WebSocketSession> iterator = sessions.iterator(); iterator.hasNext();) {
// 			WebSocketSession session = iterator.next();
// 			sendEvent(session, event);
// 		}
// 	}
// 
// 	@Override
// 	public void afterConnectionClosed(WebSocketSession session, CloseStatus status) throws Exception {
// 		sessions.remove(session);
// 	}
// 
// 	public void fetchCurrentEvents() throws JsonProcessingException, IOException {
// 		this.eventContainer = new EventContainer();
// 		Iterable<Event> list = eventService.events();
// 		eventContainer.setEvents(list);
// 
// 		for (Iterator<WebSocketSession> iterator = sessions.iterator(); iterator.hasNext();) {
// 			WebSocketSession session = iterator.next();
// 			sendEvents(session);
// 		}
// 	}
// }


// Path: src/main/java/khs/trouble/WebSocketConfiguration.java
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.WebSocketHandler;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;
import khs.trouble.controller.ServicesHandler;
import khs.trouble.controller.EventsHandler;

package khs.trouble;


//import org.springframework.beans.factory.annotation.Autowired;


@EnableWebSocket
@Configuration
public class WebSocketConfiguration implements WebSocketConfigurer {
	
//	@Autowired
//	EventsHandler eventsHandler;

	@Override
	public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
		registry.addHandler(servicesHandler(), "/ws/services").setAllowedOrigins("*");
		registry.addHandler(eventsHandler(), "/ws/events").setAllowedOrigins("*");
	}

	@Bean
	public WebSocketHandler servicesHandler() {
		return new ServicesHandler();