<commit_before>package org.yamcs.tse;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketException;
import java.net.SocketTimeoutException;
import java.util.Map;
import java.util.concurrent.TimeoutException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.yamcs.YConfiguration;

/**
 * Connect and command a device over TCP/IP. Typical use case is an instrument with LXI support.
 * 
 * Not thread safe.
 */
public class TcpIpDriver extends InstrumentDriver {

    private static final Logger log = LoggerFactory.getLogger(TcpIpDriver.class);

    private static final int POLLING_INTERVAL = 20;

    private String host;
    private int port;

    private Socket socket;

    private int lastResponseTerminationCharacter;

    public TcpIpDriver(String name, Map<String, Object> args) {
        super(name, args);
        host = YConfiguration.getString(args, "host");
        port = YConfiguration.getInt(args, "port");
        if (responseTermination != null) {
            lastResponseTerminationCharacter = responseTermination.charAt(responseTermination.length() - 1);
        }
    }

    @Override
    public void connect() throws IOException {
        if (socket != null) {
            if (socket.isConnected() && socket.isBound() && !socket.isClosed()) {
                return;
            }
            socket.close();
        }

        socket = new Socket();

        log.info("Connecting to {}:{}", host, port);
        socket.connect(new InetSocketAddress(host, port), responseTimeout);
        log.info("Connected to {}:{}", host, port);

        socket.setSoTimeout(POLLING_INTERVAL);
        if (responseTimeout < POLLING_INTERVAL) {
            socket.setSoTimeout(responseTimeout);
        }
    }

    @Override
    public void disconnect() throws IOException {
        if (socket != null) {
            socket.close();
            socket = null;
        }
    }

    @Override
    public void write(String cmd) throws IOException {
        // Ensure no reads are pending before we write. Has the drawback
        // of delaying the write. Should probably revise this code to do
        // NIO, or use another thread.
        InputStream socketIn = socket.getInputStream();
        try {
            byte[] buf = new byte[4096];
            while (socketIn.read(buf) > 0) {
                // Discard
            }
        } catch (SocketTimeoutException e) {
            // Ignore
        }

        try {
            OutputStream out = socket.getOutputStream();
            out.write((cmd + "\n").getBytes(encoding));<commit_msg>Establish new socket on connection reset<commit_after>package org.yamcs.tse;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketException;
import java.net.SocketTimeoutException;
import java.util.Map;
import java.util.concurrent.TimeoutException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.yamcs.YConfiguration;

/**
 * Connect and command a device over TCP/IP. Typical use case is an instrument with LXI support.
 * 
 * Not thread safe.
 */
public class TcpIpDriver extends InstrumentDriver {

    private static final Logger log = LoggerFactory.getLogger(TcpIpDriver.class);

    private static final int POLLING_INTERVAL = 20;

    private String host;
    private int port;

    private Socket socket;

    private int lastResponseTerminationCharacter;

    public TcpIpDriver(String name, Map<String, Object> args) {
        super(name, args);
        host = YConfiguration.getString(args, "host");
        port = YConfiguration.getInt(args, "port");
        if (responseTermination != null) {
            lastResponseTerminationCharacter = responseTermination.charAt(responseTermination.length() - 1);
        }
    }

    @Override
    public void connect() throws IOException {
        if (socket != null) {
            if (socket.isConnected() && socket.isBound() && !socket.isClosed()) {
                return;
            }
            socket.close();
        }

        socket = new Socket();

        log.info("Connecting to {}:{}", host, port);
        socket.connect(new InetSocketAddress(host, port), responseTimeout);
        log.info("Connected to {}:{}", host, port);

        socket.setSoTimeout(POLLING_INTERVAL);
        if (responseTimeout < POLLING_INTERVAL) {
            socket.setSoTimeout(responseTimeout);
        }
    }

    @Override
    public void disconnect() throws IOException {
        if (socket != null) {
            socket.close();
            socket = null;
        }
    }

    @Override
    public void write(String cmd) throws IOException {
        // Ensure no reads are pending before we write. Has the drawback
        // of delaying the write. Should probably revise this code to do
        // NIO, or use another thread.
        InputStream socketIn = socket.getInputStream();
        try {
            byte[] buf = new byte[4096];
            while (socketIn.read(buf) > 0) {
                // Discard
            }
        } catch (SocketTimeoutException e) {
            // Ignore
        } catch (SocketException e) { // For example: connection reset
            log.warn("Socket exception: " + e.getMessage());
            connect();
        }

        try {
            OutputStream out = socket.getOutputStream();
            out.write((cmd + "\n").getBytes(encoding));
{'fraction_non_alphanumeric': 0.10269784172661871, 'fraction_numerical': 0.0035971223021582736, 'mean_word_length': 2.4116564417177915, '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': '3015172', 'n_tokens_mistral': 1530, 'n_tokens_neox': 1467, 'n_words': 409}