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.EventHandler;
import mimis.event.Task; import mimis.event.Task;
import mimis.event.feedback.TextFeedback; import mimis.event.feedback.TextFeedback;
import mimis.exception.WorkerException;
import mimis.exception.worker.DeactivateException;
import mimis.manager.Exitable;
import mimis.manager.Titled; import mimis.manager.Titled;
import mimis.value.Action; import mimis.value.Action;
import mimis.value.Signal; 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 String title;
protected boolean active; protected boolean active;
@@ -27,18 +24,14 @@ public abstract class Application extends EventHandler implements Titled, Exitab
Task task = (Task) event; Task task = (Task) event;
Action action = task.getAction(); Action action = task.getAction();
switch (action) { switch (action) {
case ACTIVATE: case START:
if (task.getSignal().equals(Signal.BEGIN)) { if (task.getSignal().equals(Signal.BEGIN)) {
try { if (active()) {
if (active()) { eventRouter.add(new TextFeedback("Stop application"));
eventRouter.add(new TextFeedback("Deactivate application")); stop();
deactivate(); } else {
} else { eventRouter.add(new TextFeedback("Start application"));
eventRouter.add(new TextFeedback("Activate application")); start();
activate();
}
} catch (WorkerException e) {
log.error(e);
} }
} }
return; return;
@@ -46,15 +39,4 @@ public abstract class Application extends EventHandler implements Titled, Exitab
} }
super.event(event); 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.device.wiimote.WiimoteDevice;
import mimis.event.EventRouter; import mimis.event.EventRouter;
import mimis.event.router.GlobalRouter; import mimis.event.router.GlobalRouter;
import mimis.exception.worker.ActivateException;
import mimis.util.swing.Dialog; import mimis.util.swing.Dialog;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
@@ -39,11 +38,7 @@ public class Client {
public void start() { public void start() {
log.debug("Client"); log.debug("Client");
Mimis mimis = new Mimis(eventRouter, deviceArray); Mimis mimis = new Mimis(eventRouter, deviceArray);
try { mimis.start();
mimis.activate();
} catch (ActivateException e) {
log.fatal(e);
}
} }
public static void main(String[] args) { public static void main(String[] args) {

View File

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

View File

@@ -79,7 +79,7 @@ public class GUI extends JFrame {
protected void processWindowEvent(WindowEvent event) { protected void processWindowEvent(WindowEvent event) {
if (event.getID() == WindowEvent.WINDOW_CLOSING) { if (event.getID() == WindowEvent.WINDOW_CLOSING) {
log.debug("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.device.wiimote.WiimoteDevice;
import mimis.event.EventRouter; import mimis.event.EventRouter;
import mimis.event.router.LocalRouter; import mimis.event.router.LocalRouter;
import mimis.exception.worker.ActivateException;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
@@ -53,11 +52,7 @@ public class Main {
public void start() { public void start() {
log.debug("Main"); log.debug("Main");
Mimis mimis = new Mimis(eventRouter, applicationArray, deviceArray); Mimis mimis = new Mimis(eventRouter, applicationArray, deviceArray);
try { mimis.start();
mimis.activate();
} catch (ActivateException e) {
log.fatal(e);
}
} }
public static void main(String[] args) { public static void main(String[] args) {

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@@ -32,8 +32,8 @@ public class LircDevice extends Device implements LircButtonListener, SignalList
eventMapCycle = new LircEventMapCycle(); eventMapCycle = new LircEventMapCycle();
} }
public void activate() throws ActivateException { protected void activate() throws ActivateException {
lircService.activate(); lircService.start();
add(eventMapCycle.denonRC176); add(eventMapCycle.denonRC176);
add(eventMapCycle.philiphsRCLE011); add(eventMapCycle.philiphsRCLE011);
add(eventMapCycle.samsungBN5901015A); add(eventMapCycle.samsungBN5901015A);
@@ -42,27 +42,27 @@ public class LircDevice extends Device implements LircButtonListener, SignalList
public boolean active() { public boolean active() {
if (active && !lircService.active()) { if (active && !lircService.active()) {
try { stop();
deactivate();
} catch (DeactivateException e) {
log.error(e);
}
} else if (!active) { } else if (!active) {
if (Native.isRunning(PROGRAM)) { if (Native.isRunning(PROGRAM)) {
try { start();
activate();
} catch (ActivateException e) {
log.error(e);
}
} }
} }
return active; return active;
} }
public void deactivate() throws DeactivateException { protected void deactivate() throws DeactivateException {
log.debug("Deactivate LircDevice"); log.debug("Deactivate LircDevice");
super.deactivate(); 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) { public void add(LircButton lircButton) {
@@ -79,11 +79,4 @@ public class LircDevice extends Device implements LircButtonListener, SignalList
break; 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(); super.activate();
} }
public boolean active() { public synchronized boolean active() {
if (active && !socket.isConnected()) { if (active && !socket.isConnected()) {
active = false; active = false;
} }
return active; return active;
} }
public void deactivate() throws DeactivateException { public synchronized void deactivate() throws DeactivateException {
log.trace("Deactivate LircService"); log.trace("Deactivate LircService");
super.deactivate(); super.deactivate();
try { try {

View File

@@ -14,7 +14,7 @@ public class DenonRC176EventMap extends EventMap {
add(DenonRC176Button.TUNER_DOWN, new Task(Target.MIMIS, Action.PREVIOUS)); add(DenonRC176Button.TUNER_DOWN, new Task(Target.MIMIS, Action.PREVIOUS));
/* Application */ /* 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_NEXT, new Task(Target.APPLICATION, Action.NEXT));
add(DenonRC176Button.CD_PREVIOUS, new Task(Target.APPLICATION, Action.PREVIOUS)); add(DenonRC176Button.CD_PREVIOUS, new Task(Target.APPLICATION, Action.PREVIOUS));
add(DenonRC176Button.TAPE_REWIND, new Task(Target.APPLICATION, Action.REWIND)); 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)); add(PhiliphsRCLE011Button.DOWN, new Task(Target.MIMIS, Action.PREVIOUS));
/* Application */ /* 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_UP, new Task(Target.APPLICATION, Action.NEXT));
add(PhiliphsRCLE011Button.PROGRAM_DOWN, new Task(Target.APPLICATION, Action.PREVIOUS)); add(PhiliphsRCLE011Button.PROGRAM_DOWN, new Task(Target.APPLICATION, Action.PREVIOUS));
add(PhiliphsRCLE011Button.LEFT, new Task(Target.APPLICATION, Action.REWIND)); 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; public static final int PORT = 6789;
protected Log log = LogFactory.getLog(NetworkDevice.class); protected Log log = LogFactory.getLog(NetworkDevice.class);
protected int port;
protected Server server; protected Server server;
protected ConcurrentLinkedQueue<Client> clientList; protected ConcurrentLinkedQueue<Client> clientList;
public NetworkDevice(int port) { public NetworkDevice(int port) {
super(TITLE); super(TITLE);
this.port = port;
clientList = new ConcurrentLinkedQueue<Client>(); clientList = new ConcurrentLinkedQueue<Client>();
server = new Server(port);
} }
public NetworkDevice() { public NetworkDevice() {
this(PORT); this(PORT);
} }
public void activate() throws ActivateException { protected void activate() throws ActivateException {
log.trace("Activate NetworkDevice"); server.start();
try {
server = new Server(port);
server.activate();
} catch (IOException e) {
throw new ActivateException();
}
super.activate(); super.activate();
} }
@@ -59,14 +52,19 @@ public class NetworkDevice extends Device {
client.stop(); 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(); super.deactivate();
server.stop(); server.stop();
} }
public synchronized void exit() {
super.exit();
server.exit();
}
protected void feedback(Feedback feedback) { protected void feedback(Feedback feedback) {
for (Client client : clientList) { for (Client client : clientList) {
client.send(feedback); client.send(feedback);
@@ -75,40 +73,55 @@ public class NetworkDevice extends Device {
protected class Server extends Worker { protected class Server extends Worker {
protected ServerSocket serverSocket; protected ServerSocket serverSocket;
protected int port;
public Server(int port) throws IOException { public Server(int port) {
serverSocket = new ServerSocket(port); this.port = port;
eventRouter.add(new TextFeedback("[NetworkDevice] Wating for clients"));
} }
public boolean active() { protected void activate() throws ActivateException {
return active = !serverSocket.isClosed(); 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() { public void work() {
try { try {
eventRouter.add(new TextFeedback("[NetworkDevice] Wating for clients"));
Socket socket = serverSocket.accept(); Socket socket = serverSocket.accept();
Client client = new Client(socket); Client client = new Client(socket);
try { client.start();
client.activate();
} catch (ActivateException e) {
log.error(e);
}
eventRouter.add(new TextFeedback("[NetworkDevice] Client connected: " + socket.getInetAddress())); eventRouter.add(new TextFeedback("[NetworkDevice] Client connected: " + socket.getInetAddress()));
} catch (IOException e) { } catch (IOException e) {
log.error(e); log.error(e);
} }
} }
public void stop() { public synchronized void exit() {
super.stop(); super.exit();
try {
serverSocket.close();
} catch (IOException e) {
log.error(e);
}
for (Client client : clientList) { for (Client client : clientList) {
client.stop(); client.exit();
} }
} }
} }
@@ -151,8 +164,8 @@ public class NetworkDevice extends Device {
} }
} }
public void stop() { protected void deactivate() throws DeactivateException {
super.stop(); super.deactivate();
send(new Task(Target.SELF, Action.STOP)); send(new Task(Target.SELF, Action.STOP));
clientList.remove(this); clientList.remove(this);
try { try {

View File

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

View File

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

View File

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

View File

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

View File

@@ -22,7 +22,7 @@ public class GlobalRouter extends EventRouter {
this.port = port; this.port = port;
} }
public void activate() throws ActivateException { protected void activate() throws ActivateException {
try { try {
client = new Client(ip, port); client = new Client(ip, port);
} catch (IOException e) { } catch (IOException e) {
@@ -32,7 +32,8 @@ public class GlobalRouter extends EventRouter {
super.activate(); super.activate();
} }
public void deactivate() throws DeactivateException { protected void deactivate() throws DeactivateException {
super.deactivate();
client.stop(); client.stop();
} }
@@ -71,7 +72,8 @@ public class GlobalRouter extends EventRouter {
} }
} }
public void stop() { protected void deactivate() throws DeactivateException {
super.deactivate();
try { try {
objectInputStream.close(); objectInputStream.close();
objectOutputStream.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 javax.swing.JToggleButton;
import mimis.Worker; import mimis.Worker;
import mimis.exception.worker.ActivateException;
import mimis.exception.worker.DeactivateException;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; 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) { public void mouseClicked(MouseEvent event) {
if (manageable.active()) { if (manageable.active()) {
try { log.trace("Stop");
log.trace("Uit"); manageable.stop();
manageable.deactivate();
} catch (DeactivateException e) {
log.error(e);
}
} else { } else {
try { log.trace("Start");
log.trace("Aan"); manageable.start();
manageable.activate();
} catch (ActivateException e) {
log.error(e);
}
} }
} }

View File

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

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();
}
}