Advertisement
ImpariTuriddu

Atmosphere 0.7.1 and GWT 2.2 - BroadcasterManager

Oct 4th, 2011
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.50 KB | None | 0 0
  1. public class BroadcasterManagerImpl implements BroadcasterManager {
  2.    
  3.     private final ConcurrentMap<String,Broadcaster> BROADCASTERS;
  4.    
  5.     private final Logger logger = LoggerFactory.getLogger(getClass());
  6.    
  7.     public BroadcasterManagerImpl (int size) {
  8.         if (size < 1)
  9.             throw new IllegalArgumentException ("The size of the broadcaster list must be " +
  10.                 "greater than 0.");
  11.         this.BROADCASTERS = new ConcurrentHashMap<String,Broadcaster>(size);
  12.         logger.debug("Broadcaster map initialized. Initial capacity: " + size);
  13.     }
  14.  
  15.     /* (non-Javadoc)
  16.      * @see org.atmosphere.samples.server.BroadcasterStorage#add(java.lang.String, org.atmosphere.cpr.Broadcaster)
  17.      */
  18.     @Override
  19.     public void add (String id, Broadcaster broadcaster) {
  20.         if (id == null)
  21.             throw new IllegalArgumentException ("The id of the broadcaster to add cannot be null.");
  22.         if (broadcaster == null)
  23.             throw new IllegalArgumentException ("The broadcaster to add cannot be null.");
  24.         this.BROADCASTERS.put(id, broadcaster);
  25.         logger.debug("Added mapping: " + id + " - b'" + broadcaster.getID() + "'s" +
  26.             broadcaster.getAtmosphereResources().size());
  27.     }
  28.    
  29.     /* (non-Javadoc)
  30.      * @see org.atmosphere.samples.server.BroadcasterStorage#remove(java.lang.String)
  31.      */
  32.     @Override
  33.     public void remove (String id) {
  34.         if (id == null)
  35.             throw new IllegalArgumentException ("The id of the broadcaster to remove cannot be null.");
  36.         Broadcaster broadcaster = this.BROADCASTERS.get(id);
  37.         this.removeBroadcaster(broadcaster, id);
  38.     }
  39.    
  40.     private void removeBroadcaster (Broadcaster broadcaster, String id) {
  41.         if (broadcaster == null)
  42.             logger.debug("No broadcaster for: " + id);
  43.         else {
  44.             //check if there are atmosphere resources for the broadcaster to remove
  45.             if (broadcaster.getAtmosphereResources().size() == 0) {
  46.                 broadcaster.destroy();
  47.                 BROADCASTERS.remove(id);
  48.                 logger.debug("Removed mapping: " + id + " - b'" + broadcaster.getID() + "'");
  49.             } else
  50.                 logger.debug("NOT Removed mapping: " + id + " - b'" + broadcaster.getID() + "'s" +
  51.                         broadcaster.getAtmosphereResources().size());
  52.             //add a JMS Subscription Event (STOP) - the id is also the domain name
  53.             JMSSubscriptionEventQueue.addEvent(new JMSSubscriptionEvent(Type.STOP,id));
  54.         }
  55.     }
  56.    
  57.     /* (non-Javadoc)
  58.      * @see org.atmosphere.samples.server.BroadcasterStorage#get(java.lang.String)
  59.      */
  60.     @Override
  61.     public Broadcaster get (String id) {
  62.         if (id == null)
  63.             throw new IllegalArgumentException ("The id of the broadcaster to retrieve cannot be null.");
  64.         Broadcaster broadcaster = this.BROADCASTERS.get(id);
  65.         logger.debug("Get mapping: " + id + " - b'" + (broadcaster == null ? "null" :
  66.             broadcaster.getID() + "'s" + broadcaster.getAtmosphereResources().size()));
  67.         return broadcaster;
  68.     }
  69.    
  70.     /* (non-Javadoc)
  71.      * @see org.atmosphere.samples.server.BroadcasterStorage#getAll()
  72.      */
  73.     @Override
  74.     public Collection<Broadcaster> getAll () {
  75.         return this.BROADCASTERS.values();
  76.     }
  77.    
  78.     @Override
  79.     public String removeResource(AtmosphereResource<?,?> resource) {
  80.         Iterator<String> i = this.BROADCASTERS.keySet().iterator();
  81.         Broadcaster b;
  82.         AtmosphereResource<?,?> r = null;
  83.         String idBroadcast = null;
  84.         while (i.hasNext() && r == null) {
  85.             idBroadcast = i.next();
  86.             b = this.BROADCASTERS.get(idBroadcast);
  87.             if (b != null)
  88.                 r = b.removeAtmosphereResource(resource);
  89.             if (r != null) {
  90.                 logger.debug("Removed a connection from the broadcaster " + b.getID());
  91.                 this.removeBroadcaster(b, idBroadcast);
  92.             }
  93.         }
  94.         return idBroadcast;
  95.     }
  96. }
  97.  
  98.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement