use of org.osgi.service.upnp.UPnPEventListener in project felix by apache.
the class ThreadExporter method bindSubscribe.
/**
* This method add an UPnPEventListener Service to the OSGi Framework so that
* the Base Driver can notify all the event listener registered on the CyberLink
* UPnP device from the UPnP World.
*
* @param d Device of CyberLink that will be notified by the changing of the StateVariable
* that happen on the OSGi World
* @return ServiceRegistration of the new registered service.
*/
private ServiceRegistration bindSubscribe(Device d) {
ExporterUPnPEventListener eventer = new ExporterUPnPEventListener(d);
Properties p = new Properties();
StringBuffer sb = new StringBuffer("(|");
Vector v = new Vector();
v.add(d);
Device current;
while (v.size() != 0) {
current = (Device) v.elementAt(0);
v.remove(0);
DeviceList dl = current.getDeviceList();
for (int i = 0; i < dl.size(); i++) {
v.add(dl.elementAt(i));
}
sb.append("(").append(UPnPDevice.ID).append("=").append(current.getUDN()).append(")");
}
sb.append(")");
Filter f = null;
try {
f = Activator.bc.createFilter(sb.toString());
} catch (InvalidSyntaxException e) {
e.printStackTrace();
return null;
}
if (f != null)
p.put(UPnPEventListener.UPNP_FILTER, f);
return Activator.bc.registerService(UPnPEventListener.class.getName(), eventer, p);
}
use of org.osgi.service.upnp.UPnPEventListener in project felix by apache.
the class MyCtrlPoint method searchForListener.
public void searchForListener(Device device) {
Activator.logger.DEBUG("[Importer] searching for UPnPEventListener");
ServiceReference[] listeners = null;
try {
listeners = context.getServiceReferences(UPnPEventListener.class.getName(), null);
} catch (InvalidSyntaxException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (listeners != null) {
String deviceID = device.getUDN();
String serviceID;
String deviceType = device.getDeviceType();
String serviceType;
Hashtable hash = new Hashtable();
hash.put(UPnPDevice.ID, deviceID);
hash.put(UPnPDevice.TYPE, deviceType);
ServiceList services = device.getServiceList();
Vector eventedSers = new Vector();
for (int i = 0; i < services.size(); i++) {
Service service = (Service) services.elementAt(i);
ServiceStateTable vars = service.getServiceStateTable();
for (int j = 0; j < vars.size(); j++) {
StateVariable var = (StateVariable) vars.elementAt(j);
if (var.isSendEvents()) {
eventedSers.add(service);
break;
}
}
}
for (int i = 0; i < listeners.length; i++) {
UPnPEventListener listener = (UPnPEventListener) context.getService(listeners[i]);
Filter filter = (Filter) listeners[i].getProperty(UPnPEventListener.UPNP_FILTER);
if (filter == null) {
for (int j = 0; j < eventedSers.size(); j++) {
Service ser = (Service) eventedSers.elementAt(j);
subQueue.enqueue(new FirstMessage(ser, listener));
}
} else {
for (int j = 0; j < eventedSers.size(); j++) {
Service ser = (Service) eventedSers.elementAt(j);
serviceID = ser.getServiceID();
serviceType = ser.getServiceType();
hash.put(UPnPService.ID, serviceID);
hash.put(UPnPService.TYPE, serviceType);
boolean bool = filter.match(hash);
if (bool) {
subQueue.enqueue(new FirstMessage(ser, listener));
}
}
}
}
}
}
use of org.osgi.service.upnp.UPnPEventListener in project felix by apache.
the class MyCtrlPoint method serviceChanged.
/*
* (non-Javadoc)
*
* @see org.osgi.framework.ServiceListener#serviceChanged(org.osgi.framework.ServiceEvent)
*/
public void serviceChanged(ServiceEvent event) {
Activator.logger.DEBUG("[Importer] serviceChanged");
Activator.logger.DEBUG("Event::" + event.toString());
if (event.getType() == ServiceEvent.REGISTERED) {
/* check new listener registration */
ServiceReference serRef = event.getServiceReference();
Object obj = serRef.getProperty(UPnPEventListener.UPNP_FILTER);
/* obtain interested devices for the listener */
ServiceReference[] devicesRefs = null;
if (obj != null) {
Filter filter = (Filter) obj;
String filtra = filter.toString();
/*
* Avoid to implement the notification for device
* that are not been created by BaseDriver
*/
String newfilter = "(&" + filtra + IMPORT_FLTR + ")";
// System.out.println(newfilter);
try {
devicesRefs = context.getServiceReferences(UPnPDevice.class.getName(), newfilter);
} catch (InvalidSyntaxException e) {
e.printStackTrace();
}
if (devicesRefs != null) {
/*
*
* only if there is a compatibile device
*/
Dictionary dic = new Hashtable();
for (int i = 0; i < devicesRefs.length; i++) {
UPnPDevice device = (UPnPDevice) context.getService(devicesRefs[i]);
dic.put(UPnPDevice.ID, device.getDescriptions(null).get(UPnPDevice.UDN));
dic.put(UPnPDevice.TYPE, device.getDescriptions(null).get(UPnPDevice.TYPE));
UPnPService[] services = device.getServices();
if (services != null) {
for (int j = 0; j < services.length; j++) {
dic.put(UPnPService.ID, services[j].getId());
dic.put(UPnPService.TYPE, services[j].getType());
// TODO add method boolean serviceEvented() so we can remove the below cycle
UPnPStateVariable[] stateVars = services[j].getStateVariables();
boolean hasEventedVars = false;
for (int k = 0; k < stateVars.length && !hasEventedVars; k++) {
hasEventedVars = stateVars[k].sendsEvents();
if (hasEventedVars) {
if (filter.match(dic)) {
UPnPEventListener listener = (UPnPEventListener) context.getService(serRef);
FirstMessage msg = new FirstMessage(((UPnPServiceImpl) services[j]).getCyberService(), listener);
subQueue.enqueue(msg);
}
}
}
}
}
context.ungetService(devicesRefs[i]);
}
}
} else {
/* obj==null (interested in all devices) */
try {
String newfilter = "(!" + EXPORT_FLTR + ")";
devicesRefs = context.getServiceReferences(UPnPDevice.class.getName(), newfilter);
} catch (InvalidSyntaxException e) {
e.printStackTrace();
}
if (devicesRefs != null) {
for (int i = 0; i < devicesRefs.length; i++) {
UPnPDevice device = (UPnPDevice) context.getService(devicesRefs[i]);
UPnPService[] services = device.getServices();
if (services != null) {
for (int j = 0; j < services.length; j++) {
UPnPStateVariable[] stateVars = services[j].getStateVariables();
boolean bool = false;
for (int k = 0; k < stateVars.length; k++) {
bool = stateVars[k].sendsEvents();
if (bool) {
break;
}
}
if (bool) {
UPnPEventListener listener = (UPnPEventListener) context.getService(serRef);
FirstMessage msg = new FirstMessage(((UPnPServiceImpl) services[j]).getCyberService(), listener);
subQueue.enqueue(msg);
}
}
}
context.ungetService(devicesRefs[i]);
}
}
}
} else if (event.getType() == ServiceEvent.MODIFIED) {
Vector newServices = new Vector();
ServiceReference serRef = event.getServiceReference();
Filter filter = (Filter) serRef.getProperty(UPnPEventListener.UPNP_FILTER);
UPnPEventListener listener = (UPnPEventListener) context.getService(serRef);
ServiceReference[] devicesRefs = null;
if (filter != null) {
try {
String filtra = filter.toString();
String newfilter = "(&" + filtra + "(!" + EXPORT_FLTR + ")" + ")";
devicesRefs = context.getServiceReferences(UPnPDevice.class.getName(), newfilter);
} catch (InvalidSyntaxException e) {
e.printStackTrace();
}
if (devicesRefs != null) {
/*
*
* only if there is a compatibile device
*/
Dictionary dic = new Hashtable();
/*
* look for the service that match
*/
for (int i = 0; i < devicesRefs.length; i++) {
UPnPDevice device = (UPnPDevice) context.getService(devicesRefs[i]);
dic.put(UPnPDevice.ID, device.getDescriptions(null).get(UPnPDevice.UDN));
dic.put(UPnPDevice.TYPE, device.getDescriptions(null).get(UPnPDevice.TYPE));
UPnPService[] services = device.getServices();
if (services != null) {
for (int j = 0; j < services.length; j++) {
dic.put(UPnPService.ID, services[j].getId());
dic.put(UPnPService.TYPE, services[j].getType());
UPnPStateVariable[] stateVars = services[j].getStateVariables();
boolean hasEventedVars = false;
for (int k = 0; k < stateVars.length; k++) {
hasEventedVars = stateVars[k].sendsEvents();
if (hasEventedVars) {
break;
}
}
if (!hasEventedVars) {
continue;
}
boolean bool = filter.match(dic);
if (bool) {
newServices.add(((UPnPServiceImpl) services[j]).getCyberService());
}
}
// for services
}
// services ==null
context.ungetService(devicesRefs[i]);
}
// for devicesRefs
ListenerModified msg = new ListenerModified(newServices, listener);
subQueue.enqueue(msg);
}
// devicesrefs !=null
} else {
// interrested in all devices
try {
String newfilter = "(!(" + UPnPDevice.UPNP_EXPORT + "=*" + ")" + ")";
devicesRefs = context.getServiceReferences(UPnPDevice.class.getName(), newfilter);
} catch (InvalidSyntaxException e) {
e.printStackTrace();
}
if (devicesRefs != null) {
for (int i = 0; i < devicesRefs.length; i++) {
UPnPDevice device = (UPnPDevice) context.getService(devicesRefs[i]);
UPnPService[] services = device.getServices();
if (services != null) {
for (int j = 0; j < services.length; j++) {
UPnPStateVariable[] stateVars = services[j].getStateVariables();
boolean hasEventedVars = false;
for (int k = 0; k < stateVars.length; k++) {
hasEventedVars = stateVars[k].sendsEvents();
if (hasEventedVars) {
break;
}
}
if (hasEventedVars) {
newServices.add(((UPnPServiceImpl) services[j]).getCyberService());
}
// hasEventedvars
}
// for services
}
// services !=null
context.ungetService(devicesRefs[i]);
}
// for devicesRefs
subQueue.enqueue(new ListenerModified(newServices, listener));
}
// devicesRefs !=null
}
} else if (event.getType() == ServiceEvent.UNREGISTERING) {
UPnPEventListener listener = (UPnPEventListener) context.getService(event.getServiceReference());
if (listener != null) {
ListenerUnRegistration msg = new ListenerUnRegistration(listener);
subQueue.enqueue(msg);
}
context.ungetService(event.getServiceReference());
}
}
use of org.osgi.service.upnp.UPnPEventListener in project felix by apache.
the class Monitor method updateListener.
public synchronized void updateListener(ListenerModified msg, SubscriptionQueue subqueue, MyCtrlPoint ctrl) /*##renew, SidRenewer sidRenewer*/
{
UPnPEventListener listener = msg.getListener();
Vector newServices = msg.getNewServices();
Vector subscribed = new Vector();
Vector notSubscribed = new Vector();
for (int i = 0; i < newServices.size(); i++) {
Service ser = (Service) newServices.elementAt(i);
if (ser.isSubscribed()) {
subscribed.add(ser);
} else {
notSubscribed.add(ser);
}
}
Vector oldSids = sidListSid.getSidsFromListener(listener);
// check subscribed services
if (oldSids == null)
return;
for (int i = 0; i < notSubscribed.size(); i++) {
Service ser = (Service) notSubscribed.elementAt(i);
subqueue.enqueue(new FirstMessage(ser, listener));
}
for (int i = 0; i < oldSids.size(); i++) {
String oldSid = (String) oldSids.elementAt(i);
if (!subscribed.contains(oldSid)) {
// francesco-renew
// to check -- Listner removal from sid2Listener
unsubscribeListenerForSid(oldSid, listener, ctrl);
}
}
}
use of org.osgi.service.upnp.UPnPEventListener in project felix by apache.
the class Sid2Listeners method updateListeners.
/**
* @param sid
* @param dictionary
*/
public boolean updateListeners(String sid, String deviceID, String serviceID, Dictionary dictionary) {
Vector listeners = (Vector) sidListener.get(sid);
if (listeners != null) {
for (int i = 0; i < listeners.size(); i++) {
UPnPEventListener listener = (UPnPEventListener) listeners.elementAt(i);
listener.notifyUPnPEvent(deviceID, serviceID, dictionary);
}
return true;
}
return false;
}
Aggregations