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.midp.io.j2me.irdaobex;
027:
028: import com.sun.midp.io.j2me.irdaobex.IrNativeConnection;
029: import com.sun.midp.main.Configuration;
030: import java.io.IOException;
031: import java.io.InterruptedIOException;
032: import java.util.Vector;
033: import javax.microedition.io.StreamConnection;
034: import javax.microedition.io.StreamConnectionNotifier;
035:
036: /**
037: * Performs the IrOBEX initialization, handles the device hint bits state, and
038: * responsible for device/service discovery process.
039: */
040: final class IrOBEXControl {
041:
042: /** Default constructor. */
043: public IrOBEXControl() {
044: }
045:
046: /**
047: * Creates the IrOBEX server notifier object by passing an underlying
048: * connection notifier.
049: *
050: * @param hints hint bits
051: * @param ias service class names separated by comma
052: * @return IrOBEXNotifier object
053: * @exception IOException if something goes wrong
054: */
055: public IrOBEXNotifier createServerConnection(int hints, String[] ias)
056: throws IOException {
057: return new IrOBEXNotifier(new IrNativeNotifier(hints, ias));
058: }
059:
060: /**
061: * Attempts to connect to the first device matches the criteria specified
062: * (hint bits and IAS list). Cached devices are attempted first. If failed,
063: * discovery process is initiated to update the cache, followed by another
064: * connection attempt to a cached device. The procedure repeats until the
065: * connection is established, or timout occurs.
066: *
067: * @param hints hint bits required to be set on the device
068: * @param ias services required to be provided by the device
069: * @return IrOBEXConnection object
070: * @exception IOException if something goes wrong
071: */
072: public IrOBEXConnection createClientConnection(int hints,
073: String[] ias) throws IOException {
074: if (ias.length == 0) {
075: throw new IllegalArgumentException();
076: }
077: IrNativeConnection[] connArray = new IrNativeConnection[ias.length];
078: for (int i = 0; i < ias.length; i++) {
079: connArray[i] = null;
080: }
081: int timeout = Integer
082: .parseInt(Configuration
083: .getProperty("com.sun.midp.io.j2me.irdaobex.DiscoveryTimeout"));
084: int interval = Integer
085: .parseInt(Configuration
086: .getProperty("com.sun.midp.io.j2me.irdaobex.DiscoveryInterval"));
087: long end = System.currentTimeMillis() + timeout;
088: while (true) {
089: for (int i = 0; i < ias.length; i++) {
090: if (System.currentTimeMillis() + interval > end) {
091: throw new IOException(
092: "Could not establish connection.");
093: }
094: IrNativeConnection conn = connArray[i];
095: if (conn == null) {
096: conn = new IrNativeConnection();
097: connArray[i] = conn;
098: }
099: int[] addr;
100: try {
101: addr = conn.discover(hints, ias[i]);
102: } catch (IOException e) {
103: continue;
104: }
105: for (int j = 0; j < addr.length; j++) {
106: try {
107: while (!conn.connect(addr[j], ias[i])) {
108: if (System.currentTimeMillis() + interval > end) {
109: throw new IOException(
110: "Could not establish connection.");
111: }
112: try {
113: Thread.sleep(10);
114: } catch (InterruptedException e) {
115: throw new InterruptedIOException(
116: "Operation was interrupted.");
117: }
118: }
119: return new IrOBEXConnection(conn);
120: } catch (InterruptedIOException ie) {
121: throw ie;
122: } catch (IOException e) {
123: }
124: }
125: }
126: }
127: }
128:
129: }
|