/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package org.eclipse.jetty;

import java.util.Collections;
import java.util.HashSet;
import java.util.Observer;
import java.util.Set;
import org.json.simple.JSONObject;

/**
 *
 * @author Ayman
 */
public class WebSocketDB {
    static WebSocketDB instance;
    private Set<VisWebSocket> sockets = Collections.synchronizedSet(new HashSet<VisWebSocket>());
    private Observer observer;
    
    /** Creates a new instance of WebSocketDB */
    private WebSocketDB() {
        instance = this;
    }
    
    public void registerNewSocket(VisWebSocket socket) {
        System.out.println("Register new Socket");
        if (!sockets.contains(socket))
            sockets.add(socket);
        System.out.println("Socket count ="+sockets.size());
        socket.addObserver(observer);
        observer.update(socket, null);
    }
    
    public void unRegisterSocket(VisWebSocket socket) {
        System.out.println("unRegister Socket");
        sockets.remove(socket);
        System.out.println("Socket count ="+sockets.size());
     }
    
    static public WebSocketDB getInstance() {
        if (instance == null)
            instance = new WebSocketDB();
        
        return instance;
    }

    /**
     * @param observer the observer to set
     */
    public void setObserver(Observer observer) {
        this.observer = observer; // ViewDB
    }
    
    public void broadcastMessageJson(JSONObject msg, VisWebSocket specificSocket)
    {
        if (specificSocket != null){
            specificSocket.sendVisualizerMessage(msg);
            return;
        }
        for (VisWebSocket sock : sockets){
            System.out.println("Broadcast:"+msg.toJSONString()+"\n");
            sock.sendVisualizerMessage(msg);
        }
        //sockets.iterator().next().sendVisualizerMessage(msg);
    }
}

@@ -564,24 +564,60 @@
 server;%0A    
+public static boolean debug = false;
 %0A    /** Cre
@@ -774,32 +774,43 @@
 ocket) %7B%0A       
+ if (debug)
  System.out.prin
@@ -908,32 +908,43 @@
 socket);%0A       
+ if (debug)
  System.out.prin
@@ -1124,32 +1124,43 @@
 ocket) %7B%0A       
+ if (debug)
  System.out.prin
@@ -1220,24 +1220,35 @@
 et);%0A       
+ if (debug)
  System.out.
@@ -1887,16 +1887,27 @@
         
+ if (debug)
  System.
@@ -2016,72 +2016,8 @@
   %7D%0A
-        //sockets.iterator().next().sendVisualizerMessage(msg);%0A
     

Remove debugging lines