<?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>
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;
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 {
//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);
}
// 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
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 {
}
*/
package de.javapos.example.hardware;
-import de.javapos.example.queue.JposEventQueue;
+import de.javapos.example.queue.JposEventListener;
import jpos.JposException;
/**
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;
public void flush(int paramInt) throws JposException;
public void device(String device) throws JposException;
+
+ public void autoDisable (boolean autoDisable);
}
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() {
}
-
- /**
- *
- * @throws JposException
- */
@Override
public void claim() throws JposException {
this.claim(0);
}
@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
//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();
--- /dev/null
+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
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();
+
+}
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;
}
}