You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@felix.apache.org by fu...@apache.org on 2008/08/13 16:03:48 UTC

svn commit: r685555 - in /felix/sandbox/furfari/Version-1.0.0/basedriver/src/main/java/org/apache/felix/upnp/basedriver/importer/core: ./ event/message/ event/structs/ event/thread/

Author: furfari
Date: Wed Aug 13 07:03:46 2008
New Revision: 685555

URL: http://svn.apache.org/viewvc?rev=685555&view=rev
Log:
Removed unsed classes SidExipired.java, SidRenewer.java and Renewer.java
Cleaned FirstMessage.java
Subscriber has been temporarily modified to test Felix-642
Added DeviceChangeListener interface to MyCtrlPoint.java
Removed cyber NotifyListener interface from MyCtrlPoint.java
Removed cyber SearchResponseListener from MyCtrlPoint.java
Removed FindDeviceCntrl() method from MyCtrlPoint.java
TODO serviceFromSID MUST be improved
Removed doServiceUpdating() from MyCtrlPoint.java
Improved recursion for doDeviceRegistration() in MyCtrlPoint.java
Overridden and Synchronized  SubScriber.java() in MyCtrlPoint.java for FELIX-642 (temporarily patch)

Removed:
    felix/sandbox/furfari/Version-1.0.0/basedriver/src/main/java/org/apache/felix/upnp/basedriver/importer/core/event/message/SidExipired.java
    felix/sandbox/furfari/Version-1.0.0/basedriver/src/main/java/org/apache/felix/upnp/basedriver/importer/core/event/structs/SidRenewer.java
    felix/sandbox/furfari/Version-1.0.0/basedriver/src/main/java/org/apache/felix/upnp/basedriver/importer/core/event/thread/Renewer.java
Modified:
    felix/sandbox/furfari/Version-1.0.0/basedriver/src/main/java/org/apache/felix/upnp/basedriver/importer/core/MyCtrlPoint.java
    felix/sandbox/furfari/Version-1.0.0/basedriver/src/main/java/org/apache/felix/upnp/basedriver/importer/core/event/message/FirstMessage.java
    felix/sandbox/furfari/Version-1.0.0/basedriver/src/main/java/org/apache/felix/upnp/basedriver/importer/core/event/thread/SubScriber.java

Modified: felix/sandbox/furfari/Version-1.0.0/basedriver/src/main/java/org/apache/felix/upnp/basedriver/importer/core/MyCtrlPoint.java
URL: http://svn.apache.org/viewvc/felix/sandbox/furfari/Version-1.0.0/basedriver/src/main/java/org/apache/felix/upnp/basedriver/importer/core/MyCtrlPoint.java?rev=685555&r1=685554&r2=685555&view=diff
==============================================================================
--- felix/sandbox/furfari/Version-1.0.0/basedriver/src/main/java/org/apache/felix/upnp/basedriver/importer/core/MyCtrlPoint.java (original)
+++ felix/sandbox/furfari/Version-1.0.0/basedriver/src/main/java/org/apache/felix/upnp/basedriver/importer/core/MyCtrlPoint.java Wed Aug 13 07:03:46 2008
@@ -35,11 +35,15 @@
 import org.cybergarage.upnp.ServiceList;
 import org.cybergarage.upnp.ServiceStateTable;
 import org.cybergarage.upnp.StateVariable;
+import org.cybergarage.upnp.UPnP;
+import org.cybergarage.upnp.device.DeviceChangeListener;
 import org.cybergarage.upnp.device.NotifyListener;
 import org.cybergarage.upnp.device.SearchResponseListener;
 import org.cybergarage.upnp.event.NotifyRequest;
 import org.cybergarage.upnp.event.Property;
 import org.cybergarage.upnp.event.PropertyList;
+import org.cybergarage.upnp.event.SubscriptionRequest;
+import org.cybergarage.upnp.event.SubscriptionResponse;
 import org.cybergarage.upnp.ssdp.SSDPPacket;
 
 import org.osgi.framework.BundleContext;
@@ -72,14 +76,15 @@
 */
 public class MyCtrlPoint extends ControlPoint
 		implements
-			NotifyListener,
-			SearchResponseListener,
-			ServiceListener
+			//NotifyListener,
+			//SearchResponseListener,
+			ServiceListener,
+			DeviceChangeListener
 {
 	private BundleContext context;   
     private Hashtable devices;//key UDN value OsgideviceInfo(Osgi)
 	private SubscriptionQueue subQueue;
-	private NotifierQueue notifierQueue;
+	public NotifierQueue notifierQueue;
     
     private final String UPNP_EVENT_LISTENER_FLTR =
         "(" + Constants.OBJECTCLASS + "=" + UPnPEventListener.class.getName() + ")";
@@ -89,6 +94,8 @@
         "(" + UPnPDevice.UPNP_EXPORT + "=*" + ")";
     private final String IMPORT_FLTR =
         "(" + org.apache.felix.upnp.basedriver.util.Constants.UPNP_IMPORT + "=*" + ")";
+	private Hashtable waitingEvents;
+//	private Hashtable sidServices;
 
     
     public MyCtrlPoint(BundleContext context, SubscriptionQueue subQueue,
@@ -96,8 +103,11 @@
 		super();
 		this.context = context;
         devices = new Hashtable();
-		addNotifyListener(this);
-		addSearchResponseListener(this);
+		//addNotifyListener(this);
+        addDeviceChangeListener(this);
+		//addSearchResponseListener(this);
+        waitingEvents = new Hashtable();
+//        sidServices = new Hashtable();
 		try {
 			context.addServiceListener(this, UPNP_EVENT_LISTENER_FLTR);
 		} catch (InvalidSyntaxException e) {
@@ -114,9 +124,9 @@
         if (httpReq.isNotifyRequest() == true) {
             Activator.logger.DEBUG("[Importer] Notify Request");
 			NotifyRequest notifyReq = new NotifyRequest(httpReq);
-			String uuid = notifyReq.getSID();
-			long seq = notifyReq.getSEQ();
-			PropertyList props = notifyReq.getPropertyList();
+//			String uuid = notifyReq.getSID();
+//			long seq = notifyReq.getSEQ();
+//			PropertyList props = notifyReq.getPropertyList();
 //			int propCnt = props.size();
 //			Hashtable hash = new Hashtable();
 //			for (int n = 0; n < propCnt; n++) {
@@ -125,7 +135,7 @@
 //				String varValue = prop.getValue();
 //				hash.put(varName, varValue);
 //			}
-            newEventArrived(uuid, seq, props);
+            newEventArrived(notifyReq);
 			httpReq.returnOK();
 			return;
 		}
@@ -154,31 +164,9 @@
 			}
 		}
 	}
-
-	/*
-	 * (non-Javadoc)
-	 * 
-	 * @see org.cybergarage.upnp.device.NotifyListener#deviceNotifyReceived(org.cybergarage.upnp.ssdp.SSDPPacket)
-	 */
-	public void deviceNotifyReceived(SSDPPacket ssdpPacket) {
-        Activator.logger.DEBUG("[Importer] deviceNotifyReceived");
-        Activator.logger.PACKET(ssdpPacket.toString());
-		/*
-		 * if the packet is 
-		 * 		NOTIFY or ISALIVE or *new* ROOT	then create and register the UPnPDevice and 
-		 * 										all the embeeded device too
-		 * 		DEVICE or SERVICE	then if they already exist in OSGi do nothing otherwise I'll create and 
-		 * 							register all the UPnPDevice need starting from the root device
-		 * 		*root* BYEBYE		then I'll unregister it and all its children from OSGi Framework 
-		 * 		*service* BYEBYE	then I'll re-register the UPnPDevice that contain the service with the updated
-		 * 							properties 
-		 * 		*device* BYEBYE		then I'll re-register the UPnPDevice that contain the device with the updated
-		 * 							properties and also unregister the UPnPDevice that has left
-		 */
-		String usn = ssdpPacket.getUSN();
-		ParseUSN parseUSN = new ParseUSN(usn);
-		String udn = parseUSN.getUDN();
-        
+	
+	public void deviceAdded(Device dev) {
+		String udn = dev.getUDN();
 		ServiceReference[] refs = null;
 		String filter = "(&" + UPNP_DEVICE_FLTR + EXPORT_FLTR+ ")";
 		try {
@@ -188,99 +176,176 @@
 		}
 		if (refs != null) {
 			for (int i = 0; i < refs.length; i++) {
-				UPnPDevice dev = (UPnPDevice) context.getService(refs[i]);
-				Dictionary dic = dev.getDescriptions(null);
+				UPnPDevice osgidev = (UPnPDevice) context.getService(refs[i]);
+				Dictionary dic = osgidev.getDescriptions(null);
 				if (((String) dic.get(UPnPDevice.UDN)).equals(udn)) {
 					return;
 				}
 			}
 		}
+		if(devices.containsKey(udn) == false) {
+            Activator.logger.INFO("[Importer] registering UPnPDevice:"+dev.getFriendlyName()+"("+dev.getUDN()+")" );
+            registerUPnPDevice(dev, null, null);
+            //new SearchForListenerThread(dev).start();
+            searchForListener(dev);
+            /*
+             * now we can register all the device embedded device and service without
+             * recieving the NOTIFY 
+             */
+            //XXX Think about this choice
+            for (Iterator i = dev.getDeviceList().iterator(); i.hasNext();) {
+				Device d = (Device) i.next();
+				if (isDeviceRegistered(udn) == false)
+					doEmbeddedRegistration(d); 
+					//doDeviceRegistration(d.getUDN()); 
+			}
+        }else if(devices.containsKey(udn) == true) {
+        	Activator.logger.INFO("[Importer] UPnPDevice UDN::"+dev.getFriendlyName()+"("+dev.getUDN()+") already registered Skipping");
+        }		
+	}
 
-		if (ssdpPacket.isAlive()) {
+	public void deviceRemoved(Device cyberdevice) {
+		String udn = cyberdevice.getUDN();
+		synchronized (devices) {
 			
-            Activator.logger.DEBUG("[Importer] ssdpPacket.isAlive");
-			if (devices.containsKey(udn)) {
-                Activator.logger.DEBUG("[Importer] Device already discovered");
-				if (parseUSN.isService()) {
-                    doServiceUpdating(udn,parseUSN.getServiceType());
-				}
-			} else {
-                doDeviceRegistration(udn);
-			}
-
-		} else if (ssdpPacket.isByeBye()) {
-            Activator.logger.DEBUG("[Importer] ssdpPacket.isByeBye");
-
-            synchronized (devices) {		
-
-				if (devices.containsKey(udn)) {
-					if (parseUSN.isDevice()) {
-	                    Activator.logger.DEBUG("[Importer] parseUSN.isDevice ...unregistering all the children devices ");
-	                    
-						//unregistering all the children devices 
-						UPnPDeviceImpl dev = ((OSGiDeviceInfo) devices.get(udn)).getOSGiDevice();
-						removeOSGiandUPnPDeviceHierarchy(dev);
-	
-					} else if (parseUSN.isService()) {
-	                    Activator.logger.DEBUG("[Importer] parseUSN.isService ...registering modified device again ");
-						/* 
-						 * I have to unregister the UPnPDevice and register it again 
-						 * with the updated properties  
-						 */
-						UPnPDeviceImpl device = 
-	                        ((OSGiDeviceInfo) devices.get(udn)).getOSGiDevice();
-						ServiceRegistration registar = 
-	                        ((OSGiDeviceInfo) devices.get(udn)).getRegistration();
-						String[] oldServicesID = 
-	                        (String[]) (device.getDescriptions(null).get(UPnPService.ID));
-						String[] oldServiceType = 
-	                        (String[]) (device.getDescriptions(null).get(UPnPService.TYPE));
-	                    
-						Device cyberDevice = findDeviceCtrl(this, udn);
-						Vector vec = new Vector();
-						for (int i = 0; i < oldServiceType.length; i++) {
-							Service ser = cyberDevice.getService(oldServicesID[i]);
-							if (!(ser.getServiceType().equals(parseUSN.getServiceType()))) 
-	                        {
-								vec.add(oldServicesID[i]);
-							}
-						}
-	
-	                    //new serviceID
-						String[] actualServicesID = new String[vec.size()];
-						actualServicesID = (String[]) vec.toArray(new String[]{});
-	
-	                    //new serviceType
-						String[] actualServiceType = new String[oldServiceType.length - 1];
-						vec.clear();
-						for (int i = 0; i < oldServiceType.length; i++) {
-							if (!(oldServiceType[i].equals(parseUSN.getServiceType()))) 
-	                        {
-								vec.add(oldServiceType[i]);
-							}
-						}
-						actualServiceType = (String[]) vec.toArray(new String[]{});
-	
-	                    //unrigistering and registering again with the new properties
-						unregisterUPnPDevice(registar);
-						device.setProperty(UPnPService.ID, actualServicesID);
-						device.setProperty(UPnPService.TYPE, actualServiceType);
-						registerUPnPDevice(null, device, device.getDescriptions(null));
-						searchForListener(cyberDevice);
-					}
-				}
-				
-			}//synchronized(devices)
-		} else {
-			/*
-			 * if it is a service means that it has deleted when the 
-			 * owner was unregister so I can skip this bye-bye
-			 * 
-			 * //TODO Understand the comment
-			 *
-			 */
+		if (devices.containsKey(udn)) {
+
+                Activator.logger.DEBUG("[Importer] parseUSN.isDevice ...unregistering all the children devices ");
+                
+				//unregistering all the children devices 
+				UPnPDeviceImpl dev = ((OSGiDeviceInfo) devices.get(udn)).getOSGiDevice();
+				removeOSGiandUPnPDeviceHierarchy(dev);
+
+			}		
 		}
 	}
+
+	/*
+	 * (non-Javadoc)
+	 * 
+	 * @see org.cybergarage.upnp.device.NotifyListener#deviceNotifyReceived(org.cybergarage.upnp.ssdp.SSDPPacket)
+	 */
+//	public void deviceNotifyReceived(SSDPPacket ssdpPacket) {
+//        Activator.logger.DEBUG("[Importer] deviceNotifyReceived");
+//        Activator.logger.PACKET(ssdpPacket.toString());
+//		/*
+//		 * if the packet is 
+//		 * 		NOTIFY or ISALIVE or *new* ROOT	then create and register the UPnPDevice and 
+//		 * 										all the embeeded device too
+//		 * 		DEVICE or SERVICE	then if they already exist in OSGi do nothing otherwise I'll create and 
+//		 * 							register all the UPnPDevice need starting from the root device
+//		 * 		*root* BYEBYE		then I'll unregister it and all its children from OSGi Framework 
+//		 * 		*service* BYEBYE	then I'll re-register the UPnPDevice that contain the service with the updated
+//		 * 							properties 
+//		 * 		*device* BYEBYE		then I'll re-register the UPnPDevice that contain the device with the updated
+//		 * 							properties and also unregister the UPnPDevice that has left
+//		 */
+//		String usn = ssdpPacket.getUSN();
+//		ParseUSN parseUSN = new ParseUSN(usn);
+//		String udn = parseUSN.getUDN();
+//        
+//		ServiceReference[] refs = null;
+//		String filter = "(&" + UPNP_DEVICE_FLTR + EXPORT_FLTR+ ")";
+//		try {
+//			refs = context.getServiceReferences(UPnPDevice.class.getName(),	filter);
+//		} catch (InvalidSyntaxException e) {
+//			e.printStackTrace();
+//		}
+//		if (refs != null) {
+//			for (int i = 0; i < refs.length; i++) {
+//				UPnPDevice dev = (UPnPDevice) context.getService(refs[i]);
+//				Dictionary dic = dev.getDescriptions(null);
+//				if (((String) dic.get(UPnPDevice.UDN)).equals(udn)) {
+//					return;
+//				}
+//			}
+//		}
+//
+//		if (ssdpPacket.isAlive()) {
+//			
+//            Activator.logger.DEBUG("[Importer] ssdpPacket.isAlive");
+//			if (devices.containsKey(udn)) {
+//                Activator.logger.DEBUG("[Importer] Device already discovered");
+//				if (parseUSN.isService()) {
+//                    doServiceUpdating(udn,parseUSN.getServiceType());
+//				}
+//			} else {
+//                doDeviceRegistration(udn);
+//			}
+//
+//		} else if (ssdpPacket.isByeBye()) {
+//            Activator.logger.DEBUG("[Importer] ssdpPacket.isByeBye");
+//
+//            synchronized (devices) {		
+//
+//				if (devices.containsKey(udn)) {
+//					if (parseUSN.isDevice()) {
+//	                    Activator.logger.DEBUG("[Importer] parseUSN.isDevice ...unregistering all the children devices ");
+//	                    
+//						//unregistering all the children devices 
+//						UPnPDeviceImpl dev = ((OSGiDeviceInfo) devices.get(udn)).getOSGiDevice();
+//						removeOSGiandUPnPDeviceHierarchy(dev);
+//	
+//					} else if (parseUSN.isService()) {
+//	                    Activator.logger.DEBUG("[Importer] parseUSN.isService ...registering modified device again ");
+//						/* 
+//						 * I have to unregister the UPnPDevice and register it again 
+//						 * with the updated properties  
+//						 */
+//						UPnPDeviceImpl device = 
+//	                        ((OSGiDeviceInfo) devices.get(udn)).getOSGiDevice();
+//						ServiceRegistration registar = 
+//	                        ((OSGiDeviceInfo) devices.get(udn)).getRegistration();
+//						String[] oldServicesID = 
+//	                        (String[]) (device.getDescriptions(null).get(UPnPService.ID));
+//						String[] oldServiceType = 
+//	                        (String[]) (device.getDescriptions(null).get(UPnPService.TYPE));
+//	                    
+//						Device cyberDevice = findDeviceCtrl(udn);
+//						Vector vec = new Vector();
+//						for (int i = 0; i < oldServiceType.length; i++) {
+//							Service ser = cyberDevice.getService(oldServicesID[i]);
+//							if (!(ser.getServiceType().equals(parseUSN.getServiceType()))) 
+//	                        {
+//								vec.add(oldServicesID[i]);
+//							}
+//						}
+//	
+//	                    //new serviceID
+//						String[] actualServicesID = new String[vec.size()];
+//						actualServicesID = (String[]) vec.toArray(new String[]{});
+//	
+//	                    //new serviceType
+//						String[] actualServiceType = new String[oldServiceType.length - 1];
+//						vec.clear();
+//						for (int i = 0; i < oldServiceType.length; i++) {
+//							if (!(oldServiceType[i].equals(parseUSN.getServiceType()))) 
+//	                        {
+//								vec.add(oldServiceType[i]);
+//							}
+//						}
+//						actualServiceType = (String[]) vec.toArray(new String[]{});
+//	
+//	                    //unrigistering and registering again with the new properties
+//						unregisterUPnPDevice(registar);
+//						device.setProperty(UPnPService.ID, actualServicesID);
+//						device.setProperty(UPnPService.TYPE, actualServiceType);
+//						registerUPnPDevice(null, device, device.getDescriptions(null));
+//						searchForListener(cyberDevice);
+//					}
+//				}
+//				
+//			}//synchronized(devices)
+//		} else {
+//			/*
+//			 * if it is a service means that it has deleted when the 
+//			 * owner was unregister so I can skip this bye-bye
+//			 * 
+//			 * //TODO Understand the comment
+//			 *
+//			 */
+//		}
+//	}
     
 	public synchronized void removeOSGiandUPnPDeviceHierarchy(final UPnPDeviceImpl dev) 
     {
@@ -358,91 +423,87 @@
 
 	}
 
-	public Device findDeviceCtrl(ControlPoint ctrl, String udn) {
-		/* 
-		 * this return the device looking in all the struct
-		 */
-		
-		DeviceList devList = getDeviceList();
-		Device dev = null;
-		int i = 0;
-		while (i < devList.size() && (dev == null)) {
-			if (devList.getDevice(i).getUDN().equals(udn)) {
-				dev = devList.getDevice(i);
-				return dev;
-			}
-			dev = findDevice(udn, devList.getDevice(i));
-			i++;	
-		}
-		return dev;
-	}
+//	public Device findDeviceCtrl(String udn) {
+//		/* 
+//		 * this return the device looking in all the struct
+//		 */
+//		DeviceList rootDevList = getDeviceList();
+//		int nRootDevs = rootDevList.size();
+//		for (int n=0; n<nRootDevs; n++) {
+//			Device root = rootDevList.getDevice(n);
+//			if (root.getUDN().equals(udn))
+//				return root;
+//			else {
+//				Device dev = findDevice(udn,root);
+//				if (dev != null ) return dev;
+//			}
+//		}
+//		return null;
+//	}
     
-	public Device findDevice(String udn, Device dev) {
-		/*
-		 * look for the device if it exist, starting from the root on
-		 * cyberlink struct
-		 */
-		DeviceList devList = dev.getDeviceList();
-		Device aux = null;
-		for (int i = 0; i < devList.size(); i++) {
-			if (devList.getDevice(i).getUDN().equals(udn)) {
-				return devList.getDevice(i);
-			} else {
-				if((aux = findDevice(udn, devList.getDevice(i))) != null)
-					return aux;
-			}
-		}
-		return null;
-	}
+//	public Device findDevice(String udn, Device device) {
+//		DeviceList devList = device.getDeviceList();
+//		int size = devList.size();
+//		for (int n=0; n<size; n++) {
+//			Device dev = devList.getDevice(n);
+//			if (dev.getUDN().equals(udn))
+//				return dev;
+//			else {
+//				Device embedded = findDevice(udn,dev);
+//				if (embedded != null) return embedded;
+//			}
+//		}
+//		return null;		
+//	}
 
 	/*
 	 * (non-Javadoc)
 	 * 
 	 * @see org.cybergarage.upnp.device.SearchResponseListener#deviceSearchResponseReceived(org.cybergarage.upnp.ssdp.SSDPPacket)
 	 */
-	public void deviceSearchResponseReceived(SSDPPacket ssdpPacket) {
-        Activator.logger.DEBUG("[Importer] deviceSearchResponseReceived");
-        Activator.logger.PACKET(ssdpPacket.toString());
-
-		String usn = ssdpPacket.getUSN();
-		ParseUSN parseUSN = new ParseUSN(usn);
-		String udn = parseUSN.getUDN();
-
-		ServiceReference[] refs = null;
-        
-        String filter = "(&" + UPNP_DEVICE_FLTR + EXPORT_FLTR + ")";
-       
-		try {
-			refs = context.getServiceReferences(UPnPDevice.class.getName(),
-					filter);
-		} catch (InvalidSyntaxException e) {
-			e.printStackTrace();
-		}
-        
-		if (refs != null) {
-			for (int i = 0; i < refs.length; i++) {
-				UPnPDevice dev = (UPnPDevice) context.getService(refs[i]);
-				Dictionary dic = dev.getDescriptions(null);
-				if (((String) dic.get(UPnPDevice.UDN)).equals(udn)) {
-					return;
-				}
-			}
-		}
-
-		if (devices.containsKey(udn)) {			
-            Activator.logger.DEBUG("[Importer] Device already discovered");
-			/*
-			 * Exist the registered device either in OSGi and 
-			 * hashtable of local device
-			 */
-			if (parseUSN.isService()) {
-                doServiceUpdating(udn,parseUSN.getServiceType());
-			}
-		} else {
-            doDeviceRegistration(udn);
-		}
-
-	}
+//	public void deviceSearchResponseReceived(SSDPPacket ssdpPacket) {
+//        Activator.logger.DEBUG("[Importer] deviceSearchResponseReceived");
+//        Activator.logger.PACKET(ssdpPacket.toString());
+//
+//		String usn = ssdpPacket.getUSN();
+//		ParseUSN parseUSN = new ParseUSN(usn);
+//		String udn = parseUSN.getUDN();
+//
+//		ServiceReference[] refs = null;
+//        
+//        String filter = "(&" + UPNP_DEVICE_FLTR + EXPORT_FLTR + ")";
+//       
+//		try {
+//			refs = context.getServiceReferences(UPnPDevice.class.getName(),
+//					filter);
+//		} catch (InvalidSyntaxException e) {
+//			e.printStackTrace();
+//		}
+//        
+//		if (refs != null) {
+//			for (int i = 0; i < refs.length; i++) {
+//				UPnPDevice dev = (UPnPDevice) context.getService(refs[i]);
+//				Dictionary dic = dev.getDescriptions(null);
+//				if (((String) dic.get(UPnPDevice.UDN)).equals(udn)) {
+//					return;
+//				}
+//			}
+//		}
+//
+//		if (devices.containsKey(udn)) {			
+//            Activator.logger.DEBUG("[Importer] Device already discovered");
+//			/*
+//			 * Exist the registered device either in OSGi and 
+//			 * hashtable of local device
+//			 */
+//			if (parseUSN.isService()) {
+//                doServiceUpdating(udn,parseUSN.getServiceType());
+//			}
+//		} else {
+//            doDeviceRegistration(udn);
+//		}
+//
+//	}
 	/*
 	 * (non-Javadoc)
 	 * 
@@ -684,7 +745,16 @@
 	 */
 
 	public Service serviceFromSid(String sid) {
+		
+//		synchronized (sidServices) {
+//			return (Service) sidServices.get(sid);
+//		}
+		
+		
+		synchronized (devices) {
+			
 		Enumeration e = devices.elements();
+		//System.out.println("#### DEVICES SIZE" +devices.size());
 		Service cyberService = null;
 		while (e.hasMoreElements()) {
 			OSGiDeviceInfo deviceinfo = (OSGiDeviceInfo) e.nextElement();
@@ -703,7 +773,10 @@
 					}
 				}
 			}
+			//System.out.println("Device NOT MATCHED " +device.getDescriptions(null).get(UPnPDevice.ID));
 		}
+		}
+		
 		return null;
 	}
 
@@ -713,11 +786,23 @@
 	 * @see org.apache.felix.upnpbase.importer.MyEventListener#newEventArrived(java.lang.String,
 	 *      long, java.util.Dictionary)
 	 */
-	public void newEventArrived(String uuid, long seq, PropertyList props) {
+//	public void newEventArrived(String uuid, long seq, PropertyList props) {
+		public void newEventArrived(NotifyRequest notifyReq) {
         Activator.logger.DEBUG("[Importer] newEventArrived");
+		String uuid = notifyReq.getSID();
+
 		Service service = serviceFromSid(uuid);
-		if (service != null) {
-            int size = props.size();
+		//System.out.println("new event arrived" + " seq: " +seq + " uuis: " +uuid );
+		if (service == null) {
+			System.out.println(" but SERVICE IS NULL !!!!!!!!!!! sid: " +uuid);
+			synchronized (waitingEvents) {
+				waitingEvents.put(uuid, notifyReq);				
+			}
+		}
+		else if (service != null) {
+			long seq = notifyReq.getSEQ();
+			PropertyList props = notifyReq.getPropertyList();
+           int size = props.size();
             Hashtable hash = new Hashtable();
             for (int i = 0; i < size; i++) {
                 Property prop = props.getProperty(i);
@@ -741,116 +826,140 @@
 		}
 	}
     
-    public void doServiceUpdating(String udn,String serviceType){
-        Activator.logger.DEBUG("[Importer] check for service updating");
-        OSGiDeviceInfo deviceinfo = (OSGiDeviceInfo) devices.get(udn);
-        UPnPDeviceImpl device = deviceinfo.getOSGiDevice();
-        boolean isSerPresent = device.existServiceType(serviceType);
-        if (!isSerPresent) {
-            /*
-             * The serivice doesn't exist so it's new.
-             * Find the udn of owner device and re-register the owner
-             */
-            ServiceRegistration registar = 
-                ((OSGiDeviceInfo) devices.remove(udn)).getRegistration();
-            String[] oldServicesID = 
-                (String[]) device.getDescriptions(null).get(UPnPServiceImpl.ID);
-            String[] oldServicesType = 
-                (String[]) device.getDescriptions(null).get(UPnPServiceImpl.TYPE);
-            
-            //to handle multiple instance of a serivice of the same type
-            Device cyberDevice = findDeviceCtrl(this, udn);
-            ServiceList serviceList = cyberDevice.getServiceList();
-            ArrayList newServicesID = new ArrayList();
-
-            for (int i = 0; i < serviceList.size(); i++) {
-                if (serviceList.getService(i).getServiceType()
-                        .equals(serviceType)) 
-                {
-                    newServicesID.add(serviceList.getService(i).getServiceID());
-                }
-            }
-            
-            //adding the new servicesID 
-            String[] currentServicesID = 
-                new String[(oldServicesID.length + newServicesID.size())];
-            int endOld = 1;
-            for (int i = 0; i < oldServicesID.length; i++) {
-                currentServicesID[i] = oldServicesID[i];
-                endOld++;
-            }
-            int j = 0;
-            for (; endOld < currentServicesID.length; endOld++) {
-                currentServicesID[endOld] = (String) newServicesID.get(j);
-                j++;
-            }
-            
-            //adding the new ServiceType
-            String[] currentServicesType = new String[oldServicesType.length + 1];
-            for (int i = 0; i < oldServicesType.length; i++) {
-                currentServicesType[i] = oldServicesType[i];
-            }
-            currentServicesType[currentServicesType.length - 1] = serviceType;
-            
-            
-            // unregistring the OSGi Device
-            // and setting new properties
-            unregisterUPnPDevice(registar);
-            device.setProperty(UPnPService.ID, currentServicesID);
-            device.setProperty(UPnPServiceImpl.TYPE,currentServicesType);
-            
-            //registering the service with the updated properties
-            //TODO Check if null to the first paramaters is correct or it requires the reference to the cyberdomo upnp device
-            registerUPnPDevice(null, device, device.getDescriptions(null));
-            searchForListener(cyberDevice);
-        }   
+//    public void doServiceUpdating(String udn,String serviceType){
+//        Activator.logger.DEBUG("[Importer] check for service updating");
+//        OSGiDeviceInfo deviceinfo = (OSGiDeviceInfo) devices.get(udn);
+//        UPnPDeviceImpl device = deviceinfo.getOSGiDevice();
+//        boolean isSerPresent = device.existServiceType(serviceType);
+//        if (!isSerPresent) {
+//            /*
+//             * The serivice doesn't exist so it's new.
+//             * Find the udn of owner device and re-register the owner
+//             */
+//            ServiceRegistration registar = 
+//                ((OSGiDeviceInfo) devices.remove(udn)).getRegistration();
+//            String[] oldServicesID = 
+//                (String[]) device.getDescriptions(null).get(UPnPServiceImpl.ID);
+//            String[] oldServicesType = 
+//                (String[]) device.getDescriptions(null).get(UPnPServiceImpl.TYPE);
+//            
+//            //to handle multiple instance of a serivice of the same type
+//            Device cyberDevice = findDeviceCtrl(udn);
+//            ServiceList serviceList = cyberDevice.getServiceList();
+//            ArrayList newServicesID = new ArrayList();
+//
+//            for (int i = 0; i < serviceList.size(); i++) {
+//                if (serviceList.getService(i).getServiceType()
+//                        .equals(serviceType)) 
+//                {
+//                    newServicesID.add(serviceList.getService(i).getServiceID());
+//                }
+//            }
+//            
+//            //adding the new servicesID 
+//            String[] currentServicesID = 
+//                new String[(oldServicesID.length + newServicesID.size())];
+//            int endOld = 1;
+//            for (int i = 0; i < oldServicesID.length; i++) {
+//                currentServicesID[i] = oldServicesID[i];
+//                endOld++;
+//            }
+//            int j = 0;
+//            for (; endOld < currentServicesID.length; endOld++) {
+//                currentServicesID[endOld] = (String) newServicesID.get(j);
+//                j++;
+//            }
+//            
+//            //adding the new ServiceType
+//            String[] currentServicesType = new String[oldServicesType.length + 1];
+//            for (int i = 0; i < oldServicesType.length; i++) {
+//                currentServicesType[i] = oldServicesType[i];
+//            }
+//            currentServicesType[currentServicesType.length - 1] = serviceType;
+//            
+//            
+//            // unregistring the OSGi Device
+//            // and setting new properties
+//            unregisterUPnPDevice(registar);
+//            device.setProperty(UPnPService.ID, currentServicesID);
+//            device.setProperty(UPnPServiceImpl.TYPE,currentServicesType);
+//            
+//            //registering the service with the updated properties
+//            //TODO Check if null to the first paramaters is correct or it requires the reference to the cyberdomo upnp device
+//            registerUPnPDevice(null, device, device.getDescriptions(null));
+//            searchForListener(cyberDevice);
+//        }   
+//    }
+    
+    public boolean isDeviceRegistered(String udn){
+		
+		try {
+			ServiceReference[] refs =
+				Activator.bc.getServiceReferences(
+					UPnPDevice.class.getName(),
+					"(" + UPnPDevice.UDN + "=" + udn + ")"
+				);
+			if(refs!= null)
+				return true;
+			else
+				return false;
+		} catch (InvalidSyntaxException ignored) {
+			return false;
+		}
     }
     
-    public void doDeviceRegistration(String udn,boolean checkDouble){
-    	if(checkDouble){    		
-    		try {
-    			ServiceReference[] refs =
-    				Activator.bc.getServiceReferences(
-						UPnPDevice.class.getName(),
-						"(" + UPnPDevice.UDN + "=" + udn + ")"
-    				);
-    			if(refs!=null)
-    				return;
-			} catch (InvalidSyntaxException ignored) {
-			}
-    	}
-    	doDeviceRegistration(udn);
+    public synchronized void doEmbeddedRegistration(Device device){
+        registerUPnPDevice(device, null, null);
+        //new SearchForListenerThread(device).start();
+        searchForListener(device);
+        
+		DeviceList devList = device.getDeviceList();
+		int size = devList.size();
+		for (int n=0; n<size; n++) {
+			Device dev = devList.getDevice(n);
+			doEmbeddedRegistration(dev);
+		}
     }
+//        public synchronized void doDeviceRegistration(String udn){
+//        /*
+//         * registering the new device either if it is new root device or
+//         * a new embedded device 
+//         */
+//        Device dev = findDeviceCtrl(udn);
+//        if (dev == null) {
+//        	/*
+//        	 * In this case the UPnP SDK notifies us that a ssdp:alive has arrived,
+//        	 * but,  because the no root device ssdp:alive packet has recieved by the UPnP SDK
+//        	 * no Device is present in the UPnP SDK device list. 
+//        	 */
+//            Activator.logger.INFO("Cyberlink notified packet from UDN:" +udn+ ", but Device instance doesn't exist in Cyberlink structs! It will be Ignored");            
+//        }else if(devices.containsKey(udn) == false) {
+//            Activator.logger.INFO("[Importer] registering UPnPDevice:"+dev.getFriendlyName()+"("+dev.getUDN()+")" );
+//            registerUPnPDevice(dev, null, null);
+//            searchForListener(dev);
+//            /*
+//             * now we can register all the device embedded device and service without
+//             * recieving the NOTIFY 
+//             */
+//            //XXX Think about this choice
+//            for (Iterator i = dev.getDeviceList().iterator(); i.hasNext();) {
+//				Device d = (Device) i.next();
+//				if (isDeviceRegistered(udn) == false)
+//					doDeviceRegistration(d.getUDN());
+//			}
+//        }else if(devices.containsKey(udn) == true) {
+//        	Activator.logger.INFO("[Importer] UPnPDevice UDN::"+dev.getFriendlyName()+"("+dev.getUDN()+") already registered Skipping");
+//        }
+//    }
     
-    public synchronized void doDeviceRegistration(String udn){
-        /*
-         * registering the new device either if it is new root device or
-         * a new embedded device 
-         */
-        Device dev = findDeviceCtrl(this, udn);
-        if (dev == null) {
-        	/*
-        	 * In this case the UPnP SDK notifies us that a ssdp:alive has arrived,
-        	 * but,  because the no root device ssdp:alive packet has recieved by the UPnP SDK
-        	 * no Device is present in the UPnP SDK device list. 
-        	 */
-            Activator.logger.INFO("Cyberlink notified packet from UDN:" +udn+ ", but Device instance doesn't exist in Cyberlink structs! It will be Ignored");            
-        }else if(devices.containsKey(udn) == false) {
-            Activator.logger.INFO("[Importer] registering UPnPDevice:"+dev.getFriendlyName()+"("+dev.getUDN()+")" );
-            registerUPnPDevice(dev, null, null);
-            searchForListener(dev);
-            /*
-             * now we can register all the device embedded device and service without
-             * recieving the NOTIFY 
-             */
-            //XXX Think about this choice
-            for (Iterator i = dev.getDeviceList().iterator(); i.hasNext();) {
-				Device d = (Device) i.next();
-				doDeviceRegistration(d.getUDN(),true);
-			}
-        }else if(devices.containsKey(udn) == true) {
-        	Activator.logger.INFO("[Importer] UPnPDevice UDN::"+dev.getFriendlyName()+"("+dev.getUDN()+") already registered Skipping");
-        }
+    class SearchForListenerThread extends Thread{ 
+	private Device device;
+    	public SearchForListenerThread(Device device) {
+			this.device= device;
+		}
+    	public void run(){
+    		searchForListener(device);
+    	}
     }
     
 	public void searchForListener(Device device) {
@@ -914,4 +1023,45 @@
 
 		}
 	}
+
+	public Hashtable getWaitingEvents() {
+		return waitingEvents;
+		
+	}
+	
+	synchronized public boolean subscribe(Service service, long timeout)
+	{
+		return super.subscribe(service,timeout);
+		
+//		if (service.isSubscribed() == true) {
+//			System.out.println( "isSubsribed "+ service.getServiceID() + " " +service.getDevice().getUDN());
+//			String sid = service.getSID();
+//			return subscribe(service, sid, timeout);
+//		}
+//		
+//		Device rootDev = service.getRootDevice();
+//		if (rootDev == null)
+//			return false;
+//		String ifAddress = rootDev.getInterfaceAddress();		 
+//		SubscriptionRequest subReq = new SubscriptionRequest();
+//		subReq.setSubscribeRequest(service, getEventSubCallbackURL(ifAddress), timeout);
+//		SubscriptionResponse subRes = subReq.post();
+//		if (subRes.isSuccessful() == true) {
+//			//System.out.println( "subscription response "+ service.getServiceID() + " " +service.getDevice().getUDN());
+//			String sid = subRes.getSID();
+
+//			synchronized (sidServices) {
+//				sidServices.put(sid,service);				
+//			}
+
+//			service.setSID(sid);
+//			service.setTimeout(subRes.getTimeout());
+//			return true;
+//			
+//		}
+//		service.clearSID();
+//		return false;
+	}
+
+
 }

Modified: felix/sandbox/furfari/Version-1.0.0/basedriver/src/main/java/org/apache/felix/upnp/basedriver/importer/core/event/message/FirstMessage.java
URL: http://svn.apache.org/viewvc/felix/sandbox/furfari/Version-1.0.0/basedriver/src/main/java/org/apache/felix/upnp/basedriver/importer/core/event/message/FirstMessage.java?rev=685555&r1=685554&r2=685555&view=diff
==============================================================================
--- felix/sandbox/furfari/Version-1.0.0/basedriver/src/main/java/org/apache/felix/upnp/basedriver/importer/core/event/message/FirstMessage.java (original)
+++ felix/sandbox/furfari/Version-1.0.0/basedriver/src/main/java/org/apache/felix/upnp/basedriver/importer/core/event/message/FirstMessage.java Wed Aug 13 07:03:46 2008
@@ -57,13 +57,13 @@
 	public String getSid() {
 		return sid;
 	}
-	public Device getDevice() {
-		return device;
-	}
-	public String getDeviceID(){
-		return device.getUDN();
-	}
-	public String getServiceID(){
-		return service.getServiceID();	
-	}
+//	public Device getDevice() {
+//		return device;
+//	}
+//	public String getDeviceID(){
+//		return device.getUDN();
+//	}
+//	public String getServiceID(){
+//		return service.getServiceID();	
+//	}
 }

Modified: felix/sandbox/furfari/Version-1.0.0/basedriver/src/main/java/org/apache/felix/upnp/basedriver/importer/core/event/thread/SubScriber.java
URL: http://svn.apache.org/viewvc/felix/sandbox/furfari/Version-1.0.0/basedriver/src/main/java/org/apache/felix/upnp/basedriver/importer/core/event/thread/SubScriber.java?rev=685555&r1=685554&r2=685555&view=diff
==============================================================================
--- felix/sandbox/furfari/Version-1.0.0/basedriver/src/main/java/org/apache/felix/upnp/basedriver/importer/core/event/thread/SubScriber.java (original)
+++ felix/sandbox/furfari/Version-1.0.0/basedriver/src/main/java/org/apache/felix/upnp/basedriver/importer/core/event/thread/SubScriber.java Wed Aug 13 07:03:46 2008
@@ -20,7 +20,13 @@
 package org.apache.felix.upnp.basedriver.importer.core.event.thread;
 
 
+import java.util.Hashtable;
+
+import org.cybergarage.upnp.Device;
 import org.cybergarage.upnp.Service;
+import org.cybergarage.upnp.event.NotifyRequest;
+import org.cybergarage.upnp.event.Property;
+import org.cybergarage.upnp.event.PropertyList;
 
 import org.osgi.service.log.LogService;
 
@@ -29,9 +35,10 @@
 import org.apache.felix.upnp.basedriver.importer.core.event.message.FirstMessage;
 import org.apache.felix.upnp.basedriver.importer.core.event.message.ListenerModified;
 import org.apache.felix.upnp.basedriver.importer.core.event.message.ListenerUnRegistration;
-import org.apache.felix.upnp.basedriver.importer.core.event.message.SidExipired;
+import org.apache.felix.upnp.basedriver.importer.core.event.message.StateChanged;
 import org.apache.felix.upnp.basedriver.importer.core.event.structs.Monitor;
 import org.apache.felix.upnp.basedriver.importer.core.event.structs.SubscriptionQueue;
+import org.apache.felix.upnp.basedriver.util.Converter;
 
 /* 
 * @author <a href="mailto:dev@felix.apache.org">Felix Project Team</a>
@@ -65,25 +72,68 @@
 						if (ok) {//subcribe ok	                        
 							sid = service.getSID();
 							firstmsg.setSid(sid);                        
-							monitor.addListener(sid,firstmsg.getListener());						
+							monitor.addListener(sid,firstmsg.getListener());
+							wakeupEventsForSid(sid);
 						} else {//subscribe not ok
 							Activator.logger.log(LogService.LOG_ERROR,"Sucribe failed");
+							//System.out.println(" subscribe failed");
 						}
 					} else {// already subscribe
 						monitor.addListener(service.getSID(),firstmsg.getListener());
+						wakeupEventsForSid(service.getSID());
+						//System.out.println("already subscribed");
 					}
 				} else if (msg instanceof ListenerModified) {
 					monitor.updateListener((ListenerModified)msg,subQueue,ctrl);
 				} else if (msg instanceof ListenerUnRegistration) {
 					ListenerUnRegistration unreg=(ListenerUnRegistration)msg;
 					monitor.delListener(unreg.getListener(),ctrl);
-				} else if(msg instanceof SidExipired){
-				    Activator.logger.WARNING("[Importer] Please report the bug. Used code - should be checked and removed");
 				}
 			}
 		}
 	}
 
+	private void wakeupEventsForSid(String sid) {
+		Hashtable waitingEvents = ctrl.getWaitingEvents();
+		synchronized (waitingEvents) {
+			NotifyRequest notifyReq = (NotifyRequest) waitingEvents.remove(sid);
+			if (notifyReq != null){
+				Service service = ctrl.serviceFromSid(sid);
+
+				long seq = notifyReq.getSEQ();
+				PropertyList props = notifyReq.getPropertyList();
+	           int size = props.size();
+	            Hashtable hash = new Hashtable();
+	            for (int i = 0; i < size; i++) {
+	                Property prop = props.getProperty(i);
+	                String varName = prop.getName();
+	                String varValue = prop.getValue();
+	                String upnpType = service.getStateVariable(varName).getDataType();
+	                Object valueObj;
+	                try {
+	                    valueObj = Converter.parseString(varValue,upnpType);
+	                } catch (Exception e) {
+	                    Activator.logger.ERROR("[Importer] Bad data value in Notify event: "
+	                            +"var name="+varName +" value="+varValue +" type="+upnpType + "\n"+e);
+	                    return;
+	                }
+	                hash.put(varName, valueObj);
+	            }
+	           
+				Device device = service.getDevice();
+				StateChanged msg = new StateChanged(sid, seq, hash, device, service);
+				System.out.println("----- EVENT WAKE UP -----  sid: " + sid);
+				ctrl.notifierQueue.enqueue(msg);							
+			}
+
+			else {
+				return;
+			}
+			
+		}
+		
+	}
+
 	public void close() {
 		running  = false;	
 		subQueue.close();