Working on my JavaPOS keyboard driver.
authorgumartinm <gustavo@gumartinm.name>
Sun, 1 Apr 2012 21:13:39 +0000 (23:13 +0200)
committergumartinm <gustavo@gumartinm.name>
Sun, 1 Apr 2012 21:13:39 +0000 (23:13 +0200)
Next step: using java.nio (selector) because I do not want to block on the
file descriptor's read operation.

12 files changed:
JavaPOS/KeyBoardDriver/pom.xml
JavaPOS/KeyBoardDriver/src/main/java/de/javapos/example/JposDriverInstanceFactory.java [changed mode: 0755->0644]
JavaPOS/KeyBoardDriver/src/main/java/de/javapos/example/JposDriverInstanceFactoryImpl.java [changed mode: 0755->0644]
JavaPOS/KeyBoardDriver/src/main/java/de/javapos/example/JposServiceInstanceFactoryImpl.java [changed mode: 0755->0644]
JavaPOS/KeyBoardDriver/src/main/java/de/javapos/example/MyPOSKeyboard.java [changed mode: 0755->0644]
JavaPOS/KeyBoardDriver/src/main/java/de/javapos/example/ThreadSafe.java [changed mode: 0755->0644]
JavaPOS/KeyBoardDriver/src/main/java/de/javapos/example/hardware/BaseKeyBoardDriver.java [changed mode: 0755->0644]
JavaPOS/KeyBoardDriver/src/main/java/de/javapos/example/hardware/KeyBoardDeviceLinux.java [changed mode: 0755->0644]
JavaPOS/KeyBoardDriver/src/main/java/de/javapos/example/queue/JposEventListener.java [new file with mode: 0644]
JavaPOS/KeyBoardDriver/src/main/java/de/javapos/example/queue/JposEventQueue.java [changed mode: 0755->0644]
JavaPOS/KeyBoardDriver/src/main/java/de/javapos/example/queue/JposEventQueueImpl.java [changed mode: 0755->0644]
JavaPOS/KeyBoardDriver/src/main/resources/jpos.xml [changed mode: 0755->0644]

index b556c53..f4b424b 100644 (file)
@@ -1,5 +1,7 @@
 <?xml version="1.0" encoding="UTF-8"?>
-<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+<project xmlns="http://maven.apache.org/POM/4.0.0"
+               xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+               xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
     <modelVersion>4.0.0</modelVersion>
     <groupId>de.javapos.example</groupId>
     <artifactId>javapos-keyboard-driver</artifactId>
                <version>1.2.15</version>
                </dependency>   
     </dependencies>
+    <properties>
+        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
+    </properties>
+    <build>
+        <plugins>
+            <plugin>
+                <groupId>org.apache.maven.plugins</groupId>
+                <artifactId>maven-compiler-plugin</artifactId>
+                <version>2.0.2</version>
+                <configuration>
+                    <source>1.6</source>
+                    <target>1.6</target>
+                    <encoding>${project.build.sourceEncoding}</encoding>
+                </configuration>
+            </plugin>
+        </plugins>
+    </build>
 </project>
old mode 100755 (executable)
new mode 100644 (file)
index bf41f09..a8b81da
@@ -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
old mode 100755 (executable)
new mode 100644 (file)
index fa2c197..dc3a60a
@@ -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 {
 
 }
old mode 100755 (executable)
new mode 100644 (file)
index b4f8a9d..334b92a
@@ -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);
 }
old mode 100755 (executable)
new mode 100644 (file)
index 1a0dab1..d32e3f0
@@ -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 (file)
index 0000000..14d65d7
--- /dev/null
@@ -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
old mode 100755 (executable)
new mode 100644 (file)
index 91ef58c..8816d32
@@ -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();
+
+}
old mode 100755 (executable)
new mode 100644 (file)
index 3b09787..2816b3e
@@ -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<JposEvent> linkedBlockingQueue = new LinkedBlockingQueue<JposEvent>();
-       
-       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;
        }
 }