Skip to content
Permalink
Browse files
Moved UPnPEventNotifier.java and EventSource to the "extra" bundle.
Added dependencies with "extra" bundle to the examples projects   
Modified the evented statevariables of the examples to use UPnPLocalStateVariable (since R4) (we used UPnPStateVariableDescriptor.java now removed)
Reorganized imports with the following order:
java,javax,com,org,org.apache,org.osgi,org.apache.felix,org.apache.felix.upnp

git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@607731 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information
Francesco Furfari committed Dec 31, 2007
1 parent 8742e93 commit be59982a4deb2143320ba45fe3214d273d53536a
Show file tree
Hide file tree
Showing 52 changed files with 547 additions and 832 deletions.
@@ -19,6 +19,12 @@

package org.apache.felix.upnp.basedriver;

import org.cybergarage.upnp.UPnP;

import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceRegistration;

import org.apache.felix.upnp.basedriver.controller.impl.DriverControllerImpl;
import org.apache.felix.upnp.basedriver.export.RootDeviceExportingQueue;
import org.apache.felix.upnp.basedriver.export.RootDeviceListener;
@@ -33,10 +39,6 @@
import org.apache.felix.upnp.basedriver.tool.Util;
import org.apache.felix.upnp.extra.controller.DevicesInfo;
import org.apache.felix.upnp.extra.controller.DriverController;
import org.cybergarage.upnp.UPnP;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceRegistration;

/*
* @author <a href="mailto:dev@felix.apache.org">Felix Project Team</a>
@@ -19,15 +19,14 @@

package org.apache.felix.upnp.basedriver.controller.impl;

import org.apache.felix.upnp.extra.controller.DevicesInfo;
import org.apache.felix.upnp.extra.controller.DriverController;

import org.cybergarage.upnp.Device;
import org.cybergarage.upnp.Service;

import org.apache.felix.upnp.basedriver.Activator;
import org.apache.felix.upnp.basedriver.importer.core.MyCtrlPoint;
import org.apache.felix.upnp.basedriver.tool.Logger;
import org.apache.felix.upnp.extra.controller.DevicesInfo;
import org.apache.felix.upnp.extra.controller.DriverController;

/*
* @author <a href="mailto:dev@felix.apache.org">Felix Project Team</a>
@@ -26,9 +26,9 @@
import org.cybergarage.upnp.Argument;
import org.cybergarage.upnp.ArgumentList;
import org.cybergarage.upnp.Device;
import org.cybergarage.upnp.RootDescription;
import org.cybergarage.upnp.Service;
import org.cybergarage.upnp.StateVariable;
import org.cybergarage.upnp.RootDescription;
import org.cybergarage.upnp.xml.DeviceData;
import org.cybergarage.xml.Node;

@@ -36,11 +36,11 @@
import org.osgi.framework.ServiceReference;
import org.osgi.service.upnp.UPnPAction;
import org.osgi.service.upnp.UPnPDevice;
import org.osgi.service.upnp.UPnPException;
import org.osgi.service.upnp.UPnPService;

import org.apache.felix.upnp.basedriver.Activator;
import org.apache.felix.upnp.extra.util.Converter;
import org.osgi.service.upnp.UPnPException;

/*
* @author <a href="mailto:dev@felix.apache.org">Felix Project Team</a>
@@ -29,10 +29,10 @@
import org.cybergarage.upnp.UPnPStatus;

import org.osgi.service.upnp.UPnPAction;
import org.osgi.service.upnp.UPnPException;
import org.osgi.service.upnp.UPnPStateVariable;

import org.apache.felix.upnp.extra.util.Converter;
import org.osgi.service.upnp.UPnPException;

/*
* @author <a href="mailto:dev@felix.apache.org">Felix Project Team</a>
@@ -25,13 +25,15 @@
import java.util.Hashtable;
import java.util.Vector;

import org.apache.felix.upnp.basedriver.importer.util.DeviceSetup;
import org.cybergarage.upnp.Device;

import org.osgi.framework.BundleContext;
import org.osgi.service.upnp.UPnPDevice;
import org.osgi.service.upnp.UPnPIcon;
import org.osgi.service.upnp.UPnPService;

import org.apache.felix.upnp.basedriver.importer.util.DeviceSetup;

/*
* @author <a href="mailto:dev@felix.apache.org">Felix Project Team</a>
*/
@@ -19,8 +19,12 @@

package org.apache.felix.upnp.basedriver.importer.util;

import java.io.*;
import java.net.*;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.net.URL;

/**
* @author <a href="mailto:dev@felix.apache.org">Felix Project Team</a>
@@ -19,13 +19,17 @@

package org.apache.felix.upnp.extra.util;

import java.text.*;
import java.util.*;
import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

//import org.apache.xerces.impl.dv.util.*;
import org.apache.xerces.impl.dv.util.Base64;
import org.apache.xerces.impl.dv.util.HexBin;
import org.osgi.service.upnp.*;

import org.osgi.service.upnp.UPnPStateVariable;

/**
* @author <a href="mailto:dev@felix.apache.org">Felix Project Team</a>
@@ -17,14 +17,13 @@
* under the License.
*/

package org.apache.felix.upnp.sample.binaryLight;
package org.apache.felix.upnp.extra.util;

import java.beans.PropertyChangeListener;

/*
* @author <a href="mailto:dev@felix.apache.org">Felix Project Team</a>
* @author <a href="mailto:felix-dev@incubator.apache.org">Felix Project Team</a>
*/

public interface EventSource {
void addPropertyChangeListener(PropertyChangeListener listener);
void addPropertyChangeListener(String propertyName, PropertyChangeListener listener);
@@ -17,7 +17,7 @@
* under the License.
*/

package org.apache.felix.upnp.sample.clock;
package org.apache.felix.upnp.extra.util;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
@@ -34,41 +34,77 @@
import org.osgi.framework.ServiceReference;
import org.osgi.service.upnp.UPnPDevice;
import org.osgi.service.upnp.UPnPEventListener;
import org.osgi.service.upnp.UPnPLocalStateVariable;
import org.osgi.service.upnp.UPnPService;
import org.osgi.service.upnp.UPnPStateVariable;

/*
* @author <a href="mailto:dev@felix.apache.org">Felix Project Team</a>
*/

public class UPnPEventNotifier implements PropertyChangeListener,ServiceListener {
public class UPnPEventNotifier implements PropertyChangeListener, ServiceListener {
private BundleContext context;
private UPnPDevice device;
private UPnPService service;
private EventSource source;

private Properties UPnPTargetListener;
private String deviceId;
private String serviceId;

private Properties UPnPTargetListener;
private Vector upnpListeners = new Vector();
final private Object LOCK = new Object();

public UPnPEventNotifier(BundleContext context,UPnPDevice device,UPnPService service,EventSource source){

private class NotifierThread extends Thread {
private ServiceReference sr;
private Properties events;

public NotifierThread(ServiceReference sr, Properties events){
super("UPnPEventNotifier");
this.sr = sr;
this.events = events;
}

public void run(){
try {
UPnPEventListener listener = (UPnPEventListener) context.getService(sr);
listener.notifyUPnPEvent(deviceId,serviceId,events);
context.ungetService(sr);
} catch (Exception ignored){
}
}
}

public UPnPEventNotifier(BundleContext context, UPnPDevice device, UPnPService service, EventSource source){
this(context,device,service);

if (source != null){
UPnPStateVariable[] vars = service.getStateVariables();
if (vars != null){
for (int i=0;i<vars.length;i++){
if(vars[i].sendsEvents()){
source.addPropertyChangeListener(vars[i].getName(),this);
}
}
}
}

}

public UPnPEventNotifier(BundleContext context, UPnPDevice device, UPnPService service){
this.context=context;
this.device=device;
this.service=service;
this.source=source;
this.serviceId=service.getId();
setupUPnPListenerHouseKeeping(device);
setupUPnPListenerWatcher();
}

/**
* @param deviceId
*/
private void setupUPnPListenerHouseKeeping(UPnPDevice device) {
UPnPTargetListener = new Properties();
private void setupUPnPListenerWatcher() {
Dictionary dict = device.getDescriptions(null);
deviceId = (String) dict.get(UPnPDevice.ID);
this.deviceId = (String) dict.get(UPnPDevice.ID);
this.serviceId=service.getId();
UPnPTargetListener = new Properties();
UPnPTargetListener.put(UPnPDevice.ID,deviceId);
UPnPTargetListener.put(UPnPService.ID,serviceId);
UPnPTargetListener.put(UPnPDevice.TYPE,dict.get(UPnPDevice.TYPE));
@@ -82,8 +118,7 @@ private void setupUPnPListenerHouseKeeping(UPnPDevice device) {
} catch (Exception ex) {
System.out.println(ex);
}



ServiceReference[] listeners = null;
try {
listeners = context.getServiceReferences(UPnPEventListener.class.getName(),null);
@@ -101,56 +136,69 @@ private void setupUPnPListenerHouseKeeping(UPnPDevice device) {
} catch (Exception ex) {
System.out.println(ex);
}

Properties status = getCurrentStatus();
if (! status.isEmpty())
doNotify(status);

}
} // end LOCK

}

if (source!=null){
UPnPStateVariable[] vars = service.getStateVariables();
if (vars != null){
for (int i=0;i<vars.length;i++)
if(vars[i].sendsEvents())
source.addPropertyChangeListener(vars[i].getName(),this);
}
private Properties getCurrentStatus() {
Properties status = new Properties();
UPnPStateVariable[] vars = service.getStateVariables();
if (vars != null){
for (int i=0;i<vars.length;i++){
if(vars[i].sendsEvents()){
if (vars[i] instanceof UPnPLocalStateVariable){
UPnPLocalStateVariable localVar = (UPnPLocalStateVariable) vars[i];
String property = localVar.getName();
Object value = localVar.getCurrentValue();
status.put(property,value);
}
else {
System.out.println("[Warning] The StateVariable (" +vars[i].getName() +")"
+ " of device (ID:" + deviceId + ") "
+ "should implement the interface UPnPLocalStateVariable");
}
}
} // end for
}


return status;
}

/* (non-Javadoc)
* @see java.beans.PropertyChangeListener#propertyChange(java.beans.PropertyChangeEvent)
*/
public void propertyChange(PropertyChangeEvent evt) {

synchronized (LOCK) {

String property = evt.getPropertyName();
Object value = evt.getNewValue();
String valueString = value.toString();
Properties events = new Properties();
events.put(property,valueString);
doNotify(events);
}

private void doNotify(Properties events){

synchronized (LOCK) {
Iterator list = upnpListeners.iterator();
String property = evt.getPropertyName();
Object value = evt.getNewValue();
String valueString = value.toString();
final Properties events = new Properties();
events.put(property,valueString);
while (list.hasNext()){
final ServiceReference sr = (ServiceReference)list.next();
String[] props =sr.getPropertyKeys();
new Thread(){
public void run(){
try {
UPnPEventListener listener = (UPnPEventListener) context.getService(sr);
listener.notifyUPnPEvent(deviceId,serviceId,events);
context.ungetService(sr);
} catch (Exception ex){
System.out.println("Clock UPnPEventNotifier Err: " +ex);
System.out.println("context: " +context);
System.out.println("listener: " +context.getService(sr));
System.out.println("sr: " +sr);
System.out.println();
}
}
}.start();
new NotifierThread(sr,events).start();
}
}
}
}

private void doNotify(ServiceReference sr) {
Properties status = getCurrentStatus();
if (! status.isEmpty())
new NotifierThread(sr,status).start();
}


/* (non-Javadoc)
* @see org.osgi.framework.ServiceListener#serviceChanged(org.osgi.framework.ServiceEvent)
*/
@@ -160,10 +208,15 @@ public void serviceChanged(ServiceEvent e) {
switch(e.getType()){
case ServiceEvent.REGISTERED:{
Filter filter = (Filter) sr.getProperty(UPnPEventListener.UPNP_FILTER);
if (filter == null) addNewListener(sr);
if (filter == null) {
addNewListener(sr);
doNotify(sr);
}
else {
if (filter.match(UPnPTargetListener))
if (filter.match(UPnPTargetListener)){
addNewListener(sr);
doNotify(sr);
}
}
};break;

@@ -186,6 +239,7 @@ public void serviceChanged(ServiceEvent e) {

}


/**
* @param reference
*/

0 comments on commit be59982

Please sign in to comment.