<commit_before>package org.objectweb.proactive.p2p.daemon;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;

import org.apache.log4j.Appender;
import org.apache.log4j.Layout;
import org.apache.log4j.Logger;
import org.apache.log4j.PatternLayout;
import org.apache.log4j.RollingFileAppender;
import org.apache.log4j.WriterAppender;
import org.apache.log4j.nt.NTEventLogAppender;
import org.objectweb.proactive.core.util.ProActiveInet;
import org.objectweb.proactive.core.util.log.Loggers;
import org.objectweb.proactive.core.util.log.ProActiveLogger;


public class WinDaemon {

    private static Logger logger = ProActiveLogger.getLogger(Loggers.P2P_DAEMON);

    /* Logging */
    private static final String LOG_DIR = ".." + File.separator + "logs" + File.separator;
    private static final String MAX_SIZE = "100KB";
    private static final String LOG_PATTERN = "%d %c %x %m\n\n";
    private static WriterAppender writerAppender;

    private static final String LOG_HEADER = "[Daemon] ";

    private static final int DAEMON_PORT = 12345;

    /* Commands */

    private static final String START_P2P_CMD = "P2P_START";
    private static final String STOP_P2P_CMD = "P2P_STOP";
    private static final String REGISTER_RM_CMD = "RM_REG";
    private static final String UNREGISTER_RM_CMD = "RM_UNREG";
    private static final String KILL_CMD = "KILL";

    private DaemonState state;
    private Runner runner = null;

    static {
        configureLogging();
    }

    public static void main(String[] args) {
        WinDaemon d = new WinDaemon();
    }

    public WinDaemon() {

        this.state = new DaemonState();
        state.setPriority(Priorities.LowestPriority);
        state.setServices(Services.NotRunning);

        try {
            Thread t = startCommandListener();
            t.join();
        } catch (Exception e) {
            flush("Cannot create command listener");
            System.exit(1);
        }
    }

    private Thread startCommandListener() {
        Thread listener = new Thread(new Runnable() {
            public void run() {
                ServerSocket server;

                try {
                    server = new ServerSocket(DAEMON_PORT);
                } catch (IOException ioe) {
                    log(ioe.getMessage(), true);
                    System.exit(1);

                    return; /* To avoid a javac error */
                }

                for (;;) {
                    try {
                        Socket client = server.accept();

                        /*
                         * TODO: When the experimenting phase is done we should
                         * allow connections only from localhost
                         */
                        InputStream stream = client.getInputStream();
                        InputStreamReader ireader = new InputStreamReader(stream);
                        BufferedReader reader = new BufferedReader(ireader);
                        String line = readCommand(reader);
                        handleCommand(client, line);

                        stream.close();
                        client.close();
                    } catch (IOException ioe) {
                        ioe.printStackTrace();
                    }
                }
            }
        });
        listener.start();
        return listener;
    }

    void handleCommand(Socket client, String command) {
        Pattern pattern = Pattern.compile(";");

        String[] parts = pattern.split(command);
        if (parts.length < 2) {
            log("CorruptedMessage", true);
            return;
        }
        try {
            Priorities cmdPriority = Priorities.valueOf(parts[1]);

            if (command.startsWith(START_P2P_CMD)) {
                P2PInformation info = getP2PInformation(parts);
                tryChangeState(Services.P2PRunning, cmdPriority, info);
            } else if (command.startsWith(STOP_P2P_CMD)) {<commit_msg>Windows Daemon should reply on successful receipt of command (needed for screen-saver to properly display diagnostic messages)<commit_after>package org.objectweb.proactive.p2p.daemon;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;

import org.apache.log4j.Appender;
import org.apache.log4j.Layout;
import org.apache.log4j.Logger;
import org.apache.log4j.PatternLayout;
import org.apache.log4j.RollingFileAppender;
import org.apache.log4j.WriterAppender;
import org.apache.log4j.nt.NTEventLogAppender;
import org.objectweb.proactive.core.util.ProActiveInet;
import org.objectweb.proactive.core.util.log.Loggers;
import org.objectweb.proactive.core.util.log.ProActiveLogger;


public class WinDaemon {

    private static Logger logger = ProActiveLogger.getLogger(Loggers.P2P_DAEMON);

    /* Logging */
    private static final String LOG_DIR = ".." + File.separator + "logs" + File.separator;
    private static final String MAX_SIZE = "100KB";
    private static final String LOG_PATTERN = "%d %c %x %m\n\n";
    private static WriterAppender writerAppender;

    private static final String LOG_HEADER = "[Daemon] ";

    private static final int DAEMON_PORT = 12345;

    /* Commands */

    private static final String START_P2P_CMD = "P2P_START";
    private static final String STOP_P2P_CMD = "P2P_STOP";
    private static final String REGISTER_RM_CMD = "RM_REG";
    private static final String UNREGISTER_RM_CMD = "RM_UNREG";
    private static final String KILL_CMD = "KILL";

    private DaemonState state;
    private Runner runner = null;

    static {
        configureLogging();
    }

    public static void main(String[] args) {
        WinDaemon d = new WinDaemon();
    }

    public WinDaemon() {

        this.state = new DaemonState();
        state.setPriority(Priorities.LowestPriority);
        state.setServices(Services.NotRunning);

        try {
            Thread t = startCommandListener();
            t.join();
        } catch (Exception e) {
            flush("Cannot create command listener");
            System.exit(1);
        }
    }

    private Thread startCommandListener() {
        Thread listener = new Thread(new Runnable() {
            public void run() {
                ServerSocket server;

                try {
                    server = new ServerSocket(DAEMON_PORT);
                } catch (IOException ioe) {
                    log(ioe.getMessage(), true);
                    System.exit(1);

                    return; /* To avoid a javac error */
                }

                for (;;) {
                    try {
                        Socket client = server.accept();

                        /*
                         * TODO: When the experimenting phase is done we should
                         * allow connections only from localhost
                         */
                        InputStream stream = client.getInputStream();
                        InputStreamReader ireader = new InputStreamReader(stream);
                        BufferedReader reader = new BufferedReader(ireader);
                        String line = readCommand(reader);
                        handleCommand(client, line);
                        client.getOutputStream().write("OK".getBytes());

                        stream.close();
                        client.close();
                    } catch (IOException ioe) {
                        ioe.printStackTrace();
                    }
                }
            }
        });
        listener.start();
        return listener;
    }

    void handleCommand(Socket client, String command) {
        Pattern pattern = Pattern.compile(";");

        String[] parts = pattern.split(command);
        if (parts.length < 2) {
            log("CorruptedMessage", true);
            return;
        }
        try {
            Priorities cmdPriority = Priorities.valueOf(parts[1]);

            if (command.startsWith(START_P2P_CMD)) {
                P2PInformation info = getP2PInformation(parts);
                tryChangeState(Services.P2PRunning, cmdPriority, info);
            } else if (command.startsWith(STOP_P2P_CMD)) {
{'fraction_non_alphanumeric': 0.09203923357664233, 'fraction_numerical': 0.006843065693430657, 'mean_word_length': 2.017549896765313, 'pattern_counts': {'":': 0, '<': 5, '<?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': '4890181', 'n_tokens_mistral': 2396, 'n_tokens_neox': 2259, 'n_words': 557}