JavaPOS: fire event thread.
authorgumartinm <gustavo@gumartinm.name>
Tue, 24 Apr 2012 07:00:44 +0000 (09:00 +0200)
committergumartinm <gustavo@gumartinm.name>
Tue, 24 Apr 2012 07:00:44 +0000 (09:00 +0200)
I missed this class in the last commit related to JavaPOS...

JavaPOS/KeyBoardDriver/src/main/java/de/javapos/example/ThreadFireEvent.java [new file with mode: 0644]

diff --git a/JavaPOS/KeyBoardDriver/src/main/java/de/javapos/example/ThreadFireEvent.java b/JavaPOS/KeyBoardDriver/src/main/java/de/javapos/example/ThreadFireEvent.java
new file mode 100644 (file)
index 0000000..d9e6914
--- /dev/null
@@ -0,0 +1,67 @@
+package de.javapos.example;
+
+import jpos.events.DataEvent;
+import jpos.events.DirectIOEvent;
+import jpos.events.ErrorEvent;
+import jpos.events.JposEvent;
+import jpos.events.OutputCompleteEvent;
+import jpos.events.StatusUpdateEvent;
+import jpos.services.EventCallbacks;
+import de.javapos.example.queue.JposEventQueue;
+
+public class ThreadFireEvent extends Thread {
+       private final JposEventQueue jposEventQueue;
+       private boolean isDataEventEnabled;
+       private final EventCallbacks eventCallbacks;
+       
+       public ThreadFireEvent (JposEventQueue jposEventQueue, 
+                               boolean isDataEventEnabled, EventCallbacks  eventCallbacks) {
+               this.jposEventQueue = jposEventQueue;
+               this.isDataEventEnabled = isDataEventEnabled;
+               this.eventCallbacks = eventCallbacks;
+       }
+       
+       public void run () {
+               JposEvent jposEvent = null;
+               
+               while (true) {
+                       if (this.jposEventQueue.getNumberOfEvents() != 0) {
+                               //PROBLEMA: si DataEventEnabled es false y lo unico que hay en la cola son eventos de datos
+                               //me meto en un bucle que va a dejar la CPU frita... :/
+                               if (this.isDataEventEnabled) {
+                                       try {
+                                               jposEvent = this.jposEventQueue.getEvent();
+                                       } catch (InterruptedException e) {
+                                               //restore interrupt status.
+                                               Thread.currentThread().interrupt();
+                                               //End of thread.
+                                               break;
+                                       }
+                               }
+                               else {
+                                       //TODO: Buscar eventos que no sean DataEvent o ErrorEvent
+                               }
+                       }
+                       
+                       //TODO: Bloquearme hasta que freezeEvent sea false
+                       
+                       if (jposEvent instanceof DataEvent) {
+                               this.eventCallbacks.fireDataEvent((DataEvent)jposEvent);
+                               //TODO: synchronized?
+                               this.isDataEventEnabled = false;
+                       }
+                       if (jposEvent instanceof ErrorEvent) {
+                               this.eventCallbacks.fireErrorEvent((ErrorEvent)jposEvent);
+                       }
+                       if (jposEvent instanceof StatusUpdateEvent) {
+                               this.eventCallbacks.fireStatusUpdateEvent((StatusUpdateEvent)jposEvent);
+                       }
+                       if (jposEvent instanceof DirectIOEvent) {
+                               this.eventCallbacks.fireDirectIOEvent((DirectIOEvent)jposEvent);
+                       }
+                       if (jposEvent instanceof OutputCompleteEvent) {
+                               this.eventCallbacks.fireOutputCompleteEvent((OutputCompleteEvent)jposEvent);
+                       }
+               }
+       }
+}