Skip to main content

[datafx~datafx2:70] use WebsocketReader and DataFXEndpoint in an extension of ListDataProvide

  • From:
  • To:
  • Subject: [datafx~datafx2:70] use WebsocketReader and DataFXEndpoint in an extension of ListDataProvide
  • Date: Fri, 5 Jul 2013 13:12:35 +0000

Project:    datafx
Repository: datafx2
Revision:   70
Author:     johanvos
Date:       2013-07-05 13:06:02 UTC
Link:       

Log Message:
------------
use WebsocketReader and DataFXEndpoint in an extension of ListDataProvider


Revisions:
----------
70


Modified Paths:
---------------
datafx-websocket/pom.xml
datafx-websocket/src/main/java/org/datafx/websocket/DataFXEndpoint.java
datafx-websocket/src/main/java/org/datafx/websocket/WebSocketReader.java


Added Paths:
------------
datafx-websocket/src/main/java/org/datafx/websocket/MessageProvider.java


Diffs:
------
diff -r 5c3c49d84db6 -r 39e974c3adcb datafx-websocket/pom.xml
--- a/datafx-websocket/pom.xml  Wed Jul 03 09:55:48 2013 +0200
+++ b/datafx-websocket/pom.xml  Fri Jul 05 15:06:02 2013 +0200
@@ -23,7 +23,7 @@
     <dependency>
       <groupId>javax.websocket</groupId>
       <artifactId>javax.websocket-client-api</artifactId>
-      <version>1.0-rc5</version>
+      <version>1.0</version>
     </dependency>
     <dependency>
       <groupId>junit</groupId>

diff -r 5c3c49d84db6 -r 39e974c3adcb 
datafx-websocket/src/main/java/org/datafx/websocket/DataFXEndpoint.java
--- a/datafx-websocket/src/main/java/org/datafx/websocket/DataFXEndpoint.java 
  Wed Jul 03 09:55:48 2013 +0200
+++ b/datafx-websocket/src/main/java/org/datafx/websocket/DataFXEndpoint.java 
  Fri Jul 05 15:06:02 2013 +0200
@@ -23,7 +23,8 @@
     }
 
     public void onMessage(String message) {
-        parent.setAvailableData(message);
+        System.out.println("DataFXEndpoint got a message!!");
+        parent.addAvailableData(message);
     }
     
 }

diff -r 5c3c49d84db6 -r 39e974c3adcb 
datafx-websocket/src/main/java/org/datafx/websocket/MessageProvider.java
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ 
b/datafx-websocket/src/main/java/org/datafx/websocket/MessageProvider.java  
Fri Jul 05 15:06:02 2013 +0200
@@ -0,0 +1,33 @@
+package org.datafx.websocket;
+
+import javafx.beans.property.ListProperty;
+import javafx.beans.property.SimpleListProperty;
+import javafx.collections.FXCollections;
+import javafx.collections.ObservableList;
+import javafx.concurrent.Worker;
+import org.datafx.provider.ListObjectDataProvider;
+
+/**
+ *
+ * @author johan
+ */
+public class MessageProvider extends ListObjectDataProvider<String> {
+
+//implements DataProvider<ObservableList<String>>{
+    
+    private final WebSocketReader reader;
+    private ObservableList<String> incomingMessages = 
FXCollections.observableArrayList();
+
+    public MessageProvider (WebSocketReader reader) {
+        super(reader);
+        this.reader = reader;
+    }
+
+//    public ListProperty<String> getData() {
+//        return new SimpleListProperty(incomingMessages);
+//    }
+//
+//    public Worker<ObservableList<String>> retrieve() {
+//        throw new UnsupportedOperationException("Not supported yet."); 
//To change body of generated methods, choose Tools | Templates.
+//    }
+}

diff -r 5c3c49d84db6 -r 39e974c3adcb 
datafx-websocket/src/main/java/org/datafx/websocket/WebSocketReader.java
--- 
a/datafx-websocket/src/main/java/org/datafx/websocket/WebSocketReader.java  
Wed Jul 03 09:55:48 2013 +0200
+++ 
b/datafx-websocket/src/main/java/org/datafx/websocket/WebSocketReader.java  
Fri Jul 05 15:06:02 2013 +0200
@@ -1,12 +1,13 @@
 package org.datafx.websocket;
 
-import java.io.IOException;
 import java.net.URI;
-import java.net.URISyntaxException;
+import java.util.Collections;
+import java.util.LinkedList;
+import java.util.List;
 import java.util.logging.Level;
 import java.util.logging.Logger;
 import javax.websocket.ContainerProvider;
-import javax.websocket.DeploymentException;
+import javax.websocket.Session;
 import javax.websocket.WebSocketContainer;
 import org.datafx.reader.DataReader;
 
@@ -14,13 +15,14 @@
  *
  * @author johan
  */
-public class WebSocketReader<T> implements DataReader<T> {
+public class WebSocketReader implements DataReader<String> {//, 
WritableDataReader<T> {
     
     private String address;
     private boolean connected = false;
-    private T availableData;
-    private Object availableLock = new Object();
+    private List<String> availableData = Collections.synchronizedList(new 
LinkedList<String>());
+    private final Object availableLock = new Object();
     private boolean closed = false;
+    private Session session;
     
     public WebSocketReader (String address) {
         this.address = address;
@@ -30,25 +32,33 @@
     private void connectEndpoint () {
         try {
             WebSocketContainer container = 
ContainerProvider.getWebSocketContainer();
-            container.connectToServer(DataFXEndpoint.class, null, new 
URI(address));
-        } catch (URISyntaxException ex) {
-            
Logger.getLogger(WebSocketReader.class.getName()).log(Level.SEVERE, null, ex);
-        } catch (DeploymentException ex) {
-            
Logger.getLogger(WebSocketReader.class.getName()).log(Level.SEVERE, null, ex);
-        } catch (IOException ex) {
+            session = container.connectToServer(DataFXEndpoint.class, null, 
new URI( address));
+            connected = true;
+        } catch (Exception ex) {
             
Logger.getLogger(WebSocketReader.class.getName()).log(Level.SEVERE, null, ex);
         }
     }
     
-    public void setAvailableData (T data) {
+    public void addAvailableData (String data) {
+        System.out.println("WebSocketReader has new data");
         synchronized (this.availableLock) {
-            this.availableData = data;
+             this.availableData.add( data);
+            this.availableLock.notifyAll();
         }
+        
+        System.out.println("WebSocketReader was notified new data");
     }
     
-    public T get() {
+    public String get() {
+        System.out.println("WebSocketReader in get...");
         synchronized (this.availableLock) {
-            if (this.availableData == null) {
+            if (!connected) {
+                System.out.println("WebSocketReader needs to connect 
first...");
+                connectEndpoint();
+                System.out.println("WebSocketReader connected...");
+            }
+            if (this.availableData.isEmpty()) {
+                System.out.println("WebSocketReader waits for available 
data...");
                 try {
                     this.availableLock.wait();
                 } catch (InterruptedException ex) {
@@ -56,12 +66,18 @@
                 }
             }
         }
-        return this.availableData;
+        System.out.println("WebSocketReader GOT available data...");
+        return this.availableData.remove(0);
     }
 
     public boolean next() {
         return !closed;
     }
 
+//    public void writeBack() {
+//        session.getBasicRemote().
+//        throw new UnsupportedOperationException("Not supported yet."); 
//To change body of generated methods, choose Tools | Templates.
+//    }
+
     
 }






[datafx~datafx2:70] use WebsocketReader and DataFXEndpoint in an extension of ListDataProvide

johanvos 07/05/2013
 
 
Close
loading
Please Confirm
Close