Ingrijpende wijzigingen in Worker:

- activate() en deactivate() hernoemd naar start() en stop()
- oude handmatige start() bestaat niet meer, thread start automatisch
- oude activate() en deactivate() zijn nu callbacks
- callbacks worden uitgevoerd vannuit de Worker Thread, voor performance
- exit() toegevoegd om Thread geheel te stoppen, hierna kan opnieuw start() worden gebruikt
- Exitable interface in Application en Device verwijderd, is nu overbodig

Dit systeem doorgevoerd en gedeeltelijk getest. Nog niet verholpen:
- soms eindigt een Thread niet goed (NetworkDevice$Server en LircService), waarschijnlijk veroorzaakt door socket
This commit is contained in:
2011-09-25 19:11:30 +00:00
parent d8c9bbfef7
commit 3d0c9a55dd
38 changed files with 394 additions and 468 deletions

View File

@@ -3,14 +3,11 @@ package mimis;
import mimis.event.EventHandler;
import mimis.event.Task;
import mimis.event.feedback.TextFeedback;
import mimis.exception.WorkerException;
import mimis.exception.worker.DeactivateException;
import mimis.manager.Exitable;
import mimis.manager.Titled;
import mimis.value.Action;
import mimis.value.Signal;
public abstract class Application extends EventHandler implements Titled, Exitable {
public abstract class Application extends EventHandler implements Titled {
protected String title;
protected boolean active;
@@ -27,18 +24,14 @@ public abstract class Application extends EventHandler implements Titled, Exitab
Task task = (Task) event;
Action action = task.getAction();
switch (action) {
case ACTIVATE:
case START:
if (task.getSignal().equals(Signal.BEGIN)) {
try {
if (active()) {
eventRouter.add(new TextFeedback("Deactivate application"));
deactivate();
} else {
eventRouter.add(new TextFeedback("Activate application"));
activate();
}
} catch (WorkerException e) {
log.error(e);
if (active()) {
eventRouter.add(new TextFeedback("Stop application"));
stop();
} else {
eventRouter.add(new TextFeedback("Start application"));
start();
}
}
return;
@@ -46,15 +39,4 @@ public abstract class Application extends EventHandler implements Titled, Exitab
}
super.event(event);
}
public void stop() {
super.stop();
if (active()) {
try {
deactivate();
} catch (DeactivateException e) {
log.error(e);
}
}
}
}

View File

@@ -9,7 +9,6 @@ import mimis.device.panel.PanelDevice;
import mimis.device.wiimote.WiimoteDevice;
import mimis.event.EventRouter;
import mimis.event.router.GlobalRouter;
import mimis.exception.worker.ActivateException;
import mimis.util.swing.Dialog;
import org.apache.commons.logging.Log;
@@ -39,11 +38,7 @@ public class Client {
public void start() {
log.debug("Client");
Mimis mimis = new Mimis(eventRouter, deviceArray);
try {
mimis.activate();
} catch (ActivateException e) {
log.fatal(e);
}
mimis.start();
}
public static void main(String[] args) {

View File

@@ -2,13 +2,12 @@ package mimis;
import mimis.event.EventHandler;
import mimis.exception.worker.DeactivateException;
import mimis.manager.Exitable;
import mimis.manager.Titled;
import mimis.sequence.EventMap;
import mimis.sequence.SequenceParser;
import mimis.sequence.State;
public abstract class Device extends EventHandler implements Titled, Exitable {
public abstract class Device extends EventHandler implements Titled {
protected String title;
protected SequenceParser sequenceParser;
@@ -22,22 +21,11 @@ public abstract class Device extends EventHandler implements Titled, Exitable {
}
/* Worker */
public void deactivate() throws DeactivateException {
protected void deactivate() throws DeactivateException {
super.deactivate();
sequenceParser.reset();
}
public void stop() {
if (active()) {
try {
deactivate();
} catch (DeactivateException e) {
log.error(e);
}
}
super.stop();
}
/* SequenceParser */
protected void add(EventMap eventMap) {
sequenceParser.add(eventMap);
@@ -50,7 +38,7 @@ public abstract class Device extends EventHandler implements Titled, Exitable {
protected void reset() {
sequenceParser.reset();
}
protected void add(State state) {
sequenceParser.add(state);
}

View File

@@ -79,7 +79,7 @@ public class GUI extends JFrame {
protected void processWindowEvent(WindowEvent event) {
if (event.getID() == WindowEvent.WINDOW_CLOSING) {
log.debug("Window closing");
mimis.stop();
mimis.exit();
}
}

View File

@@ -17,7 +17,6 @@ import mimis.device.panel.PanelDevice;
import mimis.device.wiimote.WiimoteDevice;
import mimis.event.EventRouter;
import mimis.event.router.LocalRouter;
import mimis.exception.worker.ActivateException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
@@ -53,11 +52,7 @@ public class Main {
public void start() {
log.debug("Main");
Mimis mimis = new Mimis(eventRouter, applicationArray, deviceArray);
try {
mimis.activate();
} catch (ActivateException e) {
log.fatal(e);
}
mimis.start();
}
public static void main(String[] args) {

View File

@@ -6,14 +6,14 @@ import java.util.Map;
import javax.swing.JToggleButton;
import mimis.manager.Exitable;
import mimis.exception.worker.DeactivateException;
import mimis.manager.ManageButton;
import mimis.manager.Titled;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
public class Manager<T extends Worker & Titled & Exitable> extends Worker {
public class Manager<T extends Worker & Titled> extends Worker {
protected Log log = LogFactory.getLog(getClass());
protected static final long serialVersionUID = 1L;
protected static final int INTERVAL = 1000;
@@ -26,13 +26,20 @@ public class Manager<T extends Worker & Titled & Exitable> extends Worker {
createButtons();
}
public void stop() {
super.stop();
protected void deactivate() throws DeactivateException {
super.deactivate();
for (T manageable : manageableArray) {
manageable.stop();
}
}
public void exit() {
super.exit();
for (T manageable : manageableArray) {
manageable.exit();
}
}
public int count() {
return manageableArray.length;
}
@@ -50,6 +57,7 @@ public class Manager<T extends Worker & Titled & Exitable> extends Worker {
}
protected void work() {
/* Todo: timertask! */
long before = Calendar.getInstance().getTimeInMillis();
for (T manageable : manageableArray) {
boolean active = manageable.active();
@@ -60,4 +68,4 @@ public class Manager<T extends Worker & Titled & Exitable> extends Worker {
int sleep = INTERVAL - (int) (after - before);
sleep(sleep);
}
}
}

View File

@@ -5,6 +5,7 @@ import mimis.event.EventRouter;
import mimis.event.Feedback;
import mimis.event.feedback.TextFeedback;
import mimis.exception.worker.ActivateException;
import mimis.exception.worker.DeactivateException;
import mimis.sequence.SequenceParser;
import mimis.util.ArrayCycle;
import mimis.value.Action;
@@ -55,16 +56,16 @@ public class Mimis extends EventHandler {
deviceManager = new Manager<Device>(deviceArray);
}
public void activate() throws ActivateException {
protected void activate() throws ActivateException {
log.debug("Create gui");
gui = new GUI(this, applicationManager, deviceManager);
log.debug("Activate event router");
eventRouter.activate();
eventRouter.start();
log.debug("Activate managers");
applicationManager.activate();
deviceManager.activate();
applicationManager.start();
deviceManager.start();
if (applicationCycle.size() > 0) {
log.debug("Initialise application cycle");
@@ -74,8 +75,8 @@ public class Mimis extends EventHandler {
super.activate();
}
public void stop() {
super.stop();
protected void deactivate() throws DeactivateException {
super.deactivate();
log.debug("Stop GUI");
gui.stop();
@@ -86,6 +87,16 @@ public class Mimis extends EventHandler {
applicationManager.stop();
deviceManager.stop();
}
public void exit() {
super.exit();
log.debug("Exit event router");
eventRouter.exit();
log.debug("Exit managers");
applicationManager.exit();
deviceManager.exit();
}
protected void end(Action action) {
switch (action) {
@@ -98,7 +109,7 @@ public class Mimis extends EventHandler {
add(new TextFeedback("Previous application: " + applicationCycle.current().title()));
break;
case EXIT:
stop();
exit();
break;
}
}

View File

@@ -14,36 +14,42 @@ public abstract class Worker implements Runnable {
protected boolean run = false;
protected boolean active = false;
protected int work = 0;
protected boolean activate = false;
protected boolean deactivate = false;
public void start(boolean thread) {
run = true;
if (thread) {
log.debug("Start thread");
new Thread(this, getClass().getName()).start();
public final void start(boolean thread) {
if (!active) {
activate = true;
}
if (!run) {
run = true;
if (thread) {
log.debug("Start thread");
new Thread(this, getClass().getName()).start();
} else {
log.debug("Run directly");
run();
}
} else {
log.debug("Run directly");
run();
log.debug("note");
notifyAll();
}
}
public void start() {
public synchronized final void start() {
start(THREAD);
}
public void stop() {
if (active()) {
try {
deactivate();
} catch (DeactivateException e) {
log.error(e);
}
public synchronized final void stop() {
if (active) {
deactivate = true;
}
notifyAll();
}
public void exit() {
stop();
run = false;
synchronized (this) {
notifyAll();
}
log.debug(String.format("%s: %d", getClass(), work));
}
protected void sleep(int time) {
@@ -64,33 +70,37 @@ public abstract class Worker implements Runnable {
return active;
}
public void activate() throws ActivateException {
activate(THREAD);
}
public void activate(boolean thread) {
protected void activate() throws ActivateException {
active = true;
if (!run) {
start(thread);
}
synchronized (this) {
notifyAll();
}
}
public void deactivate() throws DeactivateException {
protected void deactivate() throws DeactivateException {
active = false;
synchronized (this) {
notifyAll();
}
}
public final void run() {
while (run) {
if (active()) {
++work;
while (run || deactivate) {
//log.debug("run() run=" + run + ", active=" + active + ", activate=" + activate + ", deactivate=" + deactivate);
if (activate && !active) {
try {
activate();
} catch (ActivateException e) {
log.error(e);
} finally {
activate = false;
}
} else if (deactivate && active) {
try {
deactivate();
} catch (DeactivateException e) {
log.error(e);
} finally {
deactivate = false;
}
}
if (active) {
work();
} else {
} else if (run) {
try {
synchronized (this) {
wait();

View File

@@ -2,7 +2,6 @@ package mimis.application;
import mimis.Worker;
import mimis.application.robot.RobotApplication;
import mimis.exception.worker.ActivateException;
import mimis.exception.worker.DeactivateException;
import mimis.value.Action;
import mimis.value.Key;
@@ -22,23 +21,24 @@ public class PhotoViewerApplication extends RobotApplication {
fullscreen = false;
}
public void stop() {
super.stop();
protected void deactivate() throws DeactivateException {
super.deactivate();
zoomWorker.stop();
}
public void exit() {
super.exit();
zoomWorker.exit();
}
public void begin(Action action) {
try {
switch (action) {
case VOLUME_UP:
zoomWorker.activate(1);
break;
case VOLUME_DOWN:
zoomWorker.activate(-1);
break;
}
} catch (ActivateException e) {
log.error(e);
switch (action) {
case VOLUME_UP:
zoomWorker.start(1);
break;
case VOLUME_DOWN:
zoomWorker.start(-1);
break;
}
}
@@ -47,11 +47,7 @@ public class PhotoViewerApplication extends RobotApplication {
switch (action) {
case VOLUME_UP:
case VOLUME_DOWN:
try {
zoomWorker.deactivate();
} catch (DeactivateException e) {
log.error(e);
}
zoomWorker.stop();
break;
case FORWARD:
break;
@@ -92,8 +88,8 @@ public class PhotoViewerApplication extends RobotApplication {
protected class ZoomWorker extends Worker {
protected int zoomDirection;
public void activate(int zoomDirection) throws ActivateException {
super.activate();
public void start(int zoomDirection) {
super.start();
this.zoomDirection = zoomDirection;
}

View File

@@ -34,6 +34,7 @@ public abstract class CMDApplication extends Application {
command = replaceVariables(command);
process = Runtime.getRuntime().exec(command);
} catch (IOException e) {
log.error(e);
throw new ActivateException();
}
}
@@ -42,8 +43,9 @@ public abstract class CMDApplication extends Application {
return active = Native.isRunning(program);
}
public void deactivate() throws DeactivateException {
protected synchronized void deactivate() throws DeactivateException {
super.deactivate();
log.debug(process);
if (process != null) {
process.destroy();
}

View File

@@ -14,7 +14,6 @@ public abstract class WindowsApplication extends CMDApplication {
protected final static int START_SLEEP = 500;
protected String window;
protected Process process;
protected int handle;
public WindowsApplication(String program, String title, String window) {
@@ -43,7 +42,8 @@ public abstract class WindowsApplication extends CMDApplication {
return super.active();
}
public void deactivate() throws DeactivateException {
protected void deactivate() throws DeactivateException {
super.deactivate();
close();
}

View File

@@ -23,38 +23,40 @@ public class GomPlayerApplication extends WindowsApplication {
volumeWorker = new VolumeWorker();
seekWorker = new SeekWorker();
}
public void stop() {
super.stop();
protected void deactivate() throws DeactivateException {
super.deactivate();
volumeWorker.stop();
seekWorker.stop();
}
public void exit() {
super.exit();
volumeWorker.exit();
seekWorker.exit();
}
public void begin(Action action) {
log.trace("GomPlayerApplication begin: " + action);
try {
switch (action) {
case VOLUME_UP:
volumeWorker.activate(1);
break;
case VOLUME_DOWN:
volumeWorker.activate(-1);
break;
case FORWARD:
seekWorker.activate(Amount.SMALL, 1);
break;
case REWIND:
seekWorker.activate(Amount.SMALL, -1);
break;
case NEXT:
seekWorker.activate(Amount.MEDIUM, 1);
break;
case PREVIOUS:
seekWorker.activate(Amount.MEDIUM, -1);
break;
}
} catch (ActivateException e) {
log.error(e);
switch (action) {
case VOLUME_UP:
volumeWorker.start();
break;
case VOLUME_DOWN:
volumeWorker.start();
break;
case FORWARD:
seekWorker.start(Amount.SMALL, 1);
break;
case REWIND:
seekWorker.start(Amount.SMALL, -1);
break;
case NEXT:
seekWorker.start(Amount.MEDIUM, 1);
break;
case PREVIOUS:
seekWorker.start(Amount.MEDIUM, -1);
break;
}
}
@@ -71,19 +73,11 @@ public class GomPlayerApplication extends WindowsApplication {
case REWIND:
case NEXT:
case PREVIOUS:
try {
seekWorker.deactivate();
} catch (DeactivateException e) {
log.error(e);
}
seekWorker.stop();
break;
case VOLUME_UP:
case VOLUME_DOWN:
try {
volumeWorker.deactivate();
} catch (DeactivateException e) {
log.error(e);
}
volumeWorker.stop();
break;
case FULLSCREEN:
command(0x8154);
@@ -94,8 +88,8 @@ public class GomPlayerApplication extends WindowsApplication {
protected class VolumeWorker extends Worker {
protected int volumeChangeSign;
public void activate(int volumeChangeSign) throws ActivateException {
super.activate();
public void start(int volumeChangeSign) throws ActivateException {
super.start();
this.volumeChangeSign = volumeChangeSign;
}
@@ -109,8 +103,8 @@ public class GomPlayerApplication extends WindowsApplication {
protected Amount amount;
protected int seekDirection;
public void activate(Amount amount, int seekDirection) throws ActivateException {
super.activate();
public void start(Amount amount, int seekDirection) {
super.start();
this.amount = amount;
this.seekDirection = seekDirection;
}

View File

@@ -2,7 +2,6 @@ package mimis.application.cmd.windows.winamp;
import mimis.Worker;
import mimis.application.cmd.windows.WindowsApplication;
import mimis.exception.worker.ActivateException;
import mimis.exception.worker.DeactivateException;
import mimis.value.Action;
@@ -49,31 +48,33 @@ public class WinampApplication extends WindowsApplication {
seekWorker = new SeekWorker();
}
public void stop() {
super.stop();
protected void deactivate() throws DeactivateException {
super.deactivate();
volumeWorker.stop();
seekWorker.stop();
}
public void exit() {
super.exit();
volumeWorker.exit();
seekWorker.exit();
}
public void begin(Action action) {
log.trace("WinampApplication begin: " + action);
try {
switch (action) {
case VOLUME_UP:
volumeWorker.activate(1);
break;
case VOLUME_DOWN:
volumeWorker.activate(-1);
break;
case FORWARD:
seekWorker.activate(1);
break;
case REWIND:
seekWorker.activate(-1);
break;
}
} catch (ActivateException e) {
log.error(e);
switch (action) {
case VOLUME_UP:
volumeWorker.start(1);
break;
case VOLUME_DOWN:
volumeWorker.start(-1);
break;
case FORWARD:
seekWorker.start(1);
break;
case REWIND:
seekWorker.start(-1);
break;
}
}
@@ -98,11 +99,7 @@ public class WinampApplication extends WindowsApplication {
break;
case FORWARD:
case REWIND:
try {
seekWorker.deactivate();
} catch (DeactivateException e) {
log.error(e);
}
seekWorker.stop();
break;
case MUTE:
if (muted) {
@@ -115,11 +112,7 @@ public class WinampApplication extends WindowsApplication {
break;
case VOLUME_UP:
case VOLUME_DOWN:
try {
volumeWorker.deactivate();
} catch (DeactivateException e) {
log.error(e);
}
volumeWorker.stop();
break;
case SHUFFLE:
command(WINAMP_FILE_SHUFFLE);
@@ -158,8 +151,8 @@ public class WinampApplication extends WindowsApplication {
protected class VolumeWorker extends Worker {
protected int volumeChangeSign;
public void activate(int volumeChangeSign) throws ActivateException {
super.activate();
public void start(int volumeChangeSign) {
super.start();
this.volumeChangeSign = volumeChangeSign;
}
@@ -172,8 +165,8 @@ public class WinampApplication extends WindowsApplication {
protected class SeekWorker extends Worker {
protected int seekDirection;
public void activate(int seekDirection) throws ActivateException {
super.activate();
public void start(int seekDirection) {
super.start();
this.seekDirection = seekDirection;
}

View File

@@ -2,8 +2,6 @@ package mimis.application.cmd.windows.wmp;
import mimis.Worker;
import mimis.application.cmd.windows.WindowsApplication;
import mimis.exception.worker.ActivateException;
import mimis.exception.worker.DeactivateException;
import mimis.value.Action;
public class WMPApplication extends WindowsApplication {
@@ -42,18 +40,10 @@ public class WMPApplication extends WindowsApplication {
command(18817);
break;
case VOLUME_UP:
try {
volumeWorker.activate(1);
} catch (ActivateException e) {
log.error(e);
}
volumeWorker.start(1);
break;
case VOLUME_DOWN:
try {
volumeWorker.activate(-1);
} catch (ActivateException e) {
log.error(e);
}
volumeWorker.start(-1);
break;
case SHUFFLE:
command(18842);
@@ -75,11 +65,7 @@ public class WMPApplication extends WindowsApplication {
break;
case VOLUME_UP:
case VOLUME_DOWN:
try {
volumeWorker.deactivate();
} catch (DeactivateException e) {
log.error(e);
}
volumeWorker.stop();
break;
}
}
@@ -87,8 +73,8 @@ public class WMPApplication extends WindowsApplication {
protected class VolumeWorker extends Worker {
protected int volumeChangeSign;
public void activate(int volumeChangeSign) throws ActivateException {
super.activate();
public void start(int volumeChangeSign) {
super.start();
this.volumeChangeSign = volumeChangeSign;
}

View File

@@ -34,7 +34,7 @@ public class iTunesApplication extends Application implements iTunesEventsInterf
handle = quiting = false;
}
public void activate() throws ActivateException {
protected void activate() throws ActivateException {
synchronized (iTunes) {
iTunes.connect();
if (!handle) {
@@ -62,9 +62,9 @@ public class iTunesApplication extends Application implements iTunesEventsInterf
return active;
}
public void deactivate() throws DeactivateException {
protected void deactivate() throws DeactivateException {
super.deactivate();
volumeWorker.deactivate();
volumeWorker.stop();
try {
if (QUIT) {
quiting = true;
@@ -78,11 +78,6 @@ public class iTunesApplication extends Application implements iTunesEventsInterf
}
}
public void stop() {
super.stop();
volumeWorker.stop();
}
protected void begin(Action action) {
log.trace("iTunesApplication begin: " + action);
if (!active) return;
@@ -94,18 +89,10 @@ public class iTunesApplication extends Application implements iTunesEventsInterf
iTunes.rewind();
break;
case VOLUME_UP:
try {
volumeWorker.activate(VOLUME_CHANGE_RATE);
} catch (ActivateException e) {
log.error(e);
}
volumeWorker.start(VOLUME_CHANGE_RATE);
break;
case VOLUME_DOWN:
try {
volumeWorker.activate(-VOLUME_CHANGE_RATE);
} catch (ActivateException e) {
log.error(e);
}
volumeWorker.start(-VOLUME_CHANGE_RATE);
break;
}
}
@@ -134,11 +121,7 @@ public class iTunesApplication extends Application implements iTunesEventsInterf
break;
case VOLUME_UP:
case VOLUME_DOWN:
try {
volumeWorker.deactivate();
} catch (DeactivateException e) {
log.error(e);
}
volumeWorker.stop();
break;
case SHUFFLE:
iTunes.toggleShuffle();
@@ -182,8 +165,8 @@ public class iTunesApplication extends Application implements iTunesEventsInterf
protected class VolumeWorker extends Worker {
protected int volumeChangeRate;
public void activate(int volumeChangeRate) throws ActivateException {
super.activate();
public void start(int volumeChangeRate) {
super.start();
this.volumeChangeRate = volumeChangeRate;
}

View File

@@ -25,9 +25,14 @@ public class LircApplication extends Application {
return active = lircService.active();
}
public void deactivate() throws DeactivateException {
protected void deactivate() throws DeactivateException {
super.deactivate();
lircService.deactivate();
lircService.stop();
}
public void exit() {
super.exit();
lircService.exit();
}
public void send(LircButton button) {

View File

@@ -18,11 +18,16 @@ public class iPodApplication extends LircApplication {
volumeWorker = new VolumeWorker();
}
public void stop() {
super.stop();
protected void deactivate() throws DeactivateException {
super.deactivate();
volumeWorker.stop();
}
public void exit() {
super.exit();
volumeWorker.exit();
}
protected void begin(Action action) {
log.trace("iPodApplication begin: " + action);
if (!active) return;
@@ -59,11 +64,7 @@ public class iPodApplication extends LircApplication {
break;
case VOLUME_UP:
case VOLUME_DOWN:
try {
volumeWorker.deactivate();
} catch (DeactivateException e) {
log.error(e);
}
volumeWorker.stop();
break;
}
}

View File

@@ -2,8 +2,6 @@ package mimis.application.mpc;
import mimis.Worker;
import mimis.application.cmd.windows.WindowsApplication;
import mimis.exception.worker.ActivateException;
import mimis.exception.worker.DeactivateException;
import mimis.value.Action;
public class MPCApplication extends WindowsApplication {
@@ -22,26 +20,22 @@ public class MPCApplication extends WindowsApplication {
volumeWorker = new VolumeWorker();
seekWorker = new SeekWorker();
}
public void begin(Action action) {
log.trace("MPCApplication: " + action);
try {
switch (action) {
case FORWARD:
seekWorker.activate(1);
break;
case REWIND:
seekWorker.activate(-1);
break;
case VOLUME_UP:
volumeWorker.activate(1);
break;
case VOLUME_DOWN:
volumeWorker.activate(-1);
break;
}
} catch (ActivateException e) {
log.error(e);
switch (action) {
case FORWARD:
seekWorker.start(1);
break;
case REWIND:
seekWorker.start(-1);
break;
case VOLUME_UP:
volumeWorker.start(1);
break;
case VOLUME_DOWN:
volumeWorker.start(-1);
break;
}
}
@@ -59,22 +53,14 @@ public class MPCApplication extends WindowsApplication {
break;
case FORWARD:
case REWIND:
try {
seekWorker.deactivate();
} catch (DeactivateException e) {
log.error(e);
}
seekWorker.stop();
break;
case MUTE:
command(909);
break;
case VOLUME_UP:
case VOLUME_DOWN:
try {
volumeWorker.deactivate();
} catch (DeactivateException e) {
log.error(e);
}
volumeWorker.stop();
break;
case FULLSCREEN:
command(830);
@@ -89,8 +75,8 @@ public class MPCApplication extends WindowsApplication {
protected class VolumeWorker extends Worker {
protected int volumeChangeSign;
public void activate(int volumeChangeSign) throws ActivateException {
super.activate();
public void start(int volumeChangeSign) {
super.start();
this.volumeChangeSign = volumeChangeSign;
}
@@ -103,8 +89,8 @@ public class MPCApplication extends WindowsApplication {
protected class SeekWorker extends Worker {
protected int seekDirection;
public void activate(int seekDirection) throws ActivateException {
super.activate();
public void start(int seekDirection) {
super.start();
this.seekDirection = seekDirection;
}

View File

@@ -63,13 +63,19 @@ public class VLCApplication extends CMDApplication {
}
}
public void stop() {
super.stop();
protected void deactivate() throws DeactivateException {
super.deactivate();
volumeWorker.stop();
seekWorker.stop();
Native.terminate(program);
}
public void exit() {
super.exit();
volumeWorker.exit();
seekWorker.exit();
}
public void begin(Action action) {
log.trace("VLCApplication begin: " + action);
try {
@@ -81,10 +87,10 @@ public class VLCApplication extends CMDApplication {
volumeWorker.activate("-");
break;
case FORWARD:
seekWorker.activate(Amount.SMALL, "+");
seekWorker.start(Amount.SMALL, "+");
break;
case REWIND:
seekWorker.activate(Amount.SMALL, "-");
seekWorker.start(Amount.SMALL, "-");
break;
}
} catch (ActivateException e) {
@@ -109,22 +115,14 @@ public class VLCApplication extends CMDApplication {
break;
case FORWARD:
case REWIND:
try {
seekWorker.deactivate();
} catch (DeactivateException e) {
log.error(e);
}
seekWorker.stop();
break;
case MUTE:
command("volume&val=" + toggleMute());
break;
case VOLUME_UP:
case VOLUME_DOWN:
try {
volumeWorker.deactivate();
} catch (DeactivateException e) {
log.error(e);
}
volumeWorker.stop();
break;
case SHUFFLE:
command("command=pl_random");
@@ -176,8 +174,8 @@ public class VLCApplication extends CMDApplication {
protected Amount amount;
protected String seekDirection;
public void activate(Amount amount, String seekDirection) throws ActivateException {
super.activate();
public void start(Amount amount, String seekDirection) {
super.start();
this.amount = amount;
this.seekDirection = seekDirection;
}

View File

@@ -27,7 +27,7 @@ public abstract class JavaInputDevice extends Device {
protected JavaInputListener javaInputListener;
protected Button previousDirectionalButton;
public void activate() throws ActivateException {
protected void activate() throws ActivateException {
super.activate();
try {
JXInputDevice jxinputDevice = getDevice(name);
@@ -37,14 +37,12 @@ public abstract class JavaInputDevice extends Device {
active = false;
throw new ActivateException();
}
javaInputListener.activate();
javaInputListener.start();
}
public void deactivate() throws DeactivateException {
protected void deactivate() throws DeactivateException {
super.deactivate();
if (active) {
javaInputListener.deactivate();
}
javaInputListener.stop();
}
public void processEvent(JXInputAxisEvent event) {}

View File

@@ -20,7 +20,7 @@ public class Extreme3DDevice extends JavaInputDevice {
eventMapCycle = new Extreme3DEventMapCycle();
}
public void activate() throws ActivateException {
protected void activate() throws ActivateException {
super.activate();
add(eventMapCycle.mimis);
add(eventMapCycle.player);

View File

@@ -20,7 +20,7 @@ public class RumblepadDevice extends JavaInputDevice {
eventMapCycle = new RumblepadEventMapCycle();
}
public void activate() throws ActivateException {
protected void activate() throws ActivateException {
super.activate();
add(eventMapCycle.mimis);
add(eventMapCycle.player);

View File

@@ -28,7 +28,7 @@ public class JIntellitypeDevice extends Device implements HotkeyListener, Intell
eventMapCycle = new JIntellitypeEventMapCycle();
}
public void activate() throws ActivateException {
protected void activate() throws ActivateException {
super.activate();
jit.addHotKeyListener(this);
jit.addIntellitypeListener(this);
@@ -56,14 +56,14 @@ public class JIntellitypeDevice extends Device implements HotkeyListener, Intell
}
}
public void deactivate() throws DeactivateException {
protected void deactivate() throws DeactivateException {
super.deactivate();
jit.removeHotKeyListener(this);
jit.removeIntellitypeListener(this);
}
public void stop() {
super.stop();
public void exit() {
super.exit();
jit.cleanUp();
}
}

View File

@@ -32,8 +32,8 @@ public class LircDevice extends Device implements LircButtonListener, SignalList
eventMapCycle = new LircEventMapCycle();
}
public void activate() throws ActivateException {
lircService.activate();
protected void activate() throws ActivateException {
lircService.start();
add(eventMapCycle.denonRC176);
add(eventMapCycle.philiphsRCLE011);
add(eventMapCycle.samsungBN5901015A);
@@ -42,27 +42,27 @@ public class LircDevice extends Device implements LircButtonListener, SignalList
public boolean active() {
if (active && !lircService.active()) {
try {
deactivate();
} catch (DeactivateException e) {
log.error(e);
}
stop();
} else if (!active) {
if (Native.isRunning(PROGRAM)) {
try {
activate();
} catch (ActivateException e) {
log.error(e);
}
start();
}
}
return active;
}
public void deactivate() throws DeactivateException {
protected void deactivate() throws DeactivateException {
log.debug("Deactivate LircDevice");
super.deactivate();
lircService.deactivate();
lircService.stop();
multiplexer.stop();
}
public void exit() {
log.debug("Exit LircDevice");
super.exit();
lircService.exit();
multiplexer.exit();
}
public void add(LircButton lircButton) {
@@ -79,11 +79,4 @@ public class LircDevice extends Device implements LircButtonListener, SignalList
break;
}
}
public void stop() {
log.debug("Stop LircDevice");
super.stop();
lircService.stop();
multiplexer.stop();
}
}

View File

@@ -80,14 +80,14 @@ public class LircService extends Worker {
super.activate();
}
public boolean active() {
public synchronized boolean active() {
if (active && !socket.isConnected()) {
active = false;
}
return active;
}
public void deactivate() throws DeactivateException {
public synchronized void deactivate() throws DeactivateException {
log.trace("Deactivate LircService");
super.deactivate();
try {

View File

@@ -14,7 +14,7 @@ public class DenonRC176EventMap extends EventMap {
add(DenonRC176Button.TUNER_DOWN, new Task(Target.MIMIS, Action.PREVIOUS));
/* Application */
add(DenonRC176Button.AMP_POWER, new Task(Target.APPLICATION, Action.ACTIVATE));
add(DenonRC176Button.AMP_POWER, new Task(Target.APPLICATION, Action.START));
add(DenonRC176Button.CD_NEXT, new Task(Target.APPLICATION, Action.NEXT));
add(DenonRC176Button.CD_PREVIOUS, new Task(Target.APPLICATION, Action.PREVIOUS));
add(DenonRC176Button.TAPE_REWIND, new Task(Target.APPLICATION, Action.REWIND));

View File

@@ -14,7 +14,7 @@ public class PhiliphsRCLE011EventMap extends EventMap {
add(PhiliphsRCLE011Button.DOWN, new Task(Target.MIMIS, Action.PREVIOUS));
/* Application */
add(PhiliphsRCLE011Button.POWER, new Task(Target.APPLICATION, Action.ACTIVATE));
add(PhiliphsRCLE011Button.POWER, new Task(Target.APPLICATION, Action.START));
add(PhiliphsRCLE011Button.PROGRAM_UP, new Task(Target.APPLICATION, Action.NEXT));
add(PhiliphsRCLE011Button.PROGRAM_DOWN, new Task(Target.APPLICATION, Action.PREVIOUS));
add(PhiliphsRCLE011Button.LEFT, new Task(Target.APPLICATION, Action.REWIND));

View File

@@ -28,28 +28,21 @@ public class NetworkDevice extends Device {
public static final int PORT = 6789;
protected Log log = LogFactory.getLog(NetworkDevice.class);
protected int port;
protected Server server;
protected ConcurrentLinkedQueue<Client> clientList;
public NetworkDevice(int port) {
super(TITLE);
this.port = port;
clientList = new ConcurrentLinkedQueue<Client>();
server = new Server(port);
}
public NetworkDevice() {
this(PORT);
}
public void activate() throws ActivateException {
log.trace("Activate NetworkDevice");
try {
server = new Server(port);
server.activate();
} catch (IOException e) {
throw new ActivateException();
}
protected void activate() throws ActivateException {
server.start();
super.activate();
}
@@ -59,14 +52,19 @@ public class NetworkDevice extends Device {
client.stop();
}
}
return server == null ? active : (active = server.active());
return active = server.active();
}
public void deactivate() throws DeactivateException {
protected void deactivate() throws DeactivateException {
super.deactivate();
server.stop();
}
public synchronized void exit() {
super.exit();
server.exit();
}
protected void feedback(Feedback feedback) {
for (Client client : clientList) {
client.send(feedback);
@@ -75,40 +73,55 @@ public class NetworkDevice extends Device {
protected class Server extends Worker {
protected ServerSocket serverSocket;
public Server(int port) throws IOException {
serverSocket = new ServerSocket(port);
eventRouter.add(new TextFeedback("[NetworkDevice] Wating for clients"));
protected int port;
public Server(int port) {
this.port = port;
}
public boolean active() {
return active = !serverSocket.isClosed();
protected void activate() throws ActivateException {
try {
serverSocket = new ServerSocket(port);
} catch (IOException e) {
throw new ActivateException();
}
super.activate();
}
public synchronized boolean active() {
return active = serverSocket != null && !serverSocket.isClosed();
}
protected synchronized void deactivate() throws DeactivateException {
super.deactivate();
try {
eventRouter.add(new TextFeedback("[NetworkDevice] Closing server socket"));
serverSocket.close();
} catch (IOException e) {
log.error(e);
} finally {
for (Client client : clientList) {
client.stop();
}
}
}
public void work() {
try {
eventRouter.add(new TextFeedback("[NetworkDevice] Wating for clients"));
Socket socket = serverSocket.accept();
Client client = new Client(socket);
try {
client.activate();
} catch (ActivateException e) {
log.error(e);
}
client.start();
eventRouter.add(new TextFeedback("[NetworkDevice] Client connected: " + socket.getInetAddress()));
} catch (IOException e) {
log.error(e);
}
}
public void stop() {
super.stop();
try {
serverSocket.close();
} catch (IOException e) {
log.error(e);
}
public synchronized void exit() {
super.exit();
for (Client client : clientList) {
client.stop();
client.exit();
}
}
}
@@ -151,8 +164,8 @@ public class NetworkDevice extends Device {
}
}
public void stop() {
super.stop();
protected void deactivate() throws DeactivateException {
super.deactivate();
send(new Task(Target.SELF, Action.STOP));
clientList.remove(this);
try {

View File

@@ -9,7 +9,6 @@ import javax.swing.JPanel;
import javax.swing.WindowConstants;
import mimis.Mimis;
import mimis.exception.worker.DeactivateException;
import mimis.util.Swing;
import mimis.util.swing.HoldButton;
import mimis.util.swing.HoldButtonListener;
@@ -176,11 +175,7 @@ public class Panel extends JFrame implements HoldButtonListener {
protected void processWindowEvent(WindowEvent event) {
if (event.getID() == WindowEvent.WINDOW_CLOSING) {
log.debug("Window closing");
try {
panelDevice.deactivate();
} catch (DeactivateException e) {
log.error(e);
}
panelDevice.stop();
}
}
}

View File

@@ -18,7 +18,7 @@ public class PanelDevice extends Device {
eventMapCycle = new PanelEventMapCycle();
}
public void activate() throws ActivateException {
protected void activate() throws ActivateException {
super.activate();
panel = new Panel(this);
panel.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
@@ -29,7 +29,8 @@ public class PanelDevice extends Device {
return active = panel != null;
}
public void deactivate() throws DeactivateException {
protected void deactivate() throws DeactivateException {
log.debug("deactive() " + panel);
super.deactivate();
panel.dispose();
panel = null;

View File

@@ -26,6 +26,7 @@ public class WiimoteDevice extends Device implements GestureListener {
protected static final String TITLE = "Wiimote";
protected static final int RUMBLE = 50;
protected static final int TIMEOUT = 200;
protected static final int LED_SLEEP = 50;
protected static WiimoteService wiimoteService;
protected WiimoteEventMapCycle eventMapCycle;
@@ -52,28 +53,23 @@ public class WiimoteDevice extends Device implements GestureListener {
}
/* Worker */
public void activate() throws ActivateException {
protected void activate() throws ActivateException {
add(eventMapCycle.player);
connect();
try {
wiimote = wiimoteService.getDevice(this);
ledWorker.activate();
ledWorker.start();
} catch (DeviceNotFoundException e) {
log.error(e);
throw new ActivateException();
}
super.activate();
}
public boolean active() {
if (wiimote != null) {
if (!ledWorker.active()) {
try {
ledWorker.activate();
} catch (ActivateException e) {
log.error(e);
}
ledWorker.start();
}
connected = false;
wiimote.getStatus();
@@ -86,24 +82,26 @@ public class WiimoteDevice extends Device implements GestureListener {
}
if (!connected) {
active = false;
try {
ledWorker.deactivate();
} catch (DeactivateException e) {
log.error(e);
}
ledWorker.stop();
}
}
return active;
}
public void stop() {
super.stop();
ledWorker.stop();
protected void deactivate() throws DeactivateException {
super.deactivate();
ledWorker.deactivate();
wiimoteDiscovery.deactivate();
}
public void exit() {
super.exit();
ledWorker.exit();
/*if (wiimote != null) {
disconnect();
}*/
wiimoteService.exit();
wiimoteDiscovery.stop();
wiimoteDiscovery.exit();
}
/* Events */
@@ -165,7 +163,7 @@ public class WiimoteDevice extends Device implements GestureListener {
wiimote = null;
try {
wiimote = wiimoteService.getDevice(this);
ledWorker.activate();
ledWorker.start();
} catch (DeviceNotFoundException e) {
wiimoteDiscovery.activate();
}
@@ -174,11 +172,7 @@ public class WiimoteDevice extends Device implements GestureListener {
public void connected() {
try {
wiimote = wiimoteService.getDevice(this);
try {
wiimoteDiscovery.deactivate();
} catch (DeactivateException e) {
log.error(e);
}
wiimoteDiscovery.stop();
} catch (DeviceNotFoundException e) {
log.error(e);
}
@@ -238,13 +232,14 @@ public class WiimoteDevice extends Device implements GestureListener {
ledCycle.add(3);
}
public void deactivate() throws DeactivateException {
public void deactivate() throws DeactivateException {
super.deactivate();
setLeds(1);
}
protected void work() {
setLeds(ledCycle.next());
setLeds(ledCycle.next());
sleep(LED_SLEEP);
}
protected void setLeds(int leds) {

View File

@@ -8,40 +8,30 @@ import mimis.Worker;
public abstract class EventListener extends Worker {
protected Queue<Event> eventQueue;
protected Object work;
public EventListener() {
eventQueue = new ConcurrentLinkedQueue<Event>();
work = new Object();
}
public void add(Event event) {
log.debug("[EventListener] Add event:" + event + " " + event.getTarget());
eventQueue.add(event);
synchronized (work) {
work.notifyAll();
synchronized (this) {
notifyAll();
}
}
public final void work() {
while (eventQueue.isEmpty()) {
synchronized (work) {
try {
work.wait();
} catch (InterruptedException e) {}
if (!run) {
return;
}
while (!eventQueue.isEmpty()) {
event(eventQueue.poll());
}
synchronized (this) {
try {
wait();
} catch (InterruptedException e) {
log.info(e);
}
}
event(eventQueue.poll());
}
public void stop() {
synchronized (work) {
work.notifyAll();
}
super.stop();
}
public abstract void event(Event event);

View File

@@ -22,7 +22,7 @@ public class GlobalRouter extends EventRouter {
this.port = port;
}
public void activate() throws ActivateException {
protected void activate() throws ActivateException {
try {
client = new Client(ip, port);
} catch (IOException e) {
@@ -32,7 +32,8 @@ public class GlobalRouter extends EventRouter {
super.activate();
}
public void deactivate() throws DeactivateException {
protected void deactivate() throws DeactivateException {
super.deactivate();
client.stop();
}
@@ -71,7 +72,8 @@ public class GlobalRouter extends EventRouter {
}
}
public void stop() {
protected void deactivate() throws DeactivateException {
super.deactivate();
try {
objectInputStream.close();
objectOutputStream.close();

View File

@@ -1,6 +0,0 @@
package mimis.manager;
public interface Exitable {
public void stop();
}

View File

@@ -7,9 +7,6 @@ import javax.swing.Action;
import javax.swing.JToggleButton;
import mimis.Worker;
import mimis.exception.worker.ActivateException;
import mimis.exception.worker.DeactivateException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
@@ -29,19 +26,11 @@ public class ManageButton<T extends Worker & Titled> extends JToggleButton imple
public void mouseClicked(MouseEvent event) {
if (manageable.active()) {
try {
log.trace("Uit");
manageable.deactivate();
} catch (DeactivateException e) {
log.error(e);
}
log.trace("Stop");
manageable.stop();
} else {
try {
log.trace("Aan");
manageable.activate();
} catch (ActivateException e) {
log.error(e);
}
log.trace("Start");
manageable.start();
}
}

View File

@@ -1,8 +1,6 @@
package mimis.util;
import mimis.Worker;
import mimis.exception.worker.ActivateException;
import mimis.exception.worker.DeactivateException;
import mimis.util.multiplexer.SignalListener;
import mimis.value.Signal;
@@ -27,11 +25,7 @@ public class Multiplexer extends Worker {
signalListener.add(Signal.BEGIN, object);
this.object = object;
end = true;
try {
activate();
} catch (ActivateException e) {
log.error(e);
}
start();
} else if (this.object.equals(object)) {
end = false;
notifyAll();
@@ -53,11 +47,7 @@ public class Multiplexer extends Worker {
if (end) {
signalListener.add(Signal.END, object);
object = null;
try {
deactivate();
} catch (DeactivateException e) {
log.error(e);
}
stop();
}
end = !end;
}

View File

@@ -136,7 +136,7 @@ public class Sound {
cancel();
object.notifyAll();
}
}
}
};
timer.scheduleAtFixedRate(timerTask, 0, step);
}

View File

@@ -0,0 +1,33 @@
package mimis.worker;
import mimis.Worker;
public class Periodic extends Worker {
public Periodic() {
}
protected void work() {
}
public void test() {
Worker worker = new Worker() {
protected void work() {
log.debug("work()");
sleep();
}
};
worker.start();
sleep(1000);
worker.stop();
sleep(1000);
worker.start();
worker.start();
}
public static void main(String[] args) {
new Periodic().test();
}
}