use of org.cybergarage.upnp.Service in project felix by apache.
the class DriverControllerImpl method getSCPDURL.
public String getSCPDURL(String udn, String serviceId) {
if (myCtrl == null) {
logger.WARNING("UPnP Importer is disabled. getSCPDURL() is not available");
return null;
}
if (udn == null || udn.equals(""))
throw new IllegalArgumentException("Invalid udn paramenter");
if (serviceId == null || serviceId.equals(""))
throw new IllegalArgumentException("Invalid serviceId paramenter");
Device device = myCtrl.getDevice(udn);
if (device == null) {
logger.WARNING("getSCPDURL():: No device data available for UDN: " + udn);
return null;
}
Service service = device.getService(serviceId);
if (service == null) {
logger.WARNING("getSCPDURL():: No service data available for serviceId:" + serviceId + " of UDN " + udn);
return null;
}
String scpd = service.getSCPDURL().trim();
return resolveRelativeLink(device, scpd);
}
use of org.cybergarage.upnp.Service 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.cybergarage.upnp.Service 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.cybergarage.upnp.Service in project felix by apache.
the class MyCtrlPoint method newEventArrived.
/*
* (non-Javadoc)
*
* @see org.apache.felix.upnpbase.importer.MyEventListener#newEventArrived(java.lang.String,
* long, java.util.Dictionary)
*/
public void newEventArrived(String uuid, long seq, PropertyList props) {
Activator.logger.DEBUG("[Importer] newEventArrived");
Service service = serviceFromSid(uuid);
if (service != null) {
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(uuid, seq, hash, device, service);
notifierQueue.enqueue(msg);
}
}
use of org.cybergarage.upnp.Service 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
*
*/
}
}
Aggregations