From 4f965c0dbe9c49c2e43da9625eb443d470625fa7 Mon Sep 17 00:00:00 2001 From: gumartinm Date: Sun, 1 Apr 2012 23:13:39 +0200 Subject: [PATCH] Working on my JavaPOS keyboard driver. Next step: using java.nio (selector) because I do not want to block on the file descriptor's read operation. --- JavaPOS/KeyBoardDriver/pom.xml | 21 ++++++- .../javapos/example/JposDriverInstanceFactory.java | 0 .../example/JposDriverInstanceFactoryImpl.java | 0 .../example/JposServiceInstanceFactoryImpl.java | 0 .../java/de/javapos/example/MyPOSKeyboard.java | 57 +++++++++++++++--- .../main/java/de/javapos/example/ThreadSafe.java | 8 +++ .../example/hardware/BaseKeyBoardDriver.java | 8 ++- .../example/hardware/KeyBoardDeviceLinux.java | 26 ++++---- .../javapos/example/queue/JposEventListener.java | 10 ++++ .../de/javapos/example/queue/JposEventQueue.java | 21 +++++-- .../javapos/example/queue/JposEventQueueImpl.java | 69 +++++++++------------- JavaPOS/KeyBoardDriver/src/main/resources/jpos.xml | 0 12 files changed, 146 insertions(+), 74 deletions(-) mode change 100755 => 100644 JavaPOS/KeyBoardDriver/src/main/java/de/javapos/example/JposDriverInstanceFactory.java mode change 100755 => 100644 JavaPOS/KeyBoardDriver/src/main/java/de/javapos/example/JposDriverInstanceFactoryImpl.java mode change 100755 => 100644 JavaPOS/KeyBoardDriver/src/main/java/de/javapos/example/JposServiceInstanceFactoryImpl.java mode change 100755 => 100644 JavaPOS/KeyBoardDriver/src/main/java/de/javapos/example/MyPOSKeyboard.java mode change 100755 => 100644 JavaPOS/KeyBoardDriver/src/main/java/de/javapos/example/ThreadSafe.java mode change 100755 => 100644 JavaPOS/KeyBoardDriver/src/main/java/de/javapos/example/hardware/BaseKeyBoardDriver.java mode change 100755 => 100644 JavaPOS/KeyBoardDriver/src/main/java/de/javapos/example/hardware/KeyBoardDeviceLinux.java create mode 100644 JavaPOS/KeyBoardDriver/src/main/java/de/javapos/example/queue/JposEventListener.java mode change 100755 => 100644 JavaPOS/KeyBoardDriver/src/main/java/de/javapos/example/queue/JposEventQueue.java mode change 100755 => 100644 JavaPOS/KeyBoardDriver/src/main/java/de/javapos/example/queue/JposEventQueueImpl.java mode change 100755 => 100644 JavaPOS/KeyBoardDriver/src/main/resources/jpos.xml diff --git a/JavaPOS/KeyBoardDriver/pom.xml b/JavaPOS/KeyBoardDriver/pom.xml index b556c53..f4b424b 100644 --- a/JavaPOS/KeyBoardDriver/pom.xml +++ b/JavaPOS/KeyBoardDriver/pom.xml @@ -1,5 +1,7 @@ - + 4.0.0 de.javapos.example javapos-keyboard-driver @@ -23,4 +25,21 @@ 1.2.15 + + UTF-8 + + + + + org.apache.maven.plugins + maven-compiler-plugin + 2.0.2 + + 1.6 + 1.6 + ${project.build.sourceEncoding} + + + + diff --git a/JavaPOS/KeyBoardDriver/src/main/java/de/javapos/example/JposDriverInstanceFactory.java b/JavaPOS/KeyBoardDriver/src/main/java/de/javapos/example/JposDriverInstanceFactory.java old mode 100755 new mode 100644 diff --git a/JavaPOS/KeyBoardDriver/src/main/java/de/javapos/example/JposDriverInstanceFactoryImpl.java b/JavaPOS/KeyBoardDriver/src/main/java/de/javapos/example/JposDriverInstanceFactoryImpl.java old mode 100755 new mode 100644 diff --git a/JavaPOS/KeyBoardDriver/src/main/java/de/javapos/example/JposServiceInstanceFactoryImpl.java b/JavaPOS/KeyBoardDriver/src/main/java/de/javapos/example/JposServiceInstanceFactoryImpl.java old mode 100755 new mode 100644 diff --git a/JavaPOS/KeyBoardDriver/src/main/java/de/javapos/example/MyPOSKeyboard.java b/JavaPOS/KeyBoardDriver/src/main/java/de/javapos/example/MyPOSKeyboard.java old mode 100755 new mode 100644 index bf41f09..a8b81da --- a/JavaPOS/KeyBoardDriver/src/main/java/de/javapos/example/MyPOSKeyboard.java +++ b/JavaPOS/KeyBoardDriver/src/main/java/de/javapos/example/MyPOSKeyboard.java @@ -1,6 +1,7 @@ package de.javapos.example; import de.javapos.example.hardware.BaseKeyBoardDriver; +import de.javapos.example.queue.JposEventListener; import de.javapos.example.queue.JposEventQueue; import de.javapos.example.queue.JposEventQueueImpl; import jpos.JposConst; @@ -12,20 +13,22 @@ import jpos.services.EventCallbacks; import jpos.services.POSKeyboardService112; import jpos.config.JposEntry; import jpos.config.JposEntryRegistry; +import jpos.events.DataEvent; import jpos.events.JposEvent; public class MyPOSKeyboard implements POSKeyboardService112, JposConst, POSKeyboardConst { private static final int deviceVersion12 = 1002000; private String logicalname; - private EventCallbacks callbacks = null; - private JposEntryRegistry jposEntryRegistry = null; - private JposEntry jposEntry = null; + private EventCallbacks callbacks; + private JposEntryRegistry jposEntryRegistry; + private JposEntry jposEntry; private String device; private int maxEvents; private BaseKeyBoardDriver deviceDriver; private JposDriverInstanceFactory jposDriverFactory; private JposEventQueue jposEventQueue; + private JposEventListener eventListener; @Override public int getCapPowerReporting() throws JposException { @@ -260,8 +263,12 @@ public class MyPOSKeyboard implements POSKeyboardService112, JposConst, POSKeybo //Crear la cola donde almacenamos eventos estilo FIFO. //Esto tambien puede hacerser en jpos.xml y queda todo como un puzle LOL //TODO: poner la cola de eventos en el jpos.xml - this.jposEventQueue = new JposEventQueueImpl(this.callbacks); - this.deviceDriver.addEventListener(this.jposEventQueue); + this.jposEventQueue = new JposEventQueueImpl(); + + //estaria genial poner esto en el jpos.xml y asi puede tambien cambiar el eventlistener + this.eventListener = new MyPOSKeyBoardEventListener(this.jposEventQueue, this.callbacks); + + this.deviceDriver.addEventListener(eventListener); } @@ -403,8 +410,40 @@ public class MyPOSKeyboard implements POSKeyboardService112, JposConst, POSKeybo // TODO Auto-generated method stub } - - - - + + //o mejor ¿una clase declarada en el jpos.xml que implementa JposEventListener y a la + //que en el constructor le pasamos las callbacks? :/ + @ThreadSafe + private class MyPOSKeyBoardEventListener implements JposEventListener { + private final JposEventQueue jposEventQueue; + private final EventCallbacks callbacks; + + //cuando sepa como usara la tabla de traduccion de teclas aqui debo pasarsela + //es esta clase en los metodos que implementa donde uso la tabla de traduccion de teclas + private MyPOSKeyBoardEventListener (JposEventQueue jposEventQueue, EventCallbacks callbacks) { + this.jposEventQueue = jposEventQueue; + this.callbacks = callbacks; + //En el futuro un campo con la tabla de traduccion tecla/codigo + } + + @Override + public void inputAvailable(int input) { + try { + this.jposEventQueue.putEvent(new DataEvent(this.callbacks, input)); + } catch (InterruptedException e) { + //restore interrupt status. + Thread.currentThread().interrupt(); + } + } + + @Override + public void errorOccurred(int error) { + // TODO Auto-generated method stub + } + + @Override + public void statusUpdateOccurred(int status) { + // TODO Auto-generated method stub + } + } } \ No newline at end of file diff --git a/JavaPOS/KeyBoardDriver/src/main/java/de/javapos/example/ThreadSafe.java b/JavaPOS/KeyBoardDriver/src/main/java/de/javapos/example/ThreadSafe.java old mode 100755 new mode 100644 index fa2c197..dc3a60a --- a/JavaPOS/KeyBoardDriver/src/main/java/de/javapos/example/ThreadSafe.java +++ b/JavaPOS/KeyBoardDriver/src/main/java/de/javapos/example/ThreadSafe.java @@ -1,5 +1,13 @@ package de.javapos.example; +import java.lang.annotation.ElementType; +import java.lang.annotation.Target; + +/** + * Declare classes as thread-safe. It is assumed that objects of these + * classes are thread-safe and can be shared between different threads. * + */ +@Target(value = ElementType.TYPE) public @interface ThreadSafe { } diff --git a/JavaPOS/KeyBoardDriver/src/main/java/de/javapos/example/hardware/BaseKeyBoardDriver.java b/JavaPOS/KeyBoardDriver/src/main/java/de/javapos/example/hardware/BaseKeyBoardDriver.java old mode 100755 new mode 100644 index b4f8a9d..334b92a --- a/JavaPOS/KeyBoardDriver/src/main/java/de/javapos/example/hardware/BaseKeyBoardDriver.java +++ b/JavaPOS/KeyBoardDriver/src/main/java/de/javapos/example/hardware/BaseKeyBoardDriver.java @@ -3,7 +3,7 @@ */ package de.javapos.example.hardware; -import de.javapos.example.queue.JposEventQueue; +import de.javapos.example.queue.JposEventListener; import jpos.JposException; /** @@ -30,9 +30,9 @@ public interface BaseKeyBoardDriver { public boolean isEnabled(); - public void addEventListener(JposEventQueue jposEventQueue) throws JposException; + public void addEventListener(JposEventListener jposEventListener) throws JposException; - public void removeEventListener(JposEventQueue jposEventQueue); + public void removeEventListener(JposEventListener jposEventListener); public boolean write(byte[] paramArrayOfByte, int paramInt1, int paramInt2, int paramInt3) throws JposException; @@ -49,4 +49,6 @@ public interface BaseKeyBoardDriver { public void flush(int paramInt) throws JposException; public void device(String device) throws JposException; + + public void autoDisable (boolean autoDisable); } diff --git a/JavaPOS/KeyBoardDriver/src/main/java/de/javapos/example/hardware/KeyBoardDeviceLinux.java b/JavaPOS/KeyBoardDriver/src/main/java/de/javapos/example/hardware/KeyBoardDeviceLinux.java old mode 100755 new mode 100644 index 1a0dab1..d32e3f0 --- a/JavaPOS/KeyBoardDriver/src/main/java/de/javapos/example/hardware/KeyBoardDeviceLinux.java +++ b/JavaPOS/KeyBoardDriver/src/main/java/de/javapos/example/hardware/KeyBoardDeviceLinux.java @@ -12,18 +12,18 @@ import jpos.JposConst; import jpos.JposException; import org.apache.log4j.Logger; import de.javapos.example.ThreadSafe; -import de.javapos.example.queue.JposEventQueue; +import de.javapos.example.queue.JposEventListener; public class KeyBoardDeviceLinux implements BaseKeyBoardDriver { private static final Logger logger = Logger.getLogger(KeyBoardDeviceLinux.class); //value EV_KEY from include/linux/input.h private static final int EV_KEY = 1; private Semaphore mutex = new Semaphore(1, true); - private JposEventQueue eventQueue; private final ExecutorService exec = Executors.newSingleThreadExecutor(); private DataInputStream device; private boolean isClaimed; private boolean autoDisable; + private JposEventListener eventListener; @Override public boolean isOpened() { @@ -37,11 +37,6 @@ public class KeyBoardDeviceLinux implements BaseKeyBoardDriver { } - - /** - * - * @throws JposException - */ @Override public void claim() throws JposException { this.claim(0); @@ -96,16 +91,19 @@ public class KeyBoardDeviceLinux implements BaseKeyBoardDriver { } @Override - public void addEventListener(JposEventQueue jposEventQueue) - throws JposException { - this.eventQueue = jposEventQueue; - + public void autoDisable(boolean autoDisable) { + this.autoDisable = autoDisable; } @Override - public void removeEventListener(JposEventQueue jposEventQueue) { - // TODO Auto-generated method stub + public void addEventListener(JposEventListener jposEventListener) + throws JposException { + this.eventListener = jposEventListener; + } + @Override + public void removeEventListener(JposEventListener jposEventListener) { + this.eventListener = null; } @Override @@ -220,7 +218,7 @@ public class KeyBoardDeviceLinux implements BaseKeyBoardDriver { //Because I do not have a POS I am going to use the keyboard of my computer. //see: /dev/input/by-path/ //And the Keyborad scancode is for USB devices. - String device ="/dev/input/event4"; + String device ="/dev/input/event5"; KeyBoardDeviceLinux driver = new KeyBoardDeviceLinux(); diff --git a/JavaPOS/KeyBoardDriver/src/main/java/de/javapos/example/queue/JposEventListener.java b/JavaPOS/KeyBoardDriver/src/main/java/de/javapos/example/queue/JposEventListener.java new file mode 100644 index 0000000..14d65d7 --- /dev/null +++ b/JavaPOS/KeyBoardDriver/src/main/java/de/javapos/example/queue/JposEventListener.java @@ -0,0 +1,10 @@ +package de.javapos.example.queue; + +public interface JposEventListener { + + public void inputAvailable(int input); + + public void errorOccurred(int error); + + public void statusUpdateOccurred(int status); +} \ No newline at end of file diff --git a/JavaPOS/KeyBoardDriver/src/main/java/de/javapos/example/queue/JposEventQueue.java b/JavaPOS/KeyBoardDriver/src/main/java/de/javapos/example/queue/JposEventQueue.java old mode 100755 new mode 100644 index 91ef58c..8816d32 --- a/JavaPOS/KeyBoardDriver/src/main/java/de/javapos/example/queue/JposEventQueue.java +++ b/JavaPOS/KeyBoardDriver/src/main/java/de/javapos/example/queue/JposEventQueue.java @@ -2,11 +2,22 @@ package de.javapos.example.queue; import jpos.events.JposEvent; +//Similar a WNBaseService ¿mejor una clase o implementarlo en cada servicio :/? +//¿O mejor un servicio que extienda una clase que implementa este interfaz XD? public interface JposEventQueue { - public void inputAvailable(JposEvent input); - - public void errorOccurred(JposEvent error); + public void putEvent(JposEvent paramJposEvent) throws InterruptedException; + + public void clearAllEvents(); + + public void clearInputEvents(); + + public void clearOutputEvents(); - public void statusUpdateOccurred(JposEvent status); -} \ No newline at end of file + public int getNumberOfEvents(); + + public void checkEvents(); + + public boolean eventQueueIsFull(); + +} diff --git a/JavaPOS/KeyBoardDriver/src/main/java/de/javapos/example/queue/JposEventQueueImpl.java b/JavaPOS/KeyBoardDriver/src/main/java/de/javapos/example/queue/JposEventQueueImpl.java old mode 100755 new mode 100644 index 3b09787..2816b3e --- a/JavaPOS/KeyBoardDriver/src/main/java/de/javapos/example/queue/JposEventQueueImpl.java +++ b/JavaPOS/KeyBoardDriver/src/main/java/de/javapos/example/queue/JposEventQueueImpl.java @@ -3,61 +3,46 @@ package de.javapos.example.queue; import java.util.concurrent.BlockingQueue; import java.util.concurrent.LinkedBlockingQueue; import jpos.events.JposEvent; -import jpos.services.EventCallbacks; public class JposEventQueueImpl implements JposEventQueue { - private final EventCallbacks callbacks; //§JLS Item 16: Favor composition over inheritance //Java Concurrency in Practice 4.4.2 //Not sure if this may be called "Composition" LOL private final BlockingQueue linkedBlockingQueue = new LinkedBlockingQueue(); - - public JposEventQueueImpl (EventCallbacks callbacks) { - this.callbacks = callbacks; + + @Override + public void putEvent(JposEvent paramJposEvent) throws InterruptedException { + this.linkedBlockingQueue.put(paramJposEvent); + } + + @Override + public void clearAllEvents() { + this.linkedBlockingQueue.clear(); + } + + @Override + public void clearInputEvents() { + // TODO Auto-generated method stub + } + + @Override + public void clearOutputEvents() { + // TODO Auto-generated method stub } - + @Override - public void inputAvailable(JposEvent input) { - try { - this.linkedBlockingQueue.put(input); - } catch (InterruptedException e) { - //Java Concurrency in Practice 5.4: Restore the interrupt. - //restore interrupted status. Because we do not know where this code is going to be used. - //TODO: After reading Java Concurrency in Practice chapter 7 you must decide - //if you should throw the interrupt exception or just restore the interrupt - //as I am doing right now. In the meanwhile just restoring the interrupt. - //EN MI OPIONION SERIA MEJOR NO COGER LA EXCEPCION AQUI Y PONER throws EN EL INTERFAZ - //JposEventQueue - Thread.currentThread().interrupt(); - } - + public int getNumberOfEvents() { + return this.linkedBlockingQueue.size(); } @Override - public void errorOccurred(JposEvent error) { - try { - this.linkedBlockingQueue.put(error); - } catch (InterruptedException e) { - //Java Concurrency in Practice 5.4: Restore the interrupt. - //restore interrupted status. Because we do not know where this code is going to be used. - //TODO: After reading Java Concurrency in Practice chapter 7 you must decide - //if you should throw the interrupt exception or just restore the interrupt - //as I am doing right now. In the meanwhile just restoring the interrupt. - Thread.currentThread().interrupt(); - } + public void checkEvents() { + // TODO Auto-generated method stub } @Override - public void statusUpdateOccurred(JposEvent status) { - try { - this.linkedBlockingQueue.put(status); - } catch (InterruptedException e) { - //Java Concurrency in Practice 5.4: Restore the interrupt. - //restore interrupted status. Because we do not know where this code is going to be used. - //TODO: After reading Java Concurrency in Practice chapter 7 you must decide - //if you should throw the interrupt exception or just restore the interrupt - //as I am doing right now. In the meanwhile just restoring the interrupt. - Thread.currentThread().interrupt(); - } + public boolean eventQueueIsFull() { + //No seguro de esto :/ + return false; } } diff --git a/JavaPOS/KeyBoardDriver/src/main/resources/jpos.xml b/JavaPOS/KeyBoardDriver/src/main/resources/jpos.xml old mode 100755 new mode 100644 -- 2.1.4