<commit_before>package org.objectweb.proactive.scheduler;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Date;
import java.util.Iterator;
import java.util.Vector;

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.node.Node;
import org.objectweb.proactive.core.node.NodeException;
import org.objectweb.proactive.core.node.NodeInformation;
import org.objectweb.proactive.core.util.log.Loggers;
import org.objectweb.proactive.core.util.log.ProActiveLogger;


/**
 * This is the main class used to communicate with the scheduler daemon to submit the
 * commands to the scheduler like the submission, deletion and statistics of any job
 * and the nodes status command.
 * @author cjarjouh
 *
 */
public class Communicator {
    private static Logger logger = ProActiveLogger.getLogger(Loggers.SCHEDULER_COMMAND_LISTENER);

    /* 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 String hostname = null;

    static {
        configureLogging();
    }

    /* These are the constants namely the commands that are used */
    private static final String LOG_HEADER = "[Scheduler] ";
    private static final String SUB_CMD = "sub";
    private static final String STAT_CMD = "stat";
    private static final String DEL_CMD = "del";
    private static final String NODES_CMD = "nodes";
    private static final String KILL_CMD = "kill";
    private static final String EXIT_CMD = "exit";
    private static final String SUB_PROTO = SUB_CMD + " XMLDescriptorOfTheJob";
    private static final String STAT_PROTO = STAT_CMD + " [jobId]";
    private static final String DEL_PROTO = DEL_CMD + " jobId";
    private static final String NODES_PROTO = NODES_CMD + " [nodeURL]";
    private static final String KILL_PROTO = KILL_CMD;
    private static final String EXIT_PROTO = EXIT_CMD;
    private Scheduler scheduler;

    /**
     * Tries to establish a connection with the scheduler daemon and to get the
     * scheduler object before we begin with the submission of commands
     * @param schedulerURL the scheduler url upon wich we shall connect to the scheduler
     */
    public Communicator(String schedulerURL) {
        try {
            this.scheduler = Scheduler.connectTo(schedulerURL);
            startCommandListener();
        } catch (Exception e) {
            flush("Cannot create command listener");
            System.exit(1);
        }
    }

    /**
     * Lanches the communicator program
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub	
        String schedulerURL = args[0];
        new Communicator(schedulerURL);
    }

    /**
     * This function is used to make the String right-justified. If the String is bigger
     * then pad_len then this function will add blanks in the beginning to make sure
     * that the String is right-justified over the pad_len space else it will return the
     * String unchanged.
     * @param s the string that we're trying to justify<commit_msg>added usage message in communicator<commit_after>package org.objectweb.proactive.scheduler;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Date;
import java.util.Iterator;
import java.util.Vector;

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.node.Node;
import org.objectweb.proactive.core.node.NodeException;
import org.objectweb.proactive.core.node.NodeInformation;
import org.objectweb.proactive.core.util.log.Loggers;
import org.objectweb.proactive.core.util.log.ProActiveLogger;


/**
 * This is the main class used to communicate with the scheduler daemon to submit the
 * commands to the scheduler like the submission, deletion and statistics of any job
 * and the nodes status command.
 * @author cjarjouh
 *
 */
public class Communicator {
    private static Logger logger = ProActiveLogger.getLogger(Loggers.SCHEDULER_COMMAND_LISTENER);

    /* 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 String hostname = null;

    static {
        configureLogging();
    }

    /* These are the constants namely the commands that are used */
    private static final String LOG_HEADER = "[Scheduler] ";
    private static final String SUB_CMD = "sub";
    private static final String STAT_CMD = "stat";
    private static final String DEL_CMD = "del";
    private static final String NODES_CMD = "nodes";
    private static final String KILL_CMD = "kill";
    private static final String EXIT_CMD = "exit";
    private static final String SUB_PROTO = SUB_CMD + " XMLDescriptorOfTheJob";
    private static final String STAT_PROTO = STAT_CMD + " [jobId]";
    private static final String DEL_PROTO = DEL_CMD + " jobId";
    private static final String NODES_PROTO = NODES_CMD + " [nodeURL]";
    private static final String KILL_PROTO = KILL_CMD;
    private static final String EXIT_PROTO = EXIT_CMD;
    private Scheduler scheduler;

    /**
     * Tries to establish a connection with the scheduler daemon and to get the
     * scheduler object before we begin with the submission of commands
     * @param schedulerURL the scheduler url upon wich we shall connect to the scheduler
     */
    public Communicator(String schedulerURL) {
        try {
            this.scheduler = Scheduler.connectTo(schedulerURL);
            startCommandListener();
        } catch (Exception e) {
            flush("Cannot create command listener");
            System.exit(1);
        }
    }

    /**
     * Lanches the communicator program
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub	
    	if (args.length == 1) {		
    		String schedulerURL = args[0];
    		new Communicator(schedulerURL);
    	}
    	System.err.println("Usage <java_command> Scheduler_URL");
    }

    /**
     * This function is used to make the String right-justified. If the String is bigger
     * then pad_len then this function will add blanks in the beginning to make sure
     * that the String is right-justified over the pad_len space else it will return the
     * String unchanged.
     * @param s the string that we're trying to justify
{'fraction_non_alphanumeric': 0.08119484046164291, 'fraction_numerical': 0.0033944331296673455, 'mean_word_length': 3.3949880668257757, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, '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': '369817', 'n_tokens_mistral': 2099, 'n_tokens_neox': 2052, 'n_words': 691}