001: /*
002: *
003: *
004: * Copyright 1990-2007 Sun Microsystems, Inc. All Rights Reserved.
005: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License version
009: * 2 only, as published by the Free Software Foundation.
010: *
011: * This program is distributed in the hope that it will be useful, but
012: * WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * General Public License version 2 for more details (a copy is
015: * included at /legal/license.txt).
016: *
017: * You should have received a copy of the GNU General Public License
018: * version 2 along with this work; if not, write to the Free Software
019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA
021: *
022: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
023: * Clara, CA 95054 or visit www.sun.com if you need additional
024: * information or have any questions.
025: */
026: package com.sun.kvem.jsr082.bluetooth;
027:
028: import javax.bluetooth.RemoteDevice;
029: import javax.bluetooth.UUID;
030: import javax.bluetooth.DataElement;
031: import java.util.Enumeration;
032: import java.util.Hashtable;
033:
034: /**
035: * This class saves information about every service descovery request
036: * to provide functionality of DiscoveryAgent using multiple requests
037: * via SDPClient (Service Descovery Protocol) by ServiceSelector
038: * and ServiceSearcher classes.
039: */
040: abstract class ServiceSearcherBase implements SDPResponseListener {
041: /** Mask to determine an attribute ID out of range. */
042: private static final int MASK_OVERFLOW = 0xffff0000;
043:
044: /** RemoteDevice whose response to be listened. */
045: RemoteDevice btDev;
046:
047: /**
048: * The UUIDs from SDP_ServiceSearchRequest or
049: * SDP_ServiceSearchAttrbuteRequest.
050: *
051: * @see SDPClient#serviceSearchRequest
052: * @see SDPClient#serviceSearchAttributeRequest
053: */
054: UUID[] uuidSet;
055:
056: /**
057: * Attributes list from SDP_ServiceSearchAttrbuteRequest.
058: *
059: * @see SDPClient#serviceSearchAttributeRequest
060: */
061: int[] attrSet;
062:
063: /**
064: * Creates ServiceSearcherBase and save all required info in it.
065: */
066: ServiceSearcherBase(int[] attrSet, UUID[] uuidSet,
067: RemoteDevice btDev) {
068: Object checkObj = new Object();
069: Hashtable dupChecker = new Hashtable();
070: Enumeration set;
071:
072: // checking on null
073: if (uuidSet == null) {
074: throw new NullPointerException("null instance of UUID set");
075: }
076:
077: if (btDev == null) {
078: throw new NullPointerException(
079: "null instance of RemoteDevice");
080: }
081:
082: // check UUID size
083: if (uuidSet.length == 0) {
084: throw new IllegalArgumentException("zero UUID set size");
085: }
086:
087: // attrSet checking
088: if (attrSet != null) {
089:
090: // check attrSet size
091: if (attrSet.length == 0) {
092: throw new IllegalArgumentException("zero attrSet size");
093: } else if (attrSet.length > ServiceRecordImpl.RETRIEVABLE_MAX) {
094: throw new IllegalArgumentException(
095: "attrSet size exceeding");
096: }
097:
098: for (int i = 0; i < attrSet.length; i++) {
099:
100: // check that attribute ID is valid
101: if ((attrSet[i] & MASK_OVERFLOW) != 0) {
102: throw new IllegalArgumentException(
103: "illegal attrSet");
104: }
105:
106: // check attribute ID duplication
107: if (dupChecker.put(new Integer(attrSet[i]), checkObj) != null) {
108: throw new IllegalArgumentException(
109: "duplicated attribute ID");
110: }
111: }
112: }
113:
114: // adds defaults attribute IDs
115: for (int i = 0; i <= 0x0004; i++) {
116: dupChecker.put(new Integer(i), checkObj);
117: }
118:
119: // creates attrSet to save attrIDs in the carrier
120: this .attrSet = new int[dupChecker.size()];
121: set = dupChecker.keys();
122:
123: for (int i = 0; set.hasMoreElements(); i++) {
124: this .attrSet[i] = ((Integer) set.nextElement()).intValue();
125: }
126: dupChecker.clear();
127:
128: // uuidSet checking
129: for (int i = 0; i < uuidSet.length; i++) {
130:
131: // check that UUID is not null instance
132: if (uuidSet[i] == null) {
133: throw new NullPointerException("null instance of UUID");
134: }
135:
136: // check UUID duplication
137: if (dupChecker.put(uuidSet[i], checkObj) != null) {
138: throw new IllegalArgumentException("duplicated UUID");
139: }
140: }
141:
142: // creates uuidSet to save UUIDs in the carrier
143: this .uuidSet = new UUID[dupChecker.size()];
144: set = dupChecker.keys();
145:
146: for (int i = 0; set.hasMoreElements(); i++) {
147: this .uuidSet[i] = (UUID) set.nextElement();
148: }
149:
150: this .btDev = btDev;
151: }
152:
153: /**
154: * Informs this listener about errors during Service Discovering process.
155: *
156: * @param errorCode error code recieved from server or generated locally
157: * due to transaction terminating.
158: *
159: * @param info detail information about the error
160: *
161: * @param transactionID ID of transaction response recieved within.
162: */
163: abstract public void errorResponse(int errorCode, String info,
164: int transactionID);
165:
166: /**
167: * Informs this listener about found services records.
168: *
169: * @param handleList service records handles returned by server within
170: * SDP_ServiceSearchResponse.
171: *
172: * @param transactionID ID of transaction response recieved within.
173: */
174: abstract public void serviceSearchResponse(int[] handleList,
175: int transactionID);
176:
177: /**
178: * Informs this listener about found attributes of specified service record.
179: *
180: * @param attrIDs list of attributes whose values requested from server
181: * within SDP_ServiceAttributesRequest.
182: *
183: * @param attributeValues values returned by server within
184: * SDP_ServiceAttributesResponse.
185: *
186: * @param transactionID ID of transaction response recieved within.
187: */
188: abstract public void serviceAttributeResponse(int[] attrIDs,
189: DataElement[] attributeValues, int transactionID);
190:
191: /**
192: * Informs this listener about attributes of fisrt found service record.
193: */
194: abstract public void serviceSearchAttributeResponse(int[] attrIDs,
195: DataElement[] attributeValues, int transactionID);
196: }
|