use of org.osgi.service.upnp.UPnPDevice in project felix by apache.
the class BuildDevice method addServices.
/**
* Method used to create a new Service in CyberLink world without creating the XML
*
* @param id ServiceId
* @param devUPnP the CyberLink device that where the new Service will be created
* @param sr ServiceReference to OSGi Device that used as source of the information
* for the creation of the device
*/
private static void addServices(String id, Device devUPnP, ServiceReference sr) {
UPnPDevice devOSGi = (UPnPDevice) Activator.bc.getService(sr);
if (devOSGi == null) {
// added by twa to prevent a null pointer exception
Activator.logger.WARNING("UPnP Device that cotains serviceId=" + id + " is deregistered from the framework while is exported");
return;
}
UPnPService[] services = devOSGi.getServices();
if (services == null || services.length == 0)
return;
for (int i = 0; i < services.length; i++) {
Service ser = new Service();
devUPnP.addService(ser);
ser.setServiceType(services[i].getType());
ser.setServiceID(services[i].getId());
ser.setSCPDURL(id + "/service/" + i + "/gen-desc.xml");
ser.setDescriptionURL(id + "/service/" + i + "/gen-desc.xml");
ser.setControlURL(id + "/service/" + i + "/ctrl");
ser.setEventSubURL(id + "/service/" + i + "/event");
UPnPAction[] actions = services[i].getActions();
for (int j = 0; j < actions.length; j++) {
boolean valid = true;
Action act = new Action(ser.getServiceNode());
act.setName(actions[j].getName());
ArgumentList al = new ArgumentList();
String[] names = actions[j].getInputArgumentNames();
if (names != null) {
for (int k = 0; k < names.length; k++) {
UPnPStateVariable variable = actions[j].getStateVariable(names[k]);
if (variable == null) {
/*
* //TODO Create a stict and relaxed behavior of the base driver which
* export as much it can or export only 100% complaint UPnPDevice service
*/
Activator.logger.WARNING("UPnP Device that cotains serviceId=" + id + " contains the action " + actions[j].getName() + " with the Input argument " + names[k] + " not related to any UPnPStateVariable. Thus this action won't be exported");
valid = false;
break;
}
Argument a = new Argument();
a.setDirection(Argument.IN);
a.setName(names[k]);
a.setRelatedStateVariableName(variable.getName());
al.add(a);
}
}
names = actions[j].getOutputArgumentNames();
if (names != null && valid) {
for (int k = 0; k < names.length; k++) {
UPnPStateVariable variable = actions[j].getStateVariable(names[k]);
if (variable == null) {
/*
* //TODO Create a stict and relaxed behavior of the base driver which
* export as much it can or export only 100% complaint UPnPDevice service
*/
Activator.logger.WARNING("UPnP Device that cotains serviceId=" + id + " contains the action " + actions[j].getName() + " with the Output argument " + names[k] + " not related to any UPnPStateVariable. Thus this action won't be exported");
}
Argument a = new Argument();
a.setDirection(Argument.OUT);
a.setName(names[k]);
a.setRelatedStateVariableName(variable.getName());
al.add(a);
}
}
if (valid) {
act.setArgumentList(al);
ser.addAction(act);
}
}
UPnPStateVariable[] vars = services[i].getStateVariables();
for (int j = 0; j < vars.length; j++) {
StateVariable var = new StateVariable();
var.setDataType(vars[j].getUPnPDataType());
var.setName(vars[j].getName());
var.setSendEvents(vars[j].sendsEvents());
String[] values = vars[j].getAllowedValues();
if (values != null) {
AllowedValueList avl = new AllowedValueList(values);
var.setAllowedValueList(avl);
} else if (vars[j].getMaximum() != null) {
AllowedValueRange avr = new AllowedValueRange(vars[j].getMaximum(), vars[j].getMinimum(), vars[j].getStep());
var.setAllowedValueRange(avr);
}
if (vars[j].getDefaultValue() != null)
try {
var.setDefaultValue(Converter.toString(vars[j].getDefaultValue(), vars[j].getUPnPDataType()));
} catch (Exception ignored) {
}
ser.addStateVariable(var);
}
Activator.bc.ungetService(sr);
}
}
use of org.osgi.service.upnp.UPnPDevice 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.osgi.service.upnp.UPnPDevice 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.UPnPDevice in project felix by apache.
the class ConfigurationPrinterImpl method printJSON.
private void printJSON(TreeMap componentMap, PrintWriter pw) throws IOException {
final JSONWriter writer = new JSONWriter(pw);
writer.object();
writer.key("devices");
writer.array();
// render components
for (Iterator ci = componentMap.values().iterator(); ci.hasNext(); ) {
final ServiceReference ref = (ServiceReference) ci.next();
final UPnPDevice device = (UPnPDevice) tracker.getService(ref);
if (device != null) {
writer.value(Serializer.deviceToJSON(ref, device));
}
}
writer.endArray();
writer.endObject();
writer.flush();
}
use of org.osgi.service.upnp.UPnPDevice in project felix by apache.
the class ConfigurationPrinterImpl method print.
private void print(PrintWriter pw, ServiceReference ref) {
pw.println(nameOf(ref));
pw.println(" Properties:");
String[] properties = ref.getPropertyKeys();
Arrays.sort(properties);
for (int i = 0; i < properties.length; i++) {
String key = properties[i];
if (OBJECTCLASS.equals(key) || SERVICE_ID.equals(key)) {
continue;
}
Object val = ref.getProperty(key);
val = WebConsoleUtil.toString(val);
// $NON-NLS-1$
pw.print(" ");
pw.print(key);
// $NON-NLS-1$
pw.print(" = ");
pw.println(val);
}
UPnPDevice dev = (UPnPDevice) tracker.getService(ref);
if (null == dev)
return;
UPnPService[] services = dev.getServices();
if (null != services && services.length > 0) {
pw.println(" Services:");
for (int i = 0; i < services.length; i++) {
print(pw, services[i]);
}
}
pw.println();
}
Aggregations