<commit_before>package at.irian.ankor.switching.connector.websocket;


import at.irian.ankor.messaging.MessageDeserializer;
import at.irian.ankor.path.el.SimpleELPathSyntax;
import at.irian.ankor.system.AnkorSystem;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.websocket.CloseReason;
import javax.websocket.Endpoint;
import javax.websocket.EndpointConfig;
import javax.websocket.Session;
import java.io.IOException;

/**
 * @author <NAME>
 */
public abstract class WebSocketEndpoint extends Endpoint {
    private static Logger LOG = LoggerFactory.getLogger(WebSocketEndpoint.class);

    private static final String CLIENT_ID = WebSocketEndpoint.class.getName() + ".CLIENT_ID";

    protected abstract AnkorSystem getAnkorSystem();

    protected abstract String getPath();

    @Override
    public void onOpen(Session session, EndpointConfig config) {
        String clientId = getClientId(session);
        if (clientId == null || clientId.trim().isEmpty()) {
            throw new IllegalStateException("No valid client id in path");
        }

        AnkorSystem ankorSystem = getAnkorSystem();

        registerSession(ankorSystem, clientId, session);

        LOG.debug("New client connected {}", clientId);
    }

    @Override
    public void onClose(Session session, CloseReason closeReason) {
        LOG.info("Invalidating session {} because of {}: {}", session.getId(), closeReason.getCloseCode(),
                closeReason.getReasonPhrase());
        unregisterSession(session);
    }

    @Override
    public void onError(Session session, Throwable thr) {
        LOG.error("Invalidating session {} because of error {}", session.getId(), thr.getMessage());
        unregisterSession(session);
    }

    private void registerSession(AnkorSystem ankorSystem, String clientId, Session session) {
        WebSocketSessionRegistry sessionRegistry = WebSocketConnector.getSessionRegistry(ankorSystem);
        if (!sessionRegistry.containsSession(clientId, session)) {
            sessionRegistry.addSession(clientId, session);
            MessageDeserializer<String> messageDeserializer
                    = WebSocketConnector.getSingletonMessageDeserializer(ankorSystem);
            WebSocketListener listener =
                    new WebSocketListener(getPath(), messageDeserializer, ankorSystem.getSwitchboard(),
                            SimpleELPathSyntax.getInstance(), clientId, ankorSystem.getMonitor());
            session.addMessageHandler(listener.getByteMessageHandler());<commit_msg>stress test bugfix & improvements<commit_after>package at.irian.ankor.switching.connector.websocket;


import at.irian.ankor.messaging.MessageDeserializer;
import at.irian.ankor.path.el.SimpleELPathSyntax;
import at.irian.ankor.system.AnkorSystem;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.websocket.CloseReason;
import javax.websocket.Endpoint;
import javax.websocket.EndpointConfig;
import javax.websocket.Session;

/**
 * @author <NAME>
 */
public abstract class WebSocketEndpoint extends Endpoint {
    private static Logger LOG = LoggerFactory.getLogger(WebSocketEndpoint.class);

    private static final String CLIENT_ID = WebSocketEndpoint.class.getName() + ".CLIENT_ID";

    protected abstract AnkorSystem getAnkorSystem();

    protected abstract String getPath();

    @Override
    public void onOpen(Session session, EndpointConfig config) {
        String clientId = getClientId(session);
        if (clientId == null || clientId.trim().isEmpty()) {
            throw new IllegalStateException("No valid client id in path");
        }

        AnkorSystem ankorSystem = getAnkorSystem();

        registerSession(ankorSystem, clientId, session);

        LOG.debug("New client connected {}", clientId);
    }

    @Override
    public void onClose(Session session, CloseReason closeReason) {
        LOG.info("Invalidating session {} because of {}: {}", session.getId(), closeReason.getCloseCode(),
                closeReason.getReasonPhrase());
        unregisterSession(session);
    }

    @Override
    public void onError(Session session, Throwable thr) {
        LOG.error("Invalidating session {} because of error {}", session.getId(), thr.getMessage());
        unregisterSession(session);
    }

    private void registerSession(AnkorSystem ankorSystem, String clientId, Session session) {
        WebSocketSessionRegistry sessionRegistry = WebSocketConnector.getSessionRegistry(ankorSystem);
        if (!sessionRegistry.containsSession(clientId, session)) {
            sessionRegistry.addSession(clientId, session);
            MessageDeserializer<String> messageDeserializer
                    = WebSocketConnector.getSingletonMessageDeserializer(ankorSystem);
            WebSocketListener listener =
                    new WebSocketListener(getPath(), messageDeserializer, ankorSystem.getSwitchboard(),
                            SimpleELPathSyntax.getInstance(), clientId);
            session.addMessageHandler(listener.getByteMessageHandler());
{'fraction_non_alphanumeric': 0.09497427779976256, 'fraction_numerical': 0.0007914523149980214, 'mean_word_length': 3.684893419833179, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3442547', 'n_tokens_mistral': 1306, 'n_tokens_neox': 1288, 'n_words': 281}