Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package org.kurento.tutorial.helloworld; import com.google.gson.Gson; import com.google.gson.GsonBuilder; import com.google.gson.JsonObject; import java.util.ArrayList; import org.springframework.beans.factory.annotation.Autowired; 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 org.kurento.repository.RepositoryClient; import org.kurento.repository.service.pojo.RepositoryItemPlayer; import org.kurento.repository.service.pojo.RepositoryItemRecorder; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.text.SimpleDateFormat; import java.util.Collections; import java.util.Map; import java.util.logging.Level; /** * * @author helder.rodrigues */ public class RepositoryManager extends Thread { public static final String ANSI_RED = "\u001B[31m"; public static final String ANSI_RESET = "\u001B[0m"; private static final String RECORDING_EXT = ".webm"; private ArrayList<WebSocketSession> sessions; private static final SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss-S"); private UserRegistry registry; private RepositoryClient repositoryClient; private final Logger log = LoggerFactory.getLogger(HelloWorldRecHandler.class); private boolean running = true; public RepositoryManager() { sessions = new ArrayList<>(); } public void init(WebSocketSession session, UserRegistry registry, RepositoryClient repositoryClient) { this.registry = registry; this.repositoryClient = repositoryClient; if (!sessions.contains(session)) sessions.add(session); } private RepositoryItemRecorder newRepoItem() { RepositoryItemRecorder repoItem = new RepositoryItemRecorder(); try { System.out.println(ANSI_RED + "RECYCLING REPOSITORY CLIENT" + ANSI_RESET); Map<String, String> metadata = Collections.emptyMap(); synchronized (repositoryClient) { repoItem = repositoryClient.createRepositoryItem(metadata); } System.out.println(ANSI_RED + "REPOSITORY RECYCLED" + ANSI_RESET); } catch (Exception e) { log.warn("Unable to create kurento repository items", e); } return repoItem; } public void run() { while (running) { System.out.println(ANSI_RED + "Thread is running" + ANSI_RESET); try { if (!sessions.isEmpty()) { System.out.println(ANSI_RED + "Online users: " + sessions.size() + ANSI_RESET); for (WebSocketSession sess : sessions) { UserSession user = registry.getBySession(sess); RepositoryItemRecorder repoItem = user.getRepoItem(); this.log(sess, repoItem); repoItem = newRepoItem(); synchronized (sess) { user.setRepoItem(repoItem); } this.log(sess, repoItem); System.out.println(); } } this.sleep(10000); } catch (InterruptedException ex) { java.util.logging.Logger.getLogger(RepositoryManager.class.getName()).log(Level.SEVERE, null, ex); } } } private void log(WebSocketSession sess, RepositoryItemRecorder repoItem) { System.out.println(ANSI_RED + "User ID: " + sess.getId() + " is running" + ANSI_RESET); try { System.out.println(ANSI_RED + "Repo ID: " + repoItem.getId() + ANSI_RESET); System.out.println(ANSI_RED + "Repo URL: " + repoItem.getUrl() + ANSI_RESET); } catch (Exception e) { System.out.println(ANSI_RED + "REPOSITORY RECORDER ERROR" + ANSI_RESET); java.util.logging.Logger.getLogger(RepositoryManager.class.getName()).log(Level.SEVERE, null, e); } } public void stopRunning(WebSocketSession session) { if (sessions.contains(session)) sessions.remove(session); } public void kill() { this.running = false; } }