<commit_before>package better.jsonrpc.websocket;

import java.io.IOException;

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

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ObjectNode;

import better.jsonrpc.core.JsonRpcConnection;

public class JsonRpcWsConnection extends JsonRpcConnection
	implements WebSocket, OnTextMessage {
	
	/** Currently active websocket connection */
	private Connection mConnection;
	
	public JsonRpcWsConnection() {
		super();
	}
	
	@Override
	public boolean isConnected() {
		return mConnection != null && mConnection.isOpen();
	}
	
	public void disconnect() {
		if(mConnection != null && mConnection.isOpen()) {
			mConnection.close();
		}
	}
	
	public void transmit(String data) {
		LOG.info("[" + mConnectionId + "] connection transmitting \"" + data + "\"");
		if(mConnection != null && mConnection.isOpen()) {
			try {
				mConnection.sendMessage(data);
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	
	@Override
	public void onOpen(Connection connection) {
		LOG.info("[" + mConnectionId + "] connection open");
		super.onOpen();
		mConnection = connection;
	}
	
	@Override
	public void onClose(int closeCode, String message) {
		LOG.info("[" + mConnectionId + "] connection close " + closeCode + "/" + message);
		super.onClose();
		mConnection = null;
	}
	
	@Override
	public void onMessage(String data) {
		LOG.info("[" + mConnectionId + "] connection received message \"" + data + "\"");
		try {
			JsonNode message = getServer().getMapper().readTree(data);
			if(message.isObject()) {
				ObjectNode messageObj = ObjectNode.class.cast(message);
				
				// requests and notifications
				if(messageObj.has("method")) {
					if(messageObj.has("id")) {
						handleRequest(messageObj);
					} else {
						handleNotification(messageObj);
					}
				}
				// responses
				if(messageObj.has("result") || messageObj.has("error")) {
					if(messageObj.has("id")) {
						handleResponse(messageObj);
					}
				}
			}
		} catch (JsonProcessingException e) {
			e.printStackTrace();<commit_msg>Guard connection logging properly<commit_after>package better.jsonrpc.websocket;

import java.io.IOException;
import java.util.logging.Level;

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

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ObjectNode;

import better.jsonrpc.core.JsonRpcConnection;

public class JsonRpcWsConnection extends JsonRpcConnection
	implements WebSocket, OnTextMessage {
	
	/** Currently active websocket connection */
	private Connection mConnection;
	
	public JsonRpcWsConnection() {
		super();
	}
	
	@Override
	public boolean isConnected() {
		return mConnection != null && mConnection.isOpen();
	}
	
	public void disconnect() {
		if(mConnection != null && mConnection.isOpen()) {
			mConnection.close();
		}
	}
	
	public void transmit(String data) {
        if(LOG.isLoggable(Level.FINE)) {
		    LOG.fine("[" + mConnectionId + "] transmitting \"" + data + "\"");
        }
		if(mConnection != null && mConnection.isOpen()) {
			try {
				mConnection.sendMessage(data);
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	
	@Override
	public void onOpen(Connection connection) {
        if(LOG.isLoggable(Level.INFO)) {
		    LOG.info("[" + mConnectionId + "] connection open");
        }
		super.onOpen();
		mConnection = connection;
	}
	
	@Override
	public void onClose(int closeCode, String message) {
        if(LOG.isLoggable(Level.INFO)) {
		    LOG.info("[" + mConnectionId + "] connection close " + closeCode + "/" + message);
        }
		super.onClose();
		mConnection = null;
	}
	
	@Override
	public void onMessage(String data) {
        if(LOG.isLoggable(Level.FINE)) {
		    LOG.fine("[" + mConnectionId + "] received \"" + data + "\"");
        }
		try {
			JsonNode message = getServer().getMapper().readTree(data);
			if(message.isObject()) {
				ObjectNode messageObj = ObjectNode.class.cast(message);
				
				// requests and notifications
				if(messageObj.has("method")) {
					if(messageObj.has("id")) {
						handleRequest(messageObj);
					} else {
						handleNotification(messageObj);
					}
				}
				// responses
				if(messageObj.has("result") || messageObj.has("error")) {
					if(messageObj.has("id")) {
						handleResponse(messageObj);
					}
				}
			}
		} catch (JsonProcessingException e) {
			e.printStackTrace();
{'fraction_non_alphanumeric': 0.13759108444063436, 'fraction_numerical': 0.0, 'mean_word_length': 3.6299603174603177, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 6}, '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': '7061497', 'n_tokens_mistral': 1679, 'n_tokens_neox': 1402, 'n_words': 267}