<commit_before>//
// $Id$

package com.samskivert.nexus.server;

import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.samskivert.nexus.io.GWTServerIO;

import org.eclipse.jetty.websocket.WebSocket;
import org.eclipse.jetty.websocket.WebSocketServlet;

import com.samskivert.nexus.io.GWTIO;
import com.samskivert.nexus.net.Downstream;
import com.samskivert.nexus.net.Upstream;
import com.samskivert.nexus.server.SessionManager;

import static com.samskivert.nexus.util.Log.log;

/**
 * Wires our GWT/IO into Jetty WebSockets.
 */
public class GWTIOJettyServlet extends WebSocketServlet
{
    public GWTIOJettyServlet (SessionManager smgr, GWTIO.Serializer szer)
    {
        _smgr = smgr;
        _szer = szer;
    }

    @Override
    protected void doGet (HttpServletRequest req, HttpServletResponse rsp)
        throws ServletException, IOException
    {
        getServletContext().getNamedDispatcher("default").forward(req, rsp);
    }

    @Override
    protected WebSocket doWebSocketConnect (HttpServletRequest req, String protocol)
    {
        return new GWTIOWebSocket(req.getRemoteAddr());
    }

    protected class GWTIOWebSocket implements WebSocket, SessionManager.Output
    {
        public GWTIOWebSocket (String ipaddress) {
            _ipaddress = ipaddress;
        }

        // from interface WebSocket
        public void onConnect (Outbound outbound) {
            _input = _smgr.createSession(_ipaddress, this);
        }

        // from interface WebSocket
        public void onMessage (byte frame, byte[] data, int offset, int length) {
            // TODO: is this really supported?
        }

        // from interface WebSocket
        public void onMessage (byte frame, String data) {
            try {
                _input.onMessage(GWTServerIO.newInput(_szer, data).<Upstream>readValue());
            } catch (Throwable t) {
                log.warning("WebSocket decode failure", "addr", _ipaddress, "data", data, t);
            }
        }

        // from interface WebSocket
        public void onDisconnect () {
            _input.onDisconnect();
        }

        // from interface WebSocket
        public void onFragment (boolean more, byte opcode, byte[] data, int offset, int length) {
            // nada
        }

        // from interface SessionManager.Input
        public void send (Downstream msg)
        {
            if (!_outbound.isOpen()) {
                log.warning("Dropping outbound message to closed WebSocket", "addr", _ipaddress);
                return;
            }

            String data;
            // send may be called from multiple threads, so synchronize access to our payload buffer<commit_msg>Need to initialize our _outbound reference. Added some logging, in case these unused methods ever get called.<commit_after>//
// $Id$

package com.samskivert.nexus.server;

import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.samskivert.nexus.io.GWTServerIO;

import org.eclipse.jetty.websocket.WebSocket;
import org.eclipse.jetty.websocket.WebSocketServlet;

import com.samskivert.nexus.io.GWTIO;
import com.samskivert.nexus.net.Downstream;
import com.samskivert.nexus.net.Upstream;
import com.samskivert.nexus.server.SessionManager;

import static com.samskivert.nexus.util.Log.log;

/**
 * Wires our GWT/IO into Jetty WebSockets.
 */
public class GWTIOJettyServlet extends WebSocketServlet
{
    public GWTIOJettyServlet (SessionManager smgr, GWTIO.Serializer szer)
    {
        _smgr = smgr;
        _szer = szer;
    }

    @Override
    protected void doGet (HttpServletRequest req, HttpServletResponse rsp)
        throws ServletException, IOException
    {
        getServletContext().getNamedDispatcher("default").forward(req, rsp);
    }

    @Override
    protected WebSocket doWebSocketConnect (HttpServletRequest req, String protocol)
    {
        return new GWTIOWebSocket(req.getRemoteAddr());
    }

    protected class GWTIOWebSocket implements WebSocket, SessionManager.Output
    {
        public GWTIOWebSocket (String ipaddress) {
            _ipaddress = ipaddress;
        }

        // from interface WebSocket
        public void onConnect (Outbound outbound) {
            _outbound = outbound;
            _input = _smgr.createSession(_ipaddress, this);
        }

        // from interface WebSocket
        public void onMessage (byte frame, byte[] data, int offset, int length) {
            log.info("Got binary message " + frame);
            // TODO: is this really supported?
        }

        // from interface WebSocket
        public void onMessage (byte frame, String data) {
            try {
                _input.onMessage(GWTServerIO.newInput(_szer, data).<Upstream>readValue());
            } catch (Throwable t) {
                log.warning("WebSocket decode failure", "addr", _ipaddress, "data", data, t);
            }
        }

        // from interface WebSocket
        public void onDisconnect () {
            _input.onDisconnect();
        }

        // from interface WebSocket
        public void onFragment (boolean more, byte opcode, byte[] data, int offset, int length) {
            log.info("Got fragment " + more + "/" + opcode);
            // nada
        }

        // from interface SessionManager.Input
        public void send (Downstream msg)
        {
            if (!_outbound.isOpen()) {
                log.warning("Dropping outbound message to closed WebSocket", "addr", _ipaddress);
                return;
            }

            String data;
            // send may be called from multiple threads, so synchronize access to our payload buffer
{'fraction_non_alphanumeric': 0.0887314439946019, 'fraction_numerical': 0.0, 'mean_word_length': 2.4876470588235295, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '3405266', 'n_tokens_mistral': 1670, 'n_tokens_neox': 1622, 'n_words': 427}