Move worker system to base subproject
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
package base.worker;
|
||||
package mimis;
|
||||
|
||||
import base.worker.Listener;
|
||||
import mimis.input.Button;
|
||||
import mimis.input.Feedback;
|
||||
import mimis.input.Input;
|
||||
@@ -9,7 +9,6 @@ import javax.swing.JPanel;
|
||||
import javax.swing.WindowConstants;
|
||||
|
||||
import base.exception.worker.ActivateException;
|
||||
import base.worker.Component;
|
||||
import mimis.input.Feedback;
|
||||
import mimis.input.Input;
|
||||
import mimis.manager.ButtonManager;
|
||||
|
||||
@@ -7,7 +7,6 @@ import javax.swing.UIManager;
|
||||
|
||||
import base.exception.worker.ActivateException;
|
||||
import base.exception.worker.DeactivateException;
|
||||
import base.worker.Component;
|
||||
import mimis.input.Task;
|
||||
import mimis.manager.ButtonManager;
|
||||
import mimis.manager.CurrentButtonManager;
|
||||
|
||||
@@ -2,7 +2,6 @@ package mimis;
|
||||
|
||||
import base.exception.worker.ActivateException;
|
||||
import base.exception.worker.DeactivateException;
|
||||
import base.worker.Component;
|
||||
import mimis.input.Feedback;
|
||||
import mimis.input.Task;
|
||||
import mimis.manager.Manager;
|
||||
|
||||
@@ -5,7 +5,7 @@ import java.util.Map;
|
||||
|
||||
import base.exception.worker.ActivateException;
|
||||
import base.exception.worker.DeactivateException;
|
||||
import base.worker.Component;
|
||||
import mimis.Component;
|
||||
import mimis.application.Application;
|
||||
import mimis.util.Native;
|
||||
import mimis.value.Registry;
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
package mimis.application.itunes;
|
||||
|
||||
import mimis.Component;
|
||||
import mimis.application.Application;
|
||||
import mimis.value.Action;
|
||||
import base.exception.worker.ActivateException;
|
||||
import base.exception.worker.DeactivateException;
|
||||
import base.worker.Component;
|
||||
import base.worker.Worker;
|
||||
|
||||
import com.dt.iTunesController.ITCOMDisabledReason;
|
||||
|
||||
@@ -2,7 +2,7 @@ package mimis.application.lirc;
|
||||
|
||||
import base.exception.worker.ActivateException;
|
||||
import base.exception.worker.DeactivateException;
|
||||
import base.worker.Component;
|
||||
import mimis.Component;
|
||||
import mimis.application.Application;
|
||||
import mimis.device.lirc.LircButton;
|
||||
import mimis.device.lirc.LircService;
|
||||
|
||||
@@ -4,7 +4,7 @@ import java.awt.AWTException;
|
||||
import java.awt.Robot;
|
||||
|
||||
import base.exception.worker.ActivateException;
|
||||
import base.worker.Component;
|
||||
import mimis.Component;
|
||||
import mimis.value.Key;
|
||||
|
||||
public class RobotApplication extends Component {
|
||||
|
||||
@@ -2,7 +2,7 @@ package mimis.device.javainput;
|
||||
|
||||
import base.exception.worker.ActivateException;
|
||||
import base.exception.worker.DeactivateException;
|
||||
import base.worker.Component;
|
||||
import mimis.Component;
|
||||
import mimis.device.Device;
|
||||
import mimis.exception.ButtonException;
|
||||
import mimis.exception.button.UnknownButtonException;
|
||||
|
||||
@@ -2,6 +2,7 @@ package mimis.device.jintellitype;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
import mimis.Component;
|
||||
import mimis.device.Device;
|
||||
import mimis.exception.button.UnknownButtonException;
|
||||
import mimis.input.state.Press;
|
||||
@@ -9,7 +10,6 @@ import mimis.input.state.Release;
|
||||
import mimis.value.Action;
|
||||
import base.exception.worker.ActivateException;
|
||||
import base.exception.worker.DeactivateException;
|
||||
import base.worker.Component;
|
||||
|
||||
import com.melloware.jintellitype.HotkeyListener;
|
||||
import com.melloware.jintellitype.IntellitypeListener;
|
||||
|
||||
@@ -11,8 +11,8 @@ import java.util.concurrent.ConcurrentLinkedQueue;
|
||||
|
||||
import base.exception.worker.ActivateException;
|
||||
import base.exception.worker.DeactivateException;
|
||||
import base.worker.Component;
|
||||
import base.worker.Worker;
|
||||
import mimis.Component;
|
||||
import mimis.device.Device;
|
||||
import mimis.input.Feedback;
|
||||
import mimis.input.Input;
|
||||
|
||||
@@ -2,7 +2,7 @@ package mimis.device.panel;
|
||||
|
||||
import base.exception.worker.ActivateException;
|
||||
import base.exception.worker.DeactivateException;
|
||||
import base.worker.Component;
|
||||
import mimis.Component;
|
||||
import mimis.device.Device;
|
||||
import mimis.input.state.Press;
|
||||
import mimis.input.state.Release;
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package mimis.device.wiimote;
|
||||
|
||||
import mimis.Component;
|
||||
import mimis.device.Device;
|
||||
import mimis.device.wiimote.gesture.GestureDevice;
|
||||
import mimis.device.wiimote.motion.MotionDevice;
|
||||
@@ -23,7 +24,6 @@ import wiiusej.wiiusejevents.physicalevents.MotionSensingEvent;
|
||||
import wiiusej.wiiusejevents.physicalevents.WiimoteButtonsEvent;
|
||||
import base.exception.worker.ActivateException;
|
||||
import base.exception.worker.DeactivateException;
|
||||
import base.worker.Component;
|
||||
import base.worker.Worker;
|
||||
|
||||
public class WiimoteDevice extends Component implements Device, GestureListener {
|
||||
|
||||
@@ -10,8 +10,8 @@ import java.util.ArrayList;
|
||||
|
||||
import base.exception.worker.ActivateException;
|
||||
import base.exception.worker.DeactivateException;
|
||||
import base.worker.Component;
|
||||
import base.worker.Worker;
|
||||
import mimis.Component;
|
||||
import mimis.device.lirc.LircButton;
|
||||
import mimis.device.lirc.remote.PhiliphsRCLE011Button;
|
||||
import mimis.device.wiimote.WiimoteDevice;
|
||||
|
||||
@@ -1,5 +0,0 @@
|
||||
package mimis.exception;
|
||||
|
||||
public class WorkerException extends Exception {
|
||||
protected static final long serialVersionUID = 1L;
|
||||
}
|
||||
@@ -1,7 +0,0 @@
|
||||
package mimis.exception.worker;
|
||||
|
||||
import mimis.exception.WorkerException;
|
||||
|
||||
public class ActivateException extends WorkerException {
|
||||
protected static final long serialVersionUID = 1L;
|
||||
}
|
||||
@@ -1,7 +0,0 @@
|
||||
package mimis.exception.worker;
|
||||
|
||||
import mimis.exception.WorkerException;
|
||||
|
||||
public class AlreadyActiveException extends WorkerException {
|
||||
protected static final long serialVersionUID = 1L;
|
||||
}
|
||||
@@ -1,7 +0,0 @@
|
||||
package mimis.exception.worker;
|
||||
|
||||
import mimis.exception.WorkerException;
|
||||
|
||||
public class AlreadyRunningException extends WorkerException {
|
||||
protected static final long serialVersionUID = 1L;
|
||||
}
|
||||
@@ -1,7 +0,0 @@
|
||||
package mimis.exception.worker;
|
||||
|
||||
import mimis.exception.WorkerException;
|
||||
|
||||
public class DeactivateException extends WorkerException {
|
||||
protected static final long serialVersionUID = 1L;
|
||||
}
|
||||
@@ -1,7 +0,0 @@
|
||||
package mimis.exception.worker;
|
||||
|
||||
import mimis.exception.WorkerException;
|
||||
|
||||
public class NotActiveException extends WorkerException {
|
||||
protected static final long serialVersionUID = 1L;
|
||||
}
|
||||
@@ -1,7 +0,0 @@
|
||||
package mimis.exception.worker;
|
||||
|
||||
import mimis.exception.WorkerException;
|
||||
|
||||
public class NotRunningException extends WorkerException {
|
||||
protected static final long serialVersionUID = 1L;
|
||||
}
|
||||
@@ -1,7 +0,0 @@
|
||||
package mimis.exception.worker;
|
||||
|
||||
import mimis.exception.WorkerException;
|
||||
|
||||
public class StartException extends WorkerException {
|
||||
protected static final long serialVersionUID = 1L;
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
package mimis.input.state;
|
||||
|
||||
import base.worker.Component;
|
||||
import mimis.Component;
|
||||
import mimis.input.Button;
|
||||
import mimis.input.Input;
|
||||
import mimis.input.state.State;
|
||||
|
||||
@@ -13,8 +13,8 @@ import javax.swing.JPanel;
|
||||
import javax.swing.JRadioButton;
|
||||
import javax.swing.border.TitledBorder;
|
||||
|
||||
import base.worker.Component;
|
||||
import base.worker.Worker;
|
||||
import mimis.Component;
|
||||
import mimis.input.Task;
|
||||
import mimis.router.Router;
|
||||
import mimis.util.ArrayCycle;
|
||||
|
||||
@@ -5,10 +5,11 @@ import java.awt.event.MouseListener;
|
||||
|
||||
import javax.swing.JToggleButton;
|
||||
|
||||
import mimis.Component;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import base.worker.Component;
|
||||
import base.worker.Worker;
|
||||
|
||||
public class WorkerButton extends JToggleButton implements MouseListener {
|
||||
|
||||
@@ -5,7 +5,7 @@ import java.util.HashMap;
|
||||
import java.util.Map.Entry;
|
||||
|
||||
import base.exception.worker.ActivateException;
|
||||
import base.worker.Component;
|
||||
import mimis.Component;
|
||||
import mimis.input.Input;
|
||||
import mimis.input.Task;
|
||||
import mimis.input.state.State;
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
package mimis.parser;
|
||||
|
||||
import base.worker.Component;
|
||||
import mimis.Component;
|
||||
import mimis.input.Input;
|
||||
import mimis.state.TaskMap;
|
||||
import mimis.value.Action;
|
||||
|
||||
@@ -4,7 +4,7 @@ import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map.Entry;
|
||||
|
||||
import base.worker.Component;
|
||||
import mimis.Component;
|
||||
import mimis.Main;
|
||||
import mimis.application.Application;
|
||||
import mimis.device.Device;
|
||||
|
||||
@@ -1,81 +0,0 @@
|
||||
package mimis.worker;
|
||||
|
||||
import java.util.Timer;
|
||||
import java.util.TimerTask;
|
||||
|
||||
import mimis.exception.worker.ActivateException;
|
||||
import mimis.exception.worker.DeactivateException;
|
||||
|
||||
public class IntervalWorker extends Worker {
|
||||
protected static final boolean THREAD = true;
|
||||
protected static final int INTERVAL = 500;
|
||||
|
||||
protected Timer timer;
|
||||
|
||||
public synchronized void start(boolean thread) {
|
||||
if (!active) {
|
||||
activate = true;
|
||||
timer = new Timer();
|
||||
timer.schedule(new TimerTask() {
|
||||
public void run() {
|
||||
IntervalWorker.this.run();
|
||||
}}, 0, INTERVAL);
|
||||
active = true;
|
||||
}
|
||||
if (!thread) {
|
||||
try {
|
||||
synchronized (this) {
|
||||
wait();
|
||||
}
|
||||
} catch (InterruptedException e) {
|
||||
logger.info("", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public synchronized void stop() {
|
||||
if (active) {
|
||||
timer.cancel();
|
||||
deactivate = true;
|
||||
run();
|
||||
notifyAll();
|
||||
}
|
||||
}
|
||||
|
||||
public void run() {
|
||||
if (activate && !active) {
|
||||
try {
|
||||
super.activate();
|
||||
} catch (ActivateException e) {
|
||||
logger.error("", e);
|
||||
} finally {
|
||||
activate = false;
|
||||
}
|
||||
} else if (deactivate && active) {
|
||||
try {
|
||||
super.deactivate();
|
||||
} catch (DeactivateException e) {
|
||||
logger.error("", e);
|
||||
} finally {
|
||||
deactivate = false;
|
||||
}
|
||||
}
|
||||
if (active) {
|
||||
work();
|
||||
}
|
||||
}
|
||||
|
||||
protected void work() {
|
||||
System.out.println("(-:");
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
IntervalWorker intervalWorker = new IntervalWorker();
|
||||
for (int i = 0; i < 3; ++i) {
|
||||
intervalWorker.start(false);
|
||||
System.out.println("--");
|
||||
intervalWorker.sleep(200);
|
||||
intervalWorker.stop();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,35 +0,0 @@
|
||||
package mimis.worker;
|
||||
|
||||
import java.util.Queue;
|
||||
import java.util.concurrent.ConcurrentLinkedQueue;
|
||||
|
||||
|
||||
public abstract class Listener<E> extends Worker {
|
||||
protected Queue<E> queue;
|
||||
|
||||
public Listener() {
|
||||
queue = new ConcurrentLinkedQueue<E>();
|
||||
}
|
||||
|
||||
public synchronized void add(E element) {
|
||||
queue.add(element);
|
||||
notifyAll();
|
||||
}
|
||||
|
||||
public final void work() {
|
||||
while (!queue.isEmpty()) {
|
||||
input(queue.poll());
|
||||
}
|
||||
if (!deactivate) {
|
||||
synchronized (this) {
|
||||
try {
|
||||
wait();
|
||||
} catch (InterruptedException e) {
|
||||
logger.info("", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public abstract void input(E element);
|
||||
}
|
||||
@@ -1,123 +0,0 @@
|
||||
package mimis.worker;
|
||||
|
||||
import mimis.exception.worker.ActivateException;
|
||||
import mimis.exception.worker.DeactivateException;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
public abstract class Worker implements Runnable {
|
||||
protected Logger logger = LoggerFactory.getLogger(getClass());
|
||||
|
||||
protected static final boolean THREAD = true;
|
||||
protected static final int SLEEP = 100;
|
||||
|
||||
protected boolean thread = true;
|
||||
protected boolean run = false;
|
||||
protected boolean active = false;
|
||||
protected boolean activate = false;
|
||||
protected boolean deactivate = false;
|
||||
|
||||
public Worker(boolean thread) {
|
||||
this.thread = thread;
|
||||
}
|
||||
|
||||
public Worker() {
|
||||
this(THREAD);
|
||||
}
|
||||
|
||||
public synchronized void start(boolean thread) {
|
||||
if (!active) {
|
||||
activate = true;
|
||||
}
|
||||
if (!run) {
|
||||
run = true;
|
||||
if (thread) {
|
||||
logger.debug("Start thread");
|
||||
new Thread(this, getClass().getName()).start();
|
||||
} else {
|
||||
logger.debug("Run directly");
|
||||
run();
|
||||
}
|
||||
} else {
|
||||
notifyAll();
|
||||
}
|
||||
}
|
||||
|
||||
public synchronized void start() {
|
||||
start(thread);
|
||||
}
|
||||
|
||||
public synchronized void stop() {
|
||||
if (active) {
|
||||
deactivate = true;
|
||||
}
|
||||
notifyAll();
|
||||
}
|
||||
|
||||
public void exit() {
|
||||
stop();
|
||||
run = false;
|
||||
}
|
||||
|
||||
protected void sleep(int time) {
|
||||
try {
|
||||
if (time > 0) {
|
||||
Thread.sleep(time);
|
||||
}
|
||||
} catch (InterruptedException e) {
|
||||
logger.info("", e);
|
||||
}
|
||||
}
|
||||
|
||||
protected void sleep() {
|
||||
sleep(SLEEP);
|
||||
}
|
||||
|
||||
public boolean active() {
|
||||
return active;
|
||||
}
|
||||
|
||||
protected void activate() throws ActivateException {
|
||||
active = true;
|
||||
}
|
||||
|
||||
protected void deactivate() throws DeactivateException {
|
||||
active = false;
|
||||
}
|
||||
|
||||
public void run() {
|
||||
while (run || deactivate) {
|
||||
if (activate && !active) {
|
||||
try {
|
||||
activate();
|
||||
} catch (ActivateException e) {
|
||||
logger.error("", e);
|
||||
} finally {
|
||||
activate = false;
|
||||
}
|
||||
} else if (deactivate && active) {
|
||||
try {
|
||||
deactivate();
|
||||
} catch (DeactivateException e) {
|
||||
logger.error("", e);
|
||||
} finally {
|
||||
deactivate = false;
|
||||
}
|
||||
}
|
||||
if (active) {
|
||||
work();
|
||||
} else if (run) {
|
||||
try {
|
||||
synchronized (this) {
|
||||
wait();
|
||||
}
|
||||
} catch (InterruptedException e) {
|
||||
logger.info("", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected abstract void work();
|
||||
}
|
||||
Reference in New Issue
Block a user