felix-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From furf...@apache.org
Subject svn commit: r471335 - /incubator/felix/sandbox/furfari/upnp.jmxbridge/src/main/java/org/apache/felix/upnp/jmxbridge/
Date Sun, 05 Nov 2006 02:30:20 GMT
Author: furfari
Date: Sat Nov  4 18:30:19 2006
New Revision: 471335

URL: http://svn.apache.org/viewvc?view=rev&rev=471335
Log:
Added subscribe methods to manage remote UPnPEventListener
Not all possible sequences of subscription are correctly managed yet
First solution to export Local UPnPDevice

Added:
    incubator/felix/sandbox/furfari/upnp.jmxbridge/src/main/java/org/apache/felix/upnp/jmxbridge/UPnPDeviceImpl.java
    incubator/felix/sandbox/furfari/upnp.jmxbridge/src/main/java/org/apache/felix/upnp/jmxbridge/UPnPDeviceImplMBean.java
Removed:
    incubator/felix/sandbox/furfari/upnp.jmxbridge/src/main/java/org/apache/felix/upnp/jmxbridge/UPnPDeviceMBean.java
    incubator/felix/sandbox/furfari/upnp.jmxbridge/src/main/java/org/apache/felix/upnp/jmxbridge/UPnPDeviceMBeanImpl.java
Modified:
    incubator/felix/sandbox/furfari/upnp.jmxbridge/src/main/java/org/apache/felix/upnp/jmxbridge/DriverProxy.java
    incubator/felix/sandbox/furfari/upnp.jmxbridge/src/main/java/org/apache/felix/upnp/jmxbridge/UPnPControlPoint.java
    incubator/felix/sandbox/furfari/upnp.jmxbridge/src/main/java/org/apache/felix/upnp/jmxbridge/UPnPControlPointMBean.java
    incubator/felix/sandbox/furfari/upnp.jmxbridge/src/main/java/org/apache/felix/upnp/jmxbridge/UPnPJMXBridge.java

Modified: incubator/felix/sandbox/furfari/upnp.jmxbridge/src/main/java/org/apache/felix/upnp/jmxbridge/DriverProxy.java
URL: http://svn.apache.org/viewvc/incubator/felix/sandbox/furfari/upnp.jmxbridge/src/main/java/org/apache/felix/upnp/jmxbridge/DriverProxy.java?view=diff&rev=471335&r1=471334&r2=471335
==============================================================================
--- incubator/felix/sandbox/furfari/upnp.jmxbridge/src/main/java/org/apache/felix/upnp/jmxbridge/DriverProxy.java
(original)
+++ incubator/felix/sandbox/furfari/upnp.jmxbridge/src/main/java/org/apache/felix/upnp/jmxbridge/DriverProxy.java
Sat Nov  4 18:30:19 2006
@@ -49,9 +49,13 @@
     }
     
     public String getDeviceDescriptionURI(String udn){
-        if (devicesInfo != null)
-            return devicesInfo.getLocationURL(udn);
-        return "";
+        if (devicesInfo != null){
+        	try {
+        		String uri =  devicesInfo.getLocationURL(udn);
+        		return uri;
+        	}catch (Exception ignored){}
+        }
+        return null;
     }
     
     public String getServiceDescriptionURI(String udn,String serviceId){

Modified: incubator/felix/sandbox/furfari/upnp.jmxbridge/src/main/java/org/apache/felix/upnp/jmxbridge/UPnPControlPoint.java
URL: http://svn.apache.org/viewvc/incubator/felix/sandbox/furfari/upnp.jmxbridge/src/main/java/org/apache/felix/upnp/jmxbridge/UPnPControlPoint.java?view=diff&rev=471335&r1=471334&r2=471335
==============================================================================
--- incubator/felix/sandbox/furfari/upnp.jmxbridge/src/main/java/org/apache/felix/upnp/jmxbridge/UPnPControlPoint.java
(original)
+++ incubator/felix/sandbox/furfari/upnp.jmxbridge/src/main/java/org/apache/felix/upnp/jmxbridge/UPnPControlPoint.java
Sat Nov  4 18:30:19 2006
@@ -64,9 +64,9 @@
     this.sr = context.getServiceReference(MBeanServer.class.getName());
     if (sr!=null){
       DriverProxy driver = new DriverProxy(context);
-      UPnPDeviceMBeanImpl.setDriver(driver);
+      UPnPDeviceImpl.setDriver(driver);
       this.connectToAgent(sr);
-      bridge = new UPnPJMXBridge(this,context,server);
+      bridge = new UPnPJMXBridge(this,context,driver,server);
    }
     
     

Modified: incubator/felix/sandbox/furfari/upnp.jmxbridge/src/main/java/org/apache/felix/upnp/jmxbridge/UPnPControlPointMBean.java
URL: http://svn.apache.org/viewvc/incubator/felix/sandbox/furfari/upnp.jmxbridge/src/main/java/org/apache/felix/upnp/jmxbridge/UPnPControlPointMBean.java?view=diff&rev=471335&r1=471334&r2=471335
==============================================================================
--- incubator/felix/sandbox/furfari/upnp.jmxbridge/src/main/java/org/apache/felix/upnp/jmxbridge/UPnPControlPointMBean.java
(original)
+++ incubator/felix/sandbox/furfari/upnp.jmxbridge/src/main/java/org/apache/felix/upnp/jmxbridge/UPnPControlPointMBean.java
Sat Nov  4 18:30:19 2006
@@ -27,7 +27,6 @@
 
 public interface UPnPControlPointMBean extends TabIfc {
   public String getBaseDriverVendor();
-  //public void registerListener(UPnPFilter filter, handback);
   
 
 }

Added: incubator/felix/sandbox/furfari/upnp.jmxbridge/src/main/java/org/apache/felix/upnp/jmxbridge/UPnPDeviceImpl.java
URL: http://svn.apache.org/viewvc/incubator/felix/sandbox/furfari/upnp.jmxbridge/src/main/java/org/apache/felix/upnp/jmxbridge/UPnPDeviceImpl.java?view=auto&rev=471335
==============================================================================
--- incubator/felix/sandbox/furfari/upnp.jmxbridge/src/main/java/org/apache/felix/upnp/jmxbridge/UPnPDeviceImpl.java
(added)
+++ incubator/felix/sandbox/furfari/upnp.jmxbridge/src/main/java/org/apache/felix/upnp/jmxbridge/UPnPDeviceImpl.java
Sat Nov  4 18:30:19 2006
@@ -0,0 +1,213 @@
+package org.apache.felix.upnp.jmxbridge;
+
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.util.Dictionary;
+import java.util.Enumeration;
+import java.util.HashSet;
+import java.util.Hashtable;
+import java.util.Iterator;
+
+import javax.management.Notification;
+import javax.management.NotificationBroadcasterSupport;
+import javax.management.ObjectName;
+
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.Filter;
+import org.osgi.framework.InvalidSyntaxException;
+import org.osgi.framework.ServiceRegistration;
+import org.osgi.service.upnp.UPnPAction;
+import org.osgi.service.upnp.UPnPDevice;
+import org.osgi.service.upnp.UPnPEventListener;
+import org.osgi.service.upnp.UPnPService;
+
+public class UPnPDeviceImpl extends NotificationBroadcasterSupport 
+implements UPnPDeviceImplMBean, UPnPEventListener
+{
+
+    static DriverProxy UPnPDriver;
+
+    private ObjectName myObjectName;
+	
+    private UPnPDevice device;
+    
+    private URL deviceDescriptionURL = null;
+    private String deviceDescriptionData = null;
+    
+	private Hashtable serviceDescriptionURLs = new Hashtable();
+	private Hashtable serviceDescriptionData = new Hashtable();
+
+	private Dictionary serializableDescription;
+
+
+	private BundleContext context;
+
+	private HashSet filterSet;
+	private Filter subscriptionFilters;
+	private Dictionary subscriptionDictionary;
+	private ServiceRegistration listner_reg;
+
+
+
+	
+	public static void setDriver(DriverProxy driver) {
+		UPnPDeviceImpl.UPnPDriver	= driver;	
+	}
+
+	public UPnPDeviceImpl(BundleContext context, UPnPDevice device) {
+		this.context = context;
+		this.device = device;
+		
+		String UDN = (String) device.getDescriptions(null).get(UPnPDevice.UDN);
+		try {
+			String url = UPnPDriver.getDeviceDescriptionURI(UDN);
+			deviceDescriptionURL = new URL (url);
+		} catch (MalformedURLException e) {
+			// TODO Auto-generated catch block
+			e.printStackTrace();
+		}
+		UPnPService[] services = device.getServices();
+		for (int i=0;i<services.length;i++){
+			String id = services[i].getId();
+			try {
+				String url = UPnPDriver.getServiceDescriptionURI(UDN, id);
+				serviceDescriptionURLs.put(id, new URL (url));
+			} catch (MalformedURLException e) {
+				// TODO Auto-generated catch block
+				e.printStackTrace();
+			}
+		}
+		
+	}
+
+	public void setName(ObjectName name) {
+		myObjectName = name;		
+	}
+	
+
+	public Dictionary getDeviceDescription() {
+		if (serializableDescription == null){
+			serializableDescription = new Hashtable();
+			Dictionary oldDescription = device.getDescriptions(null);
+			Enumeration keys = oldDescription.keys();
+			while (keys.hasMoreElements()){
+				Object key = keys.nextElement();
+				serializableDescription.put(key, oldDescription.get(key));
+			}
+		}
+		return serializableDescription;
+	}
+
+	public String getXMLDeviceDescription() {
+		if (deviceDescriptionData == null)
+			deviceDescriptionData = getStreamData(deviceDescriptionURL);
+		return deviceDescriptionData;
+	}
+
+	public String getXMLServiceDescription(String serviceId) {
+		String data = (String) serviceDescriptionData.get(serviceId);
+		if (data == null){
+			URL url = (URL) serviceDescriptionURLs.get(serviceId);
+			data = getStreamData(url);
+			serviceDescriptionData.put(serviceId, data);
+		}
+		return data;
+	}
+	
+	private String getStreamData(URL url){
+		StringBuffer urlData = new StringBuffer();
+		if (url != null){
+		    InputStreamReader isr;
+			try {
+				isr = new InputStreamReader(url.openStream());
+	            BufferedReader reader = new BufferedReader(isr);
+	            String response = reader.readLine();
+	            while( null != response ) {
+	            	urlData.append(response).append('\n');
+		            response = reader.readLine();
+	            }
+	            isr.close();
+			} catch (IOException e) {
+				e.printStackTrace();
+			}
+		}
+		return urlData.toString();	
+	}
+
+	public Dictionary invokeAction(String serviceId, String actionName, Dictionary params) {
+		Dictionary result = null;
+		UPnPService service = device.getService(serviceId);
+		if (service != null){
+			UPnPAction action = service.getAction(actionName);
+			if (action != null){
+				try {
+					result = action.invoke(params);
+				} catch (Exception e) {
+					e.printStackTrace();
+				}
+			}
+		}
+		return result;
+	}
+	
+	
+	public void subscribe(String keys) throws InvalidSyntaxException {
+		if (listner_reg == null){
+			filterSet = new HashSet();
+			filterSet.add(keys);
+			subscriptionFilters = context.createFilter(keys);
+			subscriptionDictionary =  new Hashtable();
+			subscriptionDictionary.put(UPnPEventListener.UPNP_FILTER, subscriptionFilters);
+			listner_reg = context.registerService(UPnPEventListener.class.getName(), this, subscriptionDictionary);
+		}
+		else {
+			if (!filterSet.contains(keys)){
+				subscriptionFilters = context.createFilter("(|" + subscriptionFilters.toString() + keys
+ ")");
+				subscriptionDictionary.put(UPnPEventListener.UPNP_FILTER, subscriptionFilters);
+				listner_reg.setProperties(subscriptionDictionary);	
+			}
+		}
+		
+	}
+
+	public void unsubscribe(String filter) throws InvalidSyntaxException {
+		if (filterSet.contains(filter)){
+			filterSet.remove(filter);
+			if (! filterSet.isEmpty()){
+		        StringBuffer filterStr = new StringBuffer().append("(|");
+	            for(Iterator iter = filterSet.iterator(); iter.hasNext();)
+	            {
+	            	filterStr.append((String)iter.next());
+	            }
+	            filterStr.append(")");
+	            subscriptionFilters = context.createFilter(filterStr.toString());
+				subscriptionDictionary.put(UPnPEventListener.UPNP_FILTER, subscriptionFilters);
+				listner_reg.setProperties(subscriptionDictionary);	
+			}
+			else {
+				listner_reg.unregister();
+				listner_reg = null;
+				filterSet = null;
+				subscriptionFilters = null;
+				subscriptionDictionary = null;
+			}
+		}
+		
+	}
+
+	public void notifyUPnPEvent(String deviceId, String serviceId, Dictionary events) {
+		Hashtable params = new Hashtable();
+		params.put(UPnPDevice.ID, deviceId);
+		params.put(UPnPService.ID, serviceId);
+		params.put("events", events);
+		Notification notification = new Notification("org.apache.felix.upnp.jmxbridge.mbean.upnpEvent",
myObjectName,0);
+		notification.setUserData(params);
+		sendNotification(notification);	
+	}
+
+
+
+}

Added: incubator/felix/sandbox/furfari/upnp.jmxbridge/src/main/java/org/apache/felix/upnp/jmxbridge/UPnPDeviceImplMBean.java
URL: http://svn.apache.org/viewvc/incubator/felix/sandbox/furfari/upnp.jmxbridge/src/main/java/org/apache/felix/upnp/jmxbridge/UPnPDeviceImplMBean.java?view=auto&rev=471335
==============================================================================
--- incubator/felix/sandbox/furfari/upnp.jmxbridge/src/main/java/org/apache/felix/upnp/jmxbridge/UPnPDeviceImplMBean.java
(added)
+++ incubator/felix/sandbox/furfari/upnp.jmxbridge/src/main/java/org/apache/felix/upnp/jmxbridge/UPnPDeviceImplMBean.java
Sat Nov  4 18:30:19 2006
@@ -0,0 +1,17 @@
+package org.apache.felix.upnp.jmxbridge;
+
+import java.util.Dictionary;
+
+import org.osgi.framework.InvalidSyntaxException;
+
+
+public interface UPnPDeviceImplMBean {
+
+	public Dictionary getDeviceDescription();
+	public String getXMLDeviceDescription();
+	public String getXMLServiceDescription(String serviceId);
+	public Dictionary invokeAction(String serviceId,String ActionName, Dictionary params);
+	public void subscribe(String filter) throws InvalidSyntaxException;
+	public void unsubscribe(String filter) throws InvalidSyntaxException;
+
+}

Modified: incubator/felix/sandbox/furfari/upnp.jmxbridge/src/main/java/org/apache/felix/upnp/jmxbridge/UPnPJMXBridge.java
URL: http://svn.apache.org/viewvc/incubator/felix/sandbox/furfari/upnp.jmxbridge/src/main/java/org/apache/felix/upnp/jmxbridge/UPnPJMXBridge.java?view=diff&rev=471335&r1=471334&r2=471335
==============================================================================
--- incubator/felix/sandbox/furfari/upnp.jmxbridge/src/main/java/org/apache/felix/upnp/jmxbridge/UPnPJMXBridge.java
(original)
+++ incubator/felix/sandbox/furfari/upnp.jmxbridge/src/main/java/org/apache/felix/upnp/jmxbridge/UPnPJMXBridge.java
Sat Nov  4 18:30:19 2006
@@ -24,11 +24,13 @@
 	private Hashtable mbeans = new Hashtable();
 	private RootDeviceListener rootdeviceListener;
 	private UPnPControlPoint controlPoint;
+	private DriverProxy UPnPDriver;
 	
-	public UPnPJMXBridge(UPnPControlPoint point, BundleContext context,MBeanServer server){
+	public UPnPJMXBridge(UPnPControlPoint point, BundleContext context,DriverProxy driver,MBeanServer
server){
 		this.bc= context;
 		this.server = server;
 		this.controlPoint = point;
+		this.UPnPDriver = driver;
 		rootdeviceListener = new RootDeviceListener(context);
 		rootdeviceListener.setDeviceNodeListener(this);
 		rootdeviceListener.activate();
@@ -39,15 +41,31 @@
 			public void run(){
 				ServiceReference sr = device.getReference();
 				UPnPDevice rootDevice = (UPnPDevice) bc.getService(sr);
-				UPnPDeviceMBeanImpl upnpMBean = new UPnPDeviceMBeanImpl(rootDevice);
+				int counter = 0;
+				String UDN = (String) rootDevice.getDescriptions(null).get(UPnPDevice.UDN);
+				String url = UPnPDriver.getDeviceDescriptionURI(UDN);
+				while (url == null && counter < 5){
+					try {
+						Thread.sleep(1000);
+						url = UPnPDriver.getDeviceDescriptionURI(UDN);
+						counter++;
+					} catch (InterruptedException e) {
+						// TODO Auto-generated catch block
+						e.printStackTrace();
+					}
+				}
+				if (url == null){
+					System.out.println("[Warning] not able to instrument UPnPDevice " + UDN);
+					return;
+				}
+				UPnPDeviceImpl upnpMBean = new UPnPDeviceImpl(bc, rootDevice);
 				try {
 					Dictionary properties = rootDevice.getDescriptions(null);
-					String friendlyname=(String) properties.get(UPnPDevice.FRIENDLY_NAME);
+					//String friendlyname=(String) properties.get(UPnPDevice.FRIENDLY_NAME);
 					String quotedUDN= UPnPDevice.UDN +"=\"" +(String) properties.get(UPnPDevice.UDN) +"\"";
-					System.out.println("quotedUDN:: "+quotedUDN);
 					ObjectName name = new ObjectName("UPnPDevice:"+quotedUDN);
-					StandardMBean mbean = new StandardMBean(upnpMBean,UPnPDeviceMBean.class);
-					server.registerMBean(mbean, name);
+					upnpMBean.setName(name);
+					server.registerMBean(upnpMBean, name);
 					mbeans.put(properties.get(UPnPDevice.UDN),name);
 					
 					controlPoint.sendNotification(new Notification("org.apache.felix.upnp.jmxbridge.mbean.registered",name,sequence++));



Mime
View raw message