<commit_before>package foam.nanos.ws;

import foam.core.*;
import org.java_websocket.server.*;
import org.java_websocket.handshake.*;
import org.java_websocket.*;
import foam.nanos.logger.Logger;

import java.net.InetSocketAddress;

public class WebSocketServer
    extends org.java_websocket.server.WebSocketServer
    implements foam.core.ContextAware
{
    // ContextAware support.
    protected X x;
    public void setX(X x) {
        this.x = x;
    }
    public X getX() {
        return x;
    }

    protected foam.nanos.box.NanoServiceRouter router_ = null;
    protected foam.nanos.box.NanoServiceRouter getRouter() {
        if ( router_ == null ) {
            router_ = getX().create(foam.nanos.box.NanoServiceRouter.class);
        }
        return router_;
    }

    public WebSocketServer(InetSocketAddress address) {
        super(address);
    }

    public void onClose(WebSocket connection, int code, String reason, boolean remote) {
    }

    public void onError(WebSocket conn, Exception ex) {
    }

    public void onMessage(WebSocket conn, String message) {
        Logger                log        = (Logger)getX().get("logger");

        System.out.println("WS Request" + message);
        log.info("Incoming websocket request");

        try {
            String                path       = conn.getResourceDescriptor();
            String[]              urlParts   = path.split("/");
            String                serviceKey = urlParts[1];
            Object                service    = getX().get(serviceKey);
            foam.dao.DAO          nSpecDAO   = (foam.dao.DAO) getX().get("nSpecDAO");
            foam.nanos.boot.NSpec spec       = (foam.nanos.boot.NSpec) nSpecDAO.find(serviceKey);

            if ( spec == null ) {
                log.warning("Request for non-existant service", serviceKey);
                return;
            }

            if ( ! spec.getServe() ) {
                log.warning("Request for service that is not serving.", serviceKey);
                return;
            }

            foam.box.RawWebSocketBox returnBox = getX().create(foam.box.RawWebSocketBox.class);
            returnBox.setSocket(conn);

            X requestContext = getX().put("returnBox", returnBox);

            FObject request = requestContext.create(foam.lib.json.JSONParser.class).parseString(message);

            if ( request == null || ! ( request instanceof foam.box.Message ) ) {
                log.warning("Invalid request");
                return;
            }

            getRouter().service(serviceKey, (foam.box.Message)request);
        } catch(Exception e) {
            log.error("Error handling websocket request", e);
        }
    }

    public void onOpen(WebSocket conn, ClientHandshake handshake) {
        Logger log = (Logger)getX().get("logger");
        log.info("WebSocket client connect.");
    }<commit_msg>Improve logging output from WebSocketServer<commit_after>package foam.nanos.ws;

import foam.core.*;
import org.java_websocket.server.*;
import org.java_websocket.handshake.*;
import org.java_websocket.*;
import foam.nanos.logger.Logger;

import java.net.InetSocketAddress;

public class WebSocketServer
    extends org.java_websocket.server.WebSocketServer
    implements foam.core.ContextAware
{
    // ContextAware support.
    protected X x;
    public void setX(X x) {
        this.x = x;
    }
    public X getX() {
        return x;
    }

    protected foam.nanos.box.NanoServiceRouter router_ = null;
    protected foam.nanos.box.NanoServiceRouter getRouter() {
        if ( router_ == null ) {
            router_ = getX().create(foam.nanos.box.NanoServiceRouter.class);
        }
        return router_;
    }

    public WebSocketServer(InetSocketAddress address) {
        super(address);
    }

    public void onClose(WebSocket connection, int code, String reason, boolean remote) {
    }

    public void onError(WebSocket conn, Exception ex) {
    }

    public void onMessage(WebSocket conn, String message) {
        Logger                log        = (Logger)getX().get("logger");

        System.out.println("WS Request" + message);
        log.info("Incoming websocket request");

        try {
            String                path       = conn.getResourceDescriptor();
            String[]              urlParts   = path.split("/");
            String                serviceKey = urlParts[1];
            Object                service    = getX().get(serviceKey);
            foam.dao.DAO          nSpecDAO   = (foam.dao.DAO) getX().get("nSpecDAO");
            foam.nanos.boot.NSpec spec       = (foam.nanos.boot.NSpec) nSpecDAO.find(serviceKey);

            if ( spec == null ) {
                log.warning("Request for non-existant service", serviceKey);
                return;
            }

            if ( ! spec.getServe() ) {
                log.warning("Request for service that is not serving.", serviceKey);
                return;
            }

            foam.box.RawWebSocketBox returnBox = getX().create(foam.box.RawWebSocketBox.class);
            returnBox.setSocket(conn);

            X requestContext = getX().put("returnBox", returnBox);

            FObject request = requestContext.create(foam.lib.json.JSONParser.class).parseString(message);

            if ( request == null ) {
                log.warning("Failed to parse request.");
                return;
            }

            if ( ! ( request instanceof foam.box.Message ) ) {
                log.warning("Request was not a box message.");
                return;
            }

            getRouter().service(serviceKey, (foam.box.Message)request);
        } catch(Exception e) {
            log.error("Error handling websocket request", e);
        }
    }

    public void onOpen(WebSocket conn, ClientHandshake handshake) {
        Logger log = (Logger)getX().get("logger");
        log.info("WebSocket client connect.");
    }
{'fraction_non_alphanumeric': 0.11062468429028456, 'fraction_numerical': 0.0003367570298029971, 'mean_word_length': 1.972972972972973, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '916831', 'n_tokens_mistral': 1667, 'n_tokens_neox': 1617, 'n_words': 350}