|
|
/home/brennan/Software/sensix/source/C^4/sensix/discovery/Descendants.javaGo to the documentation of this file.00001 00010 /* 00011 * Copyright 2008. Los Alamos National Security, LLC. This material 00012 * was produced under U.S. Government contract DE-AC52-06NA25396 for 00013 * Los Alamos National Laboratory (LANL), which is operated by Los 00014 * Alamos National Security, LLC, for the Department of Energy. The 00015 * U.S. Government has rights to use, reproduce, and distribute this 00016 * software. NEITHER THE GOVERNMENT NOR LOS ALAMOS NATIONAL SECURITY, 00017 * LLC, MAKES ANY WARRANTY, EXPRESS OR IMPLIED, OR ASSUMES ANY LEGAL 00018 * LIABILITY FOR THE USE OF THIS SOFTWARE. If software is modified to 00019 * produce derivative works, such modified software should be clearly 00020 * marked, so as not to confuse it with the version available from LANL. 00021 * 00022 * Additionally, this program is free software; you can redistribute 00023 * it and/or modify it under the terms of the GNU General Public 00024 * License as published by the Free Software Foundation; version 2 of 00025 * the License. Accordingly, this program is distributed in the hope 00026 * it will be useful, but WITHOUT ANY WARRANTY; without even the 00027 * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 00028 * PURPOSE. See the GNU General Public License for more details. 00029 * 00030 * You should have received a copy of the GNU General Public License 00031 * along with this program; if not, write to the Free Software 00032 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. 00033 */ 00034 00035 package sensix.discovery; 00036 00037 import sensix.*; 00038 00039 00040 public class Descendants 00041 { 00042 private DiscoveryService serv; 00043 00044 public Descendants(DiscoveryService ds) { 00045 serv = ds; 00046 } 00047 00048 00049 public int[] nodes() { 00050 int len = serv.descendants_size; 00051 int[] ret = new int[len]; 00052 System.arraycopy(serv.descendants_seen, 0, ret, 0, len); 00053 return ret; 00054 } 00055 00056 00057 public byte[] capabilities() { 00058 byte[] caps = new byte[serv.desc_db_size]; 00059 for (int i = 0; i < serv.desc_db_size; i++) 00060 caps[i] = serv.desc_database[i].type; 00061 return caps; 00062 } 00063 00064 00065 public boolean findNode(int id) { 00066 for (int i = 0; i < serv.descendants_size; i++) { 00067 if (serv.descendants_seen[i] == id) 00068 return true; 00069 } 00070 return false; 00071 } 00072 00073 00074 public Request[] queryNetwork(byte capability) 00075 throws DiscoveryException { 00076 int num = 0; 00077 for (int i = 0; i < serv.desc_db_size; i++) { 00078 if (serv.desc_database[i].type == capability) 00079 num++; 00080 } 00081 if (num == 0) 00082 throw new DiscoveryException(DiscoveryError.NoCapability, 00083 "Descendant capability " + 00084 SenseUtil.capabilityToString(capability) + 00085 " unavailable"); 00086 00087 Request[] objs = new Request[num]; 00088 num = 0; 00089 for (int i = 0; i < serv.desc_db_size; i++) { 00090 if (serv.desc_database[i].type == capability) 00091 num++; 00092 } 00093 return objs; 00094 } 00095 00096 00097 public Request queryNode(int id, byte capability) 00098 throws DiscoveryException { 00099 Request obj = null; 00100 int num = 0; 00101 boolean found = false; 00102 boolean cap_found = false; 00103 boolean id_found = false; 00104 00105 for (int i = 0; i < serv.desc_db_size; i++) { 00106 if (serv.desc_database[i].type == capability) 00107 cap_found = true; 00108 if (serv.desc_database[i].id == id) 00109 id_found = true; 00110 00111 if (serv.desc_database[i].type == capability && 00112 serv.desc_database[i].id == id) { 00113 found = true; 00114 break; 00115 } 00116 } 00117 if (cap_found == false) 00118 throw new DiscoveryException(DiscoveryError.NoCapability, 00119 "Capability " + 00120 SenseUtil.capabilityToString(capability) + 00121 " unavailable"); 00122 else if (id_found == false) 00123 throw new DiscoveryException(DiscoveryError.NoNode, 00124 "Node " + Integer.toString(id) + 00125 " unavailable"); 00126 else if (found == false) 00127 throw new DiscoveryException(DiscoveryError.NoCapability, 00128 "Capability " + 00129 SenseUtil.capabilityToString(capability) + 00130 " on node " + Integer.toString(id) + 00131 " unavailable"); 00132 return obj; 00133 } 00134 } |