use of org.apache.felix.upnp.basedriver.importer.core.upnp.UPnPDeviceImpl in project felix by apache.
the class MyCtrlPoint method doServiceUpdating.
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);
}
}
use of org.apache.felix.upnp.basedriver.importer.core.upnp.UPnPDeviceImpl in project felix by apache.
the class MyCtrlPoint method deviceNotifyReceived.
/*
* (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(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
*
*/
}
}
use of org.apache.felix.upnp.basedriver.importer.core.upnp.UPnPDeviceImpl in project felix by apache.
the class MyCtrlPoint method registerUPnPDevice.
public void registerUPnPDevice(Device dev, UPnPDeviceImpl upnpDev, Dictionary prop) {
/*
* registering the new Device as OSGi UPnPDevice and then add
* ServiceRegistration and UPnPDevice reference to the hashtable
* that contains local devices
*/
if (prop == null && upnpDev == null) {
UPnPDeviceImpl newDevice = new UPnPDeviceImpl(dev, context);
ServiceRegistration registration = context.registerService(UPnPDevice.class.getName(), newDevice, newDevice.getDescriptions(null));
OSGiDeviceInfo deviceInfo = new OSGiDeviceInfo(newDevice, registration);
String udn = (String) ((newDevice.getDescriptions(null)).get(UPnPDevice.UDN));
devices.put(udn, deviceInfo);
} else {
ServiceRegistration registration = context.registerService(UPnPDevice.class.getName(), upnpDev, prop);
OSGiDeviceInfo deviceInfo = new OSGiDeviceInfo(upnpDev, registration);
devices.put(upnpDev.getDescriptions(null).get(UPnPDevice.UDN), deviceInfo);
}
}
Aggregations