Francesco Furfari | ec7e175 | 2006-10-02 13:37:04 +0000 | [diff] [blame] | 1 | /*
|
| 2 | * Licensed to the Apache Software Foundation (ASF) under one
|
| 3 | * or more contributor license agreements. See the NOTICE file
|
| 4 | * distributed with this work for additional information
|
| 5 | * regarding copyright ownership. The ASF licenses this file
|
| 6 | * to you under the Apache License, Version 2.0 (the
|
| 7 | * "License"); you may not use this file except in compliance
|
| 8 | * with the License. You may obtain a copy of the License at
|
Francesco Furfari | 91af5da | 2006-04-04 23:44:23 +0000 | [diff] [blame] | 9 | *
|
Francesco Furfari | ec7e175 | 2006-10-02 13:37:04 +0000 | [diff] [blame] | 10 | * http://www.apache.org/licenses/LICENSE-2.0
|
Francesco Furfari | 91af5da | 2006-04-04 23:44:23 +0000 | [diff] [blame] | 11 | *
|
Francesco Furfari | ec7e175 | 2006-10-02 13:37:04 +0000 | [diff] [blame] | 12 | * Unless required by applicable law or agreed to in writing,
|
| 13 | * software distributed under the License is distributed on an
|
| 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
| 15 | * KIND, either express or implied. See the License for the
|
| 16 | * specific language governing permissions and limitations
|
| 17 | * under the License.
|
Francesco Furfari | 91af5da | 2006-04-04 23:44:23 +0000 | [diff] [blame] | 18 | */
|
| 19 |
|
| 20 | package org.apache.felix.upnp.tester.discovery;
|
| 21 | import java.util.*;
|
| 22 |
|
| 23 | import org.osgi.framework.*;
|
| 24 | import org.osgi.service.upnp.*;
|
| 25 |
|
Francesco Furfari | f2a6791 | 2006-07-17 17:08:02 +0000 | [diff] [blame] | 26 | /*
|
Karl Pauls | d312acc | 2007-06-18 20:38:33 +0000 | [diff] [blame^] | 27 | * @author <a href="mailto:dev@felix.apache.org">Felix Project Team</a>
|
Francesco Furfari | f2a6791 | 2006-07-17 17:08:02 +0000 | [diff] [blame] | 28 | */
|
Francesco Furfari | 91af5da | 2006-04-04 23:44:23 +0000 | [diff] [blame] | 29 | public class DeviceNode {
|
| 30 |
|
| 31 | private ServiceReference sr;
|
| 32 | private boolean isRootNode;
|
| 33 | private String udn ;
|
| 34 | private boolean hasChild;
|
| 35 | private int numberOfSons;
|
| 36 | private ArrayList children;
|
| 37 | private DeviceNode parent;
|
| 38 |
|
| 39 | public DeviceNode(ServiceReference sr){
|
| 40 | //PRE: argument is always UPnPDevice service reference
|
| 41 | if (sr == null)
|
| 42 | throw new IllegalArgumentException("null is not a valid arg in DeviceNode constructor");
|
| 43 | this.sr = sr;
|
| 44 | udn = (String) sr.getProperty(UPnPDevice.UDN);
|
| 45 | parent=null;
|
| 46 | isRootNode = (sr.getProperty(UPnPDevice.PARENT_UDN) == null);
|
| 47 | String[] sons = ((String[]) sr.getProperty(UPnPDevice.CHILDREN_UDN));
|
| 48 | hasChild = (sons != null);
|
| 49 | if (hasChild) {
|
| 50 | children = new ArrayList();
|
| 51 | numberOfSons = sons.length;
|
| 52 | }
|
| 53 | /*
|
| 54 | //Operation to let DeviceNode::isComplete() O(1)
|
| 55 | isComplete = !hasChild;
|
| 56 | */
|
| 57 | }
|
| 58 |
|
| 59 | public ServiceReference getReference(){
|
| 60 | return sr;
|
| 61 | }
|
| 62 | public UPnPDevice getDevice(BundleContext ctx){
|
| 63 | return (UPnPDevice)ctx.getService(sr);
|
| 64 | }
|
| 65 |
|
| 66 | public void attach(DeviceNode node){
|
| 67 | if (node == null)
|
| 68 | throw new IllegalArgumentException("null is not a valid arg in DeviceNode.attach() method");
|
| 69 | node.parent = this;
|
| 70 | /*
|
| 71 | //Operation to let DeviceNode::isComplete() O(1)
|
| 72 | if((numberOfSons==children.size()-1)
|
| 73 | &&(node.isComplete())){
|
| 74 | this.isComplete = true;
|
| 75 | }
|
| 76 | */
|
| 77 | children.add(node);
|
| 78 | }
|
| 79 |
|
| 80 | public DeviceNode dethatch(String name){
|
| 81 | DeviceNode dn = this.search(name);
|
| 82 | if(dn==null)
|
| 83 | return null;
|
| 84 |
|
| 85 | if(dn.parent!=null){
|
| 86 | Iterator list = dn.parent.children.iterator();
|
| 87 | while (list.hasNext()) {
|
| 88 | DeviceNode x = (DeviceNode) list.next();
|
| 89 | if(x.udn.equals(name)){
|
| 90 | list.remove();
|
| 91 | /*
|
| 92 | //Operation to let DeviceNode::isComplete() O(1)
|
| 93 | dn.parent.isComplete=false;
|
| 94 | */
|
| 95 | break;
|
| 96 | }
|
| 97 | }
|
| 98 | }
|
| 99 | dn.parent=null;
|
| 100 | return dn;
|
| 101 | }
|
| 102 |
|
| 103 | public Collection getAllChildren(){
|
| 104 | if((this.children==null)||(this.children.size()==0))
|
| 105 | return new ArrayList();
|
| 106 |
|
| 107 | Vector v = new Vector(this.children);
|
| 108 | Iterator list = this.children.iterator();
|
| 109 | while (list.hasNext()) {
|
| 110 | DeviceNode x = (DeviceNode) list.next();
|
| 111 | Collection c = x.getAllChildren();
|
| 112 | if(c==null) continue;
|
| 113 | v.addAll(c);
|
| 114 | }
|
| 115 | return v;
|
| 116 | }
|
| 117 |
|
| 118 | public Collection getChildren(){
|
| 119 | if((this.children==null)||(this.children.size()==0))
|
| 120 | return new ArrayList();
|
| 121 | return this.children;
|
| 122 | }
|
| 123 |
|
| 124 | /**
|
| 125 | * @param name <code>String</code> that contain the UDN to look for
|
| 126 | * @return return a <code>DeviceNode</code> that have the UDN equals to name and <br>
|
| 127 | * if there is any <code>DeviceNode</code> with the proper UDN value return <code>null</code>
|
| 128 | */
|
| 129 | public DeviceNode search(String name){
|
| 130 | if (name == null)
|
| 131 | throw new IllegalArgumentException("null is not a valid arg in DeviceNode.search() method");
|
| 132 | if (name.equals(udn))
|
| 133 | return this;
|
| 134 | else if (hasChild){
|
| 135 | Iterator list = children.iterator();
|
| 136 | while (list.hasNext()){
|
| 137 | DeviceNode child = (DeviceNode)list.next();
|
| 138 | DeviceNode node = child.search(name);
|
| 139 | if (node != null) return node;
|
| 140 | }
|
| 141 | }
|
| 142 | return null;
|
| 143 | }
|
| 144 |
|
| 145 | /**
|
| 146 | *
|
| 147 | * @param udn
|
| 148 | * @return <code>true</code> if and only if this <code>DeviceNode</code>
|
| 149 | * contains a DeviceNode with UDN equals to passed argument or if
|
| 150 | * its USN is equal to passed argument
|
| 151 | */
|
| 152 | public boolean contains(String udn){
|
| 153 | return this.search(udn)!=null;
|
| 154 | }
|
| 155 |
|
| 156 | public boolean isComplete(){
|
| 157 | /*
|
| 158 | //Operation to let DeviceNode::isComplete() O(1)
|
| 159 | return isComplete;
|
| 160 | */
|
| 161 | if (! hasChild) return true;
|
| 162 | if (numberOfSons != children.size())return false;
|
| 163 | Iterator list = children.iterator();
|
| 164 | while (list.hasNext()){
|
| 165 | DeviceNode child = (DeviceNode)list.next();
|
| 166 | if (! child.isComplete()) return false;
|
| 167 | }
|
| 168 | return true;
|
| 169 | }
|
| 170 |
|
| 171 | public DeviceNode isAttachable(DeviceNode node){
|
| 172 | if (node == null)
|
| 173 | throw new IllegalArgumentException("null is not a valid arg in DeviceNode.isAttachable() method");
|
| 174 | String parentUDN=(String) node.getReference().getProperty(UPnPDevice.PARENT_UDN);
|
| 175 | if(parentUDN==null) return null;
|
| 176 | return search(parentUDN);
|
| 177 | }
|
| 178 |
|
| 179 | public boolean isRoot(){
|
| 180 | return isRootNode;
|
| 181 | }
|
| 182 |
|
| 183 | public boolean equals(String udn){
|
| 184 | return this.udn.equals(udn);
|
| 185 | }
|
| 186 |
|
| 187 | public String toString(){
|
| 188 | return udn;
|
| 189 | }
|
| 190 | public boolean isLeaf() {
|
| 191 | return !hasChild;
|
| 192 | }
|
| 193 |
|
| 194 | public void print(){
|
| 195 | System.out.println("####Device Node");
|
| 196 | String[] props = sr.getPropertyKeys();
|
| 197 | for (int i=0;i< props.length;i++){
|
| 198 | Object prop= sr.getProperty(props[i]);
|
| 199 | if (prop instanceof String[]){
|
| 200 | System.out.println(props[i]+ "=");
|
| 201 | String[] multiple = (String[])prop;
|
| 202 | for (int j=0;j< multiple.length;j++){
|
| 203 | System.out.println(multiple[j]+ ";");
|
| 204 | }
|
| 205 |
|
| 206 | }
|
| 207 | else System.out.println(props[i]+ "="+ prop);
|
| 208 | }
|
| 209 | System.out.println("####Device Node");
|
| 210 | }
|
| 211 | }
|