diff --git a/java/.classpath b/java/.classpath index a949716..4fec3f4 100644 --- a/java/.classpath +++ b/java/.classpath @@ -10,5 +10,6 @@ + diff --git a/java/cfg/log4j.properties b/java/cfg/log4j.properties index f8a8ad4..01e7022 100644 --- a/java/cfg/log4j.properties +++ b/java/cfg/log4j.properties @@ -1,4 +1,8 @@ log4j.rootLogger=TRACE, CA log4j.appender.CA=org.apache.log4j.ConsoleAppender log4j.appender.CA.layout=org.apache.log4j.PatternLayout -log4j.appender.CA.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n \ No newline at end of file +log4j.appender.CA.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n +log4j.appender.CUSTOM=test.CustomAppender +log4j.appender.CUSTOM.Bla=1234 +log4j.appender.CUSTOM.layout=org.apache.log4j.PatternLayout +log4j.appender.CUSTOM.layout.ConversionPattern=[%d{MMM dd HH:mm:ss}] %-5p (%F:%L) - %m%n \ No newline at end of file diff --git a/java/client.jar b/java/client.jar index 86ea2c8..a16b1bd 100644 Binary files a/java/client.jar and b/java/client.jar differ diff --git a/java/main.jar b/java/main.jar index 4c506e7..ba79a74 100644 Binary files a/java/main.jar and b/java/main.jar differ diff --git a/java/mimis.exe b/java/mimis.exe new file mode 100644 index 0000000..93a6acf Binary files /dev/null and b/java/mimis.exe differ diff --git a/java/src/mimis/Client.java b/java/src/mimis/Client.java index e80edf4..c784918 100644 --- a/java/src/mimis/Client.java +++ b/java/src/mimis/Client.java @@ -1,24 +1,25 @@ -package mimis; - -import mimis.exception.worker.ActivateException; -import mimis.util.swing.Dialog; - -public class Client extends Main { - public static final String IP = "127.0.0.1"; - public static final int PORT = 6789; - - public Client(String ip, int port) { - super(); - //eventRouter = new GlobalRouter(ip, port); - } - - public void activate() throws ActivateException { - super.activate(); - } - - public static void main(String[] args) { - String ip = Dialog.question("Server IP:", IP); - int port = Integer.valueOf(Dialog.question("Server Port:", PORT)); - new Client(ip, port).start(); - } +package mimis; + +import mimis.exception.worker.ActivateException; +import mimis.router.GlobalRouter; +import mimis.util.swing.Dialog; + +public class Client extends Main { + public static final String IP = "127.0.0.1"; + public static final int PORT = 6789; + + public Client(String ip, int port) { + super(); + router = new GlobalRouter(ip, port); + } + + public void activate() throws ActivateException { + super.activate(); + } + + public static void main(String[] args) { + String ip = Dialog.question("Server IP:", IP); + int port = Integer.valueOf(Dialog.question("Server Port:", PORT)); + new Client(ip, port).start(); + } } \ No newline at end of file diff --git a/java/src/mimis/Main.java b/java/src/mimis/Main.java index c514c8a..fa2d92f 100644 --- a/java/src/mimis/Main.java +++ b/java/src/mimis/Main.java @@ -5,8 +5,6 @@ import java.util.ServiceLoader; import javax.swing.UIManager; -import mimis.application.Application; -import mimis.device.Device; import mimis.exception.worker.ActivateException; import mimis.exception.worker.DeactivateException; import mimis.input.Task; @@ -28,20 +26,18 @@ public class Main extends Mimis { } public static Component[] getApplications() { - ArrayList componentList = new ArrayList(); - for (Application application : ServiceLoader.load(mimis.application.Application.class)) { - if (application instanceof Component) { - componentList.add((Component) application); - } - } - return componentList.toArray(new Component[]{}); + return getComponents(mimis.application.Application.class); } public static Component[] getDevices() { + return getComponents(mimis.device.Device.class); + } + + public static Component[] getComponents(Class clazz) { ArrayList componentList = new ArrayList(); - for (Device device : ServiceLoader.load(mimis.device.Device.class)) { - if (device instanceof Component) { - componentList.add((Component) device); + for (Object object : ServiceLoader.load(clazz)) { + if (object instanceof Component) { + componentList.add((Component) object); } } return componentList.toArray(new Component[]{}); @@ -95,6 +91,8 @@ public class Main extends Mimis { case PREVIOUS: applicationManager.currentChanged(); break; + default: + break; } } diff --git a/java/src/mimis/Mimis.java b/java/src/mimis/Mimis.java index 216c95f..3185c4f 100644 --- a/java/src/mimis/Mimis.java +++ b/java/src/mimis/Mimis.java @@ -1,79 +1,81 @@ -package mimis; - -import mimis.exception.worker.ActivateException; -import mimis.exception.worker.DeactivateException; -import mimis.input.Feedback; -import mimis.input.Task; -import mimis.manager.Manager; -import mimis.parser.Parser; -import mimis.router.Router; -import mimis.util.ArrayCycle; -import mimis.value.Action; -import mimis.value.Target; -import mimis.worker.Component; - -public abstract class Mimis extends Component { - protected Component[] currentArray; - protected Manager manager; - - protected ArrayCycle componentCycle; - - public Mimis(Component... currentArray) { - this.currentArray = initialize(false, currentArray); - componentCycle = new ArrayCycle(currentArray); - router = new Router(); - manager = new Manager(initialize(true, router, new Parser())); - } - - public void activate() throws ActivateException { - manager.start(); - super.activate(); - } - - protected void deactivate() throws DeactivateException { - super.deactivate(); - manager.stop(); - } - - public void exit() { - super.exit(); - manager.exit(); - } - - public Component[] initialize(boolean start, Component... componentArray) { - for (Component component : componentArray) { - component.setRouter(router); - if (start) { - component.start(); - } - } - return componentArray; - } - - public void task(Task task) { - if (task.getTarget().equals(Target.CURRENT)) { - componentCycle.current().add(task); - } else { - super.task(task); - } - } - - public void end(Action action) { - switch (action) { - case CURRENT: - route(new Feedback("Current component: " + componentCycle.current().getTitle())); - break; - case NEXT: - log.debug("Next component"); - route(new Feedback("Next component: " + componentCycle.next().getTitle())); - break; - case PREVIOUS: - log.debug("Previous component"); - route(new Feedback("Previous component: " + componentCycle.previous().getTitle())); - break; - case EXIT: - exit(); - break; - } - } -} +package mimis; + +import mimis.exception.worker.ActivateException; +import mimis.exception.worker.DeactivateException; +import mimis.input.Feedback; +import mimis.input.Task; +import mimis.manager.Manager; +import mimis.parser.Parser; +import mimis.router.Router; +import mimis.util.ArrayCycle; +import mimis.value.Action; +import mimis.value.Target; +import mimis.worker.Component; + +public abstract class Mimis extends Component { + protected Component[] currentArray; + protected Manager manager; + + protected ArrayCycle componentCycle; + + public Mimis(Component... currentArray) { + this.currentArray = initialize(false, currentArray); + componentCycle = new ArrayCycle(currentArray); + router = new Router(); + manager = new Manager(initialize(true, router, new Parser())); + } + + public void activate() throws ActivateException { + manager.start(); + super.activate(); + } + + protected void deactivate() throws DeactivateException { + super.deactivate(); + manager.stop(); + } + + public void exit() { + super.exit(); + manager.exit(); + } + + public Component[] initialize(boolean start, Component... componentArray) { + for (Component component : componentArray) { + component.setRouter(router); + if (start) { + component.start(); + } + } + return componentArray; + } + + public void task(Task task) { + if (task.getTarget().equals(Target.CURRENT)) { + componentCycle.current().add(task); + } else { + super.task(task); + } + } + + public void end(Action action) { + switch (action) { + case CURRENT: + route(new Feedback("Current component: " + componentCycle.current().getTitle())); + break; + case NEXT: + log.debug("Next component"); + route(new Feedback("Next component: " + componentCycle.next().getTitle())); + break; + case PREVIOUS: + log.debug("Previous component"); + route(new Feedback("Previous component: " + componentCycle.previous().getTitle())); + break; + case EXIT: + exit(); + break; + default: + break; + } + } +} \ No newline at end of file diff --git a/java/src/mimis/application/itunes/iTunesApplication.java b/java/src/mimis/application/itunes/iTunesApplication.java index 1ee756e..3b3de7f 100644 --- a/java/src/mimis/application/itunes/iTunesApplication.java +++ b/java/src/mimis/application/itunes/iTunesApplication.java @@ -1,177 +1,177 @@ -package mimis.application.itunes; - -import mimis.application.Application; -import mimis.exception.worker.ActivateException; -import mimis.exception.worker.DeactivateException; -import mimis.value.Action; -import mimis.worker.Component; -import mimis.worker.Worker; - -import com.dt.iTunesController.ITCOMDisabledReason; -import com.dt.iTunesController.ITTrack; -import com.dt.iTunesController.iTunes; -import com.dt.iTunesController.iTunesEventsInterface; - -public class iTunesApplication extends Component implements Application, iTunesEventsInterface { - protected static final String TITLE = "iTunes"; - protected static final boolean EVENTS = false; - - protected static final int VOLUME_CHANGE_RATE = 5; - protected static final int VOLUME_SLEEP = 100; - protected static final String PLAYLIST_LIKE = "Like"; - protected static final String PLAYLIST_DISLIKE = "Dislike"; - - protected iTunes iTunes; - protected VolumeWorker volumeWorker; - protected boolean events; - - public iTunesApplication() { - this(EVENTS); - } - - public iTunesApplication(boolean events) { - super(TITLE); - this.events = events; - volumeWorker = new VolumeWorker(); - } - - protected synchronized void activate() throws ActivateException { - iTunes = new iTunes(); - iTunes.connect(); - if (events) { - iTunes.addEventHandler(this); - } - super.activate(); - } - - public synchronized boolean active() { - try { - iTunes.getMute(); - active = true; - } catch (Exception e) { - active = false; - } - return active; - } - - protected synchronized void deactivate() throws DeactivateException { - if (events) { - exit(); - } else { - super.deactivate(); - volumeWorker.stop(); - try { - iTunes.release(); - } catch (Exception e) { - log.error(e); - throw new DeactivateException(); - } - } - } - - public synchronized void exit() { - try { - iTunes.quit(); - } catch (Exception e) {} - volumeWorker.exit(); - super.exit(); - } - - protected void begin(Action action) { - log.trace("iTunesApplication begin: " + action); - if (!active) return; - switch (action) { - case FORWARD: - iTunes.fastForward(); - break; - case REWIND: - iTunes.rewind(); - break; - case VOLUME_UP: - volumeWorker.start(VOLUME_CHANGE_RATE); - break; - case VOLUME_DOWN: - volumeWorker.start(-VOLUME_CHANGE_RATE); - break; - } - } - - protected void end(Action action) { - log.trace("iTunesApplication end: " + action); - if (!active) return; - switch (action) { - case PLAY: - iTunes.playPause(); - break; - case NEXT: - iTunes.nextTrack(); - break; - case PREVIOUS: - iTunes.previousTrack(); - break; - case FORWARD: - iTunes.resume(); - break; - case REWIND: - iTunes.resume(); - break; - case MUTE: - iTunes.toggleMute(); - break; - case VOLUME_UP: - case VOLUME_DOWN: - volumeWorker.stop(); - break; - case SHUFFLE: - iTunes.toggleShuffle(); - break; - case REPEAT: - iTunes.cycleSongRepeat(); - break; - case LIKE: - iTunes.playlistAddCurrentTrack(PLAYLIST_LIKE); - break; - case DISLIKE: - iTunes.playlistAddCurrentTrack(PLAYLIST_DISLIKE); - break; - } - } - - protected int getVolume() { - return iTunes.getSoundVolume(); - } - - public void onDatabaseChangedEvent(int[][] deletedObjectIDs, int[][] changedObjectIDs) {} - public void onPlayerPlayEvent(ITTrack iTrack) { - if (active) { - log.trace("iTunesEvent: play"); - } - } - - public void onPlayerStopEvent(ITTrack iTrack) { - if (active) { - log.trace("iTunesEvent: stop"); - } - } - - public void onPlayerPlayingTrackChangedEvent(ITTrack iTrack) {} - public void onCOMCallsDisabledEvent(ITCOMDisabledReason reason) {} - public void onCOMCallsEnabledEvent() {} - public void onQuittingEvent() {} - public void onAboutToPromptUserToQuitEvent() {} - public void onSoundVolumeChangedEvent(int newVolume) {} - - protected class VolumeWorker extends Worker { - protected int volumeChangeRate; - - public void start(int volumeChangeRate) { - super.start(); - this.volumeChangeRate = volumeChangeRate; - } - - public void work() { - iTunes.setSoundVolume(getVolume() + volumeChangeRate); - sleep(VOLUME_SLEEP); - } - }; +package mimis.application.itunes; + +import mimis.application.Application; +import mimis.exception.worker.ActivateException; +import mimis.exception.worker.DeactivateException; +import mimis.value.Action; +import mimis.worker.Component; +import mimis.worker.Worker; + +import com.dt.iTunesController.ITCOMDisabledReason; +import com.dt.iTunesController.ITTrack; +import com.dt.iTunesController.iTunes; +import com.dt.iTunesController.iTunesEventsInterface; + +public class iTunesApplication extends Component implements Application, iTunesEventsInterface { + protected static final String TITLE = "iTunes"; + protected static final boolean EVENTS = false; + + protected static final int VOLUME_CHANGE_RATE = 5; + protected static final int VOLUME_SLEEP = 100; + protected static final String PLAYLIST_LIKE = "Like"; + protected static final String PLAYLIST_DISLIKE = "Dislike"; + + protected iTunes iTunes; + protected VolumeWorker volumeWorker; + protected boolean events; + + public iTunesApplication() { + this(EVENTS); + } + + public iTunesApplication(boolean events) { + super(TITLE); + this.events = events; + volumeWorker = new VolumeWorker(); + } + + protected synchronized void activate() throws ActivateException { + iTunes = new iTunes(); + iTunes.connect(); + if (events) { + iTunes.addEventHandler(this); + } + super.activate(); + } + + public synchronized boolean active() { + try { + iTunes.getMute(); + active = true; + } catch (Exception e) { + active = false; + } + return active; + } + + protected synchronized void deactivate() throws DeactivateException { + if (events) { + exit(); + } else { + super.deactivate(); + volumeWorker.stop(); + try { + iTunes.release(); + } catch (Exception e) { + log.error(e); + throw new DeactivateException(); + } + } + } + + public synchronized void exit() { + try { + iTunes.quit(); + } catch (Exception e) {} + volumeWorker.exit(); + super.exit(); + } + + protected void begin(Action action) { + log.trace("iTunesApplication begin: " + action); + if (!active) return; + switch (action) { + case FORWARD: + iTunes.fastForward(); + break; + case REWIND: + iTunes.rewind(); + break; + case VOLUME_UP: + volumeWorker.start(VOLUME_CHANGE_RATE); + break; + case VOLUME_DOWN: + volumeWorker.start(-VOLUME_CHANGE_RATE); + break; + } + } + + protected void end(Action action) { + log.trace("iTunesApplication end: " + action); + if (!active) return; + switch (action) { + case PLAY: + iTunes.playPause(); + break; + case NEXT: + iTunes.nextTrack(); + break; + case PREVIOUS: + iTunes.previousTrack(); + break; + case FORWARD: + iTunes.resume(); + break; + case REWIND: + iTunes.resume(); + break; + case MUTE: + iTunes.toggleMute(); + break; + case VOLUME_UP: + case VOLUME_DOWN: + volumeWorker.stop(); + break; + case SHUFFLE: + iTunes.toggleShuffle(); + break; + case REPEAT: + iTunes.cycleSongRepeat(); + break; + case LIKE: + iTunes.playlistAddCurrentTrack(PLAYLIST_LIKE); + break; + case DISLIKE: + iTunes.playlistAddCurrentTrack(PLAYLIST_DISLIKE); + break; + } + } + + protected int getVolume() { + return iTunes.getSoundVolume(); + } + + public void onDatabaseChangedEvent(int[][] deletedObjectIDs, int[][] changedObjectIDs) {} + public void onPlayerPlayEvent(ITTrack iTrack) { + if (active) { + log.trace("iTunesEvent: play"); + } + } + + public void onPlayerStopEvent(ITTrack iTrack) { + if (active) { + log.trace("iTunesEvent: stop"); + } + } + + public void onPlayerPlayingTrackChangedEvent(ITTrack iTrack) {} + public void onCOMCallsDisabledEvent(ITCOMDisabledReason reason) {} + public void onCOMCallsEnabledEvent() {} + public void onQuittingEvent() {} + public void onAboutToPromptUserToQuitEvent() {} + public void onSoundVolumeChangedEvent(int newVolume) {} + + protected class VolumeWorker extends Worker { + protected int volumeChangeRate; + + public void start(int volumeChangeRate) { + super.start(); + this.volumeChangeRate = volumeChangeRate; + } + + public void work() { + iTunes.setSoundVolume(getVolume() + volumeChangeRate); + sleep(VOLUME_SLEEP); + } + }; } \ No newline at end of file diff --git a/java/src/mimis/manager/ButtonManager.java b/java/src/mimis/manager/ButtonManager.java index 884fc84..eb766aa 100644 --- a/java/src/mimis/manager/ButtonManager.java +++ b/java/src/mimis/manager/ButtonManager.java @@ -1,81 +1,76 @@ -package mimis.manager; - -import java.awt.GridBagConstraints; -import java.awt.GridBagLayout; -import java.util.Calendar; -import java.util.HashMap; -import java.util.Map; - -import javax.swing.JPanel; -import javax.swing.JToggleButton; -import javax.swing.border.TitledBorder; - -import mimis.worker.Worker; - -public class ButtonManager extends Manager { - protected static final String TITLE = "Workers"; - - protected String title; - protected Map buttonMap; - - public ButtonManager(Worker... workerArray) { - this(TITLE, workerArray); - } - - public ButtonManager(String title, Worker... workerArray) { - super(workerArray); - this.title = title; - createButtons(); - } - - public String getTitle() { - return title; - } - - public WorkerButton[] getButtons() { - return buttonMap.values().toArray(new WorkerButton[]{}); - } - - protected void createButtons() { - buttonMap = new HashMap(); - for (Worker worker : workerList) { - WorkerButton button = new WorkerButton(worker); - buttonMap.put(worker, button); - } - } - - public JPanel createPanel() { - /* Initialize components */ - GridBagLayout gridBagLayout = new GridBagLayout(); - GridBagConstraints gridBagConstraints = new GridBagConstraints(); - JPanel panel = new JPanel(gridBagLayout); - - /* Set border */ - TitledBorder border = new TitledBorder(getTitle()); - border.setTitleJustification(TitledBorder.CENTER); - panel.setBorder(border); - - /* Initialize constraints */ - gridBagConstraints.fill = GridBagConstraints.BOTH; - gridBagConstraints.gridwidth = GridBagConstraints.REMAINDER; - gridBagConstraints.weightx = 1; - gridBagConstraints.weighty = 1; - - /* Add buttons */ - for (JToggleButton button : getButtons()) { - gridBagLayout.setConstraints(button, gridBagConstraints); - panel.add(button); - } - return panel; - } - - protected void work() { - long before = Calendar.getInstance().getTimeInMillis(); - for (Worker worker : workerList) { - buttonMap.get(worker).setPressed(worker.active()); - } - long after = Calendar.getInstance().getTimeInMillis(); - int sleep = INTERVAL - (int) (after - before); - sleep(sleep); - } -} +package mimis.manager; + +import java.awt.GridBagConstraints; +import java.awt.GridBagLayout; +import java.util.HashMap; +import java.util.Map; + +import javax.swing.JPanel; +import javax.swing.JToggleButton; +import javax.swing.border.TitledBorder; + +import mimis.worker.Worker; + +public class ButtonManager extends Manager { + protected static final String TITLE = "Workers"; + + protected String title; + protected Map buttonMap; + + public ButtonManager(Worker... workerArray) { + this(TITLE, workerArray); + } + + public ButtonManager(String title, Worker... workerArray) { + super(workerArray); + this.title = title; + createButtons(); + } + + public String getTitle() { + return title; + } + + public WorkerButton[] getButtons() { + return buttonMap.values().toArray(new WorkerButton[]{}); + } + + protected void createButtons() { + buttonMap = new HashMap(); + for (Worker worker : workerList) { + WorkerButton button = new WorkerButton(worker); + buttonMap.put(worker, button); + } + } + + public JPanel createPanel() { + /* Initialize components */ + GridBagLayout gridBagLayout = new GridBagLayout(); + GridBagConstraints gridBagConstraints = new GridBagConstraints(); + JPanel panel = new JPanel(gridBagLayout); + + /* Set border */ + TitledBorder border = new TitledBorder(getTitle()); + border.setTitleJustification(TitledBorder.CENTER); + panel.setBorder(border); + + /* Initialize constraints */ + gridBagConstraints.fill = GridBagConstraints.BOTH; + gridBagConstraints.gridwidth = GridBagConstraints.REMAINDER; + gridBagConstraints.weightx = 1; + gridBagConstraints.weighty = 1; + + /* Add buttons */ + for (JToggleButton button : getButtons()) { + gridBagLayout.setConstraints(button, gridBagConstraints); + panel.add(button); + } + return panel; + } + + protected void work() { + for (Worker worker : workerList) { + buttonMap.get(worker).setPressed(worker.active()); + } + } +} diff --git a/java/src/mimis/manager/Manager.java b/java/src/mimis/manager/Manager.java index ee50661..f1d32c1 100644 --- a/java/src/mimis/manager/Manager.java +++ b/java/src/mimis/manager/Manager.java @@ -1,55 +1,51 @@ -package mimis.manager; - -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Calendar; - -import mimis.exception.worker.DeactivateException; -import mimis.worker.Worker; - -public class Manager extends Worker { - protected static final int INTERVAL = 1000; - - protected ArrayList workerList; - - public Manager(Worker... workerArray) { - workerList = new ArrayList(); - add(workerArray); - } - - public void add(Worker... workerArray) { - workerList.addAll(Arrays.asList(workerArray)); - } - - public void remove(Worker... workerArray) { - workerList.removeAll(Arrays.asList(workerArray)); - } - - protected void deactivate() throws DeactivateException { - super.deactivate(); - for (Worker worker : workerList) { - worker.stop(); - } - } - - public void exit() { - super.exit(); +package mimis.manager; + +import java.util.ArrayList; +import java.util.Arrays; + +import mimis.exception.worker.DeactivateException; +import mimis.worker.IntervalWorker; +import mimis.worker.Worker; + +public class Manager extends IntervalWorker { + protected static final int INTERVAL = 1000; + + protected ArrayList workerList; + + public Manager(Worker... workerArray) { + workerList = new ArrayList(); + add(workerArray); + } + + public void add(Worker... workerArray) { + workerList.addAll(Arrays.asList(workerArray)); + } + + public void remove(Worker... workerArray) { + workerList.removeAll(Arrays.asList(workerArray)); + } + + protected void deactivate() throws DeactivateException { + super.deactivate(); for (Worker worker : workerList) { - worker.exit(); - } - } - - public int count() { - return workerList.size(); - } - - protected void work() { - long before = Calendar.getInstance().getTimeInMillis(); - for (Worker worker : workerList) { - worker.active(); - } - long after = Calendar.getInstance().getTimeInMillis(); - int sleep = INTERVAL - (int) (after - before); - sleep(sleep); - } -} + worker.stop(); + } + } + + public void exit() { + super.exit(); + for (Worker worker : workerList) { + worker.exit(); + } + } + + public int count() { + return workerList.size(); + } + + protected void work() { + for (Worker worker : workerList) { + worker.active(); + } + } +} diff --git a/java/src/mimis/router/GlobalRouter.java b/java/src/mimis/router/GlobalRouter.java index 5d3a2b7..212595e 100644 --- a/java/src/mimis/router/GlobalRouter.java +++ b/java/src/mimis/router/GlobalRouter.java @@ -1,90 +1,89 @@ -package mimis.router; - -import java.io.IOException; -import java.io.ObjectInputStream; -import java.io.ObjectOutputStream; -import java.net.Socket; - -import mimis.exception.worker.ActivateException; -import mimis.exception.worker.DeactivateException; -import mimis.input.Feedback; -import mimis.input.Task; -import mimis.worker.Component; -import mimis.worker.Worker; - -public class GlobalRouter extends Component { - protected String ip; - protected int port; - protected Client client; - - public GlobalRouter(String ip, int port) { - this.ip = ip; - this.port = port; - } - - protected void activate() throws ActivateException { - try { - client = new Client(ip, port); - } catch (IOException e) { - log.error(e); - throw new ActivateException(); - } - super.activate(); - } - - protected void deactivate() throws DeactivateException { - super.deactivate(); - client.stop(); - } - - public void task(Task task) { - try { - client.send(task); - } catch (IOException e) { - log.error(e); - } - } - - class Client extends Worker { - protected Socket socket; - protected ObjectInputStream objectInputStream; - protected ObjectOutputStream objectOutputStream; - - public Client(String ip, int port) throws IOException { - socket = new Socket(ip, port); - objectInputStream = new ObjectInputStream(socket.getInputStream()); - objectOutputStream = new ObjectOutputStream(socket.getOutputStream()); - } - - public void work() { - try { - Object object; - do { - object = objectInputStream.readObject(); - if (object instanceof Feedback) { - add((Feedback) object); - } - } while (object != null); - } catch (IOException e) { - log.error(e); - } catch (ClassNotFoundException e) { - log.error(e); - } - } - - protected void deactivate() throws DeactivateException { - super.deactivate(); - try { - objectInputStream.close(); - objectOutputStream.close(); - socket.close(); - } catch (IOException e) { - log.error(e); - } - } - - public void send(Object object) throws IOException { - objectOutputStream.writeObject(object); - } - } -} +package mimis.router; + +import java.io.IOException; +import java.io.ObjectInputStream; +import java.io.ObjectOutputStream; +import java.net.Socket; + +import mimis.exception.worker.ActivateException; +import mimis.exception.worker.DeactivateException; +import mimis.input.Feedback; +import mimis.input.Task; +import mimis.worker.Worker; + +public class GlobalRouter extends Router { + protected String ip; + protected int port; + protected Client client; + + public GlobalRouter(String ip, int port) { + this.ip = ip; + this.port = port; + } + + protected void activate() throws ActivateException { + try { + client = new Client(ip, port); + } catch (IOException e) { + log.error(e); + throw new ActivateException(); + } + super.activate(); + } + + protected void deactivate() throws DeactivateException { + super.deactivate(); + client.stop(); + } + + public void task(Task task) { + try { + client.send(task); + } catch (IOException e) { + log.error(e); + } + } + + class Client extends Worker { + protected Socket socket; + protected ObjectInputStream objectInputStream; + protected ObjectOutputStream objectOutputStream; + + public Client(String ip, int port) throws IOException { + socket = new Socket(ip, port); + objectInputStream = new ObjectInputStream(socket.getInputStream()); + objectOutputStream = new ObjectOutputStream(socket.getOutputStream()); + } + + public void work() { + try { + Object object; + do { + object = objectInputStream.readObject(); + if (object instanceof Feedback) { + add((Feedback) object); + } + } while (object != null); + } catch (IOException e) { + log.error(e); + } catch (ClassNotFoundException e) { + log.error(e); + } + } + + protected void deactivate() throws DeactivateException { + super.deactivate(); + try { + objectInputStream.close(); + objectOutputStream.close(); + socket.close(); + } catch (IOException e) { + log.error(e); + } + } + + public void send(Object object) throws IOException { + objectOutputStream.writeObject(object); + } + } +} diff --git a/java/src/mimis/worker/IntervalWorker.java b/java/src/mimis/worker/IntervalWorker.java new file mode 100644 index 0000000..7883a60 --- /dev/null +++ b/java/src/mimis/worker/IntervalWorker.java @@ -0,0 +1,81 @@ +package mimis.worker; + +import java.util.Timer; +import java.util.TimerTask; + +import mimis.exception.worker.ActivateException; +import mimis.exception.worker.DeactivateException; + +public class IntervalWorker extends Worker { + protected static final boolean THREAD = true; + protected static final int INTERVAL = 500; + + protected Timer timer; + + public synchronized void start(boolean thread) { + if (!active) { + activate = true; + timer = new Timer(); + timer.schedule(new TimerTask() { + public void run() { + IntervalWorker.this.run(); + }}, 0, INTERVAL); + active = true; + } + if (!thread) { + try { + synchronized (this) { + wait(); + } + } catch (InterruptedException e) { + log.info(e); + } + } + } + + public synchronized void stop() { + if (active) { + timer.cancel(); + deactivate = true; + run(); + notifyAll(); + } + } + + public void run() { + if (activate && !active) { + try { + super.activate(); + } catch (ActivateException e) { + log.error(e); + } finally { + activate = false; + } + } else if (deactivate && active) { + try { + super.deactivate(); + } catch (DeactivateException e) { + log.error(e); + } finally { + deactivate = false; + } + } + if (active) { + work(); + } + } + + protected void work() { + System.out.println("(-:"); + } + + public static void main(String[] args) { + IntervalWorker intervalWorker = new IntervalWorker(); + for (int i = 0; i < 3; ++i) { + intervalWorker.start(false); + System.out.println("--"); + intervalWorker.sleep(200); + intervalWorker.stop(); + } + } +} \ No newline at end of file diff --git a/java/src/mimis/worker/Worker.java b/java/src/mimis/worker/Worker.java index 7c41120..5919772 100644 --- a/java/src/mimis/worker/Worker.java +++ b/java/src/mimis/worker/Worker.java @@ -1,122 +1,123 @@ -package mimis.worker; - -import mimis.exception.worker.ActivateException; -import mimis.exception.worker.DeactivateException; - -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; - -public abstract class Worker implements Runnable { - protected Log log = LogFactory.getLog(getClass()); - - protected static final boolean THREAD = false; - protected static final int SLEEP = 100; - - protected boolean thread = true; - protected boolean interrupt; - protected boolean run = false; - protected boolean active = false; - protected boolean activate = false; - protected boolean deactivate = false; - - public Worker() {} - - public Worker(boolean thread) { - this.thread = thread; - } - - public synchronized 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 { - notifyAll(); - } - } - - public synchronized final void start() { - start(thread); - } - - public synchronized final void stop() { - if (active) { - deactivate = true; - } - notifyAll(); - } - - public void exit() { - stop(); - run = false; - } - - protected void sleep(int time) { - try { - if (time > 0) { - Thread.sleep(time); - } - } catch (InterruptedException e) { - log.info(e); - } - } - - protected void sleep() { - sleep(SLEEP); - } - - public boolean active() { - return active; - } - - protected void activate() throws ActivateException { - active = true; - } - - protected void deactivate() throws DeactivateException { - active = false; - } - - public final void run() { - while (run || 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 if (run) { - try { - synchronized (this) { - wait(); - } - } catch (InterruptedException e) { - log.info(e); - } - } - } - } - - protected abstract void work(); -} +package mimis.worker; + +import mimis.exception.worker.ActivateException; +import mimis.exception.worker.DeactivateException; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + +public abstract class Worker implements Runnable { + protected Log log = LogFactory.getLog(getClass()); + + protected static final boolean THREAD = true; + protected static final int SLEEP = 100; + + protected boolean thread = true; + protected boolean run = false; + protected boolean active = false; + protected boolean activate = false; + protected boolean deactivate = false; + + public Worker(boolean thread) { + this.thread = thread; + } + + public Worker() { + this(THREAD); + } + + public synchronized void start(boolean thread) { + if (!active) { + activate = true; + } + if (!run) { + run = true; + if (thread) { + log.debug("Start thread"); + new Thread(this, getClass().getName()).start(); + } else { + log.debug("Run directly"); + run(); + } + } else { + notifyAll(); + } + } + + public synchronized void start() { + start(thread); + } + + public synchronized void stop() { + if (active) { + deactivate = true; + } + notifyAll(); + } + + public void exit() { + stop(); + run = false; + } + + protected void sleep(int time) { + try { + if (time > 0) { + Thread.sleep(time); + } + } catch (InterruptedException e) { + log.info(e); + } + } + + protected void sleep() { + sleep(SLEEP); + } + + public boolean active() { + return active; + } + + protected void activate() throws ActivateException { + active = true; + } + + protected void deactivate() throws DeactivateException { + active = false; + } + + public void run() { + while (run || deactivate) { + if (activate && !active) { + try { + activate(); + } catch (ActivateException e) { + 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 if (run) { + try { + synchronized (this) { + wait(); + } + } catch (InterruptedException e) { + log.info(e); + } + } + } + } + + protected abstract void work(); +} diff --git a/java/src/test/CustomAppender.java b/java/src/test/CustomAppender.java new file mode 100644 index 0000000..1e694f8 --- /dev/null +++ b/java/src/test/CustomAppender.java @@ -0,0 +1,27 @@ +package test; + +import org.apache.log4j.AppenderSkeleton; +import org.apache.log4j.spi.LoggingEvent; + +public class CustomAppender extends AppenderSkeleton { + protected int bla = 9999; + + public void setBla(int bla) { + this.bla = bla; + } + + public int getBla() { + return bla; + } + + public boolean requiresLayout() { + return true; + } + + public void close() {} + + protected void append(LoggingEvent loggingEvent) { + System.out.print(layout.format(loggingEvent)); + } + +} diff --git a/java/wiiusej.jar b/java/wiiusej.jar new file mode 100644 index 0000000..951377e Binary files /dev/null and b/java/wiiusej.jar differ