Add task mechanism for the console
Signed-off-by: Emmanuel Bigeon <emmanuel@bigeon.fr>
This commit is contained in:
parent
db9f81c6a8
commit
e32e2428e5
@ -0,0 +1,50 @@
|
||||
/*
|
||||
* Copyright Bigeon Emmanuel (2014)
|
||||
*
|
||||
* emmanuel@bigeon.fr
|
||||
*
|
||||
* This software is a computer program whose purpose is to
|
||||
* provide a generic framework for console applications.
|
||||
*
|
||||
* This software is governed by the CeCILL license under French law and
|
||||
* abiding by the rules of distribution of free software. You can use,
|
||||
* modify and/or redistribute the software under the terms of the CeCILL
|
||||
* license as circulated by CEA, CNRS and INRIA at the following URL
|
||||
* "http://www.cecill.info".
|
||||
*
|
||||
* As a counterpart to the access to the source code and rights to copy,
|
||||
* modify and redistribute granted by the license, users are provided only
|
||||
* with a limited warranty and the software's author, the holder of the
|
||||
* economic rights, and the successive licensors have only limited
|
||||
* liability.
|
||||
*
|
||||
* In this respect, the user's attention is drawn to the risks associated
|
||||
* with loading, using, modifying and/or developing or reproducing the
|
||||
* software by the user in light of its specific status of free software,
|
||||
* that may mean that it is complicated to manipulate, and that also
|
||||
* therefore means that it is reserved for developers and experienced
|
||||
* professionals having in-depth computer knowledge. Users are therefore
|
||||
* encouraged to load and test the software's suitability as regards their
|
||||
* requirements in conditions enabling the security of their systems and/or
|
||||
* data to be ensured and, more generally, to use and operate it in the
|
||||
* same conditions as regards security.
|
||||
*
|
||||
* The fact that you are presently reading this means that you have had
|
||||
* knowledge of the CeCILL license and that you accept its terms.
|
||||
*/
|
||||
/**
|
||||
* gclc:fr.bigeon.gclc.proc.InterruptionListener.java
|
||||
* Created on: May 10, 2017
|
||||
*/
|
||||
package fr.bigeon.gclc.proc;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* TODO
|
||||
*
|
||||
* @author Emmanuel Bigeon
|
||||
*
|
||||
*/
|
||||
public interface InterruptionListener {
|
||||
void interrupted();
|
||||
}
|
77
gclc/src/main/java/fr/bigeon/gclc/proc/ProcessKill.java
Normal file
77
gclc/src/main/java/fr/bigeon/gclc/proc/ProcessKill.java
Normal file
@ -0,0 +1,77 @@
|
||||
/*
|
||||
* Copyright Bigeon Emmanuel (2014)
|
||||
*
|
||||
* emmanuel@bigeon.fr
|
||||
*
|
||||
* This software is a computer program whose purpose is to
|
||||
* provide a generic framework for console applications.
|
||||
*
|
||||
* This software is governed by the CeCILL license under French law and
|
||||
* abiding by the rules of distribution of free software. You can use,
|
||||
* modify and/or redistribute the software under the terms of the CeCILL
|
||||
* license as circulated by CEA, CNRS and INRIA at the following URL
|
||||
* "http://www.cecill.info".
|
||||
*
|
||||
* As a counterpart to the access to the source code and rights to copy,
|
||||
* modify and redistribute granted by the license, users are provided only
|
||||
* with a limited warranty and the software's author, the holder of the
|
||||
* economic rights, and the successive licensors have only limited
|
||||
* liability.
|
||||
*
|
||||
* In this respect, the user's attention is drawn to the risks associated
|
||||
* with loading, using, modifying and/or developing or reproducing the
|
||||
* software by the user in light of its specific status of free software,
|
||||
* that may mean that it is complicated to manipulate, and that also
|
||||
* therefore means that it is reserved for developers and experienced
|
||||
* professionals having in-depth computer knowledge. Users are therefore
|
||||
* encouraged to load and test the software's suitability as regards their
|
||||
* requirements in conditions enabling the security of their systems and/or
|
||||
* data to be ensured and, more generally, to use and operate it in the
|
||||
* same conditions as regards security.
|
||||
*
|
||||
* The fact that you are presently reading this means that you have had
|
||||
* knowledge of the CeCILL license and that you accept its terms.
|
||||
*/
|
||||
/**
|
||||
* gclc:fr.bigeon.gclc.proc.ProcessList.java
|
||||
* Created on: May 10, 2017
|
||||
*/
|
||||
package fr.bigeon.gclc.proc;
|
||||
|
||||
import fr.bigeon.gclc.command.Command;
|
||||
import fr.bigeon.gclc.exception.CommandRunException;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* TODO
|
||||
*
|
||||
* @author Emmanuel Bigeon
|
||||
*
|
||||
*/
|
||||
public class ProcessKill extends Command {
|
||||
private final TaskPool pool;
|
||||
|
||||
/** @param name the command name
|
||||
* @param pool the pool */
|
||||
public ProcessKill(String name, TaskPool pool) {
|
||||
super(name);
|
||||
this.pool = pool;
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see fr.bigeon.gclc.command.ICommand#execute(java.lang.String[])
|
||||
*/
|
||||
@Override
|
||||
public void execute(String... args) throws CommandRunException {
|
||||
pool.get(args[0]).setRunning(false);
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see fr.bigeon.gclc.command.ICommand#tip()
|
||||
*/
|
||||
@Override
|
||||
public String tip() {
|
||||
return "List all processes";
|
||||
}
|
||||
|
||||
}
|
101
gclc/src/main/java/fr/bigeon/gclc/proc/ProcessList.java
Normal file
101
gclc/src/main/java/fr/bigeon/gclc/proc/ProcessList.java
Normal file
@ -0,0 +1,101 @@
|
||||
/*
|
||||
* Copyright Bigeon Emmanuel (2014)
|
||||
*
|
||||
* emmanuel@bigeon.fr
|
||||
*
|
||||
* This software is a computer program whose purpose is to
|
||||
* provide a generic framework for console applications.
|
||||
*
|
||||
* This software is governed by the CeCILL license under French law and
|
||||
* abiding by the rules of distribution of free software. You can use,
|
||||
* modify and/or redistribute the software under the terms of the CeCILL
|
||||
* license as circulated by CEA, CNRS and INRIA at the following URL
|
||||
* "http://www.cecill.info".
|
||||
*
|
||||
* As a counterpart to the access to the source code and rights to copy,
|
||||
* modify and redistribute granted by the license, users are provided only
|
||||
* with a limited warranty and the software's author, the holder of the
|
||||
* economic rights, and the successive licensors have only limited
|
||||
* liability.
|
||||
*
|
||||
* In this respect, the user's attention is drawn to the risks associated
|
||||
* with loading, using, modifying and/or developing or reproducing the
|
||||
* software by the user in light of its specific status of free software,
|
||||
* that may mean that it is complicated to manipulate, and that also
|
||||
* therefore means that it is reserved for developers and experienced
|
||||
* professionals having in-depth computer knowledge. Users are therefore
|
||||
* encouraged to load and test the software's suitability as regards their
|
||||
* requirements in conditions enabling the security of their systems and/or
|
||||
* data to be ensured and, more generally, to use and operate it in the
|
||||
* same conditions as regards security.
|
||||
*
|
||||
* The fact that you are presently reading this means that you have had
|
||||
* knowledge of the CeCILL license and that you accept its terms.
|
||||
*/
|
||||
/**
|
||||
* gclc:fr.bigeon.gclc.proc.ProcessList.java
|
||||
* Created on: May 10, 2017
|
||||
*/
|
||||
package fr.bigeon.gclc.proc;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
|
||||
import fr.bigeon.gclc.command.Command;
|
||||
import fr.bigeon.gclc.exception.CommandRunException;
|
||||
import fr.bigeon.gclc.exception.CommandRunExceptionType;
|
||||
import fr.bigeon.gclc.manager.ConsoleManager;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* TODO
|
||||
*
|
||||
* @author Emmanuel Bigeon
|
||||
*
|
||||
*/
|
||||
public class ProcessList extends Command {
|
||||
private final TaskPool pool;
|
||||
|
||||
private final ConsoleManager manager;
|
||||
|
||||
/** @param name the command name
|
||||
* @param pool the pool
|
||||
* @param manager the console manager */
|
||||
public ProcessList(String name, TaskPool pool,
|
||||
ConsoleManager manager) {
|
||||
super(name);
|
||||
this.pool = pool;
|
||||
this.manager = manager;
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see fr.bigeon.gclc.command.ICommand#execute(java.lang.String[])
|
||||
*/
|
||||
@Override
|
||||
public void execute(String... args) throws CommandRunException {
|
||||
ArrayList<String> pids = new ArrayList<>(pool.getPIDs());
|
||||
Collections.sort(pids);
|
||||
for (String string : pids) {
|
||||
try {
|
||||
// XXX Format?
|
||||
manager.println(
|
||||
string + "\t" + pool.get(string).getName());
|
||||
} catch (IOException e) {
|
||||
throw new CommandRunException(
|
||||
CommandRunExceptionType.INTERACTION,
|
||||
"Unable to communicate with user", e, this);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see fr.bigeon.gclc.command.ICommand#tip()
|
||||
*/
|
||||
@Override
|
||||
public String tip() {
|
||||
return "List all processes";
|
||||
}
|
||||
|
||||
}
|
77
gclc/src/main/java/fr/bigeon/gclc/proc/Task.java
Normal file
77
gclc/src/main/java/fr/bigeon/gclc/proc/Task.java
Normal file
@ -0,0 +1,77 @@
|
||||
/*
|
||||
* Copyright Bigeon Emmanuel (2014)
|
||||
*
|
||||
* emmanuel@bigeon.fr
|
||||
*
|
||||
* This software is a computer program whose purpose is to
|
||||
* provide a generic framework for console applications.
|
||||
*
|
||||
* This software is governed by the CeCILL license under French law and
|
||||
* abiding by the rules of distribution of free software. You can use,
|
||||
* modify and/or redistribute the software under the terms of the CeCILL
|
||||
* license as circulated by CEA, CNRS and INRIA at the following URL
|
||||
* "http://www.cecill.info".
|
||||
*
|
||||
* As a counterpart to the access to the source code and rights to copy,
|
||||
* modify and redistribute granted by the license, users are provided only
|
||||
* with a limited warranty and the software's author, the holder of the
|
||||
* economic rights, and the successive licensors have only limited
|
||||
* liability.
|
||||
*
|
||||
* In this respect, the user's attention is drawn to the risks associated
|
||||
* with loading, using, modifying and/or developing or reproducing the
|
||||
* software by the user in light of its specific status of free software,
|
||||
* that may mean that it is complicated to manipulate, and that also
|
||||
* therefore means that it is reserved for developers and experienced
|
||||
* professionals having in-depth computer knowledge. Users are therefore
|
||||
* encouraged to load and test the software's suitability as regards their
|
||||
* requirements in conditions enabling the security of their systems and/or
|
||||
* data to be ensured and, more generally, to use and operate it in the
|
||||
* same conditions as regards security.
|
||||
*
|
||||
* The fact that you are presently reading this means that you have had
|
||||
* knowledge of the CeCILL license and that you accept its terms.
|
||||
*/
|
||||
/**
|
||||
* gclc:fr.bigeon.gclc.proc.ThreadCommand.java
|
||||
* Created on: May 10, 2017
|
||||
*/
|
||||
package fr.bigeon.gclc.proc;
|
||||
|
||||
/** Tasks are named runnable that can be interrupted.
|
||||
* <p>
|
||||
* Good practice for those objects include an absence of interaction with the
|
||||
* user (otherwise the user may not know from which running command comes
|
||||
* information and requests) and unicity of the object to have a coherent
|
||||
* control through the {@link #setRunning(boolean)} method.
|
||||
* <p>
|
||||
* Typical cases where such command can be useful is for an application that
|
||||
* does long computations.
|
||||
*
|
||||
* @author Emmanuel Bigeon */
|
||||
public interface Task extends Runnable {
|
||||
/** @return the task name */
|
||||
public String getName();
|
||||
|
||||
/** @return if the command is supposed to be running */
|
||||
boolean isRunning();
|
||||
|
||||
/** Set the running state.
|
||||
* <p>
|
||||
* This method should be only called by external objects with the false
|
||||
* argument. Calling this method with true has unspecified behavior and
|
||||
* could do nothing as well as restart the command for example.
|
||||
*
|
||||
* @param running the running state */
|
||||
void setRunning(boolean running);
|
||||
|
||||
/** Add a listener for this command end of execution
|
||||
*
|
||||
* @param listener the listener */
|
||||
void addInterruptionListener(InterruptionListener listener);
|
||||
|
||||
/** Remove a listener of this command end of execution
|
||||
*
|
||||
* @param listener the listener */
|
||||
void rmInterruptionListener(InterruptionListener listener);
|
||||
}
|
94
gclc/src/main/java/fr/bigeon/gclc/proc/TaskPool.java
Normal file
94
gclc/src/main/java/fr/bigeon/gclc/proc/TaskPool.java
Normal file
@ -0,0 +1,94 @@
|
||||
/*
|
||||
* Copyright Bigeon Emmanuel (2014)
|
||||
*
|
||||
* emmanuel@bigeon.fr
|
||||
*
|
||||
* This software is a computer program whose purpose is to
|
||||
* provide a generic framework for console applications.
|
||||
*
|
||||
* This software is governed by the CeCILL license under French law and
|
||||
* abiding by the rules of distribution of free software. You can use,
|
||||
* modify and/or redistribute the software under the terms of the CeCILL
|
||||
* license as circulated by CEA, CNRS and INRIA at the following URL
|
||||
* "http://www.cecill.info".
|
||||
*
|
||||
* As a counterpart to the access to the source code and rights to copy,
|
||||
* modify and redistribute granted by the license, users are provided only
|
||||
* with a limited warranty and the software's author, the holder of the
|
||||
* economic rights, and the successive licensors have only limited
|
||||
* liability.
|
||||
*
|
||||
* In this respect, the user's attention is drawn to the risks associated
|
||||
* with loading, using, modifying and/or developing or reproducing the
|
||||
* software by the user in light of its specific status of free software,
|
||||
* that may mean that it is complicated to manipulate, and that also
|
||||
* therefore means that it is reserved for developers and experienced
|
||||
* professionals having in-depth computer knowledge. Users are therefore
|
||||
* encouraged to load and test the software's suitability as regards their
|
||||
* requirements in conditions enabling the security of their systems and/or
|
||||
* data to be ensured and, more generally, to use and operate it in the
|
||||
* same conditions as regards security.
|
||||
*
|
||||
* The fact that you are presently reading this means that you have had
|
||||
* knowledge of the CeCILL license and that you accept its terms.
|
||||
*/
|
||||
/**
|
||||
* gclc:fr.bigeon.gclc.proc.TaskPool.java
|
||||
* Created on: May 10, 2017
|
||||
*/
|
||||
package fr.bigeon.gclc.proc;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.Map;
|
||||
|
||||
/** <p>
|
||||
* TODO
|
||||
*
|
||||
* @author Emmanuel Bigeon */
|
||||
public class TaskPool {
|
||||
private final Map<String, Task> running = new HashMap<>();
|
||||
private int count = 0;
|
||||
private final Object lock = new Object();
|
||||
|
||||
public void add(final Task cmd) {
|
||||
final String pid = getPID();
|
||||
synchronized (lock) {
|
||||
running.put(pid, cmd);
|
||||
}
|
||||
cmd.addInterruptionListener(new InterruptionListener() {
|
||||
|
||||
@Override
|
||||
public void interrupted() {
|
||||
synchronized (lock) {
|
||||
running.remove(pid);
|
||||
count = Math.min(count, Integer.parseInt(pid));
|
||||
}
|
||||
cmd.rmInterruptionListener(this);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/** @return the process id */
|
||||
private String getPID() {
|
||||
synchronized (lock) {
|
||||
String pid;
|
||||
do {
|
||||
pid = Integer.toString(count++);
|
||||
} while (running.containsKey(pid));
|
||||
return pid;
|
||||
}
|
||||
}
|
||||
|
||||
public Task get(String pid) {
|
||||
synchronized (lock) {
|
||||
return running.get(pid);
|
||||
}
|
||||
}
|
||||
|
||||
/** @return the pids */
|
||||
public Collection<String> getPIDs() {
|
||||
return new HashSet<>(running.keySet());
|
||||
}
|
||||
}
|
74
gclc/src/main/java/fr/bigeon/gclc/proc/TaskSpawner.java
Normal file
74
gclc/src/main/java/fr/bigeon/gclc/proc/TaskSpawner.java
Normal file
@ -0,0 +1,74 @@
|
||||
/*
|
||||
* Copyright Bigeon Emmanuel (2014)
|
||||
*
|
||||
* emmanuel@bigeon.fr
|
||||
*
|
||||
* This software is a computer program whose purpose is to
|
||||
* provide a generic framework for console applications.
|
||||
*
|
||||
* This software is governed by the CeCILL license under French law and
|
||||
* abiding by the rules of distribution of free software. You can use,
|
||||
* modify and/or redistribute the software under the terms of the CeCILL
|
||||
* license as circulated by CEA, CNRS and INRIA at the following URL
|
||||
* "http://www.cecill.info".
|
||||
*
|
||||
* As a counterpart to the access to the source code and rights to copy,
|
||||
* modify and redistribute granted by the license, users are provided only
|
||||
* with a limited warranty and the software's author, the holder of the
|
||||
* economic rights, and the successive licensors have only limited
|
||||
* liability.
|
||||
*
|
||||
* In this respect, the user's attention is drawn to the risks associated
|
||||
* with loading, using, modifying and/or developing or reproducing the
|
||||
* software by the user in light of its specific status of free software,
|
||||
* that may mean that it is complicated to manipulate, and that also
|
||||
* therefore means that it is reserved for developers and experienced
|
||||
* professionals having in-depth computer knowledge. Users are therefore
|
||||
* encouraged to load and test the software's suitability as regards their
|
||||
* requirements in conditions enabling the security of their systems and/or
|
||||
* data to be ensured and, more generally, to use and operate it in the
|
||||
* same conditions as regards security.
|
||||
*
|
||||
* The fact that you are presently reading this means that you have had
|
||||
* knowledge of the CeCILL license and that you accept its terms.
|
||||
*/
|
||||
/**
|
||||
* gclc:fr.bigeon.gclc.proc.ProcessList.java
|
||||
* Created on: May 10, 2017
|
||||
*/
|
||||
package fr.bigeon.gclc.proc;
|
||||
|
||||
import fr.bigeon.gclc.command.Command;
|
||||
import fr.bigeon.gclc.exception.CommandRunException;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* TODO
|
||||
*
|
||||
* @author Emmanuel Bigeon
|
||||
*
|
||||
*/
|
||||
public abstract class TaskSpawner extends Command {
|
||||
private final TaskPool pool;
|
||||
|
||||
/** @param name the command name
|
||||
* @param pool the pool */
|
||||
public TaskSpawner(String name, TaskPool pool) {
|
||||
super(name);
|
||||
this.pool = pool;
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see fr.bigeon.gclc.command.ICommand#execute(java.lang.String[])
|
||||
*/
|
||||
@Override
|
||||
public final void execute(String... args) throws CommandRunException {
|
||||
Task task = createTask(args);
|
||||
Thread th = new Thread(task);
|
||||
th.start();
|
||||
pool.add(task);
|
||||
}
|
||||
|
||||
/** @param args the arguments */
|
||||
protected abstract Task createTask(String... args);
|
||||
}
|
Loading…
Reference in New Issue
Block a user