00001
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035 package sensix.discovery;
00036
00037 import sensix.*;
00038
00039
00040 public class Siblings
00041 {
00042 private DiscoveryService serv;
00043
00044 public Siblings(DiscoveryService ds) {
00045 serv = ds;
00046 }
00047
00048
00049 public int[] nodes() {
00050 int len = serv.siblings_size;
00051 int[] ret = new int[len];
00052 System.arraycopy(serv.siblings_seen, 0, ret, 0, len);
00053 return ret;
00054 }
00055
00056
00057 public byte[] capabilities() {
00058 byte[] caps = new byte[serv.sib_db_size];
00059 for (int i = 0; i < serv.sib_db_size; i++)
00060 caps[i] = serv.sib_database[i].type;
00061 return caps;
00062 }
00063
00064
00065 public boolean findNode(int id) {
00066 for (int i = 0; i < serv.siblings_size; i++) {
00067 if (serv.siblings_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.sib_db_size; i++) {
00078 if (serv.sib_database[i].type == capability)
00079 num++;
00080 }
00081 if (num == 0)
00082 throw new DiscoveryException(DiscoveryError.NoCapability,
00083 "Sibling capability " +
00084 SenseUtil.capabilityToString(capability) +
00085 " unavailable");
00086
00087 Request[] objs = new Request[num];
00088 num = 0;
00089 for (int i = 0; i < serv.sib_db_size; i++) {
00090 if (serv.sib_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.sib_db_size; i++) {
00106 if (serv.sib_database[i].type == capability)
00107 cap_found = true;
00108 if (serv.sib_database[i].id == id)
00109 id_found = true;
00110
00111 if (serv.sib_database[i].type == capability &&
00112 serv.sib_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 }