Changeset 141 for trunk/sonorous

Show
Ignore:
Timestamp:
31/12/09 02:27:54 (7 months ago)
Author:
mbooth
Message:

Sonorous now responds to its first UPnP event -- changing the zone name on another controller is reflected in Sonorous! Hurray!

Location:
trunk/sonorous/uk.co.matbooth.sonorous/src/uk/co/matbooth/sonorous
Files:
4 modified

Legend:

Unmodified
Added
Removed
  • trunk/sonorous/uk.co.matbooth.sonorous/src/uk/co/matbooth/sonorous/upnp/SubscriptionManager.java

    r140 r141  
    88package uk.co.matbooth.sonorous.upnp; 
    99 
     10import java.util.ArrayList; 
     11import java.util.Dictionary; 
     12import java.util.Enumeration; 
    1013import java.util.HashMap; 
     14import java.util.List; 
    1115import java.util.Properties; 
    1216 
    1317import org.eclipse.core.runtime.ILog; 
    1418import org.eclipse.core.runtime.IStatus; 
     19import org.eclipse.core.runtime.MultiStatus; 
    1520import org.eclipse.core.runtime.Status; 
    1621import org.osgi.framework.BundleContext; 
     
    7378                    + serviceId + " on " + deviceId)); 
    7479        } else { 
    75             log.log(new Status(IStatus.WARNING, Activator.ID, "No subscription to " 
    76                     + serviceId + " on " + deviceId)); 
     80            log.log(new Status(IStatus.WARNING, Activator.ID, 
     81                    "No subscription exists to " + serviceId + " on " + deviceId)); 
    7782        } 
    7883    } 
    7984 
    80     private class Subscription { 
     85    /** 
     86     * TODO 
     87     */ 
     88    private class Subscription implements UPnPEventListener { 
    8189 
    8290        private final ServiceRegistration reg; 
     91        private final UPnPEventListener listener; 
    8392 
    84         public Subscription(BundleContext context, String filter, 
    85                 UPnPEventListener listener) throws Exception { 
     93        public Subscription(BundleContext context, String filter, UPnPEventListener l) 
     94                throws Exception { 
    8695            Properties p = new Properties(); 
    8796            p.put(UPnPEventListener.UPNP_FILTER, context.createFilter(filter)); 
    88             reg = context.registerService(UPnPEventListener.class.getName(), listener, p); 
     97            listener = l; 
     98            reg = context.registerService(UPnPEventListener.class.getName(), this, p); 
    8999        } 
    90100 
     
    92102            reg.unregister(); 
    93103        } 
     104 
     105        public void notifyUPnPEvent(String deviceId, String serviceId, Dictionary events) { 
     106            // Log the event before sending it up to the real listener 
     107            List<IStatus> details = new ArrayList<IStatus>(); 
     108            for (Enumeration<String> keys = events.keys(); keys.hasMoreElements();) { 
     109                String key = keys.nextElement(); 
     110                details.add(new Status(IStatus.INFO, Activator.ID, key + ": " 
     111                        + events.get(key))); 
     112            } 
     113            MultiStatus status = new MultiStatus(Activator.ID, IStatus.INFO, details 
     114                    .toArray(new IStatus[details.size()]), "Event received from " 
     115                    + serviceId + " on " + deviceId, null); 
     116            log.log(status); 
     117            listener.notifyUPnPEvent(deviceId, serviceId, events); 
     118        } 
    94119    } 
    95120} 
  • trunk/sonorous/uk.co.matbooth.sonorous/src/uk/co/matbooth/sonorous/zones/Zone.java

    r140 r141  
    88package uk.co.matbooth.sonorous.zones; 
    99 
     10import java.beans.PropertyChangeListener; 
     11import java.beans.PropertyChangeSupport; 
    1012import java.util.ArrayList; 
    1113import java.util.Dictionary; 
     
    2224 */ 
    2325public class Zone implements UPnPEventListener { 
     26    private final PropertyChangeSupport pcs = new PropertyChangeSupport(this); 
     27 
     28    /** 
     29     * The device tree this zone is based on. 
     30     */ 
    2431    private final DeviceNode rootNode; 
    2532 
    2633    /** 
    27      * List of all the services we want to subscribe to. 
     34     * List of all the services provided by the device tree that we want to 
     35     * subscribe to. 
    2836     */ 
    2937    private static final String SERVICES[] = { "urn:upnp-org:serviceId:DeviceProperties", 
    30             "urn:upnp-org:serviceId:ZoneGroupTopology", }; 
     38//          "urn:upnp-org:serviceId:ZoneGroupTopology", // TODO 
     39    }; 
    3140 
    3241    /** 
     
    3645    private final List<String[]> subscriptions = new ArrayList<String[]>(); 
    3746 
     47    private String zoneName = ""; 
     48 
    3849    public Zone(DeviceNode device) { 
    3950        rootNode = device; 
     
    4253    public String getRootUDN() { 
    4354        return rootNode.getUDN(); 
     55    } 
     56 
     57    public String getZoneName() { 
     58        return zoneName; 
    4459    } 
    4560 
     
    6277    } 
    6378 
     79    /** 
     80     * Implement UPnPEventListener. Update the zone's properties and fire off 
     81     * property change events. 
     82     */ 
    6483    public void notifyUPnPEvent(String deviceId, String serviceId, Dictionary events) { 
    65         Enumeration en = events.keys(); 
    66         System.out.println(); 
    67         System.out.println("Event!"); 
    68         System.out.println("UDN: " + deviceId); 
    69         System.out.println("ServiceID: " + serviceId); 
    70         for (; en.hasMoreElements();) { 
    71             String ssvName = (String) en.nextElement(); 
    72             Object value = events.get(ssvName); 
    73             System.out.println("Variable: " + ssvName + " = " + value); 
     84        if (serviceId.equals("urn:upnp-org:serviceId:DeviceProperties")) { 
     85            for (Enumeration<String> keys = events.keys(); keys.hasMoreElements();) { 
     86                String key = keys.nextElement(); 
     87                if (key.equals("ZoneName")) 
     88                    pcs.firePropertyChange(key, zoneName, zoneName = (String) events 
     89                            .get(key)); 
     90            } 
     91            // return; 
     92        } 
     93        if (serviceId.equals("urn:upnp-org:serviceId:ZoneGroupTopology")) { 
     94            for (Enumeration<String> keys = events.keys(); keys.hasMoreElements();) { 
     95 
     96            } 
     97            // return; 
    7498        } 
    7599    } 
     100 
     101    /** 
     102     * Adds a listener to receive property change events from this zone. 
     103     *  
     104     * @param listener 
     105     *            a PropertyChangeListener implementation 
     106     */ 
     107    public void addPropertyChangeListener(PropertyChangeListener listener) { 
     108        pcs.addPropertyChangeListener(listener); 
     109    } 
     110 
     111    /** 
     112     * Removes the specified listener so that it no longer receives property 
     113     * change events from this zone. 
     114     *  
     115     * @param listener 
     116     *            a PropertyChangeListener implementation 
     117     */ 
     118    public void removePropertyChangeListener(PropertyChangeListener listener) { 
     119        pcs.removePropertyChangeListener(listener); 
     120    } 
     121 
    76122} 
  • trunk/sonorous/uk.co.matbooth.sonorous/src/uk/co/matbooth/sonorous/zones/ZoneLabelProvider.java

    r140 r141  
    2222        switch (columnIndex) { 
    2323        case 0: 
    24             return zone.getRootUDN(); 
     24            return zone.getZoneName(); 
    2525        default: 
    2626            return "?"; 
  • trunk/sonorous/uk.co.matbooth.sonorous/src/uk/co/matbooth/sonorous/zones/ZoneManager.java

    r140 r141  
    88package uk.co.matbooth.sonorous.zones; 
    99 
     10import java.beans.PropertyChangeEvent; 
     11import java.beans.PropertyChangeListener; 
    1012import java.util.ArrayList; 
    1113import java.util.HashSet; 
     
    2123 * and notifies interested parties of changes to that model. 
    2224 */ 
    23 public class ZoneManager implements DeviceListener { 
     25public class ZoneManager implements DeviceListener, PropertyChangeListener { 
    2426    private static ZoneManager provider; 
    2527 
     
    6062            Zone z = new Zone(event.getDevice()); 
    6163            if (zones.add(z)) { 
     64                z.addPropertyChangeListener(this); 
    6265                z.startSubscriptions(); 
    6366                fireZoneChanged(); 
     
    7679                if (z.getRootUDN().equals(event.getDevice().getUDN())) { 
    7780                    z.stopSubscriptions(); 
     81                    z.removePropertyChangeListener(this); 
    7882                    i.remove(); 
    7983                    fireZoneChanged(); 
     
    8185            } 
    8286        } 
     87    } 
     88 
     89    /** 
     90     * Implement PropertyChangeListener. Fire an event when a zone changes. 
     91     */ 
     92    public void propertyChange(PropertyChangeEvent evt) { 
     93        fireZoneChanged(); 
    8394    } 
    8495